File: /usr/src/linux/fs/ext2/ialloc.c

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