2 * bmap.c - NILFS block mapping.
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Koji Sato <koji@osrg.net>.
24 #include <linux/string.h>
25 #include <linux/errno.h>
35 struct inode
*nilfs_bmap_get_dat(const struct nilfs_bmap
*bmap
)
37 struct the_nilfs
*nilfs
= bmap
->b_inode
->i_sb
->s_fs_info
;
42 static int nilfs_bmap_convert_error(struct nilfs_bmap
*bmap
,
43 const char *fname
, int err
)
45 struct inode
*inode
= bmap
->b_inode
;
48 nilfs_error(inode
->i_sb
, fname
,
49 "broken bmap (inode number=%lu)\n", inode
->i_ino
);
56 * nilfs_bmap_lookup_at_level - find a data block or node block
60 * @ptrp: place to store the value associated to @key
62 * Description: nilfs_bmap_lookup_at_level() finds a record whose key
63 * matches @key in the block at @level of the bmap.
65 * Return Value: On success, 0 is returned and the record associated with @key
66 * is stored in the place pointed by @ptrp. On error, one of the following
67 * negative error codes is returned.
71 * %-ENOMEM - Insufficient amount of memory available.
73 * %-ENOENT - A record associated with @key does not exist.
75 int nilfs_bmap_lookup_at_level(struct nilfs_bmap
*bmap
, __u64 key
, int level
,
81 down_read(&bmap
->b_sem
);
82 ret
= bmap
->b_ops
->bop_lookup(bmap
, key
, level
, ptrp
);
84 ret
= nilfs_bmap_convert_error(bmap
, __func__
, ret
);
87 if (NILFS_BMAP_USE_VBN(bmap
)) {
88 ret
= nilfs_dat_translate(nilfs_bmap_get_dat(bmap
), *ptrp
,
95 up_read(&bmap
->b_sem
);
99 int nilfs_bmap_lookup_contig(struct nilfs_bmap
*bmap
, __u64 key
, __u64
*ptrp
,
104 down_read(&bmap
->b_sem
);
105 ret
= bmap
->b_ops
->bop_lookup_contig(bmap
, key
, ptrp
, maxblocks
);
106 up_read(&bmap
->b_sem
);
108 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
111 static int nilfs_bmap_do_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
113 __u64 keys
[NILFS_BMAP_SMALL_HIGH
+ 1];
114 __u64 ptrs
[NILFS_BMAP_SMALL_HIGH
+ 1];
117 if (bmap
->b_ops
->bop_check_insert
!= NULL
) {
118 ret
= bmap
->b_ops
->bop_check_insert(bmap
, key
);
120 n
= bmap
->b_ops
->bop_gather_data(
121 bmap
, keys
, ptrs
, NILFS_BMAP_SMALL_HIGH
+ 1);
124 ret
= nilfs_btree_convert_and_insert(
125 bmap
, key
, ptr
, keys
, ptrs
, n
);
127 bmap
->b_u
.u_flags
|= NILFS_BMAP_LARGE
;
134 return bmap
->b_ops
->bop_insert(bmap
, key
, ptr
);
138 * nilfs_bmap_insert - insert a new key-record pair into a bmap
143 * Description: nilfs_bmap_insert() inserts the new key-record pair specified
144 * by @key and @rec into @bmap.
146 * Return Value: On success, 0 is returned. On error, one of the following
147 * negative error codes is returned.
151 * %-ENOMEM - Insufficient amount of memory available.
153 * %-EEXIST - A record associated with @key already exist.
155 int nilfs_bmap_insert(struct nilfs_bmap
*bmap
,
161 down_write(&bmap
->b_sem
);
162 ret
= nilfs_bmap_do_insert(bmap
, key
, rec
);
163 up_write(&bmap
->b_sem
);
165 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
168 static int nilfs_bmap_do_delete(struct nilfs_bmap
*bmap
, __u64 key
)
170 __u64 keys
[NILFS_BMAP_LARGE_LOW
+ 1];
171 __u64 ptrs
[NILFS_BMAP_LARGE_LOW
+ 1];
174 if (bmap
->b_ops
->bop_check_delete
!= NULL
) {
175 ret
= bmap
->b_ops
->bop_check_delete(bmap
, key
);
177 n
= bmap
->b_ops
->bop_gather_data(
178 bmap
, keys
, ptrs
, NILFS_BMAP_LARGE_LOW
+ 1);
181 ret
= nilfs_direct_delete_and_convert(
182 bmap
, key
, keys
, ptrs
, n
);
184 bmap
->b_u
.u_flags
&= ~NILFS_BMAP_LARGE
;
191 return bmap
->b_ops
->bop_delete(bmap
, key
);
194 int nilfs_bmap_last_key(struct nilfs_bmap
*bmap
, unsigned long *key
)
199 down_read(&bmap
->b_sem
);
200 ret
= bmap
->b_ops
->bop_last_key(bmap
, &lastkey
);
201 up_read(&bmap
->b_sem
);
204 ret
= nilfs_bmap_convert_error(bmap
, __func__
, ret
);
211 * nilfs_bmap_delete - delete a key-record pair from a bmap
215 * Description: nilfs_bmap_delete() deletes the key-record pair specified by
218 * Return Value: On success, 0 is returned. On error, one of the following
219 * negative error codes is returned.
223 * %-ENOMEM - Insufficient amount of memory available.
225 * %-ENOENT - A record associated with @key does not exist.
227 int nilfs_bmap_delete(struct nilfs_bmap
*bmap
, unsigned long key
)
231 down_write(&bmap
->b_sem
);
232 ret
= nilfs_bmap_do_delete(bmap
, key
);
233 up_write(&bmap
->b_sem
);
235 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
238 static int nilfs_bmap_do_truncate(struct nilfs_bmap
*bmap
, unsigned long key
)
243 ret
= bmap
->b_ops
->bop_last_key(bmap
, &lastkey
);
250 while (key
<= lastkey
) {
251 ret
= nilfs_bmap_do_delete(bmap
, lastkey
);
254 ret
= bmap
->b_ops
->bop_last_key(bmap
, &lastkey
);
265 * nilfs_bmap_truncate - truncate a bmap to a specified key
269 * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
270 * greater than or equal to @key from @bmap.
272 * Return Value: On success, 0 is returned. On error, one of the following
273 * negative error codes is returned.
277 * %-ENOMEM - Insufficient amount of memory available.
279 int nilfs_bmap_truncate(struct nilfs_bmap
*bmap
, unsigned long key
)
283 down_write(&bmap
->b_sem
);
284 ret
= nilfs_bmap_do_truncate(bmap
, key
);
285 up_write(&bmap
->b_sem
);
287 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
291 * nilfs_bmap_clear - free resources a bmap holds
294 * Description: nilfs_bmap_clear() frees resources associated with @bmap.
296 void nilfs_bmap_clear(struct nilfs_bmap
*bmap
)
298 down_write(&bmap
->b_sem
);
299 if (bmap
->b_ops
->bop_clear
!= NULL
)
300 bmap
->b_ops
->bop_clear(bmap
);
301 up_write(&bmap
->b_sem
);
305 * nilfs_bmap_propagate - propagate dirty state
309 * Description: nilfs_bmap_propagate() marks the buffers that directly or
310 * indirectly refer to the block specified by @bh dirty.
312 * Return Value: On success, 0 is returned. On error, one of the following
313 * negative error codes is returned.
317 * %-ENOMEM - Insufficient amount of memory available.
319 int nilfs_bmap_propagate(struct nilfs_bmap
*bmap
, struct buffer_head
*bh
)
323 down_write(&bmap
->b_sem
);
324 ret
= bmap
->b_ops
->bop_propagate(bmap
, bh
);
325 up_write(&bmap
->b_sem
);
327 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
331 * nilfs_bmap_lookup_dirty_buffers -
333 * @listp: pointer to buffer head list
335 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap
*bmap
,
336 struct list_head
*listp
)
338 if (bmap
->b_ops
->bop_lookup_dirty_buffers
!= NULL
)
339 bmap
->b_ops
->bop_lookup_dirty_buffers(bmap
, listp
);
343 * nilfs_bmap_assign - assign a new block number to a block
345 * @bhp: pointer to buffer head
346 * @blocknr: block number
347 * @binfo: block information
349 * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
350 * buffer specified by @bh.
352 * Return Value: On success, 0 is returned and the buffer head of a newly
353 * create buffer and the block information associated with the buffer are
354 * stored in the place pointed by @bh and @binfo, respectively. On error, one
355 * of the following negative error codes is returned.
359 * %-ENOMEM - Insufficient amount of memory available.
361 int nilfs_bmap_assign(struct nilfs_bmap
*bmap
,
362 struct buffer_head
**bh
,
363 unsigned long blocknr
,
364 union nilfs_binfo
*binfo
)
368 down_write(&bmap
->b_sem
);
369 ret
= bmap
->b_ops
->bop_assign(bmap
, bh
, blocknr
, binfo
);
370 up_write(&bmap
->b_sem
);
372 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
376 * nilfs_bmap_mark - mark block dirty
381 * Description: nilfs_bmap_mark() marks the block specified by @key and @level
384 * Return Value: On success, 0 is returned. On error, one of the following
385 * negative error codes is returned.
389 * %-ENOMEM - Insufficient amount of memory available.
391 int nilfs_bmap_mark(struct nilfs_bmap
*bmap
, __u64 key
, int level
)
395 if (bmap
->b_ops
->bop_mark
== NULL
)
398 down_write(&bmap
->b_sem
);
399 ret
= bmap
->b_ops
->bop_mark(bmap
, key
, level
);
400 up_write(&bmap
->b_sem
);
402 return nilfs_bmap_convert_error(bmap
, __func__
, ret
);
406 * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
409 * Description: nilfs_test_and_clear() is the atomic operation to test and
410 * clear the dirty state of @bmap.
412 * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
414 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap
*bmap
)
418 down_write(&bmap
->b_sem
);
419 ret
= nilfs_bmap_dirty(bmap
);
420 nilfs_bmap_clear_dirty(bmap
);
421 up_write(&bmap
->b_sem
);
429 __u64
nilfs_bmap_data_get_key(const struct nilfs_bmap
*bmap
,
430 const struct buffer_head
*bh
)
432 struct buffer_head
*pbh
;
435 key
= page_index(bh
->b_page
) << (PAGE_CACHE_SHIFT
-
436 bmap
->b_inode
->i_blkbits
);
437 for (pbh
= page_buffers(bh
->b_page
); pbh
!= bh
; pbh
= pbh
->b_this_page
)
443 __u64
nilfs_bmap_find_target_seq(const struct nilfs_bmap
*bmap
, __u64 key
)
447 diff
= key
- bmap
->b_last_allocated_key
;
448 if ((nilfs_bmap_keydiff_abs(diff
) < NILFS_INODE_BMAP_SIZE
) &&
449 (bmap
->b_last_allocated_ptr
!= NILFS_BMAP_INVALID_PTR
) &&
450 (bmap
->b_last_allocated_ptr
+ diff
> 0))
451 return bmap
->b_last_allocated_ptr
+ diff
;
453 return NILFS_BMAP_INVALID_PTR
;
456 #define NILFS_BMAP_GROUP_DIV 8
457 __u64
nilfs_bmap_find_target_in_group(const struct nilfs_bmap
*bmap
)
459 struct inode
*dat
= nilfs_bmap_get_dat(bmap
);
460 unsigned long entries_per_group
= nilfs_palloc_entries_per_group(dat
);
461 unsigned long group
= bmap
->b_inode
->i_ino
/ entries_per_group
;
463 return group
* entries_per_group
+
464 (bmap
->b_inode
->i_ino
% NILFS_BMAP_GROUP_DIV
) *
465 (entries_per_group
/ NILFS_BMAP_GROUP_DIV
);
468 static struct lock_class_key nilfs_bmap_dat_lock_key
;
469 static struct lock_class_key nilfs_bmap_mdt_lock_key
;
472 * nilfs_bmap_read - read a bmap from an inode
474 * @raw_inode: on-disk inode
476 * Description: nilfs_bmap_read() initializes the bmap @bmap.
478 * Return Value: On success, 0 is returned. On error, the following negative
479 * error code is returned.
481 * %-ENOMEM - Insufficient amount of memory available.
483 int nilfs_bmap_read(struct nilfs_bmap
*bmap
, struct nilfs_inode
*raw_inode
)
485 if (raw_inode
== NULL
)
486 memset(bmap
->b_u
.u_data
, 0, NILFS_BMAP_SIZE
);
488 memcpy(bmap
->b_u
.u_data
, raw_inode
->i_bmap
, NILFS_BMAP_SIZE
);
490 init_rwsem(&bmap
->b_sem
);
492 bmap
->b_inode
= &NILFS_BMAP_I(bmap
)->vfs_inode
;
493 switch (bmap
->b_inode
->i_ino
) {
495 bmap
->b_ptr_type
= NILFS_BMAP_PTR_P
;
496 bmap
->b_last_allocated_key
= 0;
497 bmap
->b_last_allocated_ptr
= NILFS_BMAP_NEW_PTR_INIT
;
498 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_dat_lock_key
);
500 case NILFS_CPFILE_INO
:
501 case NILFS_SUFILE_INO
:
502 bmap
->b_ptr_type
= NILFS_BMAP_PTR_VS
;
503 bmap
->b_last_allocated_key
= 0;
504 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
505 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_mdt_lock_key
);
507 case NILFS_IFILE_INO
:
508 lockdep_set_class(&bmap
->b_sem
, &nilfs_bmap_mdt_lock_key
);
511 bmap
->b_ptr_type
= NILFS_BMAP_PTR_VM
;
512 bmap
->b_last_allocated_key
= 0;
513 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
517 return (bmap
->b_u
.u_flags
& NILFS_BMAP_LARGE
) ?
518 nilfs_btree_init(bmap
) : nilfs_direct_init(bmap
);
522 * nilfs_bmap_write - write back a bmap to an inode
524 * @raw_inode: on-disk inode
526 * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
528 void nilfs_bmap_write(struct nilfs_bmap
*bmap
, struct nilfs_inode
*raw_inode
)
530 down_write(&bmap
->b_sem
);
531 memcpy(raw_inode
->i_bmap
, bmap
->b_u
.u_data
,
532 NILFS_INODE_BMAP_SIZE
* sizeof(__le64
));
533 if (bmap
->b_inode
->i_ino
== NILFS_DAT_INO
)
534 bmap
->b_last_allocated_ptr
= NILFS_BMAP_NEW_PTR_INIT
;
536 up_write(&bmap
->b_sem
);
539 void nilfs_bmap_init_gc(struct nilfs_bmap
*bmap
)
541 memset(&bmap
->b_u
, 0, NILFS_BMAP_SIZE
);
542 init_rwsem(&bmap
->b_sem
);
543 bmap
->b_inode
= &NILFS_BMAP_I(bmap
)->vfs_inode
;
544 bmap
->b_ptr_type
= NILFS_BMAP_PTR_U
;
545 bmap
->b_last_allocated_key
= 0;
546 bmap
->b_last_allocated_ptr
= NILFS_BMAP_INVALID_PTR
;
548 nilfs_btree_init_gc(bmap
);
551 void nilfs_bmap_save(const struct nilfs_bmap
*bmap
,
552 struct nilfs_bmap_store
*store
)
554 memcpy(store
->data
, bmap
->b_u
.u_data
, sizeof(store
->data
));
555 store
->last_allocated_key
= bmap
->b_last_allocated_key
;
556 store
->last_allocated_ptr
= bmap
->b_last_allocated_ptr
;
557 store
->state
= bmap
->b_state
;
560 void nilfs_bmap_restore(struct nilfs_bmap
*bmap
,
561 const struct nilfs_bmap_store
*store
)
563 memcpy(bmap
->b_u
.u_data
, store
->data
, sizeof(store
->data
));
564 bmap
->b_last_allocated_key
= store
->last_allocated_key
;
565 bmap
->b_last_allocated_ptr
= store
->last_allocated_ptr
;
566 bmap
->b_state
= store
->state
;