Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / alloc.c
1 /*
2  * alloc.c --- allocate new inodes, blocks for ext2fs
3  *
4  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14
15 #include <linux/fs.h>
16 #include <linux/ext2_fs.h>
17
18 #include "ext2fs.h"
19
20 /*
21  * Right now, just search forward from the parent directory's block
22  * group to find the next free inode.
23  *
24  * Should have a special policy for directories.
25  */
26 errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode, char *map,
27                            ino_t *ret)
28 {
29         int     dir_group = 0;
30         ino_t   i;
31         ino_t   start_inode;
32
33         if (!map)
34                 map = fs->inode_map;
35         if (!map)
36                 return EXT2_ET_NO_INODE_BITMAP;
37         
38         if (dir > 0) 
39                 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
40
41         start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
42         i = start_inode;
43         if (i < EXT2_FIRST_INO)
44                 i = EXT2_FIRST_INO;
45
46         do {
47                 if (!ext2fs_test_inode_bitmap(fs, map, i))
48                         break;
49                 i++;
50                 if (i > fs->super->s_inodes_count)
51                         i = EXT2_FIRST_INO;
52         } while (i != start_inode);
53         
54         if (ext2fs_test_inode_bitmap(fs, map, i))
55                 return ENOSPC;
56         *ret = i;
57         return 0;
58 }
59
60 /*
61  * Stupid algorithm --- we now just search forward starting from the
62  * goal.  Should put in a smarter one someday....
63  */
64 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal, char *map, blk_t *ret)
65 {
66         blk_t   i = goal;
67
68         if (!map)
69                 map = fs->block_map;
70         if (!map)
71                 return EXT2_ET_NO_BLOCK_BITMAP;
72         if (!i)
73                 i = fs->super->s_first_data_block;
74         do {
75                 if (!ext2fs_test_block_bitmap(fs, map, i)) {
76                         *ret = i;
77                         return 0;
78                 }
79                 i++;
80                 if (i > fs->super->s_blocks_count)
81                         i = fs->super->s_first_data_block;
82         } while (i != goal);
83         return ENOSPC;
84 }
85
86 static int check_blocks_free(ext2_filsys fs, char *map, blk_t blk, int num)
87 {
88         int     i;
89
90         for (i=0; i < num; i++) {
91                 if ((blk+i) > fs->super->s_blocks_count)
92                         return 0;
93                 if (ext2fs_test_block_bitmap(fs, map, blk+i))
94                         return 0;
95         }
96         return 1;
97 }
98
99 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
100                                  int num, char *map, blk_t *ret)
101 {
102         blk_t   b = start;
103
104         if (!map)
105                 map = fs->block_map;
106         if (!map)
107                 return EXT2_ET_NO_BLOCK_BITMAP;
108         if (!b)
109                 b = fs->super->s_first_data_block;
110         if (!finish)
111                 finish = start;
112         if (!num)
113                 num = 1;
114         do {
115                 if (check_blocks_free(fs, map, b, num)) {
116                         *ret = b;
117                         return 0;
118                 }
119                 b++;
120                 if (b > fs->super->s_blocks_count)
121                         b = fs->super->s_first_data_block;
122         } while (b != finish);
123         return ENOSPC;
124 }
125