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