1 // SPDX-License-Identifier: GPL-2.0+
3 * bmap.c - NILFS block mapping.
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Koji Sato.
11 #include <linux/string.h>
12 #include <linux/errno.h>
22 struct inode
*nilfs_bmap_get_dat(const struct nilfs_bmap
*bmap
)
24 struct the_nilfs
*nilfs
= bmap
->b_inode
->i_sb
->s_fs_info
;
29 static int nilfs_bmap_convert_error(struct nilfs_bmap
*bmap
,
30 const char *fname
, int err
)
32 struct inode
*inode
= bmap
->b_inode
;
35 __nilfs_error(inode
->i_sb
, fname
,
36 "broken bmap (inode number=%lu)", inode
->i_ino
);
43 * nilfs_bmap_lookup_at_level - find a data block or node block
47 * @ptrp: place to store the value associated to @key
49 * Description: nilfs_bmap_lookup_at_level() finds a record whose key
50 * matches @key in the block at @level of the bmap.
52 * Return Value: On success, 0 is returned and the record associated with @key
53 * is stored in the place pointed by @ptrp. On error, one of the following
54 * negative error codes is returned.
58 * %-ENOMEM - Insufficient amount of memory available.
60 * %-ENOENT - A record associated with @key does not exist.
62 int nilfs_bmap_lookup_at_level(struct nilfs_bmap
*bmap
, __u64 key
, int level
,
68 down_read(&bmap
->b_sem
);
69 ret
= bmap
->b_ops
->bop_lookup(bmap
, key
, level
, ptrp
);
71 ret
= nilfs_bmap_convert_error(bmap
, __func__
, ret
);
74 if (NILFS_BMAP_USE_VBN(bmap
)) {
75 ret
= nilfs_dat_translate(nilfs_bmap_get_dat(bmap
), *ptrp
,
82 up_read(&bmap
->b_sem
);
86 int nilfs_bmap_lookup_contig(struct nilfs_bmap
*bmap
, __u64 key
, __u64
*ptrp
,
87 unsigned int maxblocks
)
91 down_read(&bmap
->b_sem
);
92 ret
= bmap
->b_ops
->bop_lookup_contig(bmap
, key
, ptrp
, maxblocks
);
93 up_read(&bmap
->b_sem
);
95 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
98 static int nilfs_bmap_do_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
100 __u64 keys
[NILFS_BMAP_SMALL_HIGH
+ 1];
101 __u64 ptrs
[NILFS_BMAP_SMALL_HIGH
+ 1];
104 if (bmap
->b_ops
->bop_check_insert
!= NULL
) {
105 ret
= bmap
->b_ops
->bop_check_insert(bmap
, key
);
107 n
= bmap
->b_ops
->bop_gather_data(
108 bmap
, keys
, ptrs
, NILFS_BMAP_SMALL_HIGH
+ 1);
111 ret
= nilfs_btree_convert_and_insert(
112 bmap
, key
, ptr
, keys
, ptrs
, n
);
114 bmap
->b_u
.u_flags
|= NILFS_BMAP_LARGE
;
121 return bmap
->b_ops
->bop_insert(bmap
, key
, ptr
);
125 * nilfs_bmap_insert - insert a new key-record pair into a bmap
130 * Description: nilfs_bmap_insert() inserts the new key-record pair specified
131 * by @key and @rec into @bmap.
133 * Return Value: On success, 0 is returned. On error, one of the following
134 * negative error codes is returned.
138 * %-ENOMEM - Insufficient amount of memory available.
140 * %-EEXIST - A record associated with @key already exist.
142 int nilfs_bmap_insert(struct nilfs_bmap
*bmap
, __u64 key
, unsigned long rec
)
146 down_write(&bmap
->b_sem
);
147 ret
= nilfs_bmap_do_insert(bmap
, key
, rec
);
148 up_write(&bmap
->b_sem
);
150 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
153 static int nilfs_bmap_do_delete(struct nilfs_bmap
*bmap
, __u64 key
)
155 __u64 keys
[NILFS_BMAP_LARGE_LOW
+ 1];
156 __u64 ptrs
[NILFS_BMAP_LARGE_LOW
+ 1];
159 if (bmap
->b_ops
->bop_check_delete
!= NULL
) {
160 ret
= bmap
->b_ops
->bop_check_delete(bmap
, key
);
162 n
= bmap
->b_ops
->bop_gather_data(
163 bmap
, keys
, ptrs
, NILFS_BMAP_LARGE_LOW
+ 1);
166 ret
= nilfs_direct_delete_and_convert(
167 bmap
, key
, keys
, ptrs
, n
);
169 bmap
->b_u
.u_flags
&= ~NILFS_BMAP_LARGE
;
176 return bmap
->b_ops
->bop_delete(bmap
, key
);
180 * nilfs_bmap_seek_key - seek a valid entry and return its key
182 * @start: start key number
183 * @keyp: place to store valid key
185 * Description: nilfs_bmap_seek_key() seeks a valid key on @bmap
186 * starting from @start, and stores it to @keyp if found.
188 * Return Value: On success, 0 is returned. On error, one of the following
189 * negative error codes is returned.
193 * %-ENOMEM - Insufficient amount of memory available.
195 * %-ENOENT - No valid entry was found
197 int nilfs_bmap_seek_key(struct nilfs_bmap
*bmap
, __u64 start
, __u64
*keyp
)
201 down_read(&bmap
->b_sem
);
202 ret
= bmap
->b_ops
->bop_seek_key(bmap
, start
, keyp
);
203 up_read(&bmap
->b_sem
);
206 ret
= nilfs_bmap_convert_error(bmap
, __func__
, ret
);
210 int nilfs_bmap_last_key(struct nilfs_bmap
*bmap
, __u64
*keyp
)
214 down_read(&bmap
->b_sem
);
215 ret
= bmap
->b_ops
->bop_last_key(bmap
, keyp
);
216 up_read(&bmap
->b_sem
);
219 ret
= nilfs_bmap_convert_error(bmap
, __func__
, ret
);
224 * nilfs_bmap_delete - delete a key-record pair from a bmap
228 * Description: nilfs_bmap_delete() deletes the key-record pair specified by
231 * Return Value: On success, 0 is returned. On error, one of the following
232 * negative error codes is returned.
236 * %-ENOMEM - Insufficient amount of memory available.
238 * %-ENOENT - A record associated with @key does not exist.
240 int nilfs_bmap_delete(struct nilfs_bmap
*bmap
, __u64 key
)
244 down_write(&bmap
->b_sem
);
245 ret
= nilfs_bmap_do_delete(bmap
, key
);
246 up_write(&bmap
->b_sem
);
248 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
251 static int nilfs_bmap_do_truncate(struct nilfs_bmap
*bmap
, __u64 key
)
256 ret
= bmap
->b_ops
->bop_last_key(bmap
, &lastkey
);
263 while (key
<= lastkey
) {
264 ret
= nilfs_bmap_do_delete(bmap
, lastkey
);
267 ret
= bmap
->b_ops
->bop_last_key(bmap
, &lastkey
);
278 * nilfs_bmap_truncate - truncate a bmap to a specified key
282 * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
283 * greater than or equal to @key from @bmap.
285 * Return Value: On success, 0 is returned. On error, one of the following
286 * negative error codes is returned.
290 * %-ENOMEM - Insufficient amount of memory available.
292 int nilfs_bmap_truncate(struct nilfs_bmap
*bmap
, __u64 key
)
296 down_write(&bmap
->b_sem
);
297 ret
= nilfs_bmap_do_truncate(bmap
, key
);
298 up_write(&bmap
->b_sem
);
300 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
304 * nilfs_bmap_clear - free resources a bmap holds
307 * Description: nilfs_bmap_clear() frees resources associated with @bmap.
309 void nilfs_bmap_clear(struct nilfs_bmap
*bmap
)
311 down_write(&bmap
->b_sem
);
312 if (bmap
->b_ops
->bop_clear
!= NULL
)
313 bmap
->b_ops
->bop_clear(bmap
);
314 up_write(&bmap
->b_sem
);
318 * nilfs_bmap_propagate - propagate dirty state
322 * Description: nilfs_bmap_propagate() marks the buffers that directly or
323 * indirectly refer to the block specified by @bh dirty.
325 * Return Value: On success, 0 is returned. On error, one of the following
326 * negative error codes is returned.
330 * %-ENOMEM - Insufficient amount of memory available.
332 int nilfs_bmap_propagate(struct nilfs_bmap
*bmap
, struct buffer_head
*bh
)
336 down_write(&bmap
->b_sem
);
337 ret
= bmap
->b_ops
->bop_propagate(bmap
, bh
);
338 up_write(&bmap
->b_sem
);
340 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
344 * nilfs_bmap_lookup_dirty_buffers -
346 * @listp: pointer to buffer head list
348 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap
*bmap
,
349 struct list_head
*listp
)
351 if (bmap
->b_ops
->bop_lookup_dirty_buffers
!= NULL
)
352 bmap
->b_ops
->bop_lookup_dirty_buffers(bmap
, listp
);
356 * nilfs_bmap_assign - assign a new block number to a block
358 * @bh: pointer to buffer head
359 * @blocknr: block number
360 * @binfo: block information
362 * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
363 * buffer specified by @bh.
365 * Return Value: On success, 0 is returned and the buffer head of a newly
366 * create buffer and the block information associated with the buffer are
367 * stored in the place pointed by @bh and @binfo, respectively. On error, one
368 * of the following negative error codes is returned.
372 * %-ENOMEM - Insufficient amount of memory available.
374 int nilfs_bmap_assign(struct nilfs_bmap
*bmap
,
375 struct buffer_head
**bh
,
376 unsigned long blocknr
,
377 union nilfs_binfo
*binfo
)
381 down_write(&bmap
->b_sem
);
382 ret
= bmap
->b_ops
->bop_assign(bmap
, bh
, blocknr
, binfo
);
383 up_write(&bmap
->b_sem
);
385 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
389 * nilfs_bmap_mark - mark block dirty
394 * Description: nilfs_bmap_mark() marks the block specified by @key and @level
397 * Return Value: On success, 0 is returned. On error, one of the following
398 * negative error codes is returned.
402 * %-ENOMEM - Insufficient amount of memory available.
404 int nilfs_bmap_mark(struct nilfs_bmap
*bmap
, __u64 key
, int level
)
408 if (bmap
->b_ops
->bop_mark
== NULL
)
411 down_write(&bmap
->b_sem
);
412 ret
= bmap
->b_ops
->bop_mark(bmap
, key
, level
);
413 up_write(&bmap
->b_sem
);
415 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
419 * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
422 * Description: nilfs_test_and_clear() is the atomic operation to test and
423 * clear the dirty state of @bmap.
425 * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
427 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap
*bmap
)
431 down_write(&bmap
->b_sem
);
432 ret
= nilfs_bmap_dirty(bmap
);
433 nilfs_bmap_clear_dirty(bmap
);
434 up_write(&bmap
->b_sem
);
442 __u64
nilfs_bmap_data_get_key(const struct nilfs_bmap
*bmap
,
443 const struct buffer_head
*bh
)
445 struct buffer_head
*pbh
;
448 key
= page_index(bh
->b_page
) << (PAGE_SHIFT
-
449 bmap
->b_inode
->i_blkbits
);
450 for (pbh
= page_buffers(bh
->b_page
); pbh
!= bh
; pbh
= pbh
->b_this_page
)
456 __u64
nilfs_bmap_find_target_seq(const struct nilfs_bmap
*bmap
, __u64 key
)
460 diff
= key
- bmap
->b_last_allocated_key
;
461 if ((nilfs_bmap_keydiff_abs(diff
) < NILFS_INODE_BMAP_SIZE
) &&
462 (bmap
->b_last_allocated_ptr
!= NILFS_BMAP_INVALID_PTR
) &&
463 (bmap
->b_last_allocated_ptr
+ diff
> 0))
464 return bmap
->b_last_allocated_ptr
+ diff
;
466 return NILFS_BMAP_INVALID_PTR
;
469 #define NILFS_BMAP_GROUP_DIV 8
470 __u64
nilfs_bmap_find_target_in_group(const struct nilfs_bmap
*bmap
)
472 struct inode
*dat
= nilfs_bmap_get_dat(bmap
);
473 unsigned long entries_per_group
= nilfs_palloc_entries_per_group(dat
);
474 unsigned long group
= bmap
->b_inode
->i_ino
/ entries_per_group
;
476 return group
* entries_per_group
+
477 (bmap
->b_inode
->i_ino
% NILFS_BMAP_GROUP_DIV
) *
478 (entries_per_group
/ NILFS_BMAP_GROUP_DIV
);
481 static struct lock_class_key nilfs_bmap_dat_lock_key
;
482 static struct lock_class_key nilfs_bmap_mdt_lock_key
;
485 * nilfs_bmap_read - read a bmap from an inode
487 * @raw_inode: on-disk inode
489 * Description: nilfs_bmap_read() initializes the bmap @bmap.
491 * Return Value: On success, 0 is returned. On error, the following negative
492 * error code is returned.
494 * %-ENOMEM - Insufficient amount of memory available.
496 int nilfs_bmap_read(struct nilfs_bmap
*bmap
, struct nilfs_inode
*raw_inode
)
498 if (raw_inode
== NULL
)
499 memset(bmap
->b_u
.u_data
, 0, NILFS_BMAP_SIZE
);
501 memcpy(bmap
->b_u
.u_data
, raw_inode
->i_bmap
, NILFS_BMAP_SIZE
);
503 init_rwsem(&bmap
->b_sem
);
505 bmap
->b_inode
= &NILFS_BMAP_I(bmap
)->vfs_inode
;
506 switch (bmap
->b_inode
->i_ino
) {
508 bmap
->b_ptr_type
= NILFS_BMAP_PTR_P
;
509 bmap
->b_last_allocated_key
= 0;
510 bmap
->b_last_allocated_ptr
= NILFS_BMAP_NEW_PTR_INIT
;
511 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_dat_lock_key
);
513 case NILFS_CPFILE_INO
:
514 case NILFS_SUFILE_INO
:
515 bmap
->b_ptr_type
= NILFS_BMAP_PTR_VS
;
516 bmap
->b_last_allocated_key
= 0;
517 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
518 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_mdt_lock_key
);
520 case NILFS_IFILE_INO
:
521 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_mdt_lock_key
);
524 bmap
->b_ptr_type
= NILFS_BMAP_PTR_VM
;
525 bmap
->b_last_allocated_key
= 0;
526 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
530 return (bmap
->b_u
.u_flags
& NILFS_BMAP_LARGE
) ?
531 nilfs_btree_init(bmap
) : nilfs_direct_init(bmap
);
535 * nilfs_bmap_write - write back a bmap to an inode
537 * @raw_inode: on-disk inode
539 * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
541 void nilfs_bmap_write(struct nilfs_bmap
*bmap
, struct nilfs_inode
*raw_inode
)
543 down_write(&bmap
->b_sem
);
544 memcpy(raw_inode
->i_bmap
, bmap
->b_u
.u_data
,
545 NILFS_INODE_BMAP_SIZE
* sizeof(__le64
));
546 if (bmap
->b_inode
->i_ino
== NILFS_DAT_INO
)
547 bmap
->b_last_allocated_ptr
= NILFS_BMAP_NEW_PTR_INIT
;
549 up_write(&bmap
->b_sem
);
552 void nilfs_bmap_init_gc(struct nilfs_bmap
*bmap
)
554 memset(&bmap
->b_u
, 0, NILFS_BMAP_SIZE
);
555 init_rwsem(&bmap
->b_sem
);
556 bmap
->b_inode
= &NILFS_BMAP_I(bmap
)->vfs_inode
;
557 bmap
->b_ptr_type
= NILFS_BMAP_PTR_U
;
558 bmap
->b_last_allocated_key
= 0;
559 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
561 nilfs_btree_init_gc(bmap
);
564 void nilfs_bmap_save(const struct nilfs_bmap
*bmap
,
565 struct nilfs_bmap_store
*store
)
567 memcpy(store
->data
, bmap
->b_u
.u_data
, sizeof(store
->data
));
568 store
->last_allocated_key
= bmap
->b_last_allocated_key
;
569 store
->last_allocated_ptr
= bmap
->b_last_allocated_ptr
;
570 store
->state
= bmap
->b_state
;
573 void nilfs_bmap_restore(struct nilfs_bmap
*bmap
,
574 const struct nilfs_bmap_store
*store
)
576 memcpy(bmap
->b_u
.u_data
, store
->data
, sizeof(store
->data
));
577 bmap
->b_last_allocated_key
= store
->last_allocated_key
;
578 bmap
->b_last_allocated_ptr
= store
->last_allocated_ptr
;
579 bmap
->b_state
= store
->state
;