Whamcloud - gitweb
6e7b55379645f91d8f55f1f31c46d9eeda67db28
[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 #include <linux/ext2_fs.h>
27
28 #include "ext2fs.h"
29
30 /*
31  * Right now, just search forward from the parent directory's block
32  * group to find the next free inode.
33  *
34  * Should have a special policy for directories.
35  */
36 errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
37                            ext2fs_inode_bitmap map, ino_t *ret)
38 {
39         ino_t   dir_group = 0;
40         ino_t   i;
41         ino_t   start_inode;
42
43         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44         
45         if (!map)
46                 map = fs->inode_map;
47         if (!map)
48                 return EXT2_ET_NO_INODE_BITMAP;
49         
50         if (dir > 0) 
51                 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
52
53         start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
54         if (start_inode < EXT2_FIRST_INODE(fs->super))
55                 start_inode = EXT2_FIRST_INODE(fs->super);
56         i = start_inode;
57
58         do {
59                 if (!ext2fs_test_inode_bitmap(map, i))
60                         break;
61                 i++;
62                 if (i > fs->super->s_inodes_count)
63                         i = EXT2_FIRST_INODE(fs->super);
64         } while (i != start_inode);
65         
66         if (ext2fs_test_inode_bitmap(map, i))
67                 return EXT2_ET_INODE_ALLOC_FAIL;
68         *ret = i;
69         return 0;
70 }
71
72 /*
73  * Stupid algorithm --- we now just search forward starting from the
74  * goal.  Should put in a smarter one someday....
75  */
76 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
77                            ext2fs_block_bitmap map, blk_t *ret)
78 {
79         blk_t   i;
80
81         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
82
83         if (!map)
84                 map = fs->block_map;
85         if (!map)
86                 return EXT2_ET_NO_BLOCK_BITMAP;
87         if (!goal || (goal >= fs->super->s_blocks_count))
88                 goal = fs->super->s_first_data_block;
89         i = goal;
90         do {
91                 if (!ext2fs_test_block_bitmap(map, i)) {
92                         *ret = i;
93                         return 0;
94                 }
95                 i++;
96                 if (i >= fs->super->s_blocks_count)
97                         i = fs->super->s_first_data_block;
98         } while (i != goal);
99         return EXT2_ET_BLOCK_ALLOC_FAIL;
100 }
101
102 /*
103  * This function zeros out the allocated block, and updates all of the
104  * appropriate filesystem records.
105  */
106 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
107                              char *block_buf, blk_t *ret)
108 {
109         errcode_t       retval;
110         blk_t           block;
111         int             group;
112         char            *buf = 0;
113
114         if (!block_buf) {
115                 retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
116                 if (retval)
117                         return retval;
118                 block_buf = buf;
119         }
120         memset(block_buf, 0, fs->blocksize);
121
122         if (!fs->block_map) {
123                 retval = ext2fs_read_block_bitmap(fs);
124                 if (retval)
125                         goto fail;
126         }
127
128         retval = ext2fs_new_block(fs, goal, 0, &block);
129         if (retval)
130                 goto fail;
131
132         retval = io_channel_write_blk(fs->io, block, 1, block_buf);
133         if (retval)
134                 goto fail;
135         
136         fs->super->s_free_blocks_count--;
137         group = ext2fs_group_of_blk(fs, block);
138         fs->group_desc[group].bg_free_blocks_count--;
139         ext2fs_mark_block_bitmap(fs->block_map, block);
140         ext2fs_mark_super_dirty(fs);
141         ext2fs_mark_bb_dirty(fs);
142         *ret = block;
143         return 0;
144
145 fail:
146         if (buf)
147                 ext2fs_free_mem((void **) &buf);
148         return retval;
149 }
150
151 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
152                                  int num, ext2fs_block_bitmap map, blk_t *ret)
153 {
154         blk_t   b = start;
155
156         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
157
158         if (!map)
159                 map = fs->block_map;
160         if (!map)
161                 return EXT2_ET_NO_BLOCK_BITMAP;
162         if (!b)
163                 b = fs->super->s_first_data_block;
164         if (!finish)
165                 finish = start;
166         if (!num)
167                 num = 1;
168         do {
169                 if (b+num-1 > fs->super->s_blocks_count)
170                         b = fs->super->s_first_data_block;
171                 if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
172                         *ret = b;
173                         return 0;
174                 }
175                 b++;
176         } while (b != finish);
177         return EXT2_ET_BLOCK_ALLOC_FAIL;
178 }
179