Whamcloud - gitweb
Fix dbg_print() format for unsigned long long.
[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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
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 "ext2_fs.h"
27 #include "ext2fs.h"
28
29 #define min(a, b) ((a) < (b) ? (a) : (b))
30
31 #undef DEBUG
32
33 #ifdef DEBUG
34 # define dbg_printf(f, a...)  do {printf(f, ## a); fflush(stdout); } while (0)
35 #else
36 # define dbg_printf(f, a...)
37 #endif
38
39 /*
40  * Clear the uninit block bitmap flag if necessary
41  */
42 void ext2fs_clear_block_uninit(ext2_filsys fs, dgrp_t group)
43 {
44         if (group >= fs->group_desc_count ||
45             !ext2fs_has_group_desc_csum(fs) ||
46             !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
47                 return;
48
49         /* uninit block bitmaps are now initialized in read_bitmaps() */
50
51         ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
52         ext2fs_group_desc_csum_set(fs, group);
53         ext2fs_mark_super_dirty(fs);
54         ext2fs_mark_bb_dirty(fs);
55 }
56
57 /*
58  * Check for uninit inode bitmaps and deal with them appropriately
59  */
60 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
61                           dgrp_t group)
62 {
63         ext2_ino_t      i, ino;
64
65         if (group >= fs->group_desc_count ||
66             !ext2fs_has_group_desc_csum(fs) ||
67             !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
68                 return;
69
70         ino = (group * fs->super->s_inodes_per_group) + 1;
71         for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
72                 ext2fs_fast_unmark_inode_bitmap2(map, ino);
73
74         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
75         /* Mimics what the kernel does */
76         ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
77         ext2fs_group_desc_csum_set(fs, group);
78         ext2fs_mark_ib_dirty(fs);
79         ext2fs_mark_super_dirty(fs);
80 }
81
82 /*
83  * Right now, just search forward from the parent directory's block
84  * group to find the next free inode.
85  *
86  * Should have a special policy for directories.
87  */
88 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
89                            int mode EXT2FS_ATTR((unused)),
90                            ext2fs_inode_bitmap map, ext2_ino_t *ret)
91 {
92         ext2_ino_t      start_inode = 0;
93         ext2_ino_t      i, ino_in_group, upto, first_zero;
94         errcode_t       retval;
95         dgrp_t          group;
96
97         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
98
99         if (!map)
100                 map = fs->inode_map;
101         if (!map)
102                 return EXT2_ET_NO_INODE_BITMAP;
103
104         if (dir > 0) {
105                 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
106                 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
107         }
108         if (start_inode < EXT2_FIRST_INODE(fs->super))
109                 start_inode = EXT2_FIRST_INODE(fs->super);
110         if (start_inode > fs->super->s_inodes_count)
111                 return EXT2_ET_INODE_ALLOC_FAIL;
112         i = start_inode;
113         do {
114                 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
115                 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
116
117                 check_inode_uninit(fs, map, group);
118                 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
119                 if (i < start_inode && upto >= start_inode)
120                         upto = start_inode - 1;
121                 if (upto > fs->super->s_inodes_count)
122                         upto = fs->super->s_inodes_count;
123
124                 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
125                                                               &first_zero);
126                 if (retval == 0) {
127                         i = first_zero;
128                         break;
129                 }
130                 if (retval != ENOENT)
131                         return EXT2_ET_INODE_ALLOC_FAIL;
132                 i = upto + 1;
133                 if (i > fs->super->s_inodes_count)
134                         i = EXT2_FIRST_INODE(fs->super);
135         } while (i != start_inode);
136
137         if (ext2fs_test_inode_bitmap2(map, i))
138                 return EXT2_ET_INODE_ALLOC_FAIL;
139         *ret = i;
140         return 0;
141 }
142
143 /*
144  * Stupid algorithm --- we now just search forward starting from the
145  * goal.  Should put in a smarter one someday....
146  */
147 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
148                            ext2fs_block_bitmap map, blk64_t *ret)
149 {
150         errcode_t retval;
151         blk64_t b = 0;
152         errcode_t (*gab)(ext2_filsys fs, blk64_t goal, blk64_t *ret);
153
154         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
155
156         if (!map && fs->get_alloc_block) {
157                 /*
158                  * In case there are clients out there whose get_alloc_block
159                  * handlers call ext2fs_new_block2 with a NULL block map,
160                  * temporarily swap out the function pointer so that we don't
161                  * end up in an infinite loop.
162                  */
163                 gab = fs->get_alloc_block;
164                 fs->get_alloc_block = NULL;
165                 retval = gab(fs, goal, &b);
166                 fs->get_alloc_block = gab;
167                 goto allocated;
168         }
169         if (!map)
170                 map = fs->block_map;
171         if (!map)
172                 return EXT2_ET_NO_BLOCK_BITMAP;
173         if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
174                 goal = fs->super->s_first_data_block;
175         goal &= ~EXT2FS_CLUSTER_MASK(fs);
176
177         retval = ext2fs_find_first_zero_block_bitmap2(map,
178                         goal, ext2fs_blocks_count(fs->super) - 1, &b);
179         if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
180                 retval = ext2fs_find_first_zero_block_bitmap2(map,
181                         fs->super->s_first_data_block, goal - 1, &b);
182 allocated:
183         if (retval == ENOENT)
184                 return EXT2_ET_BLOCK_ALLOC_FAIL;
185         if (retval)
186                 return retval;
187
188         ext2fs_clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
189         *ret = b;
190         return 0;
191 }
192
193 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
194                            ext2fs_block_bitmap map, blk_t *ret)
195 {
196         errcode_t retval;
197         blk64_t val;
198         retval = ext2fs_new_block2(fs, goal, map, &val);
199         if (!retval)
200                 *ret = (blk_t) val;
201         return retval;
202 }
203
204 /*
205  * This function zeros out the allocated block, and updates all of the
206  * appropriate filesystem records.
207  */
208 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
209                              char *block_buf, blk64_t *ret)
210 {
211         errcode_t       retval;
212         blk64_t         block;
213
214         if (fs->get_alloc_block) {
215                 retval = (fs->get_alloc_block)(fs, goal, &block);
216                 if (retval)
217                         goto fail;
218         } else {
219                 if (!fs->block_map) {
220                         retval = ext2fs_read_block_bitmap(fs);
221                         if (retval)
222                                 goto fail;
223                 }
224
225                 retval = ext2fs_new_block2(fs, goal, 0, &block);
226                 if (retval)
227                         goto fail;
228         }
229
230         if (block_buf) {
231                 memset(block_buf, 0, fs->blocksize);
232                 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
233         } else
234                 retval = ext2fs_zero_blocks2(fs, block, 1, NULL, NULL);
235         if (retval)
236                 goto fail;
237
238         ext2fs_block_alloc_stats2(fs, block, +1);
239         *ret = block;
240
241 fail:
242         return retval;
243 }
244
245 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
246                              char *block_buf, blk_t *ret)
247 {
248         errcode_t retval;
249         blk64_t val;
250         retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
251         if (!retval)
252                 *ret = (blk_t) val;
253         return retval;
254 }
255
256 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
257                                  int num, ext2fs_block_bitmap map, blk64_t *ret)
258 {
259         blk64_t b = start;
260         int     c_ratio;
261
262         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
263
264         if (!map)
265                 map = fs->block_map;
266         if (!map)
267                 return EXT2_ET_NO_BLOCK_BITMAP;
268         if (!b)
269                 b = fs->super->s_first_data_block;
270         if (!finish)
271                 finish = start;
272         if (!num)
273                 num = 1;
274         c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
275         b &= ~(c_ratio - 1);
276         finish &= ~(c_ratio -1);
277         do {
278                 if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
279                         if (finish > start)
280                                 return EXT2_ET_BLOCK_ALLOC_FAIL;
281                         b = fs->super->s_first_data_block;
282                 }
283                 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
284                         *ret = b;
285                         return 0;
286                 }
287                 b += c_ratio;
288         } while (b != finish);
289         return EXT2_ET_BLOCK_ALLOC_FAIL;
290 }
291
292 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
293                                  int num, ext2fs_block_bitmap map, blk_t *ret)
294 {
295         errcode_t retval;
296         blk64_t val;
297         retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
298         if(!retval)
299                 *ret = (blk_t) val;
300         return retval;
301 }
302
303 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
304                                      errcode_t (*func)(ext2_filsys fs,
305                                                        blk64_t goal,
306                                                        blk64_t *ret),
307                                      errcode_t (**old)(ext2_filsys fs,
308                                                        blk64_t goal,
309                                                        blk64_t *ret))
310 {
311         if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
312                 return;
313
314         if (old)
315                 *old = fs->get_alloc_block;
316
317         fs->get_alloc_block = func;
318 }
319
320 blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
321                                struct ext2_inode *inode, blk64_t lblk)
322 {
323         dgrp_t                  group;
324         __u8                    log_flex;
325         struct ext2fs_extent    extent;
326         ext2_extent_handle_t    handle = NULL;
327         errcode_t               err;
328
329         if (inode == NULL || ext2fs_inode_data_blocks2(fs, inode) == 0)
330                 goto no_blocks;
331
332         if (inode->i_flags & EXT4_INLINE_DATA_FL)
333                 goto no_blocks;
334
335         if (inode->i_flags & EXT4_EXTENTS_FL) {
336                 err = ext2fs_extent_open2(fs, ino, inode, &handle);
337                 if (err)
338                         goto no_blocks;
339                 err = ext2fs_extent_goto2(handle, 0, lblk);
340                 if (err)
341                         goto no_blocks;
342                 err = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
343                 if (err)
344                         goto no_blocks;
345                 ext2fs_extent_free(handle);
346                 return extent.e_pblk + (lblk - extent.e_lblk);
347         }
348
349         /* block mapped file; see if block zero is mapped? */
350         if (inode->i_block[0])
351                 return inode->i_block[0];
352
353 no_blocks:
354         ext2fs_extent_free(handle);
355         log_flex = fs->super->s_log_groups_per_flex;
356         group = ext2fs_group_of_ino(fs, ino);
357         if (log_flex)
358                 group = group & ~((1 << (log_flex)) - 1);
359         return ext2fs_group_first_block2(fs, group);
360 }
361
362 /*
363  * Starting at _goal_, scan around the filesystem to find a run of free blocks
364  * that's at least _len_ blocks long.  Possible flags:
365  * - EXT2_NEWRANGE_EXACT_GOAL: The range of blocks must start at _goal_.
366  * - EXT2_NEWRANGE_MIN_LENGTH: do not return a allocation shorter than _len_.
367  * - EXT2_NEWRANGE_ZERO_BLOCKS: Zero blocks pblk to pblk+plen before returning.
368  *
369  * The starting block is returned in _pblk_ and the length is returned via
370  * _plen_.  The blocks are not marked in the bitmap; the caller must mark
371  * however much of the returned run they actually use, hopefully via
372  * ext2fs_block_alloc_stats_range().
373  *
374  * This function can return a range that is longer than what was requested.
375  */
376 errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
377                            blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
378                            blk64_t *plen)
379 {
380         errcode_t retval;
381         blk64_t start, end, b;
382         int looped = 0;
383         blk64_t max_blocks = ext2fs_blocks_count(fs->super);
384         errcode_t (*nrf)(ext2_filsys fs, int flags, blk64_t goal,
385                          blk64_t len, blk64_t *pblk, blk64_t *plen);
386
387         dbg_printf("%s: flags=0x%x goal=%llu len=%llu\n", __func__, flags,
388                    goal, len);
389         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
390         if (len == 0 || (flags & ~EXT2_NEWRANGE_ALL_FLAGS))
391                 return EXT2_ET_INVALID_ARGUMENT;
392
393         if (!map && fs->new_range) {
394                 /*
395                  * In case there are clients out there whose new_range
396                  * handlers call ext2fs_new_range with a NULL block map,
397                  * temporarily swap out the function pointer so that we don't
398                  * end up in an infinite loop.
399                  */
400                 nrf = fs->new_range;
401                 fs->new_range = NULL;
402                 retval = nrf(fs, flags, goal, len, pblk, plen);
403                 fs->new_range = nrf;
404                 if (retval)
405                         return retval;
406                 start = *pblk;
407                 end = *pblk + *plen;
408                 goto allocated;
409         }
410         if (!map)
411                 map = fs->block_map;
412         if (!map)
413                 return EXT2_ET_NO_BLOCK_BITMAP;
414         if (!goal || goal >= ext2fs_blocks_count(fs->super))
415                 goal = fs->super->s_first_data_block;
416
417         start = goal;
418         while (!looped || start <= goal) {
419                 retval = ext2fs_find_first_zero_block_bitmap2(map, start,
420                                                               max_blocks - 1,
421                                                               &start);
422                 if (retval == ENOENT) {
423                         /*
424                          * If there are no free blocks beyond the starting
425                          * point, try scanning the whole filesystem, unless the
426                          * user told us only to allocate from _goal_, or if
427                          * we're already scanning the whole filesystem.
428                          */
429                         if (flags & EXT2_NEWRANGE_FIXED_GOAL ||
430                             start == fs->super->s_first_data_block)
431                                 goto fail;
432                         start = fs->super->s_first_data_block;
433                         continue;
434                 } else if (retval)
435                         goto errout;
436
437                 if (flags & EXT2_NEWRANGE_FIXED_GOAL && start != goal)
438                         goto fail;
439
440                 b = min(start + len - 1, max_blocks - 1);
441                 retval =  ext2fs_find_first_set_block_bitmap2(map, start, b,
442                                                               &end);
443                 if (retval == ENOENT)
444                         end = b + 1;
445                 else if (retval)
446                         goto errout;
447
448                 if (!(flags & EXT2_NEWRANGE_MIN_LENGTH) ||
449                     (end - start) >= len) {
450                         /* Success! */
451                         *pblk = start;
452                         *plen = end - start;
453                         dbg_printf("%s: new_range goal=%llu--%llu "
454                                    "blk=%llu--%llu %llu\n",
455                                    __func__, goal, goal + len - 1,
456                                    *pblk, *pblk + *plen - 1, *plen);
457 allocated:
458                         for (b = start; b < end;
459                              b += fs->super->s_blocks_per_group)
460                                 ext2fs_clear_block_uninit(fs,
461                                                 ext2fs_group_of_blk2(fs, b));
462                         return 0;
463                 }
464
465                 if (flags & EXT2_NEWRANGE_FIXED_GOAL)
466                         goto fail;
467                 start = end;
468                 if (start >= max_blocks) {
469                         if (looped)
470                                 goto fail;
471                         looped = 1;
472                         start = fs->super->s_first_data_block;
473                 }
474         }
475
476 fail:
477         retval = EXT2_ET_BLOCK_ALLOC_FAIL;
478 errout:
479         return retval;
480 }
481
482 void ext2fs_set_new_range_callback(ext2_filsys fs,
483         errcode_t (*func)(ext2_filsys fs, int flags, blk64_t goal,
484                                blk64_t len, blk64_t *pblk, blk64_t *plen),
485         errcode_t (**old)(ext2_filsys fs, int flags, blk64_t goal,
486                                blk64_t len, blk64_t *pblk, blk64_t *plen))
487 {
488         if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
489                 return;
490
491         if (old)
492                 *old = fs->new_range;
493
494         fs->new_range = func;
495 }
496
497 errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
498                              blk_t len, blk64_t *ret)
499 {
500         int newr_flags = EXT2_NEWRANGE_MIN_LENGTH;
501         errcode_t retval;
502         blk64_t plen;
503
504         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
505         if (len == 0 || (flags & ~EXT2_ALLOCRANGE_ALL_FLAGS))
506                 return EXT2_ET_INVALID_ARGUMENT;
507
508         if (flags & EXT2_ALLOCRANGE_FIXED_GOAL)
509                 newr_flags |= EXT2_NEWRANGE_FIXED_GOAL;
510
511         retval = ext2fs_new_range(fs, newr_flags, goal, len, NULL, ret, &plen);
512         if (retval)
513                 return retval;
514
515         if (plen < len)
516                 return EXT2_ET_BLOCK_ALLOC_FAIL;
517
518         if (flags & EXT2_ALLOCRANGE_ZERO_BLOCKS) {
519                 retval = ext2fs_zero_blocks2(fs, *ret, len, NULL, NULL);
520                 if (retval)
521                         return retval;
522         }
523
524         ext2fs_block_alloc_stats_range(fs, *ret, len, +1);
525         return retval;
526 }