25 #include <linux/types.h>
28 #include <linux/bitops.h>
29 #include <linux/slab.h>
39 static inline unsigned long
40 nilfs_palloc_groups_per_desc_block(
const struct inode *
inode)
50 static inline unsigned long
51 nilfs_palloc_groups_count(
const struct inode *inode)
79 nilfs_palloc_groups_per_desc_block(inode) *
97 *offset =
do_div(group, nilfs_palloc_entries_per_group(inode));
110 nilfs_palloc_desc_blkoff(
const struct inode *inode,
unsigned long group)
112 unsigned long desc_block =
113 group / nilfs_palloc_groups_per_desc_block(inode);
114 return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
126 nilfs_palloc_bitmap_blkoff(
const struct inode *inode,
unsigned long group)
128 unsigned long desc_offset =
129 group % nilfs_palloc_groups_per_desc_block(inode);
130 return nilfs_palloc_desc_blkoff(inode, group) + 1 +
131 desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
141 nilfs_palloc_group_desc_nfrees(
struct inode *inode,
unsigned long group,
160 nilfs_palloc_group_desc_add_entries(
struct inode *inode,
176 nilfs_palloc_entry_blkoff(
const struct inode *inode,
__u64 nr)
178 unsigned long group, group_offset;
180 group = nilfs_palloc_group(inode, nr, &group_offset);
182 return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
183 group_offset / NILFS_MDT(inode)->mi_entries_per_block;
192 static void nilfs_palloc_desc_block_init(
struct inode *inode,
193 struct buffer_head *bh,
void *kaddr)
196 unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
199 nfrees =
cpu_to_le32(nilfs_palloc_entries_per_group(inode));
206 static int nilfs_palloc_get_block(
struct inode *inode,
unsigned long blkoff,
209 struct buffer_head *,
211 struct buffer_head **bhp,
218 if (prev->
bh && blkoff == prev->
blkoff) {
249 static int nilfs_palloc_get_desc_block(
struct inode *inode,
251 int create,
struct buffer_head **bhp)
255 return nilfs_palloc_get_block(inode,
256 nilfs_palloc_desc_blkoff(inode, group),
257 create, nilfs_palloc_desc_block_init,
268 static int nilfs_palloc_get_bitmap_block(
struct inode *inode,
270 int create,
struct buffer_head **bhp)
274 return nilfs_palloc_get_block(inode,
275 nilfs_palloc_bitmap_blkoff(inode, group),
288 int create,
struct buffer_head **bhp)
292 return nilfs_palloc_get_block(inode,
293 nilfs_palloc_entry_blkoff(inode, nr),
306 nilfs_palloc_block_get_group_desc(
const struct inode *inode,
308 const struct buffer_head *bh,
void *kaddr)
311 group % nilfs_palloc_groups_per_desc_block(inode);
322 const struct buffer_head *bh,
void *kaddr)
324 unsigned long entry_offset, group_offset;
326 nilfs_palloc_group(inode, nr, &group_offset);
327 entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
329 return kaddr + bh_offset(bh) +
330 entry_offset * NILFS_MDT(inode)->mi_entry_size;
341 static int nilfs_palloc_find_available_slot(
struct inode *inode,
361 for (i = 0, curr = end;
391 nilfs_palloc_rest_groups_in_desc_block(
const struct inode *inode,
392 unsigned long curr,
unsigned long max)
394 return min_t(
unsigned long,
395 nilfs_palloc_groups_per_desc_block(inode) -
396 curr % nilfs_palloc_groups_per_desc_block(inode),
408 struct buffer_head *desc_bh, *bitmap_bh;
411 void *desc_kaddr, *bitmap_kaddr;
412 unsigned long group, maxgroup, ngroups;
413 unsigned long group_offset, maxgroup_offset;
414 unsigned long n, entries_per_group, groups_per_desc_block;
418 ngroups = nilfs_palloc_groups_count(inode);
419 maxgroup = ngroups - 1;
420 group = nilfs_palloc_group(inode, req->
pr_entry_nr, &group_offset);
421 entries_per_group = nilfs_palloc_entries_per_group(inode);
422 groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
424 for (i = 0; i < ngroups; i +=
n) {
425 if (group >= ngroups) {
428 maxgroup = nilfs_palloc_group(inode, req->
pr_entry_nr,
429 &maxgroup_offset) - 1;
431 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
434 desc_kaddr =
kmap(desc_bh->b_page);
435 desc = nilfs_palloc_block_get_group_desc(
436 inode, group, desc_bh, desc_kaddr);
437 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
439 for (j = 0; j <
n; j++, desc++, group++) {
440 if (nilfs_palloc_group_desc_nfrees(inode, group, desc)
442 ret = nilfs_palloc_get_bitmap_block(
443 inode, group, 1, &bitmap_bh);
446 bitmap_kaddr =
kmap(bitmap_bh->b_page);
447 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
448 pos = nilfs_palloc_find_available_slot(
449 inode, group, group_offset, bitmap,
453 nilfs_palloc_group_desc_add_entries(
454 inode, group, desc, -1);
456 entries_per_group * group +
pos;
458 kunmap(bitmap_bh->b_page);
464 kunmap(bitmap_bh->b_page);
494 nilfs_mdt_mark_dirty(inode);
509 unsigned long group, group_offset;
511 void *desc_kaddr, *bitmap_kaddr;
513 group = nilfs_palloc_group(inode, req->
pr_entry_nr, &group_offset);
515 desc = nilfs_palloc_block_get_group_desc(inode, group,
521 group_offset, bitmap))
525 nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
532 nilfs_mdt_mark_dirty(inode);
547 void *desc_kaddr, *bitmap_kaddr;
549 unsigned long group, group_offset;
551 group = nilfs_palloc_group(inode, req->
pr_entry_nr, &group_offset);
553 desc = nilfs_palloc_block_get_group_desc(inode, group,
558 group_offset, bitmap))
562 nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
583 struct buffer_head *desc_bh, *bitmap_bh;
584 unsigned long group, group_offset;
587 group = nilfs_palloc_group(inode, req->
pr_entry_nr, &group_offset);
588 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
591 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
625 nilfs_palloc_group_is_in(
struct inode *inode,
unsigned long group,
__u64 nr)
629 first = group * nilfs_palloc_entries_per_group(inode);
630 last = first + nilfs_palloc_entries_per_group(inode) - 1;
631 return (nr >= first) && (nr <= last);
642 struct buffer_head *desc_bh, *bitmap_bh;
645 void *desc_kaddr, *bitmap_kaddr;
646 unsigned long group, group_offset;
649 for (i = 0; i < nitems; i =
j) {
650 group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
651 ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
654 ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
660 desc_kaddr =
kmap(desc_bh->b_page);
661 desc = nilfs_palloc_block_get_group_desc(
662 inode, group, desc_bh, desc_kaddr);
663 bitmap_kaddr =
kmap(bitmap_bh->b_page);
664 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
666 (j < nitems) && nilfs_palloc_group_is_in(inode, group,
669 nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
672 group_offset, bitmap)) {
674 "%s: entry number %llu already freed\n",
676 (
unsigned long long)entry_nrs[j]);
681 nilfs_palloc_group_desc_add_entries(inode, group, desc, n);
683 kunmap(bitmap_bh->b_page);
688 nilfs_mdt_mark_dirty(inode);
699 NILFS_MDT(inode)->mi_palloc_cache =
cache;
707 spin_lock(&cache->
lock);
714 spin_unlock(&cache->
lock);
720 NILFS_MDT(inode)->mi_palloc_cache =
NULL;