1 // SPDX-License-Identifier: GPL-2.0+
3 * NILFS dat/inode allocator
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
7 * Originally written by Koji Sato.
8 * Two allocators were unified by Ryusuke Konishi and Amagai Yoshiji.
11 #include <linux/types.h>
12 #include <linux/buffer_head.h>
14 #include <linux/bitops.h>
15 #include <linux/slab.h>
21 * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
22 * descriptor block can maintain
23 * @inode: inode of metadata file using this allocator
25 static inline unsigned long
26 nilfs_palloc_groups_per_desc_block(const struct inode
*inode
)
28 return i_blocksize(inode
) /
29 sizeof(struct nilfs_palloc_group_desc
);
33 * nilfs_palloc_groups_count - get maximum number of groups
34 * @inode: inode of metadata file using this allocator
36 static inline unsigned long
37 nilfs_palloc_groups_count(const struct inode
*inode
)
39 return 1UL << (BITS_PER_LONG
- (inode
->i_blkbits
+ 3 /* log2(8) */));
43 * nilfs_palloc_init_blockgroup - initialize private variables for allocator
44 * @inode: inode of metadata file using this allocator
45 * @entry_size: size of the persistent object
47 int nilfs_palloc_init_blockgroup(struct inode
*inode
, unsigned int 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;
63 * Number of blocks in a group including entry blocks
66 mi
->mi_blocks_per_desc_block
=
67 nilfs_palloc_groups_per_desc_block(inode
) *
68 mi
->mi_blocks_per_group
+ 1;
70 * Number of blocks per descriptor including the
77 * nilfs_palloc_group - get group number and offset from an entry number
78 * @inode: inode of metadata file using this allocator
79 * @nr: serial number of the entry (e.g. inode number)
80 * @offset: pointer to store offset number in the group
82 static unsigned long nilfs_palloc_group(const struct inode
*inode
, __u64 nr
,
83 unsigned long *offset
)
87 *offset
= do_div(group
, nilfs_palloc_entries_per_group(inode
));
92 * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
93 * @inode: inode of metadata file using this allocator
94 * @group: group number
96 * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
97 * block which contains a descriptor of the specified group.
100 nilfs_palloc_desc_blkoff(const struct inode
*inode
, unsigned long group
)
102 unsigned long desc_block
=
103 group
/ nilfs_palloc_groups_per_desc_block(inode
);
104 return desc_block
* NILFS_MDT(inode
)->mi_blocks_per_desc_block
;
108 * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
109 * @inode: inode of metadata file using this allocator
110 * @group: group number
112 * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
113 * block used to allocate/deallocate entries in the specified group.
116 nilfs_palloc_bitmap_blkoff(const struct inode
*inode
, unsigned long group
)
118 unsigned long desc_offset
=
119 group
% nilfs_palloc_groups_per_desc_block(inode
);
120 return nilfs_palloc_desc_blkoff(inode
, group
) + 1 +
121 desc_offset
* NILFS_MDT(inode
)->mi_blocks_per_group
;
125 * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
126 * @desc: pointer to descriptor structure for the group
127 * @lock: spin lock protecting @desc
130 nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc
*desc
,
136 nfree
= le32_to_cpu(desc
->pg_nfrees
);
142 * nilfs_palloc_group_desc_add_entries - adjust count of free entries
143 * @desc: pointer to descriptor structure for the group
144 * @lock: spin lock protecting @desc
145 * @n: delta to be added
148 nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc
*desc
,
149 spinlock_t
*lock
, u32 n
)
154 le32_add_cpu(&desc
->pg_nfrees
, n
);
155 nfree
= le32_to_cpu(desc
->pg_nfrees
);
161 * nilfs_palloc_entry_blkoff - get block offset of an entry block
162 * @inode: inode of metadata file using this allocator
163 * @nr: serial number of the entry (e.g. inode number)
166 nilfs_palloc_entry_blkoff(const struct inode
*inode
, __u64 nr
)
168 unsigned long group
, group_offset
;
170 group
= nilfs_palloc_group(inode
, nr
, &group_offset
);
172 return nilfs_palloc_bitmap_blkoff(inode
, group
) + 1 +
173 group_offset
/ NILFS_MDT(inode
)->mi_entries_per_block
;
177 * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
178 * @inode: inode of metadata file
179 * @bh: buffer head of the buffer to be initialized
180 * @from: kernel address mapped for a chunk of the block
182 * This function does not yet support the case where block size > PAGE_SIZE.
184 static void nilfs_palloc_desc_block_init(struct inode
*inode
,
185 struct buffer_head
*bh
, void *from
)
187 struct nilfs_palloc_group_desc
*desc
= from
;
188 unsigned long n
= nilfs_palloc_groups_per_desc_block(inode
);
191 nfrees
= cpu_to_le32(nilfs_palloc_entries_per_group(inode
));
193 desc
->pg_nfrees
= nfrees
;
198 static int nilfs_palloc_get_block(struct inode
*inode
, unsigned long blkoff
,
200 void (*init_block
)(struct inode
*,
201 struct buffer_head
*,
203 struct buffer_head
**bhp
,
204 struct nilfs_bh_assoc
*prev
,
210 if (prev
->bh
&& blkoff
== prev
->blkoff
&&
211 likely(buffer_uptodate(prev
->bh
))) {
219 ret
= nilfs_mdt_get_block(inode
, blkoff
, create
, init_block
, bhp
);
223 * The following code must be safe for change of the
224 * cache contents during the get block call.
229 prev
->blkoff
= blkoff
;
236 * nilfs_palloc_delete_block - delete a block on the persistent allocator file
237 * @inode: inode of metadata file using this allocator
238 * @blkoff: block offset
239 * @prev: nilfs_bh_assoc struct of the last used buffer
240 * @lock: spin lock protecting @prev
242 static int nilfs_palloc_delete_block(struct inode
*inode
, unsigned long blkoff
,
243 struct nilfs_bh_assoc
*prev
,
247 if (prev
->bh
&& blkoff
== prev
->blkoff
) {
252 return nilfs_mdt_delete_block(inode
, blkoff
);
256 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
257 * @inode: inode of metadata file using this allocator
258 * @group: group number
259 * @create: create flag
260 * @bhp: pointer to store the resultant buffer head
262 static int nilfs_palloc_get_desc_block(struct inode
*inode
,
264 int create
, struct buffer_head
**bhp
)
266 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
268 return nilfs_palloc_get_block(inode
,
269 nilfs_palloc_desc_blkoff(inode
, group
),
270 create
, nilfs_palloc_desc_block_init
,
271 bhp
, &cache
->prev_desc
, &cache
->lock
);
275 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
276 * @inode: inode of metadata file using this allocator
277 * @group: group number
278 * @create: create flag
279 * @bhp: pointer to store the resultant buffer head
281 static int nilfs_palloc_get_bitmap_block(struct inode
*inode
,
283 int create
, struct buffer_head
**bhp
)
285 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
287 return nilfs_palloc_get_block(inode
,
288 nilfs_palloc_bitmap_blkoff(inode
, group
),
290 &cache
->prev_bitmap
, &cache
->lock
);
294 * nilfs_palloc_delete_bitmap_block - delete a bitmap block
295 * @inode: inode of metadata file using this allocator
296 * @group: group number
298 static int nilfs_palloc_delete_bitmap_block(struct inode
*inode
,
301 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
303 return nilfs_palloc_delete_block(inode
,
304 nilfs_palloc_bitmap_blkoff(inode
,
306 &cache
->prev_bitmap
, &cache
->lock
);
310 * nilfs_palloc_get_entry_block - get buffer head of an entry block
311 * @inode: inode of metadata file using this allocator
312 * @nr: serial number of the entry (e.g. inode number)
313 * @create: create flag
314 * @bhp: pointer to store the resultant buffer head
316 int nilfs_palloc_get_entry_block(struct inode
*inode
, __u64 nr
,
317 int create
, struct buffer_head
**bhp
)
319 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
321 return nilfs_palloc_get_block(inode
,
322 nilfs_palloc_entry_blkoff(inode
, nr
),
324 &cache
->prev_entry
, &cache
->lock
);
328 * nilfs_palloc_delete_entry_block - delete an entry block
329 * @inode: inode of metadata file using this allocator
330 * @nr: serial number of the entry
332 static int nilfs_palloc_delete_entry_block(struct inode
*inode
, __u64 nr
)
334 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
336 return nilfs_palloc_delete_block(inode
,
337 nilfs_palloc_entry_blkoff(inode
, nr
),
338 &cache
->prev_entry
, &cache
->lock
);
342 * nilfs_palloc_group_desc_offset - calculate the byte offset of a group
343 * descriptor in the folio containing it
344 * @inode: inode of metadata file using this allocator
345 * @group: group number
346 * @bh: buffer head of the group descriptor block
348 * Return: Byte offset in the folio of the group descriptor for @group.
350 static size_t nilfs_palloc_group_desc_offset(const struct inode
*inode
,
352 const struct buffer_head
*bh
)
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
));
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
364 * Return: Byte offset in the folio of the bitmap block for @bh.
366 static size_t nilfs_palloc_bitmap_offset(const struct buffer_head
*bh
)
368 return offset_in_folio(bh
->b_folio
, bh
->b_data
);
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
378 * Return: Byte offset in the folio of the entry @nr.
380 size_t nilfs_palloc_entry_offset(const struct inode
*inode
, __u64 nr
,
381 const struct buffer_head
*bh
)
383 unsigned long entry_index_in_group
, entry_index_in_block
;
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
;
389 return offset_in_folio(bh
->b_folio
, bh
->b_data
) +
390 entry_index_in_block
* NILFS_MDT(inode
)->mi_entry_size
;
394 * nilfs_palloc_find_available_slot - find available slot in a group
395 * @bitmap: bitmap of the group
396 * @target: offset number of an entry in the group (start point)
397 * @bsize: size in bits
398 * @lock: spin lock protecting @bitmap
399 * @wrap: whether to wrap around
401 static int nilfs_palloc_find_available_slot(unsigned char *bitmap
,
402 unsigned long target
,
404 spinlock_t
*lock
, bool wrap
)
406 int pos
, end
= bsize
;
408 if (likely(target
< bsize
)) {
411 pos
= nilfs_find_next_zero_bit(bitmap
, end
, pos
);
414 if (!nilfs_set_bit_atomic(lock
, pos
, bitmap
))
416 } while (++pos
< end
);
424 for (pos
= 0; pos
< end
; pos
++) {
425 pos
= nilfs_find_next_zero_bit(bitmap
, end
, pos
);
428 if (!nilfs_set_bit_atomic(lock
, pos
, bitmap
))
436 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
437 * in a group descriptor block
438 * @inode: inode of metadata file using this allocator
439 * @curr: current group number
440 * @max: maximum number of groups
443 nilfs_palloc_rest_groups_in_desc_block(const struct inode
*inode
,
444 unsigned long curr
, unsigned long max
)
446 return min_t(unsigned long,
447 nilfs_palloc_groups_per_desc_block(inode
) -
448 curr
% nilfs_palloc_groups_per_desc_block(inode
),
453 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
454 * @inode: inode of metadata file using this allocator
455 * @desc_blocks: descriptor blocks number [out]
457 static int nilfs_palloc_count_desc_blocks(struct inode
*inode
,
458 unsigned long *desc_blocks
)
463 ret
= nilfs_bmap_last_key(NILFS_I(inode
)->i_bmap
, &blknum
);
465 *desc_blocks
= DIV_ROUND_UP(
466 (unsigned long)blknum
,
467 NILFS_MDT(inode
)->mi_blocks_per_desc_block
);
472 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
474 * @inode: inode of metadata file using this allocator
475 * @desc_blocks: known current descriptor blocks count
477 static inline bool nilfs_palloc_mdt_file_can_grow(struct inode
*inode
,
478 unsigned long desc_blocks
)
480 return (nilfs_palloc_groups_per_desc_block(inode
) * desc_blocks
) <
481 nilfs_palloc_groups_count(inode
);
485 * nilfs_palloc_count_max_entries - count max number of entries that can be
486 * described by descriptor blocks count
487 * @inode: inode of metadata file using this allocator
488 * @nused: current number of used entries
489 * @nmaxp: max number of entries [out]
491 int nilfs_palloc_count_max_entries(struct inode
*inode
, u64 nused
, u64
*nmaxp
)
493 unsigned long desc_blocks
= 0;
494 u64 entries_per_desc_block
, nmax
;
497 err
= nilfs_palloc_count_desc_blocks(inode
, &desc_blocks
);
501 entries_per_desc_block
= (u64
)nilfs_palloc_entries_per_group(inode
) *
502 nilfs_palloc_groups_per_desc_block(inode
);
503 nmax
= entries_per_desc_block
* desc_blocks
;
506 nilfs_palloc_mdt_file_can_grow(inode
, desc_blocks
))
507 nmax
+= entries_per_desc_block
;
517 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
518 * @inode: inode of metadata file using this allocator
519 * @req: nilfs_palloc_req structure exchanged for the allocation
520 * @wrap: whether to wrap around
522 int nilfs_palloc_prepare_alloc_entry(struct inode
*inode
,
523 struct nilfs_palloc_req
*req
, bool wrap
)
525 struct buffer_head
*desc_bh
, *bitmap_bh
;
526 struct nilfs_palloc_group_desc
*desc
;
527 unsigned char *bitmap
;
529 unsigned long group
, maxgroup
, ngroups
;
530 unsigned long group_offset
, maxgroup_offset
;
531 unsigned long n
, entries_per_group
;
536 ngroups
= nilfs_palloc_groups_count(inode
);
537 maxgroup
= ngroups
- 1;
538 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
539 entries_per_group
= nilfs_palloc_entries_per_group(inode
);
541 for (i
= 0; i
< ngroups
; i
+= n
) {
542 if (group
>= ngroups
&& wrap
) {
545 maxgroup
= nilfs_palloc_group(inode
, req
->pr_entry_nr
,
546 &maxgroup_offset
) - 1;
548 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
552 doff
= nilfs_palloc_group_desc_offset(inode
, group
, desc_bh
);
553 desc
= kmap_local_folio(desc_bh
->b_folio
, doff
);
554 n
= nilfs_palloc_rest_groups_in_desc_block(inode
, group
,
556 for (j
= 0; j
< n
; j
++, group
++, group_offset
= 0) {
557 lock
= nilfs_mdt_bgl_lock(inode
, group
);
558 if (nilfs_palloc_group_desc_nfrees(&desc
[j
], lock
) == 0)
562 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 1,
564 if (unlikely(ret
< 0)) {
570 * Re-kmap the folio containing the first (and
571 * subsequent) group descriptors.
573 desc
= kmap_local_folio(desc_bh
->b_folio
, doff
);
575 boff
= nilfs_palloc_bitmap_offset(bitmap_bh
);
576 bitmap
= kmap_local_folio(bitmap_bh
->b_folio
, boff
);
577 pos
= nilfs_palloc_find_available_slot(
578 bitmap
, group_offset
, entries_per_group
, lock
,
581 * Since the search for a free slot in the second and
582 * subsequent bitmap blocks always starts from the
583 * beginning, the wrap flag only has an effect on the
586 kunmap_local(bitmap
);
597 /* no entries left */
601 /* found a free entry */
602 nilfs_palloc_group_desc_add_entries(&desc
[j
], lock
, -1);
603 req
->pr_entry_nr
= entries_per_group
* group
+ pos
;
606 req
->pr_desc_bh
= desc_bh
;
607 req
->pr_bitmap_bh
= bitmap_bh
;
612 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
613 * @inode: inode of metadata file using this allocator
614 * @req: nilfs_palloc_req structure exchanged for the allocation
616 void nilfs_palloc_commit_alloc_entry(struct inode
*inode
,
617 struct nilfs_palloc_req
*req
)
619 mark_buffer_dirty(req
->pr_bitmap_bh
);
620 mark_buffer_dirty(req
->pr_desc_bh
);
621 nilfs_mdt_mark_dirty(inode
);
623 brelse(req
->pr_bitmap_bh
);
624 brelse(req
->pr_desc_bh
);
628 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
629 * @inode: inode of metadata file using this allocator
630 * @req: nilfs_palloc_req structure exchanged for the removal
632 void nilfs_palloc_commit_free_entry(struct inode
*inode
,
633 struct nilfs_palloc_req
*req
)
635 unsigned long group
, group_offset
;
637 struct nilfs_palloc_group_desc
*desc
;
638 unsigned char *bitmap
;
641 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
642 doff
= nilfs_palloc_group_desc_offset(inode
, group
, req
->pr_desc_bh
);
643 desc
= kmap_local_folio(req
->pr_desc_bh
->b_folio
, doff
);
645 boff
= nilfs_palloc_bitmap_offset(req
->pr_bitmap_bh
);
646 bitmap
= kmap_local_folio(req
->pr_bitmap_bh
->b_folio
, boff
);
647 lock
= nilfs_mdt_bgl_lock(inode
, group
);
649 if (!nilfs_clear_bit_atomic(lock
, group_offset
, bitmap
))
650 nilfs_warn(inode
->i_sb
,
651 "%s (ino=%lu): entry number %llu already freed",
652 __func__
, inode
->i_ino
,
653 (unsigned long long)req
->pr_entry_nr
);
655 nilfs_palloc_group_desc_add_entries(desc
, lock
, 1);
657 kunmap_local(bitmap
);
660 mark_buffer_dirty(req
->pr_desc_bh
);
661 mark_buffer_dirty(req
->pr_bitmap_bh
);
662 nilfs_mdt_mark_dirty(inode
);
664 brelse(req
->pr_bitmap_bh
);
665 brelse(req
->pr_desc_bh
);
669 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
670 * @inode: inode of metadata file using this allocator
671 * @req: nilfs_palloc_req structure exchanged for the allocation
673 void nilfs_palloc_abort_alloc_entry(struct inode
*inode
,
674 struct nilfs_palloc_req
*req
)
676 struct nilfs_palloc_group_desc
*desc
;
678 unsigned char *bitmap
;
679 unsigned long group
, group_offset
;
682 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
683 doff
= nilfs_palloc_group_desc_offset(inode
, group
, req
->pr_desc_bh
);
684 desc
= kmap_local_folio(req
->pr_desc_bh
->b_folio
, doff
);
686 boff
= nilfs_palloc_bitmap_offset(req
->pr_bitmap_bh
);
687 bitmap
= kmap_local_folio(req
->pr_bitmap_bh
->b_folio
, boff
);
688 lock
= nilfs_mdt_bgl_lock(inode
, group
);
690 if (!nilfs_clear_bit_atomic(lock
, group_offset
, bitmap
))
691 nilfs_warn(inode
->i_sb
,
692 "%s (ino=%lu): entry number %llu already freed",
693 __func__
, inode
->i_ino
,
694 (unsigned long long)req
->pr_entry_nr
);
696 nilfs_palloc_group_desc_add_entries(desc
, lock
, 1);
698 kunmap_local(bitmap
);
701 brelse(req
->pr_bitmap_bh
);
702 brelse(req
->pr_desc_bh
);
704 req
->pr_entry_nr
= 0;
705 req
->pr_bitmap_bh
= NULL
;
706 req
->pr_desc_bh
= NULL
;
710 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
711 * @inode: inode of metadata file using this allocator
712 * @req: nilfs_palloc_req structure exchanged for the removal
714 int nilfs_palloc_prepare_free_entry(struct inode
*inode
,
715 struct nilfs_palloc_req
*req
)
717 struct buffer_head
*desc_bh
, *bitmap_bh
;
718 unsigned long group
, group_offset
;
721 group
= nilfs_palloc_group(inode
, req
->pr_entry_nr
, &group_offset
);
722 ret
= nilfs_palloc_get_desc_block(inode
, group
, 1, &desc_bh
);
725 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 1, &bitmap_bh
);
731 req
->pr_desc_bh
= desc_bh
;
732 req
->pr_bitmap_bh
= bitmap_bh
;
737 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
738 * @inode: inode of metadata file using this allocator
739 * @req: nilfs_palloc_req structure exchanged for the removal
741 void nilfs_palloc_abort_free_entry(struct inode
*inode
,
742 struct nilfs_palloc_req
*req
)
744 brelse(req
->pr_bitmap_bh
);
745 brelse(req
->pr_desc_bh
);
747 req
->pr_entry_nr
= 0;
748 req
->pr_bitmap_bh
= NULL
;
749 req
->pr_desc_bh
= NULL
;
753 * nilfs_palloc_freev - deallocate a set of persistent objects
754 * @inode: inode of metadata file using this allocator
755 * @entry_nrs: array of entry numbers to be deallocated
756 * @nitems: number of entries stored in @entry_nrs
758 int nilfs_palloc_freev(struct inode
*inode
, __u64
*entry_nrs
, size_t nitems
)
760 struct buffer_head
*desc_bh
, *bitmap_bh
;
761 struct nilfs_palloc_group_desc
*desc
;
762 unsigned char *bitmap
;
764 unsigned long group
, group_offset
;
765 __u64 group_min_nr
, last_nrs
[8];
766 const unsigned long epg
= nilfs_palloc_entries_per_group(inode
);
767 const unsigned int epb
= NILFS_MDT(inode
)->mi_entries_per_block
;
768 unsigned int entry_start
, end
, pos
;
773 for (i
= 0; i
< nitems
; i
= j
) {
774 int change_group
= false;
775 int nempties
= 0, n
= 0;
777 group
= nilfs_palloc_group(inode
, entry_nrs
[i
], &group_offset
);
778 ret
= nilfs_palloc_get_desc_block(inode
, group
, 0, &desc_bh
);
781 ret
= nilfs_palloc_get_bitmap_block(inode
, group
, 0,
788 /* Get the first entry number of the group */
789 group_min_nr
= (__u64
)group
* epg
;
791 boff
= nilfs_palloc_bitmap_offset(bitmap_bh
);
792 bitmap
= kmap_local_folio(bitmap_bh
->b_folio
, boff
);
793 lock
= nilfs_mdt_bgl_lock(inode
, group
);
796 entry_start
= rounddown(group_offset
, epb
);
798 if (!nilfs_clear_bit_atomic(lock
, group_offset
,
800 nilfs_warn(inode
->i_sb
,
801 "%s (ino=%lu): entry number %llu already freed",
802 __func__
, inode
->i_ino
,
803 (unsigned long long)entry_nrs
[j
]);
809 if (j
>= nitems
|| entry_nrs
[j
] < group_min_nr
||
810 entry_nrs
[j
] >= group_min_nr
+ epg
) {
813 group_offset
= entry_nrs
[j
] - group_min_nr
;
814 if (group_offset
>= entry_start
&&
815 group_offset
< entry_start
+ epb
) {
816 /* This entry is in the same block */
821 /* Test if the entry block is empty or not */
822 end
= entry_start
+ epb
;
823 pos
= nilfs_find_next_bit(bitmap
, end
, entry_start
);
825 last_nrs
[nempties
++] = entry_nrs
[j
- 1];
826 if (nempties
>= ARRAY_SIZE(last_nrs
))
833 /* Go on to the next entry block */
834 entry_start
= rounddown(group_offset
, epb
);
837 kunmap_local(bitmap
);
838 mark_buffer_dirty(bitmap_bh
);
841 for (k
= 0; k
< nempties
; k
++) {
842 ret
= nilfs_palloc_delete_entry_block(inode
,
844 if (ret
&& ret
!= -ENOENT
)
845 nilfs_warn(inode
->i_sb
,
846 "error %d deleting block that object (entry=%llu, ino=%lu) belongs to",
847 ret
, (unsigned long long)last_nrs
[k
],
851 doff
= nilfs_palloc_group_desc_offset(inode
, group
, desc_bh
);
852 desc
= kmap_local_folio(desc_bh
->b_folio
, doff
);
853 nfree
= nilfs_palloc_group_desc_add_entries(desc
, lock
, n
);
855 mark_buffer_dirty(desc_bh
);
856 nilfs_mdt_mark_dirty(inode
);
859 if (nfree
== nilfs_palloc_entries_per_group(inode
)) {
860 ret
= nilfs_palloc_delete_bitmap_block(inode
, group
);
861 if (ret
&& ret
!= -ENOENT
)
862 nilfs_warn(inode
->i_sb
,
863 "error %d deleting bitmap block of group=%lu, ino=%lu",
864 ret
, group
, inode
->i_ino
);
870 void nilfs_palloc_setup_cache(struct inode
*inode
,
871 struct nilfs_palloc_cache
*cache
)
873 NILFS_MDT(inode
)->mi_palloc_cache
= cache
;
874 spin_lock_init(&cache
->lock
);
877 void nilfs_palloc_clear_cache(struct inode
*inode
)
879 struct nilfs_palloc_cache
*cache
= NILFS_MDT(inode
)->mi_palloc_cache
;
881 spin_lock(&cache
->lock
);
882 brelse(cache
->prev_desc
.bh
);
883 brelse(cache
->prev_bitmap
.bh
);
884 brelse(cache
->prev_entry
.bh
);
885 cache
->prev_desc
.bh
= NULL
;
886 cache
->prev_bitmap
.bh
= NULL
;
887 cache
->prev_entry
.bh
= NULL
;
888 spin_unlock(&cache
->lock
);
891 void nilfs_palloc_destroy_cache(struct inode
*inode
)
893 nilfs_palloc_clear_cache(inode
);
894 NILFS_MDT(inode
)->mi_palloc_cache
= NULL
;