Skip to content

Commit 21cf934

Browse files
konisakpm00
authored andcommitted
nilfs2: convert persistent object allocator to be folio-based
Regarding the persistent oject allocator, a common mechanism for allocating objects in metadata files such as inodes and DAT entries, convert the page-based implementation to a folio-based implementation. In this conversion, helper functions nilfs_palloc_group_desc_offset() and nilfs_palloc_bitmap_offset() are added and used to calculate the byte offset within a folio of a group descriptor structure and bitmap, respectively, to replace kmap_local_page with kmap_local_folio. In addition, a helper function called nilfs_palloc_entry_offset() is provided to facilitate common calculation of the byte offset within a folio of metadata file entries managed in the persistent object allocator format. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 832acfe commit 21cf934

File tree

2 files changed

+89
-50
lines changed

2 files changed

+89
-50
lines changed

fs/nilfs2/alloc.c

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,55 @@ static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
339339
}
340340

341341
/**
342-
* nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
342+
* nilfs_palloc_group_desc_offset - calculate the byte offset of a group
343+
* descriptor in the folio containing it
343344
* @inode: inode of metadata file using this allocator
344345
* @group: group number
345-
* @bh: buffer head of the buffer storing the group descriptor block
346-
* @kaddr: kernel address mapped for the page including the buffer
346+
* @bh: buffer head of the group descriptor block
347+
*
348+
* Return: Byte offset in the folio of the group descriptor for @group.
347349
*/
348-
static struct nilfs_palloc_group_desc *
349-
nilfs_palloc_block_get_group_desc(const struct inode *inode,
350-
unsigned long group,
351-
const struct buffer_head *bh, void *kaddr)
350+
static size_t nilfs_palloc_group_desc_offset(const struct inode *inode,
351+
unsigned long group,
352+
const struct buffer_head *bh)
352353
{
353-
return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
354-
group % nilfs_palloc_groups_per_desc_block(inode);
354+
return offset_in_folio(bh->b_folio, bh->b_data) +
355+
sizeof(struct nilfs_palloc_group_desc) *
356+
(group % nilfs_palloc_groups_per_desc_block(inode));
357+
}
358+
359+
/**
360+
* nilfs_palloc_bitmap_offset - calculate the byte offset of a bitmap block
361+
* in the folio containing it
362+
* @bh: buffer head of the bitmap block
363+
*
364+
* Return: Byte offset in the folio of the bitmap block for @bh.
365+
*/
366+
static size_t nilfs_palloc_bitmap_offset(const struct buffer_head *bh)
367+
{
368+
return offset_in_folio(bh->b_folio, bh->b_data);
369+
}
370+
371+
/**
372+
* nilfs_palloc_entry_offset - calculate the byte offset of an entry in the
373+
* folio containing it
374+
* @inode: inode of metadata file using this allocator
375+
* @nr: serial number of the entry (e.g. inode number)
376+
* @bh: buffer head of the entry block
377+
*
378+
* Return: Byte offset in the folio of the entry @nr.
379+
*/
380+
size_t nilfs_palloc_entry_offset(const struct inode *inode, __u64 nr,
381+
const struct buffer_head *bh)
382+
{
383+
unsigned long entry_index_in_group, entry_index_in_block;
384+
385+
nilfs_palloc_group(inode, nr, &entry_index_in_group);
386+
entry_index_in_block = entry_index_in_group %
387+
NILFS_MDT(inode)->mi_entries_per_block;
388+
389+
return offset_in_folio(bh->b_folio, bh->b_data) +
390+
entry_index_in_block * NILFS_MDT(inode)->mi_entry_size;
355391
}
356392

