1 /*
2  *  linux/fs/ext2/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@dcs.ed.ac.uk), 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/config.h>
16 #include <linux/fs.h>
17 #include <linux/ext2_fs.h>
18 #include <linux/locks.h>
19 #include <linux/quotaops.h>
20 
21 
22 /*
23  * ialloc.c contains the inodes allocation and deallocation routines
24  */
25 
26 /*
27  * The free inodes are managed by bitmaps.  A file system contains several
28  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
29  * block for inodes, N blocks for the inode table and data blocks.
30  *
31  * The file system contains group descriptors which are located after the
32  * super block.  Each descriptor contains the number of the bitmap block and
33  * the free blocks count in the block.  The descriptors are loaded in memory
34  * when a file system is mounted (see ext2_read_super).
35  */
36 
37 
38 /*
39  * Read the inode allocation bitmap for a given block_group, reading
40  * into the specified slot in the superblock's bitmap cache.
41  *
42  * Return buffer_head of bitmap on success or NULL.
43  */
read_inode_bitmap(struct super_block * sb,unsigned long block_group)44 static struct buffer_head *read_inode_bitmap (struct super_block * sb,
45 					       unsigned long block_group)
46 {
47 	struct ext2_group_desc *desc;
48 	struct buffer_head *bh = NULL;
49 
50 	desc = ext2_get_group_desc(sb, block_group, NULL);
51 	if (!desc)
52 		goto error_out;
53 
54 	bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
55 	if (!bh)
56 		ext2_error (sb, "read_inode_bitmap",
57 			    "Cannot read inode bitmap - "
58 			    "block_group = %lu, inode_bitmap = %lu",
59 			    block_group, (unsigned long) desc->bg_inode_bitmap);
60 error_out:
61 	return bh;
62 }
63 
64 /*
65  * load_inode_bitmap loads the inode bitmap for a blocks group
66  *
67  * It maintains a cache for the last bitmaps loaded.  This cache is managed
68  * with a LRU algorithm.
69  *
70  * Notes:
71  * 1/ There is one cache per mounted file system.
72  * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
73  *    this function reads the bitmap without maintaining a LRU cache.
74  *
75  * Return the buffer_head of the bitmap or the ERR_PTR(error)
76  */
load_inode_bitmap(struct super_block * sb,unsigned int block_group)77 static struct buffer_head *load_inode_bitmap (struct super_block * sb,
78 					      unsigned int block_group)
79 {
80 	int i, slot = 0;
81 	struct ext2_sb_info *sbi = &sb->u.ext2_sb;
82 	struct buffer_head *bh = sbi->s_inode_bitmap[0];
83 
84 	if (block_group >= sbi->s_groups_count)
85 		ext2_panic (sb, "load_inode_bitmap",
86 			    "block_group >= groups_count - "
87 			    "block_group = %d, groups_count = %lu",
88 			     block_group, sbi->s_groups_count);
89 
90 	if (sbi->s_loaded_inode_bitmaps > 0 &&
91 	    sbi->s_inode_bitmap_number[0] == block_group && bh)
92 		goto found;
93 
94 	if (sbi->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
95 		slot = block_group;
96 		bh = sbi->s_inode_bitmap[slot];
97 		if (!bh)
98 			goto read_it;
99 		if (sbi->s_inode_bitmap_number[slot] == slot)
100 			goto found;
101 		ext2_panic (sb, "load_inode_bitmap",
102 			    "block_group != inode_bitmap_number");
103 	}
104 
105 	bh = NULL;
106 	for (i = 0; i < sbi->s_loaded_inode_bitmaps &&
107 		    sbi->s_inode_bitmap_number[i] != block_group;
108 	     i++)
109 		;
110 	if (i < sbi->s_loaded_inode_bitmaps)
111 		bh = sbi->s_inode_bitmap[i];
112 	else if (sbi->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
113 		sbi->s_loaded_inode_bitmaps++;
114 	else
115 		brelse (sbi->s_inode_bitmap[--i]);
116 
117 	while (i--) {
118 		sbi->s_inode_bitmap_number[i+1] = sbi->s_inode_bitmap_number[i];
119 		sbi->s_inode_bitmap[i+1] = sbi->s_inode_bitmap[i];
120 	}
121 
122 read_it:
123 	if (!bh)
124 		bh = read_inode_bitmap (sb, block_group);
125 	sbi->s_inode_bitmap_number[slot] = block_group;
126 	sbi->s_inode_bitmap[slot] = bh;
127 	if (!bh)
128 		return ERR_PTR(-EIO);
129 found:
130 	return bh;
131 }
132 
133 /*
134  * NOTE! When we get the inode, we're the only people
135  * that have access to it, and as such there are no
136  * race conditions we have to worry about. The inode
137  * is not on the hash-lists, and it cannot be reached
138  * through the filesystem because the directory entry
139  * has been deleted earlier.
140  *
141  * HOWEVER: we must make sure that we get no aliases,
142  * which means that we have to call "clear_inode()"
143  * _before_ we mark the inode not in use in the inode
144  * bitmaps. Otherwise a newly created file might use
145  * the same inode number (not actually the same pointer
146  * though), and then we'd have two inodes sharing the
147  * same inode number and space on the harddisk.
148  */
ext2_free_inode(struct inode * inode)149 void ext2_free_inode (struct inode * inode)
150 {
151 	struct super_block * sb = inode->i_sb;
152 	int is_directory;
153 	unsigned long ino;
154 	struct buffer_head * bh;
155 	struct buffer_head * bh2;
156 	unsigned long block_group;
157 	unsigned long bit;
158 	struct ext2_group_desc * desc;
159 	struct ext2_super_block * es;
160 
161 	ino = inode->i_ino;
162 	ext2_debug ("freeing inode %lu\n", ino);
163 
164 	/*
165 	 * Note: we must free any quota before locking the superblock,
166 	 * as writing the quota to disk may need the lock as well.
167 	 */
168 	if (!is_bad_inode(inode)) {
169 		/* Quota is already initialized in iput() */
170 	    	DQUOT_FREE_INODE(inode);
171 		DQUOT_DROP(inode);
172 	}
173 
174 	lock_super (sb);
175 	es = sb->u.ext2_sb.s_es;
176 	is_directory = S_ISDIR(inode->i_mode);
177 
178 	/* Do this BEFORE marking the inode not in use or returning an error */
179 	clear_inode (inode);
180 
181 	if (ino < EXT2_FIRST_INO(sb) ||
182 	    ino > le32_to_cpu(es->s_inodes_count)) {
183 		ext2_error (sb, "ext2_free_inode",
184 			    "reserved or nonexistent inode %lu", ino);
185 		goto error_return;
186 	}
187 	block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
188 	bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
189 	bh = load_inode_bitmap (sb, block_group);
190 	if (IS_ERR(bh))
191 		goto error_return;
192 
193 	/* Ok, now we can actually update the inode bitmaps.. */
194 	if (!ext2_clear_bit (bit, bh->b_data))
195 		ext2_error (sb, "ext2_free_inode",
196 			      "bit already cleared for inode %lu", ino);
197 	else {
198 		desc = ext2_get_group_desc (sb, block_group, &bh2);
199 		if (desc) {
200 			desc->bg_free_inodes_count =
201 				cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
202 			if (is_directory)
203 				desc->bg_used_dirs_count =
204 					cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
205 		}
206 		mark_buffer_dirty(bh2);
207 		es->s_free_inodes_count =
208 			cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
209 		mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
210 	}
211 	mark_buffer_dirty(bh);
212 	if (sb->s_flags & MS_SYNCHRONOUS) {
213 		ll_rw_block (WRITE, 1, &bh);
214 		wait_on_buffer (bh);
215 	}
216 	sb->s_dirt = 1;
217 error_return:
218 	unlock_super (sb);
219 }
220 
221 /*
222  * There are two policies for allocating an inode.  If the new inode is
223  * a directory, then a forward search is made for a block group with both
224  * free space and a low directory-to-inode ratio; if that fails, then of
225  * the groups with above-average free space, that group with the fewest
226  * directories already is chosen.
227  *
228  * For other inodes, search forward from the parent directory\'s block
229  * group to find a free inode.
230  */
231 
find_group_dir(struct super_block * sb,int parent_group)232 static int find_group_dir(struct super_block *sb, int parent_group)
233 {
234 	struct ext2_super_block * es = sb->u.ext2_sb.s_es;
235 	int ngroups = sb->u.ext2_sb.s_groups_count;
236 	int avefreei = le32_to_cpu(es->s_free_inodes_count) / ngroups;
237 	struct ext2_group_desc *desc, *best_desc = NULL;
238 	struct buffer_head *bh, *best_bh = NULL;
239 	int group, best_group = -1;
240 
241 	for (group = 0; group < ngroups; group++) {
242 		desc = ext2_get_group_desc (sb, group, &bh);
243 		if (!desc || !desc->bg_free_inodes_count)
244 			continue;
245 		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
246 			continue;
247 		if (!best_desc ||
248 		    (le16_to_cpu(desc->bg_free_blocks_count) >
249 		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
250 			best_group = group;
251 			best_desc = desc;
252 			best_bh = bh;
253 		}
254 	}
255 	if (!best_desc)
256 		return -1;
257 	best_desc->bg_free_inodes_count =
258 		cpu_to_le16(le16_to_cpu(best_desc->bg_free_inodes_count) - 1);
259 	best_desc->bg_used_dirs_count =
260 		cpu_to_le16(le16_to_cpu(best_desc->bg_used_dirs_count) + 1);
261 	mark_buffer_dirty(best_bh);
262 	return best_group;
263 }
264 
find_group_other(struct super_block * sb,int parent_group)265 static int find_group_other(struct super_block *sb, int parent_group)
266 {
267 	int ngroups = sb->u.ext2_sb.s_groups_count;
268 	struct ext2_group_desc *desc;
269 	struct buffer_head *bh;
270 	int group, i;
271 
272 	/*
273 	 * Try to place the inode in its parent directory
274 	 */
275 	group = parent_group;
276 	desc = ext2_get_group_desc (sb, group, &bh);
277 	if (desc && le16_to_cpu(desc->bg_free_inodes_count))
278 		goto found;
279 
280 	/*
281 	 * Use a quadratic hash to find a group with a
282 	 * free inode
283 	 */
284 	for (i = 1; i < ngroups; i <<= 1) {
285 		group += i;
286 		if (group >= ngroups)
287 			group -= ngroups;
288 		desc = ext2_get_group_desc (sb, group, &bh);
289 		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
290 			goto found;
291 	}
292 
293 	/*
294 	 * That failed: try linear search for a free inode
295 	 */
296 	group = parent_group + 1;
297 	for (i = 2; i < ngroups; i++) {
298 		if (++group >= ngroups)
299 			group = 0;
300 		desc = ext2_get_group_desc (sb, group, &bh);
301 		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
302 			goto found;
303 	}
304 
305 	return -1;
306 
307 found:
308 	desc->bg_free_inodes_count =
309 		cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) - 1);
310 	mark_buffer_dirty(bh);
311 	return group;
312 }
313 
ext2_new_inode(const struct inode * dir,int mode)314 struct inode * ext2_new_inode (const struct inode * dir, int mode)
315 {
316 	struct super_block * sb;
317 	struct buffer_head * bh;
318 	struct buffer_head * bh2;
319 	int group, i;
320 	ino_t ino;
321 	struct inode * inode;
322 	struct ext2_group_desc * desc;
323 	struct ext2_super_block * es;
324 	int err;
325 
326 	sb = dir->i_sb;
327 	inode = new_inode(sb);
328 	if (!inode)
329 		return ERR_PTR(-ENOMEM);
330 
331 	lock_super (sb);
332 	es = sb->u.ext2_sb.s_es;
333 repeat:
334 	if (S_ISDIR(mode))
335 		group = find_group_dir(sb, dir->u.ext2_i.i_block_group);
336 	else
337 		group = find_group_other(sb, dir->u.ext2_i.i_block_group);
338 
339 	err = -ENOSPC;
340 	if (group == -1)
341 		goto fail;
342 
343 	err = -EIO;
344 	bh = load_inode_bitmap (sb, group);
345 	if (IS_ERR(bh))
346 		goto fail2;
347 
348 	i = ext2_find_first_zero_bit ((unsigned long *) bh->b_data,
349 				      EXT2_INODES_PER_GROUP(sb));
350 	if (i >= EXT2_INODES_PER_GROUP(sb))
351 		goto bad_count;
352 	ext2_set_bit (i, bh->b_data);
353 
354 	mark_buffer_dirty(bh);
355 	if (sb->s_flags & MS_SYNCHRONOUS) {
356 		ll_rw_block (WRITE, 1, &bh);
357 		wait_on_buffer (bh);
358 	}
359 
360 	ino = group * EXT2_INODES_PER_GROUP(sb) + i + 1;
361 	if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
362 		ext2_error (sb, "ext2_new_inode",
363 			    "reserved inode or inode > inodes count - "
364 			    "block_group = %d,inode=%ld", group, ino);
365 		err = -EIO;
366 		goto fail2;
367 	}
368 
369 	es->s_free_inodes_count =
370 		cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
371 	mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
372 	sb->s_dirt = 1;
373 	inode->i_uid = current->fsuid;
374 	if (test_opt (sb, GRPID))
375 		inode->i_gid = dir->i_gid;
376 	else if (dir->i_mode & S_ISGID) {
377 		inode->i_gid = dir->i_gid;
378 		if (S_ISDIR(mode))
379 			mode |= S_ISGID;
380 	} else
381 		inode->i_gid = current->fsgid;
382 	inode->i_mode = mode;
383 
384 	inode->i_ino = ino;
385 	inode->i_blksize = PAGE_SIZE;	/* This is the optimal IO size (for stat), not the fs block size */
386 	inode->i_blocks = 0;
387 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
388 	inode->u.ext2_i.i_state = EXT2_STATE_NEW;
389 	inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags & ~EXT2_BTREE_FL;
390 	if (S_ISLNK(mode))
391 		inode->u.ext2_i.i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
392 	inode->u.ext2_i.i_block_group = group;
393 	ext2_set_inode_flags(inode);
394 	insert_inode_hash(inode);
395 	inode->i_generation = event++;
396 	mark_inode_dirty(inode);
397 
398 	unlock_super (sb);
399 	if(DQUOT_ALLOC_INODE(inode)) {
400 		DQUOT_DROP(inode);
401 		inode->i_flags |= S_NOQUOTA;
402 		inode->i_nlink = 0;
403 		iput(inode);
404 		return ERR_PTR(-EDQUOT);
405 	}
406 	ext2_debug ("allocating inode %lu\n", inode->i_ino);
407 	return inode;
408 
409 fail2:
410 	desc = ext2_get_group_desc (sb, group, &bh2);
411 	desc->bg_free_inodes_count =
412 		cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
413 	if (S_ISDIR(mode))
414 		desc->bg_used_dirs_count =
415 			cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
416 	mark_buffer_dirty(bh2);
417 fail:
418 	unlock_super(sb);
419 	make_bad_inode(inode);
420 	iput(inode);
421 	return ERR_PTR(err);
422 
423 bad_count:
424 	ext2_error (sb, "ext2_new_inode",
425 		    "Free inodes count corrupted in group %d",
426 		    group);
427 	/* Is it really ENOSPC? */
428 	err = -ENOSPC;
429 	if (sb->s_flags & MS_RDONLY)
430 		goto fail;
431 
432 	desc = ext2_get_group_desc (sb, group, &bh2);
433 	desc->bg_free_inodes_count = 0;
434 	mark_buffer_dirty(bh2);
435 	goto repeat;
436 }
437 
ext2_count_free_inodes(struct super_block * sb)438 unsigned long ext2_count_free_inodes (struct super_block * sb)
439 {
440 #ifdef EXT2FS_DEBUG
441 	struct ext2_super_block * es;
442 	unsigned long desc_count = 0, bitmap_count = 0;
443 	int i;
444 
445 	lock_super (sb);
446 	es = sb->u.ext2_sb.s_es;
447 	for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
448 		struct ext2_group_desc *desc = ext2_get_group_desc (sb, i, NULL);
449 		struct buffer_head *bh;
450 		unsigned x;
451 
452 		if (!desc)
453 			continue;
454 		desc_count += le16_to_cpu(desc->bg_free_inodes_count);
455 		bh = load_inode_bitmap (sb, i);
456 		if (IS_ERR(bh))
457 			continue;
458 
459 		x = ext2_count_free (bh, EXT2_INODES_PER_GROUP(sb) / 8);
460 		printk ("group %d: stored = %d, counted = %lu\n",
461 			i, le16_to_cpu(desc->bg_free_inodes_count), x);
462 		bitmap_count += x;
463 	}
464 	printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
465 		le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
466 	unlock_super (sb);
467 	return desc_count;
468 #else
469 	return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
470 #endif
471 }
472 
473 #ifdef CONFIG_EXT2_CHECK
474 /* Called at mount-time, super-block is locked */
ext2_check_inodes_bitmap(struct super_block * sb)475 void ext2_check_inodes_bitmap (struct super_block * sb)
476 {
477 	struct ext2_super_block * es = sb->u.ext2_sb.s_es;
478 	unsigned long desc_count = 0, bitmap_count = 0;
479 	int i;
480 
481 	for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
482 		struct ext2_group_desc *desc = ext2_get_group_desc(sb, i, NULL);
483 		struct buffer_head *bh;
484 		unsigned x;
485 
486 		if (!desc)
487 			continue;
488 		desc_count += le16_to_cpu(desc->bg_free_inodes_count);
489 		bh = load_inode_bitmap (sb, i);
490 		if (IS_ERR(bh))
491 			continue;
492 
493 		x = ext2_count_free (bh, EXT2_INODES_PER_GROUP(sb) / 8);
494 		if (le16_to_cpu(desc->bg_free_inodes_count) != x)
495 			ext2_error (sb, "ext2_check_inodes_bitmap",
496 				    "Wrong free inodes count in group %d, "
497 				    "stored = %d, counted = %lu", i,
498 				    le16_to_cpu(desc->bg_free_inodes_count), x);
499 		bitmap_count += x;
500 	}
501 	if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
502 		ext2_error (sb, "ext2_check_inodes_bitmap",
503 			    "Wrong free inodes count in super block, "
504 			    "stored = %lu, counted = %lu",
505 			    (unsigned long)le32_to_cpu(es->s_free_inodes_count),
506 			    bitmap_count);
507 }
508 #endif
509