2 * alloc.c - NILFS dat/inode allocator
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Original code was written by Koji Sato <koji@osrg.net>.
21 * Two allocators were unified by Ryusuke Konishi <ryusuke@osrg.net>,
22 * Amagai Yoshiji <amagai@osrg.net>.
25 #include <linux/types.h>
26 #include <linux/buffer_head.h>
28 #include <linux/bitops.h>
29 #include <linux/slab.h>
34 static inline unsigned long
35 nilfs_palloc_groups_per_desc_block(const struct inode
*inode
)
37 return (1UL << inode
->i_blkbits
) /
38 sizeof(struct nilfs_palloc_group_desc
);
41 static inline unsigned long
42 nilfs_palloc_groups_count(const struct inode
*inode
)
44 return 1UL << (BITS_PER_LONG
- (inode
->i_blkbits
+ 3 /* log2(8) */));
47 int nilfs_palloc_init_blockgroup(struct inode
*inode
, unsigned entry_size
)
49 struct nilfs_mdt_info
*mi
= NILFS_MDT(inode
);
51 mi
->mi_bgl
= kmalloc(sizeof(*mi
->mi_bgl
), GFP_NOFS
);
55 bgl_lock_init(mi
->mi_bgl
);
57 nilfs_mdt_set_entry_size(inode
, entry_size
, 0);
59 mi
->mi_blocks_per_group
=
60 DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode
),
61 mi
->mi_entries_per_block
) + 1;
62 /* Number of blocks in a group including entry blocks and
64 mi
->mi_blocks_per_desc_block
=
65 nilfs_palloc_groups_per_desc_block(inode
) *
66 mi
->mi_blocks_per_group
+ 1;
67 /* Number of blocks per descriptor including the
72 static unsigned long nilfs_palloc_group(const struct inode
*inode
, __u64 nr
,
73 unsigned long *offset
)
77 *offset
= do_div(group
, nilfs_palloc_entries_per_group(inode
));
82 nilfs_palloc_desc_blkoff(const struct inode
*inode
, unsigned long group
)
84 unsigned long desc_block
=
85 group
/ nilfs_palloc_groups_per_desc_block(inode
);
86 return desc_block
* NILFS_MDT(inode
)->mi_blocks_per_desc_block
;
90 nilfs_palloc_bitmap_blkoff(const struct inode
*inode
, unsigned long group
)
92 unsigned long desc_offset
=
93 group
% nilfs_palloc_groups_per_desc_block(inode
);
94 return nilfs_palloc_desc_blkoff(inode
, group
) + 1 +
95 desc_offset
* NILFS_MDT(inode
)->mi_blocks_per_group
;
99 nilfs_palloc_group_desc_nfrees(struct inode
*inode
, unsigned long group
,
100 const struct nilfs_palloc_group_desc
*desc
)
104 spin_lock(nilfs_mdt_bgl_lock(inode
, group
));
105 nfree
= le32_to_cpu(desc
->pg_nfrees
);
106 spin_unlock(nilfs_mdt_bgl_lock(inode
, group
));
111 nilfs_palloc_group_desc_add_entries(struct inode
*inode
,
113 struct nilfs_palloc_group_desc
*desc
,
116 spin_lock(nilfs_mdt_bgl_lock(inode
, group
));
117 le32_add_cpu(&desc
->pg_nfrees
, n
);
118 spin_unlock(nilfs_mdt_bgl_lock(inode
, group
));
122 nilfs_palloc_entry_blkoff(const struct inode
*inode
, __u64 nr
)
124 unsigned long group
, group_offset
;
126 group
= nilfs_palloc_group(inode
, nr
, &group_offset
);
128 return nilfs_palloc_bitmap_blkoff(inode
, group
) + 1 +
129 group_offset
/ NILFS_MDT(inode
)->mi_entries_per_block
;
132 static void nilfs_palloc_desc_block_init(struct inode
*inode
,
133 struct buffer_head
*bh
, void *kaddr
)
135 struct nilfs_palloc_group_desc
*desc
= kaddr
+ bh_offset(bh
);
136 unsigned long n
= nilfs_palloc_groups_per_desc_block(inode
);
139 nfrees
= cpu_to_le32(nilfs_palloc_entries_per_group(inode
));
141 desc
->pg_nfrees
= nfrees
;
146 static int nilfs_palloc_get_block(struct inode
*inode
, unsigned long blkoff
,
148 void (*init_block
)(struct inode
*,
149 struct buffer_head
*,
151 struct buffer_head
**bhp
,
152 struct nilfs_bh_assoc
*prev
,
158 if (prev
->bh
&& blkoff
== prev
->blkoff
) {
166 ret
= nilfs_mdt_get_block(inode
, blkoff
, create
, init_block
, bhp
);
170 * The following code must be safe for change of the
171 * cache contents during the get block call.
176 prev
->blkoff
= blkoff
;
182 static int nilfs_palloc_get_desc_block(struct inode
*inode
,
184 int create
, struct buffer_head
**bhp
)
186 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
188 return nilfs_palloc_get_block(inode
,
189 nilfs_palloc_desc_blkoff(inode
, group
),
190 create
, nilfs_palloc_desc_block_init
,
191 bhp
, &cache
->prev_desc
, &cache
->lock
);
194 static int nilfs_palloc_get_bitmap_block(struct inode
*inode
,
196 int create
, struct buffer_head
**bhp
)
198 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
200 return nilfs_palloc_get_block(inode
,
201 nilfs_palloc_bitmap_blkoff(inode
, group
),
203 &cache
->prev_bitmap
, &cache
->lock
);
206 int nilfs_palloc_get_entry_block(struct inode
*inode
, __u64 nr
,
207 int create
, struct buffer_head
**bhp
)
209 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
211 return nilfs_palloc_get_block(inode
,
212 nilfs_palloc_entry_blkoff(inode
, nr
),
214 &cache
->prev_entry
, &cache
->lock
);
217 static struct nilfs_palloc_group_desc
*
218 nilfs_palloc_block_get_group_desc(const struct inode
*inode
,
220 const struct buffer_head
*bh
, void *kaddr
)
222 return (struct nilfs_palloc_group_desc
*)(kaddr
+ bh_offset(bh
)) +
223 group
% nilfs_palloc_groups_per_desc_block(inode
);
226 void *nilfs_palloc_block_get_entry(const struct inode
*inode
, __u64 nr
,
227 const struct buffer_head
*bh
, void *kaddr
)
229 unsigned long entry_offset
, group_offset
;
231 nilfs_palloc_group(inode
, nr
, &group_offset
);
232 entry_offset
= group_offset
% NILFS_MDT(inode
)->mi_entries_per_block
;
234 return kaddr
+ bh_offset(bh
) +
235 entry_offset
* NILFS_MDT(inode
)->mi_entry_size
;
238 static int nilfs_palloc_find_available_slot(struct inode
*inode
,
240 unsigned long target
,
241 unsigned char *bitmap
,
242 int bsize
) /* size in bits */
244 int curr
, pos
, end
, i
;
247 end
= (target
+ BITS_PER_LONG
- 1) & ~(BITS_PER_LONG
- 1);
250 pos
= nilfs_find_next_zero_bit(bitmap
, end
, target
);
252 !nilfs_set_bit_atomic(
253 nilfs_mdt_bgl_lock(inode
, group
), pos
, bitmap
))
258 for (i
= 0, curr
= end
;
260 i
+= BITS_PER_LONG
, curr
+= BITS_PER_LONG
) {
264 while (*((unsigned long *)bitmap
+ curr
/ BITS_PER_LONG
)
266 end
= curr
+ BITS_PER_LONG
;
269 pos
= nilfs_find_next_zero_bit(bitmap
, end
, curr
);
271 !nilfs_set_bit_atomic(
272 nilfs_mdt_bgl_lock(inode
, group
), pos
,
281 nilfs_palloc_rest_groups_in_desc_block(const struct inode
*inode
,
282 unsigned long curr
, unsigned long max
)
284 return min_t(unsigned long,
285 nilfs_palloc_groups_per_desc_block(inode
) -
286 curr
% nilfs_palloc_groups_per_desc_block(inode
),
290 int nilfs_palloc_prepare_alloc_entry(struct inode
*inode
,
291 struct nilfs_palloc_req
*req
)
293 struct buffer_head
*desc_bh
, *bitmap_bh
;
294 struct nilfs_palloc_group_desc
*desc
;
295 unsigned char *bitmap
;
296 void *desc_kaddr
, *bitmap_kaddr
;
297 unsigned long group
, maxgroup
, ngroups
;
298 unsigned long group_offset
, maxgroup_offset
;
299 unsigned long n
, entries_per_group
, groups_per_desc_block
;
303 ngroups
= nilfs_palloc_groups_count(inode
);
304 maxgroup
= ngroups
- 1;
305 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
306 entries_per_group
= nilfs_palloc_entries_per_group(inode
);
307 groups_per_desc_block
= nilfs_palloc_groups_per_desc_block(inode
);
309 for (i
= 0; i
< ngroups
; i
+= n
) {
310 if (group
>= ngroups
) {
313 maxgroup
= nilfs_palloc_group(inode
, req
->pr_entry_nr
,
314 &maxgroup_offset
) - 1;
316 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
319 desc_kaddr
= kmap(desc_bh
->b_page
);
320 desc
= nilfs_palloc_block_get_group_desc(
321 inode
, group
, desc_bh
, desc_kaddr
);
322 n
= nilfs_palloc_rest_groups_in_desc_block(inode
, group
,
324 for (j
= 0; j
< n
; j
++, desc
++, group
++) {
325 if (nilfs_palloc_group_desc_nfrees(inode
, group
, desc
)
327 ret
= nilfs_palloc_get_bitmap_block(
328 inode
, group
, 1, &bitmap_bh
);
331 bitmap_kaddr
= kmap(bitmap_bh
->b_page
);
332 bitmap
= bitmap_kaddr
+ bh_offset(bitmap_bh
);
333 pos
= nilfs_palloc_find_available_slot(
334 inode
, group
, group_offset
, bitmap
,
337 /* found a free entry */
338 nilfs_palloc_group_desc_add_entries(
339 inode
, group
, desc
, -1);
341 entries_per_group
* group
+ pos
;
342 kunmap(desc_bh
->b_page
);
343 kunmap(bitmap_bh
->b_page
);
345 req
->pr_desc_bh
= desc_bh
;
346 req
->pr_bitmap_bh
= bitmap_bh
;
349 kunmap(bitmap_bh
->b_page
);
356 kunmap(desc_bh
->b_page
);
360 /* no entries left */
364 kunmap(desc_bh
->b_page
);
369 void nilfs_palloc_commit_alloc_entry(struct inode
*inode
,
370 struct nilfs_palloc_req
*req
)
372 nilfs_mdt_mark_buffer_dirty(req
->pr_bitmap_bh
);
373 nilfs_mdt_mark_buffer_dirty(req
->pr_desc_bh
);
374 nilfs_mdt_mark_dirty(inode
);
376 brelse(req
->pr_bitmap_bh
);
377 brelse(req
->pr_desc_bh
);
380 void nilfs_palloc_commit_free_entry(struct inode
*inode
,
381 struct nilfs_palloc_req
*req
)
383 struct nilfs_palloc_group_desc
*desc
;
384 unsigned long group
, group_offset
;
385 unsigned char *bitmap
;
386 void *desc_kaddr
, *bitmap_kaddr
;
388 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
389 desc_kaddr
= kmap(req
->pr_desc_bh
->b_page
);
390 desc
= nilfs_palloc_block_get_group_desc(inode
, group
,
391 req
->pr_desc_bh
, desc_kaddr
);
392 bitmap_kaddr
= kmap(req
->pr_bitmap_bh
->b_page
);
393 bitmap
= bitmap_kaddr
+ bh_offset(req
->pr_bitmap_bh
);
395 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode
, group
),
396 group_offset
, bitmap
))
397 printk(KERN_WARNING
"%s: entry number %llu already freed\n",
398 __func__
, (unsigned long long)req
->pr_entry_nr
);
400 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, 1);
402 kunmap(req
->pr_bitmap_bh
->b_page
);
403 kunmap(req
->pr_desc_bh
->b_page
);
405 nilfs_mdt_mark_buffer_dirty(req
->pr_desc_bh
);
406 nilfs_mdt_mark_buffer_dirty(req
->pr_bitmap_bh
);
407 nilfs_mdt_mark_dirty(inode
);
409 brelse(req
->pr_bitmap_bh
);
410 brelse(req
->pr_desc_bh
);
413 void nilfs_palloc_abort_alloc_entry(struct inode
*inode
,
414 struct nilfs_palloc_req
*req
)
416 struct nilfs_palloc_group_desc
*desc
;
417 void *desc_kaddr
, *bitmap_kaddr
;
418 unsigned char *bitmap
;
419 unsigned long group
, group_offset
;
421 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
422 desc_kaddr
= kmap(req
->pr_desc_bh
->b_page
);
423 desc
= nilfs_palloc_block_get_group_desc(inode
, group
,
424 req
->pr_desc_bh
, desc_kaddr
);
425 bitmap_kaddr
= kmap(req
->pr_bitmap_bh
->b_page
);
426 bitmap
= bitmap_kaddr
+ bh_offset(req
->pr_bitmap_bh
);
427 if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode
, group
),
428 group_offset
, bitmap
))
429 printk(KERN_WARNING
"%s: entry number %llu already freed\n",
430 __func__
, (unsigned long long)req
->pr_entry_nr
);
432 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, 1);
434 kunmap(req
->pr_bitmap_bh
->b_page
);
435 kunmap(req
->pr_desc_bh
->b_page
);
437 brelse(req
->pr_bitmap_bh
);
438 brelse(req
->pr_desc_bh
);
440 req
->pr_entry_nr
= 0;
441 req
->pr_bitmap_bh
= NULL
;
442 req
->pr_desc_bh
= NULL
;
445 int nilfs_palloc_prepare_free_entry(struct inode
*inode
,
446 struct nilfs_palloc_req
*req
)
448 struct buffer_head
*desc_bh
, *bitmap_bh
;
449 unsigned long group
, group_offset
;
452 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
453 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
456 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 1, &bitmap_bh
);
462 req
->pr_desc_bh
= desc_bh
;
463 req
->pr_bitmap_bh
= bitmap_bh
;
467 void nilfs_palloc_abort_free_entry(struct inode
*inode
,
468 struct nilfs_palloc_req
*req
)
470 brelse(req
->pr_bitmap_bh
);
471 brelse(req
->pr_desc_bh
);
473 req
->pr_entry_nr
= 0;
474 req
->pr_bitmap_bh
= NULL
;
475 req
->pr_desc_bh
= NULL
;
479 nilfs_palloc_group_is_in(struct inode
*inode
, unsigned long group
, __u64 nr
)
483 first
= group
* nilfs_palloc_entries_per_group(inode
);
484 last
= first
+ nilfs_palloc_entries_per_group(inode
) - 1;
485 return (nr
>= first
) && (nr
<= last
);
488 int nilfs_palloc_freev(struct inode
*inode
, __u64
*entry_nrs
, size_t nitems
)
490 struct buffer_head
*desc_bh
, *bitmap_bh
;
491 struct nilfs_palloc_group_desc
*desc
;
492 unsigned char *bitmap
;
493 void *desc_kaddr
, *bitmap_kaddr
;
494 unsigned long group
, group_offset
;
497 for (i
= 0; i
< nitems
; i
+= n
) {
498 group
= nilfs_palloc_group(inode
, entry_nrs
[i
], &group_offset
);
499 ret
= nilfs_palloc_get_desc_block(inode
, group
, 0, &desc_bh
);
502 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 0,
508 desc_kaddr
= kmap(desc_bh
->b_page
);
509 desc
= nilfs_palloc_block_get_group_desc(
510 inode
, group
, desc_bh
, desc_kaddr
);
511 bitmap_kaddr
= kmap(bitmap_bh
->b_page
);
512 bitmap
= bitmap_kaddr
+ bh_offset(bitmap_bh
);
514 (j
< nitems
) && nilfs_palloc_group_is_in(inode
, group
,
517 nilfs_palloc_group(inode
, entry_nrs
[j
], &group_offset
);
518 if (!nilfs_clear_bit_atomic(
519 nilfs_mdt_bgl_lock(inode
, group
),
520 group_offset
, bitmap
)) {
522 "%s: entry number %llu already freed\n",
524 (unsigned long long)entry_nrs
[j
]);
527 nilfs_palloc_group_desc_add_entries(inode
, group
, desc
, n
);
529 kunmap(bitmap_bh
->b_page
);
530 kunmap(desc_bh
->b_page
);
532 nilfs_mdt_mark_buffer_dirty(desc_bh
);
533 nilfs_mdt_mark_buffer_dirty(bitmap_bh
);
534 nilfs_mdt_mark_dirty(inode
);
542 void nilfs_palloc_setup_cache(struct inode
*inode
,
543 struct nilfs_palloc_cache
*cache
)
545 NILFS_MDT(inode
)->mi_palloc_cache
= cache
;
546 spin_lock_init(&cache
->lock
);
549 void nilfs_palloc_clear_cache(struct inode
*inode
)
551 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
553 spin_lock(&cache
->lock
);
554 brelse(cache
->prev_desc
.bh
);
555 brelse(cache
->prev_bitmap
.bh
);
556 brelse(cache
->prev_entry
.bh
);
557 cache
->prev_desc
.bh
= NULL
;
558 cache
->prev_bitmap
.bh
= NULL
;
559 cache
->prev_entry
.bh
= NULL
;
560 spin_unlock(&cache
->lock
);
563 void nilfs_palloc_destroy_cache(struct inode
*inode
)
565 nilfs_palloc_clear_cache(inode
);
566 NILFS_MDT(inode
)->mi_palloc_cache
= NULL
;