357393
/**
@@ -508,7 +544,7 @@ int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
508544
struct buffer_head *desc_bh, *bitmap_bh;
509545
struct nilfs_palloc_group_desc *desc;
510546
unsigned char *bitmap;
511-
void *desc_kaddr, *bitmap_kaddr;
547+
size_t doff, boff;
512548
unsigned long group, maxgroup, ngroups;
513549
unsigned long group_offset, maxgroup_offset;
514550
unsigned long n, entries_per_group;
@@ -531,30 +567,32 @@ int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
531567
ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
532568
if (ret < 0)
533569
return ret;
534-
desc_kaddr = kmap_local_page(desc_bh->b_page);
535-
desc = nilfs_palloc_block_get_group_desc(
536-
inode, group, desc_bh, desc_kaddr);
570+
571+
doff = nilfs_palloc_group_desc_offset(inode, group, desc_bh);
572+
desc = kmap_local_folio(desc_bh->b_folio, doff);
537573
n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
538574
maxgroup);
539-
for (j = 0; j < n; j++, desc++, group++, group_offset = 0) {
575+
for (j = 0; j < n; j++, group++, group_offset = 0) {
540576
lock = nilfs_mdt_bgl_lock(inode, group);
541-
if (nilfs_palloc_group_desc_nfrees(desc, lock) == 0)
577+
if (nilfs_palloc_group_desc_nfrees(&desc[j], lock) == 0)
542578
continue;
543579

544-
kunmap_local(desc_kaddr);
580+
kunmap_local(desc);
545581
ret = nilfs_palloc_get_bitmap_block(inode, group, 1,
546582
&bitmap_bh);
547583
if (unlikely(ret < 0)) {
548584
brelse(desc_bh);
549585
return ret;
550586
}
551587

552-
desc_kaddr = kmap_local_page(desc_bh->b_page);
553-
desc = nilfs_palloc_block_get_group_desc(
554-
inode, group, desc_bh, desc_kaddr);
588+
/*
589+
* Re-kmap the folio containing the first (and
590+
* subsequent) group descriptors.
591+
*/
592+
desc = kmap_local_folio(desc_bh->b_folio, doff);
555593

556-
bitmap_kaddr = kmap_local_page(bitmap_bh->b_page);
557-
bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
594+
boff = nilfs_palloc_bitmap_offset(bitmap_bh);
595+
bitmap = kmap_local_folio(bitmap_bh->b_folio, boff);
558596
pos = nilfs_palloc_find_available_slot(
559597
bitmap, group_offset, entries_per_group, lock,
560598
wrap);
@@ -564,14 +602,14 @@ int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
564602
* beginning, the wrap flag only has an effect on the
565603
* first search.
566604
*/
567-
kunmap_local(bitmap_kaddr);
605+
kunmap_local(bitmap);
568606
if (pos >= 0)
569607
goto found;
570608

571609
brelse(bitmap_bh);
572610
}
573611

574-
kunmap_local(desc_kaddr);
612+
kunmap_local(desc);
575613
brelse(desc_bh);
576614
}
577615

@@ -580,9 +618,9 @@ int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
580618

581619
found:
582620
/* found a free entry */
583-
nilfs_palloc_group_desc_add_entries(desc, lock, -1);
621+
nilfs_palloc_group_desc_add_entries(&desc[j], lock, -1);
584622
req->pr_entry_nr = entries_per_group * group + pos;
585-
kunmap_local(desc_kaddr);
623+
kunmap_local(desc);
586624

