blob: 2dc5fae6a6ee7e4aa7b96516b8a26e589d5cfa90 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * alloc.c - NILFS dat/inode allocator
4 *
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 *
7 * Originally written by Koji Sato.
8 * Two allocators were unified by Ryusuke Konishi and Amagai Yoshiji.
9 */
10
11#include <linux/types.h>
12#include <linux/buffer_head.h>
13#include <linux/fs.h>
14#include <linux/bitops.h>
15#include <linux/slab.h>
16#include "mdt.h"
17#include "alloc.h"
18
19
20/**
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
24 */
25static inline unsigned long
26nilfs_palloc_groups_per_desc_block(const struct inode *inode)
27{
28 return i_blocksize(inode) /
29 sizeof(struct nilfs_palloc_group_desc);
30}
31
32/**
33 * nilfs_palloc_groups_count - get maximum number of groups
34 * @inode: inode of metadata file using this allocator
35 */
36static inline unsigned long
37nilfs_palloc_groups_count(const struct inode *inode)
38{
39 return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
40}
41
42/**
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
46 */
47int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned int entry_size)
48{
49 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
50
51 mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
52 if (!mi->mi_bgl)
53 return -ENOMEM;
54
55 bgl_lock_init(mi->mi_bgl);
56
57 nilfs_mdt_set_entry_size(inode, entry_size, 0);
58
59 mi->mi_blocks_per_group =
60 DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
61 mi->mi_entries_per_block) + 1;
62 /*
63 * Number of blocks in a group including entry blocks
64 * and a bitmap block
65 */
66 mi->mi_blocks_per_desc_block =
67 nilfs_palloc_groups_per_desc_block(inode) *
68 mi->mi_blocks_per_group + 1;
69 /*
70 * Number of blocks per descriptor including the
71 * descriptor block
72 */
73 return 0;
74}
75
76/**
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
81 */
82static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
83 unsigned long *offset)
84{
85 __u64 group = nr;
86
87 *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
88 return group;
89}
90
91/**
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
95 *
96 * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
97 * block which contains a descriptor of the specified group.
98 */
99static unsigned long
100nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
101{
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;
105}
106
107/**
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
111 *
112 * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
113 * block used to allocate/deallocate entries in the specified group.
114 */
115static unsigned long
116nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
117{
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;
122}
123
124/**
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
128 */
129static unsigned long
130nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
131 spinlock_t *lock)
132{
133 unsigned long nfree;
134
135 spin_lock(lock);
136 nfree = le32_to_cpu(desc->pg_nfrees);
137 spin_unlock(lock);
138 return nfree;
139}
140
141/**
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
146 */
147static u32
148nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
149 spinlock_t *lock, u32 n)
150{
151 u32 nfree;
152
153 spin_lock(lock);
154 le32_add_cpu(&desc->pg_nfrees, n);
155 nfree = le32_to_cpu(desc->pg_nfrees);
156 spin_unlock(lock);
157 return nfree;
158}
159
160/**
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)
164 */
165static unsigned long
166nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
167{
168 unsigned long group, group_offset;
169
170 group = nilfs_palloc_group(inode, nr, &group_offset);
171
172 return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
173 group_offset / NILFS_MDT(inode)->mi_entries_per_block;
174}
175
176/**
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 * @kaddr: kernel address mapped for the page including the buffer
181 */
182static void nilfs_palloc_desc_block_init(struct inode *inode,
183 struct buffer_head *bh, void *kaddr)
184{
185 struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
186 unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
187 __le32 nfrees;
188
189 nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
190 while (n-- > 0) {
191 desc->pg_nfrees = nfrees;
192 desc++;
193 }
194}
195
196static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
197 int create,
198 void (*init_block)(struct inode *,
199 struct buffer_head *,
200 void *),
201 struct buffer_head **bhp,
202 struct nilfs_bh_assoc *prev,
203 spinlock_t *lock)
204{
205 int ret;
206
207 spin_lock(lock);
208 if (prev->bh && blkoff == prev->blkoff &&
209 likely(buffer_uptodate(prev->bh))) {
210 get_bh(prev->bh);
211 *bhp = prev->bh;
212 spin_unlock(lock);
213 return 0;
214 }
215 spin_unlock(lock);
216
217 ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
218 if (!ret) {
219 spin_lock(lock);
220 /*
221 * The following code must be safe for change of the
222 * cache contents during the get block call.
223 */
224 brelse(prev->bh);
225 get_bh(*bhp);
226 prev->bh = *bhp;
227 prev->blkoff = blkoff;
228 spin_unlock(lock);
229 }
230 return ret;
231}
232
233/**
234 * nilfs_palloc_delete_block - delete a block on the persistent allocator file
235 * @inode: inode of metadata file using this allocator
236 * @blkoff: block offset
237 * @prev: nilfs_bh_assoc struct of the last used buffer
238 * @lock: spin lock protecting @prev
239 */
240static int nilfs_palloc_delete_block(struct inode *inode, unsigned long blkoff,
241 struct nilfs_bh_assoc *prev,
242 spinlock_t *lock)
243{
244 spin_lock(lock);
245 if (prev->bh && blkoff == prev->blkoff) {
246 brelse(prev->bh);
247 prev->bh = NULL;
248 }
249 spin_unlock(lock);
250 return nilfs_mdt_delete_block(inode, blkoff);
251}
252
253/**
254 * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
255 * @inode: inode of metadata file using this allocator
256 * @group: group number
257 * @create: create flag
258 * @bhp: pointer to store the resultant buffer head
259 */
260static int nilfs_palloc_get_desc_block(struct inode *inode,
261 unsigned long group,
262 int create, struct buffer_head **bhp)
263{
264 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
265
266 return nilfs_palloc_get_block(inode,
267 nilfs_palloc_desc_blkoff(inode, group),
268 create, nilfs_palloc_desc_block_init,
269 bhp, &cache->prev_desc, &cache->lock);
270}
271
272/**
273 * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
274 * @inode: inode of metadata file using this allocator
275 * @group: group number
276 * @create: create flag
277 * @bhp: pointer to store the resultant buffer head
278 */
279static int nilfs_palloc_get_bitmap_block(struct inode *inode,
280 unsigned long group,
281 int create, struct buffer_head **bhp)
282{
283 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
284
285 return nilfs_palloc_get_block(inode,
286 nilfs_palloc_bitmap_blkoff(inode, group),
287 create, NULL, bhp,
288 &cache->prev_bitmap, &cache->lock);
289}
290
291/**
292 * nilfs_palloc_delete_bitmap_block - delete a bitmap block
293 * @inode: inode of metadata file using this allocator
294 * @group: group number
295 */
296static int nilfs_palloc_delete_bitmap_block(struct inode *inode,
297 unsigned long group)
298{
299 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
300
301 return nilfs_palloc_delete_block(inode,
302 nilfs_palloc_bitmap_blkoff(inode,
303 group),
304 &cache->prev_bitmap, &cache->lock);
305}
306
307/**
308 * nilfs_palloc_get_entry_block - get buffer head of an entry block
309 * @inode: inode of metadata file using this allocator
310 * @nr: serial number of the entry (e.g. inode number)
311 * @create: create flag
312 * @bhp: pointer to store the resultant buffer head
313 */
314int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
315 int create, struct buffer_head **bhp)
316{
317 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
318
319 return nilfs_palloc_get_block(inode,
320 nilfs_palloc_entry_blkoff(inode, nr),
321 create, NULL, bhp,
322 &cache->prev_entry, &cache->lock);
323}
324
325/**
326 * nilfs_palloc_delete_entry_block - delete an entry block
327 * @inode: inode of metadata file using this allocator
328 * @nr: serial number of the entry
329 */
330static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
331{
332 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
333
334 return nilfs_palloc_delete_block(inode,
335 nilfs_palloc_entry_blkoff(inode, nr),
336 &cache->prev_entry, &cache->lock);
337}
338
339/**
340 * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
341 * @inode: inode of metadata file using this allocator
342 * @group: group number
343 * @bh: buffer head of the buffer storing the group descriptor block
344 * @kaddr: kernel address mapped for the page including the buffer
345 */
346static struct nilfs_palloc_group_desc *
347nilfs_palloc_block_get_group_desc(const struct inode *inode,
348 unsigned long group,
349 const struct buffer_head *bh, void *kaddr)
350{
351 return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
352 group % nilfs_palloc_groups_per_desc_block(inode);
353}
354
355/**
356 * nilfs_palloc_block_get_entry - get kernel address of an entry
357 * @inode: inode of metadata file using this allocator
358 * @nr: serial number of the entry (e.g. inode number)
359 * @bh: buffer head of the buffer storing the entry block
360 * @kaddr: kernel address mapped for the page including the buffer
361 */
362void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
363 const struct buffer_head *bh, void *kaddr)
364{
365 unsigned long entry_offset, group_offset;
366
367 nilfs_palloc_group(inode, nr, &group_offset);
368 entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
369
370 return kaddr + bh_offset(bh) +
371 entry_offset * NILFS_MDT(inode)->mi_entry_size;
372}
373
374/**
375 * nilfs_palloc_find_available_slot - find available slot in a group
376 * @bitmap: bitmap of the group
377 * @target: offset number of an entry in the group (start point)
378 * @bsize: size in bits
379 * @lock: spin lock protecting @bitmap
380 * @wrap: whether to wrap around
381 */
382static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
383 unsigned long target,
384 unsigned int bsize,
385 spinlock_t *lock, bool wrap)
386{
387 int pos, end = bsize;
388
389 if (likely(target < bsize)) {
390 pos = target;
391 do {
392 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
393 if (pos >= end)
394 break;
395 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
396 return pos;
397 } while (++pos < end);
398
399 end = target;
400 }
401 if (!wrap)
402 return -ENOSPC;
403
404 /* wrap around */
405 for (pos = 0; pos < end; pos++) {
406 pos = nilfs_find_next_zero_bit(bitmap, end, pos);
407 if (pos >= end)
408 break;
409 if (!nilfs_set_bit_atomic(lock, pos, bitmap))
410 return pos;
411 }
412
413 return -ENOSPC;
414}
415
416/**
417 * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
418 * in a group descriptor block
419 * @inode: inode of metadata file using this allocator
420 * @curr: current group number
421 * @max: maximum number of groups
422 */
423static unsigned long
424nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
425 unsigned long curr, unsigned long max)
426{
427 return min_t(unsigned long,
428 nilfs_palloc_groups_per_desc_block(inode) -
429 curr % nilfs_palloc_groups_per_desc_block(inode),
430 max - curr + 1);
431}
432
433/**
434 * nilfs_palloc_count_desc_blocks - count descriptor blocks number
435 * @inode: inode of metadata file using this allocator
436 * @desc_blocks: descriptor blocks number [out]
437 */
438static int nilfs_palloc_count_desc_blocks(struct inode *inode,
439 unsigned long *desc_blocks)
440{
441 __u64 blknum;
442 int ret;
443
444 ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
445 if (likely(!ret))
446 *desc_blocks = DIV_ROUND_UP(
447 (unsigned long)blknum,
448 NILFS_MDT(inode)->mi_blocks_per_desc_block);
449 return ret;
450}
451
452/**
453 * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
454 * MDT file growing
455 * @inode: inode of metadata file using this allocator
456 * @desc_blocks: known current descriptor blocks count
457 */
458static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
459 unsigned long desc_blocks)
460{
461 return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
462 nilfs_palloc_groups_count(inode);
463}
464
465/**
466 * nilfs_palloc_count_max_entries - count max number of entries that can be
467 * described by descriptor blocks count
468 * @inode: inode of metadata file using this allocator
469 * @nused: current number of used entries
470 * @nmaxp: max number of entries [out]
471 */
472int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
473{
474 unsigned long desc_blocks = 0;
475 u64 entries_per_desc_block, nmax;
476 int err;
477
478 err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
479 if (unlikely(err))
480 return err;
481
482 entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
483 nilfs_palloc_groups_per_desc_block(inode);
484 nmax = entries_per_desc_block * desc_blocks;
485
486 if (nused == nmax &&
487 nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
488 nmax += entries_per_desc_block;
489
490 if (nused > nmax)
491 return -ERANGE;
492
493 *nmaxp = nmax;
494 return 0;
495}
496
497/**
498 * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
499 * @inode: inode of metadata file using this allocator
500 * @req: nilfs_palloc_req structure exchanged for the allocation
501 * @wrap: whether to wrap around
502 */
503int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
504 struct nilfs_palloc_req *req, bool wrap)
505{
506 struct buffer_head *desc_bh, *bitmap_bh;
507 struct nilfs_palloc_group_desc *desc;
508 unsigned char *bitmap;
509 void *desc_kaddr, *bitmap_kaddr;
510 unsigned long group, maxgroup, ngroups;
511 unsigned long group_offset, maxgroup_offset;
512 unsigned long n, entries_per_group;
513 unsigned long i, j;
514 spinlock_t *lock;
515 int pos, ret;
516
517 ngroups = nilfs_palloc_groups_count(inode);
518 maxgroup = ngroups - 1;
519 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
520 entries_per_group = nilfs_palloc_entries_per_group(inode);
521
522 for (i = 0; i < ngroups; i += n) {
523 if (group >= ngroups && wrap) {
524 /* wrap around */
525 group = 0;
526 maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
527 &maxgroup_offset) - 1;
528 }
529 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
530 if (ret < 0)
531 return ret;
532 desc_kaddr = kmap(desc_bh->b_page);
533 desc = nilfs_palloc_block_get_group_desc(
534 inode, group, desc_bh, desc_kaddr);
535 n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
536 maxgroup);
537 for (j = 0; j < n; j++, desc++, group++) {
538 lock = nilfs_mdt_bgl_lock(inode, group);
539 if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
540 ret = nilfs_palloc_get_bitmap_block(
541 inode, group, 1, &bitmap_bh);
542 if (ret < 0)
543 goto out_desc;
544 bitmap_kaddr = kmap(bitmap_bh->b_page);
545 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
546 pos = nilfs_palloc_find_available_slot(
547 bitmap, group_offset,
548 entries_per_group, lock, wrap);
549 /*
550 * Since the search for a free slot in the
551 * second and subsequent bitmap blocks always
552 * starts from the beginning, the wrap flag
553 * only has an effect on the first search.
554 */
555 if (pos >= 0) {
556 /* found a free entry */
557 nilfs_palloc_group_desc_add_entries(
558 desc, lock, -1);
559 req->pr_entry_nr =
560 entries_per_group * group + pos;
561 kunmap(desc_bh->b_page);
562 kunmap(bitmap_bh->b_page);
563
564 req->pr_desc_bh = desc_bh;
565 req->pr_bitmap_bh = bitmap_bh;
566 return 0;
567 }
568 kunmap(bitmap_bh->b_page);
569 brelse(bitmap_bh);
570 }
571
572 group_offset = 0;
573 }
574
575 kunmap(desc_bh->b_page);
576 brelse(desc_bh);
577 }
578
579 /* no entries left */
580 return -ENOSPC;
581
582 out_desc:
583 kunmap(desc_bh->b_page);
584 brelse(desc_bh);
585 return ret;
586}
587
588/**
589 * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
590 * @inode: inode of metadata file using this allocator
591 * @req: nilfs_palloc_req structure exchanged for the allocation
592 */
593void nilfs_palloc_commit_alloc_entry(struct inode *inode,
594 struct nilfs_palloc_req *req)
595{
596 mark_buffer_dirty(req->pr_bitmap_bh);
597 mark_buffer_dirty(req->pr_desc_bh);
598 nilfs_mdt_mark_dirty(inode);
599
600 brelse(req->pr_bitmap_bh);
601 brelse(req->pr_desc_bh);
602}
603
604/**
605 * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
606 * @inode: inode of metadata file using this allocator
607 * @req: nilfs_palloc_req structure exchanged for the removal
608 */
609void nilfs_palloc_commit_free_entry(struct inode *inode,
610 struct nilfs_palloc_req *req)
611{
612 struct nilfs_palloc_group_desc *desc;
613 unsigned long group, group_offset;
614 unsigned char *bitmap;
615 void *desc_kaddr, *bitmap_kaddr;
616 spinlock_t *lock;
617
618 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
619 desc_kaddr = kmap(req->pr_desc_bh->b_page);
620 desc = nilfs_palloc_block_get_group_desc(inode, group,
621 req->pr_desc_bh, desc_kaddr);
622 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
623 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
624 lock = nilfs_mdt_bgl_lock(inode, group);
625
626 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
627 nilfs_warn(inode->i_sb,
628 "%s (ino=%lu): entry number %llu already freed",
629 __func__, inode->i_ino,
630 (unsigned long long)req->pr_entry_nr);
631 else
632 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
633
634 kunmap(req->pr_bitmap_bh->b_page);
635 kunmap(req->pr_desc_bh->b_page);
636
637 mark_buffer_dirty(req->pr_desc_bh);
638 mark_buffer_dirty(req->pr_bitmap_bh);
639 nilfs_mdt_mark_dirty(inode);
640
641 brelse(req->pr_bitmap_bh);
642 brelse(req->pr_desc_bh);
643}
644
645/**
646 * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
647 * @inode: inode of metadata file using this allocator
648 * @req: nilfs_palloc_req structure exchanged for the allocation
649 */
650void nilfs_palloc_abort_alloc_entry(struct inode *inode,
651 struct nilfs_palloc_req *req)
652{
653 struct nilfs_palloc_group_desc *desc;
654 void *desc_kaddr, *bitmap_kaddr;
655 unsigned char *bitmap;
656 unsigned long group, group_offset;
657 spinlock_t *lock;
658
659 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
660 desc_kaddr = kmap(req->pr_desc_bh->b_page);
661 desc = nilfs_palloc_block_get_group_desc(inode, group,
662 req->pr_desc_bh, desc_kaddr);
663 bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
664 bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
665 lock = nilfs_mdt_bgl_lock(inode, group);
666
667 if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
668 nilfs_warn(inode->i_sb,
669 "%s (ino=%lu): entry number %llu already freed",
670 __func__, inode->i_ino,
671 (unsigned long long)req->pr_entry_nr);
672 else
673 nilfs_palloc_group_desc_add_entries(desc, lock, 1);
674
675 kunmap(req->pr_bitmap_bh->b_page);
676 kunmap(req->pr_desc_bh->b_page);
677
678 brelse(req->pr_bitmap_bh);
679 brelse(req->pr_desc_bh);
680
681 req->pr_entry_nr = 0;
682 req->pr_bitmap_bh = NULL;
683 req->pr_desc_bh = NULL;
684}
685
686/**
687 * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
688 * @inode: inode of metadata file using this allocator
689 * @req: nilfs_palloc_req structure exchanged for the removal
690 */
691int nilfs_palloc_prepare_free_entry(struct inode *inode,
692 struct nilfs_palloc_req *req)
693{
694 struct buffer_head *desc_bh, *bitmap_bh;
695 unsigned long group, group_offset;
696 int ret;
697
698 group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
699 ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
700 if (ret < 0)
701 return ret;
702 ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
703 if (ret < 0) {
704 brelse(desc_bh);
705 return ret;
706 }
707
708 req->pr_desc_bh = desc_bh;
709 req->pr_bitmap_bh = bitmap_bh;
710 return 0;
711}
712
713/**
714 * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
715 * @inode: inode of metadata file using this allocator
716 * @req: nilfs_palloc_req structure exchanged for the removal
717 */
718void nilfs_palloc_abort_free_entry(struct inode *inode,
719 struct nilfs_palloc_req *req)
720{
721 brelse(req->pr_bitmap_bh);
722 brelse(req->pr_desc_bh);
723
724 req->pr_entry_nr = 0;
725 req->pr_bitmap_bh = NULL;
726 req->pr_desc_bh = NULL;
727}
728
729/**
730 * nilfs_palloc_freev - deallocate a set of persistent objects
731 * @inode: inode of metadata file using this allocator
732 * @entry_nrs: array of entry numbers to be deallocated
733 * @nitems: number of entries stored in @entry_nrs
734 */
735int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
736{
737 struct buffer_head *desc_bh, *bitmap_bh;
738 struct nilfs_palloc_group_desc *desc;
739 unsigned char *bitmap;
740 void *desc_kaddr, *bitmap_kaddr;
741 unsigned long group, group_offset;
742 __u64 group_min_nr, last_nrs[8];
743 const unsigned long epg = nilfs_palloc_entries_per_group(inode);
744 const unsigned int epb = NILFS_MDT(inode)->mi_entries_per_block;
745 unsigned int entry_start, end, pos;
746 spinlock_t *lock;
747 int i, j, k, ret;
748 u32 nfree;
749
750 for (i = 0; i < nitems; i = j) {
751 int change_group = false;
752 int nempties = 0, n = 0;
753
754 group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
755 ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
756 if (ret < 0)
757 return ret;
758 ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
759 &bitmap_bh);
760 if (ret < 0) {
761 brelse(desc_bh);
762 return ret;
763 }
764
765 /* Get the first entry number of the group */
766 group_min_nr = (__u64)group * epg;
767
768 bitmap_kaddr = kmap(bitmap_bh->b_page);
769 bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
770 lock = nilfs_mdt_bgl_lock(inode, group);
771
772 j = i;
773 entry_start = rounddown(group_offset, epb);
774 do {
775 if (!nilfs_clear_bit_atomic(lock, group_offset,
776 bitmap)) {
777 nilfs_warn(inode->i_sb,
778 "%s (ino=%lu): entry number %llu already freed",
779 __func__, inode->i_ino,
780 (unsigned long long)entry_nrs[j]);
781 } else {
782 n++;
783 }
784
785 j++;
786 if (j >= nitems || entry_nrs[j] < group_min_nr ||
787 entry_nrs[j] >= group_min_nr + epg) {
788 change_group = true;
789 } else {
790 group_offset = entry_nrs[j] - group_min_nr;
791 if (group_offset >= entry_start &&
792 group_offset < entry_start + epb) {
793 /* This entry is in the same block */
794 continue;
795 }
796 }
797
798 /* Test if the entry block is empty or not */
799 end = entry_start + epb;
800 pos = nilfs_find_next_bit(bitmap, end, entry_start);
801 if (pos >= end) {
802 last_nrs[nempties++] = entry_nrs[j - 1];
803 if (nempties >= ARRAY_SIZE(last_nrs))
804 break;
805 }
806
807 if (change_group)
808 break;
809
810 /* Go on to the next entry block */
811 entry_start = rounddown(group_offset, epb);
812 } while (true);
813
814 kunmap(bitmap_bh->b_page);
815 mark_buffer_dirty(bitmap_bh);
816 brelse(bitmap_bh);
817
818 for (k = 0; k < nempties; k++) {
819 ret = nilfs_palloc_delete_entry_block(inode,
820 last_nrs[k]);
821 if (ret && ret != -ENOENT)
822 nilfs_warn(inode->i_sb,
823 "error %d deleting block that object (entry=%llu, ino=%lu) belongs to",
824 ret, (unsigned long long)last_nrs[k],
825 inode->i_ino);
826 }
827
828 desc_kaddr = kmap_atomic(desc_bh->b_page);
829 desc = nilfs_palloc_block_get_group_desc(
830 inode, group, desc_bh, desc_kaddr);
831 nfree = nilfs_palloc_group_desc_add_entries(desc, lock, n);
832 kunmap_atomic(desc_kaddr);
833 mark_buffer_dirty(desc_bh);
834 nilfs_mdt_mark_dirty(inode);
835 brelse(desc_bh);
836
837 if (nfree == nilfs_palloc_entries_per_group(inode)) {
838 ret = nilfs_palloc_delete_bitmap_block(inode, group);
839 if (ret && ret != -ENOENT)
840 nilfs_warn(inode->i_sb,
841 "error %d deleting bitmap block of group=%lu, ino=%lu",
842 ret, group, inode->i_ino);
843 }
844 }
845 return 0;
846}
847
848void nilfs_palloc_setup_cache(struct inode *inode,
849 struct nilfs_palloc_cache *cache)
850{
851 NILFS_MDT(inode)->mi_palloc_cache = cache;
852 spin_lock_init(&cache->lock);
853}
854
855void nilfs_palloc_clear_cache(struct inode *inode)
856{
857 struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
858
859 spin_lock(&cache->lock);
860 brelse(cache->prev_desc.bh);
861 brelse(cache->prev_bitmap.bh);
862 brelse(cache->prev_entry.bh);
863 cache->prev_desc.bh = NULL;
864 cache->prev_bitmap.bh = NULL;
865 cache->prev_entry.bh = NULL;
866 spin_unlock(&cache->lock);
867}
868
869void nilfs_palloc_destroy_cache(struct inode *inode)
870{
871 nilfs_palloc_clear_cache(inode);
872 NILFS_MDT(inode)->mi_palloc_cache = NULL;
873}