Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/fs/ext2/ialloc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  BSD ufs-inspired inode and directory allocation by 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *  Big-endian to little-endian byte-swapping/bitmaps by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *        David S. Miller (davem@caip.rutgers.edu), 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "ext2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * ialloc.c contains the inodes allocation and deallocation routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * The free inodes are managed by bitmaps.  A file system contains several
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * block for inodes, N blocks for the inode table and data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * The file system contains group descriptors which are located after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * super block.  Each descriptor contains the number of the bitmap block and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * the free blocks count in the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Read the inode allocation bitmap for a given block_group, reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * into the specified slot in the superblock's bitmap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * Return buffer_head of bitmap on success or NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) read_inode_bitmap(struct super_block * sb, unsigned long block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct ext2_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	desc = ext2_get_group_desc(sb, block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		ext2_error(sb, "read_inode_bitmap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			    "Cannot read inode bitmap - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			    "block_group = %lu, inode_bitmap = %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			    block_group, le32_to_cpu(desc->bg_inode_bitmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static void ext2_release_inode(struct super_block *sb, int group, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct ext2_group_desc * desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	desc = ext2_get_group_desc(sb, group, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		ext2_error(sb, "ext2_release_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			"can't get descriptor for group %d", group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	le16_add_cpu(&desc->bg_free_inodes_count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		le16_add_cpu(&desc->bg_used_dirs_count, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	percpu_counter_inc(&EXT2_SB(sb)->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * NOTE! When we get the inode, we're the only people
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * that have access to it, and as such there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * race conditions we have to worry about. The inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * is not on the hash-lists, and it cannot be reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * through the filesystem because the directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * has been deleted earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * HOWEVER: we must make sure that we get no aliases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * which means that we have to call "clear_inode()"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * _before_ we mark the inode not in use in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * bitmaps. Otherwise a newly created file might use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * the same inode number (not actually the same pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * though), and then we'd have two inodes sharing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * same inode number and space on the harddisk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) void ext2_free_inode (struct inode * inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct super_block * sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int is_directory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct buffer_head *bitmap_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned long block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	unsigned long bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct ext2_super_block * es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ext2_debug ("freeing inode %lu\n", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * Note: we must free any quota before locking the superblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * as writing the quota to disk may need the lock as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/* Quota is already initialized in iput() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	dquot_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	dquot_drop(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	es = EXT2_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	is_directory = S_ISDIR(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (ino < EXT2_FIRST_INO(sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	    ino > le32_to_cpu(es->s_inodes_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		ext2_error (sb, "ext2_free_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			    "reserved or nonexistent inode %lu", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	bitmap_bh = read_inode_bitmap(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!bitmap_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* Ok, now we can actually update the inode bitmaps.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				bit, (void *) bitmap_bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ext2_error (sb, "ext2_free_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			      "bit already cleared for inode %lu", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		ext2_release_inode(sb, block_group, is_directory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	mark_buffer_dirty(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		sync_dirty_buffer(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * We perform asynchronous prereading of the new inode's inode block when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * we create the inode, in the expectation that the inode will be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * back soon.  There are two reasons:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * - When creating a large number of files, the async prereads will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  *   nicely merged into large reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * - When writing out a large number of inodes, we don't need to keep on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  *   stalling the writes while we read the inode block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * FIXME: ext2_get_group_desc() needs to be simplified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void ext2_preread_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	unsigned long block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	unsigned long block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct ext2_group_desc * gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct backing_dev_info *bdi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	bdi = inode_to_bdi(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (bdi_rw_congested(bdi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (gdp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * Figure out the offset within the block group inode table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				EXT2_INODE_SIZE(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	block = le32_to_cpu(gdp->bg_inode_table) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				(offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	sb_breadahead(inode->i_sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * There are two policies for allocating an inode.  If the new inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * a directory, then a forward search is made for a block group with both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * free space and a low directory-to-inode ratio; if that fails, then of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * the groups with above-average free space, that group with the fewest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * directories already is chosen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * For other inodes, search forward from the parent directory\'s block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * group to find a free inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int find_group_dir(struct super_block *sb, struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int ngroups = EXT2_SB(sb)->s_groups_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int avefreei = ext2_count_free_inodes(sb) / ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct ext2_group_desc *desc, *best_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int group, best_group = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	for (group = 0; group < ngroups; group++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (!desc || !desc->bg_free_inodes_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (!best_desc || 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		    (le16_to_cpu(desc->bg_free_blocks_count) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			best_group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			best_desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return best_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * Orlov's allocator for directories. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * We always try to spread first-level directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * If there are blockgroups with both free inodes and free blocks counts 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * not worse than average we return one with smallest directory count. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * Otherwise we simply return a random group. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * For the rest rules look so: 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * It's OK to put directory into a group unless 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * it has too many directories already (max_dirs) or 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * it has too few free inodes left (min_inodes) or 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * it has too few free blocks left (min_blocks) or 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * it's already running too large debt (max_debt). 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * Parent's group is preferred, if it doesn't satisfy these 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * conditions we search cyclically through the rest. If none 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * of the groups look good we just look for a group with more 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * free inodes than average (starting at parent's group). 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  * Debt is incremented each time we allocate a directory and decremented 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  * when we allocate an inode, within 0--255. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  */ 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) #define INODE_COST 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define BLOCK_COST 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static int find_group_orlov(struct super_block *sb, struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	int parent_group = EXT2_I(parent)->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	struct ext2_sb_info *sbi = EXT2_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct ext2_super_block *es = sbi->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int ngroups = sbi->s_groups_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	int freei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int avefreei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	int free_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int avefreeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int blocks_per_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int ndirs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int max_debt, max_dirs, min_blocks, min_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int group = -1, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct ext2_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	avefreei = freei / ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	avefreeb = free_blocks / ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if ((parent == d_inode(sb->s_root)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	    (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		struct ext2_group_desc *best_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		int best_ndir = inodes_per_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		int best_group = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		group = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		parent_group = (unsigned)group % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			group = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			if (!desc || !desc->bg_free_inodes_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			best_group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			best_desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		if (best_group >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			desc = best_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			group = best_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (ndirs == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		ndirs = 1;	/* percpu_counters are approximate... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	min_inodes = avefreei - inodes_per_group / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (max_debt * INODE_COST > inodes_per_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		max_debt = inodes_per_group / INODE_COST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (max_debt > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		max_debt = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (max_debt == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		max_debt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		group = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (!desc || !desc->bg_free_inodes_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (sbi->s_debts[group] >= max_debt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) fallback:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		group = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		if (!desc || !desc->bg_free_inodes_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (avefreei) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		 * The free-inodes counter is approximate, and for really small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		 * filesystems the above test can fail to find any blockgroups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		avefreei = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	return group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static int find_group_other(struct super_block *sb, struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int parent_group = EXT2_I(parent)->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int ngroups = EXT2_SB(sb)->s_groups_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	struct ext2_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int group, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * Try to place the inode in its parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	group = parent_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			le16_to_cpu(desc->bg_free_blocks_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 * We're going to place this inode in a different blockgroup from its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 * parent.  We want to cause files in a common directory to all land in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * the same blockgroup.  But we want files which are in a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * directory which shares a blockgroup with our parent to land in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 * different blockgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	 * So add our directory's i_ino into the starting point for the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	group = (group + parent->i_ino) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	 * Use a quadratic hash to find a group with a free inode and some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	 * free blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	for (i = 1; i < ngroups; i <<= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		group += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		if (group >= ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			group -= ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 				le16_to_cpu(desc->bg_free_blocks_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	 * That failed: try linear search for a free inode, even if that group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	 * has no free blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	group = parent_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		if (++group >= ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		desc = ext2_get_group_desc (sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct inode *ext2_new_inode(struct inode *dir, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			     const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	struct buffer_head *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	int group, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	ino_t ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	struct inode * inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct ext2_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	struct ext2_super_block *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	struct ext2_inode_info *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct ext2_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	ei = EXT2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	sbi = EXT2_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	es = sbi->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (test_opt(sb, OLDALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			group = find_group_dir(sb, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			group = find_group_orlov(sb, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	} else 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		group = find_group_other(sb, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (group == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	for (i = 0; i < sbi->s_groups_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		gdp = ext2_get_group_desc(sb, group, &bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		if (!gdp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			if (++group == sbi->s_groups_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 				group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		bitmap_bh = read_inode_bitmap(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		if (!bitmap_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) repeat_in_this_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 					      EXT2_INODES_PER_GROUP(sb), ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		if (ino >= EXT2_INODES_PER_GROUP(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			 * Rare race: find_group_xx() decided that there were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			 * free inodes in this group, but by the time we tried
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			 * to allocate one, they're all gone.  This can also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			 * occur because the counters which find_group_orlov()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			 * uses are approximate.  So just go and search the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			 * next block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			if (++group == sbi->s_groups_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 						ino, bitmap_bh->b_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			/* we lost this inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 				/* this group is exhausted, try next group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 				if (++group == sbi->s_groups_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 					group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			/* try to find free inode in the same group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			goto repeat_in_this_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	 * Scanned all blockgroups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) got:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	mark_buffer_dirty(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		sync_dirty_buffer(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		ext2_error (sb, "ext2_new_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			    "reserved inode or inode > inodes count - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			    "block_group = %d,inode=%lu", group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			    (unsigned long) ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	percpu_counter_dec(&sbi->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		percpu_counter_inc(&sbi->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	spin_lock(sb_bgl_lock(sbi, group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	le16_add_cpu(&gdp->bg_free_inodes_count, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		if (sbi->s_debts[group] < 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			sbi->s_debts[group]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		le16_add_cpu(&gdp->bg_used_dirs_count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		if (sbi->s_debts[group])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			sbi->s_debts[group]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	spin_unlock(sb_bgl_lock(sbi, group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	mark_buffer_dirty(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (test_opt(sb, GRPID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		inode->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		inode->i_gid = dir->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		inode_init_owner(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	inode->i_ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	inode->i_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	memset(ei->i_data, 0, sizeof(ei->i_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	ei->i_flags =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	ei->i_faddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	ei->i_frag_no = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	ei->i_frag_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	ei->i_file_acl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	ei->i_dir_acl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	ei->i_dtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	ei->i_block_alloc_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	ei->i_block_group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	ei->i_dir_start_lookup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	ei->i_state = EXT2_STATE_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	ext2_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	spin_lock(&sbi->s_next_gen_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	inode->i_generation = sbi->s_next_generation++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	spin_unlock(&sbi->s_next_gen_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (insert_inode_locked(inode) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		ext2_error(sb, "ext2_new_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			   "inode number already in use - inode=%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			   (unsigned long) ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	err = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		goto fail_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	err = dquot_alloc_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		goto fail_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	err = ext2_init_acl(inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	err = ext2_init_security(inode, dir, qstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	ext2_debug("allocating inode %lu\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	ext2_preread_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) fail_free_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	dquot_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) fail_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	dquot_drop(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	inode->i_flags |= S_NOQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	discard_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) unsigned long ext2_count_free_inodes (struct super_block * sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	struct ext2_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	unsigned long desc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	int i;	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) #ifdef EXT2FS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	struct ext2_super_block *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	unsigned long bitmap_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	es = EXT2_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		unsigned x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		desc = ext2_get_group_desc (sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		desc_count += le16_to_cpu(desc->bg_free_inodes_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		bitmap_bh = read_inode_bitmap(sb, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		if (!bitmap_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		printk("group %d: stored = %d, counted = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			i, le16_to_cpu(desc->bg_free_inodes_count), x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		bitmap_count += x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		(unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		desc_count, bitmap_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	return desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		desc = ext2_get_group_desc (sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		desc_count += le16_to_cpu(desc->bg_free_inodes_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	return desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* Called at mount-time, super-block is locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) unsigned long ext2_count_dirs (struct super_block * sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	unsigned long count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		count += le16_to_cpu(gdp->bg_used_dirs_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)