587625
req->pr_desc_bh = desc_bh;
588626
req->pr_bitmap_bh = bitmap_bh;
@@ -613,18 +651,18 @@ void nilfs_palloc_commit_alloc_entry(struct inode *inode,
613651
void nilfs_palloc_commit_free_entry(struct inode *inode,
614652
struct nilfs_palloc_req *req)
615653
{
616-
struct nilfs_palloc_group_desc *desc;
617654
unsigned long group, group_offset;
655+
size_t doff, boff;
656+
struct nilfs_palloc_group_desc *desc;
618657
unsigned char *bitmap;
619-
void *desc_kaddr, *bitmap_kaddr;
620658
spinlock_t *lock;
621659

622660
group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
623-
desc_kaddr = kmap_local_page(req->pr_desc_bh->b_page);
624-
desc = nilfs_palloc_block_get_group_desc(inode, group,
625-
req->pr_desc_bh, desc_kaddr);
626-
bitmap_kaddr = kmap_local_page(req->pr_bitmap_bh->b_page);
627-
bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
661+
doff = nilfs_palloc_group_desc_offset(inode, group, req->pr_desc_bh);
662+
desc = kmap_local_folio(req->pr_desc_bh->b_folio, doff);
663+
664+
boff = nilfs_palloc_bitmap_offset(req->pr_bitmap_bh);
665+
bitmap = kmap_local_folio(req->pr_bitmap_bh->b_folio, boff);
628666
lock = nilfs_mdt_bgl_lock(inode, group);
629667

630668
if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
@@ -635,8 +673,8 @@ void nilfs_palloc_commit_free_entry(struct inode *inode,
635673
else
636674
nilfs_palloc_group_desc_add_entries(desc, lock, 1);
637675

638-
kunmap_local(bitmap_kaddr);
639-
kunmap_local(desc_kaddr);
676+
kunmap_local(bitmap);
677+
kunmap_local(desc);
640678

641679
mark_buffer_dirty(req->pr_desc_bh);
642680
mark_buffer_dirty(req->pr_bitmap_bh);
@@ -655,17 +693,17 @@ void nilfs_palloc_abort_alloc_entry(struct inode *inode,
655693
struct nilfs_palloc_req *req)
656694
{
657695
struct nilfs_palloc_group_desc *desc;
658-
void *desc_kaddr, *bitmap_kaddr;
696+
size_t doff, boff;
659697
unsigned char *bitmap;
660698
unsigned long group, group_offset;
661699
spinlock_t *lock;
662700

663701
group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
664-
desc_kaddr = kmap_local_page(req->pr_desc_bh->b_page);
665-
desc = nilfs_palloc_block_get_group_desc(inode, group,
666-
req->pr_desc_bh, desc_kaddr);
667-
bitmap_kaddr = kmap_local_page(req->pr_bitmap_bh->b_page);
668-
bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
702+
doff = nilfs_palloc_group_desc_offset(inode, group, req->pr_desc_bh);
703+
desc = kmap_local_folio(req->pr_desc_bh->b_folio, doff);
704+
705+
boff = nilfs_palloc_bitmap_offset(req->pr_bitmap_bh);
706+
bitmap = kmap_local_folio(req->pr_bitmap_bh->b_folio, boff);
669707
lock = nilfs_mdt_bgl_lock(inode, group);
670708

671709
if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
@@ -676,8 +714,8 @@ void nilfs_palloc_abort_alloc_entry(struct inode *inode,
676714
else
677715
nilfs_palloc_group_desc_add_entries(desc, lock, 1);
678716

679-
kunmap_local(bitmap_kaddr);
680-
kunmap_local(desc_kaddr);
717+
kunmap_local(bitmap);
718+
kunmap_local(desc);
681719

682720
brelse(req->pr_bitmap_bh);
683721
brelse(req->pr_desc_bh);
@@ -741,7 +779,7 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
741779
struct buffer_head *desc_bh, *bitmap_bh;
742780
struct nilfs_palloc_group_desc *desc;
743781
unsigned char *bitmap;
744-
void *desc_kaddr, *bitmap_kaddr;
782+
size_t doff, boff;
745783
unsigned long group, group_offset;
746784
__u64 group_min_nr, last_nrs[8];
747785
const unsigned long epg = nilfs_palloc_entries_per_group(inode);
@@ -769,8 +807,8 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
769807
/* Get the first entry number of the group */
770808
group_min_nr = (__u64)group * epg;
771809

772-
bitmap_kaddr = kmap_local_page(bitmap_bh->b_page);
773-
bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
810+
boff = nilfs_palloc_bitmap_offset(bitmap_bh);
811+
bitmap = kmap_local_folio(bitmap_bh->b_folio, boff);
774812
lock = nilfs_mdt_bgl_lock(inode, group);
775813

776814
j = i;
@@ -815,7 +853,7 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
815853
entry_start = rounddown(group_offset, epb);
816854
} while (true);
817855

818-
kunmap_local(bitmap_kaddr);
856+
kunmap_local(bitmap);
819857
mark_buffer_dirty(bitmap_bh);
820858
brelse(bitmap_bh);
821859

@@ -829,11 +867,10 @@ int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
829867
inode->i_ino);
830868
}
831869

832-
desc_kaddr = kmap_local_page(desc_bh->b_page);
833-
desc = nilfs_palloc_block_get_group_desc(
834-
inode, group, desc_bh, desc_kaddr);
870+
doff = nilfs_palloc_group_desc_offset(inode, group, desc_bh);
871+
desc = kmap_local_folio(desc_bh->b_folio, doff);
835872
nfree = nilfs_palloc_group_desc_add_entries(desc, lock, n);
836-
kunmap_local(desc_kaddr);
873+
kunmap_local(desc);
837874
mark_buffer_dirty(desc_bh);
838875
nilfs_mdt_mark_dirty(inode);
839876
brelse(desc_bh);

fs/nilfs2/alloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ int nilfs_palloc_get_entry_block(struct inode *, __u64, int,
3333
struct buffer_head **);
3434
void *nilfs_palloc_block_get_entry(const struct inode *, __u64,
3535
const struct buffer_head *, void *);
36+
size_t nilfs_palloc_entry_offset(const struct inode *inode, __u64 nr,
37+
const struct buffer_head *bh);
3638

3739
int nilfs_palloc_count_max_entries(struct inode *, u64, u64 *);
3840

0 commit comments

Comments
 (0)