Replace spl with critical sections.
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_linux_ialloc.c
1 /*
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  *
7  * $FreeBSD: src/sys/gnu/ext2fs/ext2_linux_ialloc.c,v 1.13.2.2 2001/08/14 18:03:19 gallatin Exp $
8  * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_linux_ialloc.c,v 1.5 2005/06/06 15:09:37 drhodus Exp $
9  */
10 /*
11  *  linux/fs/ext2/ialloc.c
12  *
13  * Copyright (C) 1992, 1993, 1994, 1995
14  * Remy Card (card@masi.ibp.fr)
15  * Laboratoire MASI - Institut Blaise Pascal
16  * Universite Pierre et Marie Curie (Paris VI)
17  *
18  *  BSD ufs-inspired inode and directory allocation by 
19  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
20  */
21
22 /*
23  * The free inodes are managed by bitmaps.  A file system contains several
24  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
25  * block for inodes, N blocks for the inode table and data blocks.
26  *
27  * The file system contains group descriptors which are located after the
28  * super block.  Each descriptor contains the number of the bitmap block and
29  * the free blocks count in the block.  The descriptors are loaded in memory
30  * when a file system is mounted (see ext2_read_super).
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/buf.h>
36 #include <sys/proc.h>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39
40 #include <vfs/ufs/quota.h>
41 #include <vfs/ufs/inode.h>
42 #include <vfs/ufs/ufsmount.h>
43 #include "ext2_extern.h"
44 #include "ext2_fs.h"
45 #include "ext2_fs_sb.h"
46 #include "fs.h"
47 #include <sys/stat.h>
48 #include <sys/buf2.h>
49
50 #ifdef __i386__
51 #include "i386-bitops.h"
52 #elif defined(__alpha__)
53 #include "alpha-bitops.h"
54 #else
55 #error please provide bit operation functions
56 #endif
57
58 /* this is supposed to mark a buffer dirty on ready for delayed writing
59  */
60 void mark_buffer_dirty(struct buf *bh)
61 {
62         crit_enter();
63         bh->b_flags |= B_DIRTY;
64         crit_exit();
65
66
67 struct ext2_group_desc * get_group_desc (struct mount * mp,
68                                                 unsigned int block_group,
69                                                 struct buffer_head ** bh)
70 {
71         struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
72         unsigned long group_desc;
73         unsigned long desc;
74         struct ext2_group_desc * gdp;
75
76         if (block_group >= sb->s_groups_count)
77                 panic ("get_group_desc: "
78                             "block_group >= groups_count - "
79                             "block_group = %d, groups_count = %lu",
80                             block_group, sb->s_groups_count);
81
82         group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
83         desc = block_group % EXT2_DESC_PER_BLOCK(sb);
84         if (!sb->s_group_desc[group_desc])
85                 panic ( "get_group_desc:"
86                             "Group descriptor not loaded - "
87                             "block_group = %d, group_desc = %lu, desc = %lu",
88                              block_group, group_desc, desc);
89         gdp = (struct ext2_group_desc *) 
90                 sb->s_group_desc[group_desc]->b_data;
91         if (bh)
92                 *bh = sb->s_group_desc[group_desc];
93         return gdp + desc;
94 }
95
96 static void read_inode_bitmap (struct mount * mp,
97                                unsigned long block_group,
98                                unsigned int bitmap_nr)
99 {
100         struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
101         struct ext2_group_desc * gdp;
102         struct buffer_head * bh;
103         int     error;
104
105         gdp = get_group_desc (mp, block_group, NULL);
106         if ((error = bread (VFSTOUFS(mp)->um_devvp, 
107                             fsbtodb(sb, gdp->bg_inode_bitmap), 
108                             sb->s_blocksize, &bh)) != 0)
109                 panic ( "read_inode_bitmap:"
110                             "Cannot read inode bitmap - "
111                             "block_group = %lu, inode_bitmap = %lu",
112                             block_group, (unsigned long) gdp->bg_inode_bitmap);
113         sb->s_inode_bitmap_number[bitmap_nr] = block_group;
114         sb->s_inode_bitmap[bitmap_nr] = bh;
115         LCK_BUF(bh)
116 }
117
118 /*
119  * load_inode_bitmap loads the inode bitmap for a blocks group
120  *
121  * It maintains a cache for the last bitmaps loaded.  This cache is managed
122  * with a LRU algorithm.
123  *
124  * Notes:
125  * 1/ There is one cache per mounted file system.
126  * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
127  *    this function reads the bitmap without maintaining a LRU cache.
128  */
129 static int load_inode_bitmap (struct mount * mp,
130                               unsigned int block_group)
131 {
132         struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
133         int i, j;
134         unsigned long inode_bitmap_number;
135         struct buffer_head * inode_bitmap;
136
137         if (block_group >= sb->s_groups_count)
138                 panic ("load_inode_bitmap:"
139                             "block_group >= groups_count - "
140                             "block_group = %d, groups_count = %lu",
141                              block_group, sb->s_groups_count);
142         if (sb->s_loaded_inode_bitmaps > 0 &&
143             sb->s_inode_bitmap_number[0] == block_group)
144                 return 0;
145         if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
146                 if (sb->s_inode_bitmap[block_group]) {
147                         if (sb->s_inode_bitmap_number[block_group] != 
148                                 block_group)
149                                 panic ( "load_inode_bitmap:"
150                                     "block_group != inode_bitmap_number");
151                         else
152                                 return block_group;
153                 } else {
154                         read_inode_bitmap (mp, block_group, block_group);
155                         return block_group;
156                 }
157         }
158
159         for (i = 0; i < sb->s_loaded_inode_bitmaps &&
160                     sb->s_inode_bitmap_number[i] != block_group;
161              i++)
162                 ;
163         if (i < sb->s_loaded_inode_bitmaps &&
164             sb->s_inode_bitmap_number[i] == block_group) {
165                 inode_bitmap_number = sb->s_inode_bitmap_number[i];
166                 inode_bitmap = sb->s_inode_bitmap[i];
167                 for (j = i; j > 0; j--) {
168                         sb->s_inode_bitmap_number[j] =
169                                 sb->s_inode_bitmap_number[j - 1];
170                         sb->s_inode_bitmap[j] =
171                                 sb->s_inode_bitmap[j - 1];
172                 }
173                 sb->s_inode_bitmap_number[0] = inode_bitmap_number;
174                 sb->s_inode_bitmap[0] = inode_bitmap;
175         } else {
176                 if (sb->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
177                         sb->s_loaded_inode_bitmaps++;
178                 else
179                         ULCK_BUF(sb->s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1])
180                 for (j = sb->s_loaded_inode_bitmaps - 1; j > 0; j--) {
181                         sb->s_inode_bitmap_number[j] =
182                                 sb->s_inode_bitmap_number[j - 1];
183                         sb->s_inode_bitmap[j] =
184                                 sb->s_inode_bitmap[j - 1];
185                 }
186                 read_inode_bitmap (mp, block_group, 0);
187         }
188         return 0;
189 }
190
191
192 void ext2_free_inode (struct inode * inode)
193 {
194         struct ext2_sb_info * sb;
195         struct buffer_head * bh;
196         struct buffer_head * bh2;
197         unsigned long block_group;
198         unsigned long bit;
199         int bitmap_nr;
200         struct ext2_group_desc * gdp;
201         struct ext2_super_block * es;
202
203         if (!inode)
204                 return;
205
206         if (inode->i_nlink) {
207                 printf ("ext2_free_inode: inode has nlink=%d\n",
208                         inode->i_nlink);
209                 return;
210         }
211
212         ext2_debug ("freeing inode %lu\n", inode->i_number);
213
214         sb = inode->i_e2fs;
215         lock_super (DEVVP(inode));
216         if (inode->i_number < EXT2_FIRST_INO ||
217             inode->i_number > sb->s_es->s_inodes_count) {
218                 printf ("free_inode reserved inode or nonexistent inode");
219                 unlock_super (DEVVP(inode));
220                 return;
221         }
222         es = sb->s_es;
223         block_group = (inode->i_number - 1) / EXT2_INODES_PER_GROUP(sb);
224         bit = (inode->i_number - 1) % EXT2_INODES_PER_GROUP(sb);
225         bitmap_nr = load_inode_bitmap (ITOV(inode)->v_mount, block_group);
226         bh = sb->s_inode_bitmap[bitmap_nr];
227         if (!clear_bit (bit, bh->b_data))       
228                 printf ( "ext2_free_inode:"
229                       "bit already cleared for inode %lu",
230                       (unsigned long)inode->i_number);
231         else {
232                 gdp = get_group_desc (ITOV(inode)->v_mount, block_group, &bh2);
233                 gdp->bg_free_inodes_count++;
234                 if (S_ISDIR(inode->i_mode)) 
235                         gdp->bg_used_dirs_count--;
236                 mark_buffer_dirty(bh2);
237                 es->s_free_inodes_count++;
238         }
239         mark_buffer_dirty(bh);
240 /*** XXX
241         if (sb->s_flags & MS_SYNCHRONOUS) {
242                 ll_rw_block (WRITE, 1, &bh);
243                 wait_on_buffer (bh);
244         }
245 ***/
246         sb->s_dirt = 1;
247         unlock_super (DEVVP(inode));
248 }
249
250 #if linux
251 /*
252  * This function increments the inode version number
253  *
254  * This may be used one day by the NFS server
255  */
256 static void inc_inode_version (struct inode * inode,
257                                struct ext2_group_desc *gdp,
258                                int mode)
259 {
260         unsigned long inode_block;
261         struct buffer_head * bh;
262         struct ext2_inode * raw_inode;
263
264         inode_block = gdp->bg_inode_table + (((inode->i_number - 1) %
265                         EXT2_INODES_PER_GROUP(inode->i_sb)) /
266                         EXT2_INODES_PER_BLOCK(inode->i_sb));
267         bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
268         if (!bh) {
269                 printf ("inc_inode_version Cannot load inode table block - "
270                             "inode=%lu, inode_block=%lu\n",
271                             inode->i_number, inode_block);
272                 inode->u.ext2_i.i_version = 1;
273                 return;
274         }
275         raw_inode = ((struct ext2_inode *) bh->b_data) +
276                         (((inode->i_number - 1) %
277                         EXT2_INODES_PER_GROUP(inode->i_sb)) %
278                         EXT2_INODES_PER_BLOCK(inode->i_sb));
279         raw_inode->i_version++;
280         inode->u.ext2_i.i_version = raw_inode->i_version;
281         bdwrite (bh);
282 }
283
284 #endif /* linux */
285
286 /*
287  * There are two policies for allocating an inode.  If the new inode is
288  * a directory, then a forward search is made for a block group with both
289  * free space and a low directory-to-inode ratio; if that fails, then of
290  * the groups with above-average free space, that group with the fewest
291  * directories already is chosen.
292  *
293  * For other inodes, search forward from the parent directory\'s block
294  * group to find a free inode.
295  */
296 /*
297  * this functino has been reduced to the actual 'find the inode number' part
298  */
299 ino_t ext2_new_inode (const struct inode * dir, int mode)
300 {
301         struct ext2_sb_info * sb;
302         struct buffer_head * bh;
303         struct buffer_head * bh2;
304         int i, j, avefreei;
305         int bitmap_nr;
306         struct ext2_group_desc * gdp;
307         struct ext2_group_desc * tmp;
308         struct ext2_super_block * es;
309
310         if (!dir)
311                 return 0;
312         sb = dir->i_e2fs;
313
314         lock_super (DEVVP(dir));
315         es = sb->s_es;
316 repeat:
317         gdp = NULL; i=0;
318
319         if (S_ISDIR(mode)) {
320                 avefreei = es->s_free_inodes_count /
321                         sb->s_groups_count;
322 /* I am not yet convinced that this next bit is necessary.
323                 i = dir->u.ext2_i.i_block_group;
324                 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
325                         tmp = get_group_desc (sb, i, &bh2);
326                         if ((tmp->bg_used_dirs_count << 8) < 
327                             tmp->bg_free_inodes_count) {
328                                 gdp = tmp;
329                                 break;
330                         }
331                         else
332                         i = ++i % sb->u.ext2_sb.s_groups_count;
333                 }
334 */
335                 if (!gdp) {
336                         for (j = 0; j < sb->s_groups_count; j++) {
337                                 tmp = get_group_desc(ITOV(dir)->v_mount,j,&bh2);
338                                 if (tmp->bg_free_inodes_count &&
339                                         tmp->bg_free_inodes_count >= avefreei) {
340                                         if (!gdp || 
341                                             (tmp->bg_free_blocks_count >
342                                              gdp->bg_free_blocks_count)) {
343                                                 i = j;
344                                                 gdp = tmp;
345                                         }
346                                 }
347                         }
348                 }
349         }
350         else 
351         {
352                 /*
353                  * Try to place the inode in its parent directory
354                  */
355                 i = dir->i_block_group;
356                 tmp = get_group_desc (ITOV(dir)->v_mount, i, &bh2);
357                 if (tmp->bg_free_inodes_count)
358                         gdp = tmp;
359                 else
360                 {
361                         /*
362                          * Use a quadratic hash to find a group with a
363                          * free inode
364                          */
365                         for (j = 1; j < sb->s_groups_count; j <<= 1) {
366                                 i += j;
367                                 if (i >= sb->s_groups_count)
368                                         i -= sb->s_groups_count;
369                                 tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2);
370                                 if (tmp->bg_free_inodes_count) {
371                                         gdp = tmp;
372                                         break;
373                                 }
374                         }
375                 }
376                 if (!gdp) {
377                         /*
378                          * That failed: try linear search for a free inode
379                          */
380                         i = dir->i_block_group + 1;
381                         for (j = 2; j < sb->s_groups_count; j++) {
382                                 if (++i >= sb->s_groups_count)
383                                         i = 0;
384                                 tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2);
385                                 if (tmp->bg_free_inodes_count) {
386                                         gdp = tmp;
387                                         break;
388                                 }
389                         }
390                 }
391         }
392
393         if (!gdp) {
394                 unlock_super (DEVVP(dir));
395                 return 0;
396         }
397         bitmap_nr = load_inode_bitmap (ITOV(dir)->v_mount, i);
398         bh = sb->s_inode_bitmap[bitmap_nr];
399         if ((j = find_first_zero_bit ((unsigned long *) bh->b_data,
400                                       EXT2_INODES_PER_GROUP(sb))) <
401             EXT2_INODES_PER_GROUP(sb)) {
402                 if (set_bit (j, bh->b_data)) {
403                         printf ( "ext2_new_inode:"
404                                       "bit already set for inode %d", j);
405                         goto repeat;
406                 }
407 /* Linux now does the following:
408                 mark_buffer_dirty(bh);
409                 if (sb->s_flags & MS_SYNCHRONOUS) {
410                         ll_rw_block (WRITE, 1, &bh);
411                         wait_on_buffer (bh);
412                 }
413 */
414                 mark_buffer_dirty(bh);
415         } else {
416                 if (gdp->bg_free_inodes_count != 0) {
417                         printf ( "ext2_new_inode:"
418                                     "Free inodes count corrupted in group %d",
419                                     i);
420                         unlock_super (DEVVP(dir));
421                         return 0;
422                 }
423                 goto repeat;
424         }
425         j += i * EXT2_INODES_PER_GROUP(sb) + 1;
426         if (j < EXT2_FIRST_INO || j > es->s_inodes_count) {
427                 printf ( "ext2_new_inode:"
428                             "reserved inode or inode > inodes count - "
429                             "block_group = %d,inode=%d", i, j);
430                 unlock_super (DEVVP(dir));
431                 return 0;
432         }
433         gdp->bg_free_inodes_count--;
434         if (S_ISDIR(mode))
435                 gdp->bg_used_dirs_count++;
436         mark_buffer_dirty(bh2);
437         es->s_free_inodes_count--;
438         /* mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1); */
439         sb->s_dirt = 1;
440         unlock_super (DEVVP(dir));
441         return j;
442 }
443
444 #ifdef unused
445 static unsigned long ext2_count_free_inodes (struct mount * mp)
446 {
447 #ifdef EXT2FS_DEBUG
448         struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs;
449         struct ext2_super_block * es;
450         unsigned long desc_count, bitmap_count, x;
451         int bitmap_nr;
452         struct ext2_group_desc * gdp;
453         int i;
454
455         lock_super (VFSTOUFS(mp)->um_devvp);
456         es = sb->s_es;
457         desc_count = 0;
458         bitmap_count = 0;
459         gdp = NULL;
460         for (i = 0; i < sb->s_groups_count; i++) {
461                 gdp = get_group_desc (mp, i, NULL);
462                 desc_count += gdp->bg_free_inodes_count;
463                 bitmap_nr = load_inode_bitmap (mp, i);
464                 x = ext2_count_free (sb->s_inode_bitmap[bitmap_nr],
465                                      EXT2_INODES_PER_GROUP(sb) / 8);
466                 ext2_debug ("group %d: stored = %d, counted = %lu\n",
467                         i, gdp->bg_free_inodes_count, x);
468                 bitmap_count += x;
469         }
470         ext2_debug("stored = %lu, computed = %lu, %lu\n",
471                 es->s_free_inodes_count, desc_count, bitmap_count);
472         unlock_super (VFSTOUFS(mp)->um_devvp);
473         return desc_count;
474 #else
475         return VFSTOUFS(mp)->um_e2fsb->s_free_inodes_count;
476 #endif
477 }
478 #endif /* unused */
479
480 #ifdef LATER
481 void ext2_check_inodes_bitmap (struct mount * mp)
482 {
483         struct ext2_super_block * es;
484         unsigned long desc_count, bitmap_count, x;
485         int bitmap_nr;
486         struct ext2_group_desc * gdp;
487         int i;
488
489         lock_super (sb);
490         es = sb->u.ext2_sb.s_es;
491         desc_count = 0;
492         bitmap_count = 0;
493         gdp = NULL;
494         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
495                 gdp = get_group_desc (sb, i, NULL);
496                 desc_count += gdp->bg_free_inodes_count;
497                 bitmap_nr = load_inode_bitmap (sb, i);
498                 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
499                                      EXT2_INODES_PER_GROUP(sb) / 8);
500                 if (gdp->bg_free_inodes_count != x)
501                         printf ( "ext2_check_inodes_bitmap:"
502                                     "Wrong free inodes count in group %d, "
503                                     "stored = %d, counted = %lu", i,
504                                     gdp->bg_free_inodes_count, x);
505                 bitmap_count += x;
506         }
507         if (es->s_free_inodes_count != bitmap_count)
508                 printf ( "ext2_check_inodes_bitmap:"
509                             "Wrong free inodes count in super block, "
510                             "stored = %lu, counted = %lu",
511                             (unsigned long) es->s_free_inodes_count, bitmap_count);
512         unlock_super (sb);
513 }
514 #endif