6 #include <linux/time.h>
8 #include <linux/errno.h>
10 #include <linux/kernel.h>
16 #define PREALLOCATION_SIZE 9
20 #define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)
22 #define _ALLOC_concentrating_formatted_nodes 0
23 #define _ALLOC_displacing_large_files 1
24 #define _ALLOC_displacing_new_packing_localities 2
25 #define _ALLOC_old_hashed_relocation 3
26 #define _ALLOC_new_hashed_relocation 4
27 #define _ALLOC_skip_busy 5
28 #define _ALLOC_displace_based_on_dirid 6
29 #define _ALLOC_hashed_formatted_nodes 7
30 #define _ALLOC_old_way 8
31 #define _ALLOC_hundredth_slices 9
32 #define _ALLOC_dirid_groups 10
33 #define _ALLOC_oid_groups 11
34 #define _ALLOC_packing_groups 12
36 #define concentrating_formatted_nodes(s) test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
37 #define displacing_large_files(s) test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
38 #define displacing_new_packing_localities(s) test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))
40 #define SET_OPTION(optname) \
42 reiserfs_info(s, "block allocator option \"%s\" is set", #optname); \
43 set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
45 #define TEST_OPTION(optname, s) \
46 test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
48 static inline void get_bit_address(
struct super_block *
s,
50 unsigned int *bmap_nr,
63 unsigned int bmap_count = reiserfs_bmap_count(s);
67 "block number is out of range %lu (%u)",
72 get_bit_address(s, block, &bmap, &offset);
77 &(REISERFS_SB(s)->s_properties)))) {
78 b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
80 block <= bmap1 + bmap_count) {
82 "can't be freed or reused",
89 "can't be freed or reused",
95 if (bmap >= bmap_count) {
97 "is out of range: block=%lu, bitmap_nr=%u",
113 static inline int is_block_in_journal(
struct super_block *
s,
unsigned int bmap,
135 unsigned int bmap_n,
int *beg,
int boundary,
136 int min,
int max,
int unfm)
140 struct buffer_head *bh;
146 RFALSE(bmap_n >= reiserfs_bmap_count(s),
"Bitmap %u is out of "
147 "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
156 "for bitmap %d", bmap_n);
173 ((
unsigned long *)(bh->b_data), boundary, *beg);
175 if (*beg + min > boundary) {
181 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
184 for (end = *beg + 1;; end++) {
185 if (end >= *beg + max || end >= boundary
192 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
198 if (end - *beg >= min) {
202 for (i = *beg; i <
end; i++) {
209 if (i >= *beg + min) {
243 char *hash_in =
NULL;
250 hash_in = (
char *)(&
id);
252 bm = hash % reiserfs_bmap_count(s);
257 if (bm >= reiserfs_bmap_count(s))
268 int bm = bmap_hash_id(s,
id);
299 if (parent_dir == 1 || block_group_used(dir->
i_sb, parent_dir))
312 int min,
int max,
int unfm,
sector_t file_block)
314 int nr_allocated = 0;
319 unsigned int bm, off;
320 unsigned int end_bm, end_off;
329 get_bit_address(s, *start, &bm, &off);
330 get_bit_address(s, finish, &end_bm, &end_off);
331 if (bm > reiserfs_bmap_count(s))
333 if (end_bm > reiserfs_bmap_count(s))
334 end_bm = reiserfs_bmap_count(s);
350 for (; bm < end_bm; bm++, off = 0) {
351 if ((off && (!unfm || (file_block != 0)))
355 scan_bitmap_block(th, bm, &off, off_max,
361 get_bit_address(s, *start, &bm, &off);
364 for (; bm < end_bm; bm++, off = 0) {
366 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
372 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
375 *start = bm * off_max + off;
386 struct buffer_head *sbh, *bmbh;
398 get_bit_address(s, block, &nr, &offset);
400 if (nr >= reiserfs_bmap_count(s)) {
415 "block %lu: bit already cleared", block);
427 dquot_free_block_nodirty(inode, 1);
437 RFALSE(!s,
"vs-4061: trying to free block on nonexistent device");
443 "Trying to free block outside file system "
444 "boundaries (%lu > %lu)",
450 _reiserfs_free_block(th, inode, block, for_unformatted);
459 "vs-4060: trying to free block on nonexistent device");
462 _reiserfs_free_block(th, inode, block, 1);
472 #ifdef CONFIG_REISERFS_CHECK
475 "inode has negative prealloc blocks count.");
484 reiserfs_update_sd(th, inode);
496 __discard_prealloc(th, ei);
505 while (!list_empty(plist)) {
509 #ifdef CONFIG_REISERFS_CHECK
512 "inode is in prealloc list but has "
513 "no preallocated blocks.");
516 __discard_prealloc(th, ei);
530 char *this_char, *
value;
532 REISERFS_SB(s)->s_alloc_options.bits = 0;
534 while ((this_char =
strsep(&options,
":")) !=
NULL) {
538 if (!
strcmp(this_char,
"concentrating_formatted_nodes")) {
544 if (temp <= 0 || temp > 100) {
545 REISERFS_SB(s)->s_alloc_options.border = 10;
547 REISERFS_SB(s)->s_alloc_options.border =
552 if (!
strcmp(this_char,
"displacing_large_files")) {
554 REISERFS_SB(s)->s_alloc_options.large_file_size =
559 if (!
strcmp(this_char,
"displacing_new_packing_localities")) {
564 if (!
strcmp(this_char,
"old_hashed_relocation")) {
569 if (!
strcmp(this_char,
"new_hashed_relocation")) {
574 if (!
strcmp(this_char,
"dirid_groups")) {
578 if (!
strcmp(this_char,
"oid_groups")) {
582 if (!
strcmp(this_char,
"packing_groups")) {
586 if (!
strcmp(this_char,
"hashed_formatted_nodes")) {
591 if (!
strcmp(this_char,
"skip_busy")) {
596 if (!
strcmp(this_char,
"hundredth_slices")) {
601 if (!
strcmp(this_char,
"old_way")) {
606 if (!
strcmp(this_char,
"displace_based_on_dirid")) {
611 if (!
strcmp(this_char,
"preallocmin")) {
612 REISERFS_SB(s)->s_alloc_options.preallocmin =
618 if (!
strcmp(this_char,
"preallocsize")) {
619 REISERFS_SB(s)->s_alloc_options.preallocsize =
655 print_sep(seq, &first);
656 if (REISERFS_SB(s)->s_alloc_options.border != 10) {
657 seq_printf(seq,
"concentrating_formatted_nodes=%d",
658 100 / REISERFS_SB(s)->s_alloc_options.border);
660 seq_puts(seq,
"concentrating_formatted_nodes");
663 print_sep(seq, &first);
664 if (REISERFS_SB(s)->s_alloc_options.large_file_size != 16) {
666 REISERFS_SB(s)->s_alloc_options.large_file_size);
668 seq_puts(seq,
"displacing_large_files");
671 print_sep(seq, &first);
672 seq_puts(seq,
"displacing_new_packing_localities");
675 print_sep(seq, &first);
676 seq_puts(seq,
"old_hashed_relocation");
679 print_sep(seq, &first);
680 seq_puts(seq,
"new_hashed_relocation");
683 print_sep(seq, &first);
687 print_sep(seq, &first);
691 print_sep(seq, &first);
695 print_sep(seq, &first);
696 seq_puts(seq,
"hashed_formatted_nodes");
699 print_sep(seq, &first);
703 print_sep(seq, &first);
707 print_sep(seq, &first);
711 print_sep(seq, &first);
712 seq_puts(seq,
"displace_based_on_dirid");
714 if (REISERFS_SB(s)->s_alloc_options.preallocmin != 0) {
715 print_sep(seq, &first);
717 REISERFS_SB(s)->s_alloc_options.preallocmin);
719 if (REISERFS_SB(s)->s_alloc_options.preallocsize != 17) {
720 print_sep(seq, &first);
722 REISERFS_SB(s)->s_alloc_options.preallocsize);
730 hash_in = (
char *)&hint->
key.k_dir_id;
734 hash_in = (
char *)&hint->
key.k_dir_id;
760 dirid = hint->
key.k_dir_id;
763 bm = bmap_hash_id(sb, dirid);
790 hash = (hint->
inode->i_sb->s_blocksize << 3);
793 bm = bmap_hash_id(hint->
inode->i_sb, oid);
794 hash = bm * (hint->
inode->i_sb->s_blocksize << 3);
806 struct buffer_head *bh;
818 RFALSE(!bh,
"green-4002: Illegal path specified to get_left_neighbor");
831 while (pos_in_item >= 0) {
850 static inline void set_border_in_hint(
struct super_block *s,
857 hint->
end = border - 1;
868 4) % (hint->
end - hint->
beg);
873 4) % (hint->
end - hint->
beg);
881 hash_in = (
char *)&hint->
key.k_dir_id;
895 return hint->
block ==
896 REISERFS_SB(hint->
th->t_super)->s_alloc_options.large_file_size;
899 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
904 hint->
th->displace_new_blocks = 0;
907 4) % (hint->
end - hint->
beg);
923 4) % (hint->
end - hint->
beg - 1);
972 set_border_in_hint(s, hint);
974 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
979 && hint->
th->displace_new_blocks) {
980 displace_new_packing_locality(hint);
992 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
993 displace_large_file(hint);
1000 hash_formatted_node(hint);
1004 unfm_hint = get_left_neighbor(hint);
1015 old_hashed_relocation(hint);
1019 REISERFS_I(hint->
inode)->i_prealloc_block)
1021 REISERFS_I(hint->
inode)->i_prealloc_block;
1029 hundredth_slices(hint);
1036 old_hashed_relocation(hint);
1041 new_hashed_relocation(hint);
1047 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1070 && hint->
inode->i_size >=
1071 REISERFS_SB(hint->
th->t_super)->s_alloc_options.
1072 preallocmin * hint->
inode->i_sb->s_blocksize)
1074 REISERFS_SB(hint->
th->t_super)->s_alloc_options.
1089 int rest = amount_needed;
1092 while (rest > 0 && start <= finish) {
1093 nr_allocated = scan_bitmap(hint->
th, &start, finish, min,
1094 rest + prealloc_size,
1097 if (nr_allocated == 0)
1101 while (rest > 0 && nr_allocated > 0) {
1102 *new_blocknrs++ = start++;
1108 if (nr_allocated > 0) {
1110 list_add(&REISERFS_I(hint->
inode)->i_prealloc_list,
1113 REISERFS_I(hint->
inode)->i_prealloc_block =
start;
1114 REISERFS_I(hint->
inode)->i_prealloc_count =
1120 return (amount_needed - rest);
1123 static inline int blocknrs_and_prealloc_arrays_from_search_start
1125 int amount_needed) {
1130 int nr_allocated = 0;
1132 determine_prealloc_size(hint);
1135 #ifdef REISERQUOTA_DEBUG
1137 "reiserquota: allocating %d blocks id=%u",
1138 amount_needed, hint->
inode->i_uid);
1141 dquot_alloc_block_nodirty(hint->
inode, amount_needed);
1145 #ifdef REISERQUOTA_DEBUG
1147 "reiserquota: allocating (prealloc) %d blocks id=%u",
1150 quota_ret = dquot_prealloc_block_nodirty(hint->
inode,
1175 #ifdef REISERQUOTA_DEBUG
1177 "reiserquota: freeing (nospace) %d blocks id=%u",
1181 hint->
inode->i_uid);
1184 dquot_free_block_nodirty(hint->
inode,
1188 while (nr_allocated--)
1190 new_blocknrs[nr_allocated],
1195 }
while ((nr_allocated += allocate_without_wrapping_disk(hint,
1207 nr_allocated + REISERFS_I(hint->
inode)->i_prealloc_count) {
1209 #ifdef REISERQUOTA_DEBUG
1211 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1214 REISERFS_I(hint->
inode)->i_prealloc_count,
1215 hint->
inode->i_uid);
1217 dquot_free_block_nodirty(hint->
inode, amount_needed +
1219 REISERFS_I(hint->
inode)->
1232 struct inode *inode = hint->
inode;
1234 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1235 while (amount_needed) {
1237 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1238 REISERFS_I(inode)->i_prealloc_count--;
1242 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1243 list_del(&REISERFS_I(inode)->i_prealloc_list);
1249 return amount_needed;
1255 int initial_amount_needed = amount_needed;
1261 amount_needed - reserved_by_us)
1270 amount_needed = use_preallocated_list_if_available
1271 (hint, new_blocknrs, amount_needed);
1272 if (amount_needed == 0)
1275 new_blocknrs += (initial_amount_needed - amount_needed);
1279 determine_search_start(hint, amount_needed);
1284 ret = blocknrs_and_prealloc_arrays_from_search_start
1285 (hint, new_blocknrs, amount_needed);
1292 while (amount_needed++ < initial_amount_needed) {
1294 *(--new_blocknrs), 1);
1301 struct buffer_head *bh,
1304 unsigned long *
cur = (
unsigned long *)(bh->b_data + bh->b_size);
1309 "corrupted: first bit must be 1", bh->b_blocknr);
1313 while (--cur >= (
unsigned long *)bh->b_data) {
1317 else if (*cur != ~0
L)
1327 struct buffer_head *bh;
1332 &(REISERFS_SB(sb)->s_properties))))
1333 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 +
bitmap;
1334 else if (bitmap == 0)
1337 bh = sb_bread(sb, block);
1340 "reading failed", __func__, block);
1342 if (buffer_locked(bh)) {
1348 BUG_ON(!buffer_uptodate(bh));
1361 unsigned int bmap_nr = reiserfs_bmap_count(sb);
1363 bitmap =
vmalloc(
sizeof(*bitmap) * bmap_nr);
1367 memset(bitmap, 0xff,
sizeof(*bitmap) * bmap_nr);