Whamcloud - gitweb
ChangeLog, openfs.c:
[tools/e2fsprogs.git] / lib / ext2fs / alloc.c
1 /*
2  * alloc.c --- allocate new inodes, blocks for ext2fs
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  * 
11  */
12
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <time.h>
18 #include <string.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #if EXT2_FLAT_INCLUDES
27 #include "ext2_fs.h"
28 #else
29 #include <linux/ext2_fs.h>
30 #endif
31
32 #include "ext2fs.h"
33
34 /*
35  * Right now, just search forward from the parent directory's block
36  * group to find the next free inode.
37  *
38  * Should have a special policy for directories.
39  */
40 errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
41                            ext2fs_inode_bitmap map, ino_t *ret)
42 {
43         ino_t   dir_group = 0;
44         ino_t   i;
45         ino_t   start_inode;
46
47         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
48         
49         if (!map)
50                 map = fs->inode_map;
51         if (!map)
52                 return EXT2_ET_NO_INODE_BITMAP;
53         
54         if (dir > 0) 
55                 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
56
57         start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
58         if (start_inode < EXT2_FIRST_INODE(fs->super))
59                 start_inode = EXT2_FIRST_INODE(fs->super);
60         i = start_inode;
61
62         do {
63                 if (!ext2fs_test_inode_bitmap(map, i))
64                         break;
65                 i++;
66                 if (i > fs->super->s_inodes_count)
67                         i = EXT2_FIRST_INODE(fs->super);
68         } while (i != start_inode);
69         
70         if (ext2fs_test_inode_bitmap(map, i))
71                 return EXT2_ET_INODE_ALLOC_FAIL;
72         *ret = i;
73         return 0;
74 }
75
76 /*
77  * Stupid algorithm --- we now just search forward starting from the
78  * goal.  Should put in a smarter one someday....
79  */
80 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
81                            ext2fs_block_bitmap map, blk_t *ret)
82 {
83         blk_t   i;
84
85         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
86
87         if (!map)
88                 map = fs->block_map;
89         if (!map)
90                 return EXT2_ET_NO_BLOCK_BITMAP;
91         if (!goal || (goal >= fs->super->s_blocks_count))
92                 goal = fs->super->s_first_data_block;
93         i = goal;
94         do {
95                 if (!ext2fs_test_block_bitmap(map, i)) {
96                         *ret = i;
97                         return 0;
98                 }
99                 i++;
100                 if (i >= fs->super->s_blocks_count)
101                         i = fs->super->s_first_data_block;
102         } while (i != goal);
103         return EXT2_ET_BLOCK_ALLOC_FAIL;
104 }
105
106 /*
107  * This function zeros out the allocated block, and updates all of the
108  * appropriate filesystem records.
109  */
110 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
111                              char *block_buf, blk_t *ret)
112 {
113         errcode_t       retval;
114         blk_t           block;
115         int             group;
116         char            *buf = 0;
117
118         if (!block_buf) {
119                 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
120                 if (retval)
121                         return retval;
122                 block_buf = buf;
123         }
124         memset(block_buf, 0, fs->blocksize);
125
126         if (!fs->block_map) {
127                 retval = ext2fs_read_block_bitmap(fs);
128                 if (retval)
129                         goto fail;
130         }
131
132         retval = ext2fs_new_block(fs, goal, 0, &block);
133         if (retval)
134                 goto fail;
135
136         retval = io_channel_write_blk(fs->io, block, 1, block_buf);
137         if (retval)
138                 goto fail;
139         
140         fs->super->s_free_blocks_count--;
141         group = ext2fs_group_of_blk(fs, block);
142         fs->group_desc[group].bg_free_blocks_count--;
143         ext2fs_mark_block_bitmap(fs->block_map, block);
144         ext2fs_mark_super_dirty(fs);
145         ext2fs_mark_bb_dirty(fs);
146         *ret = block;
147         return 0;
148
149 fail:
150         if (buf)
151                 ext2fs_free_mem((void **) &buf);
152         return retval;
153 }
154
155 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
156                                  int num, ext2fs_block_bitmap map, blk_t *ret)
157 {
158         blk_t   b = start;
159
160         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
161
162         if (!map)
163                 map = fs->block_map;
164         if (!map)
165                 return EXT2_ET_NO_BLOCK_BITMAP;
166         if (!b)
167                 b = fs->super->s_first_data_block;
168         if (!finish)
169                 finish = start;
170         if (!num)
171                 num = 1;
172         do {
173                 if (b+num-1 > fs->super->s_blocks_count)
174                         b = fs->super->s_first_data_block;
175                 if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
176                         *ret = b;
177                         return 0;
178                 }
179                 b++;
180         } while (b != finish);
181         return EXT2_ET_BLOCK_ALLOC_FAIL;
182 }
183