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>
34 struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
36 return nilfs_dat_inode(NILFS_I_NILFS(bmap->b_inode));
39 int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level,
45 down_read(&bmap->b_sem);
46 ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
49 if (NILFS_BMAP_USE_VBN(bmap)) {
50 ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp,
57 up_read(&bmap->b_sem);
61 int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp,
66 down_read(&bmap->b_sem);
67 ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks);
68 up_read(&bmap->b_sem);
73 * nilfs_bmap_lookup - find a record
76 * @recp: pointer to record
78 * Description: nilfs_bmap_lookup() finds a record whose key matches @key in
81 * Return Value: On success, 0 is returned and the record associated with @key
82 * is stored in the place pointed by @recp. On error, one of the following
83 * negative error codes is returned.
87 * %-ENOMEM - Insufficient amount of memory available.
89 * %-ENOENT - A record associated with @key does not exist.
91 int nilfs_bmap_lookup(struct nilfs_bmap *bmap,
98 /* XXX: use macro for level 1 */
99 ret = nilfs_bmap_lookup_at_level(bmap, key, 1, &ptr);
105 static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
107 __u64 keys[NILFS_BMAP_SMALL_HIGH + 1];
108 __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1];
111 if (bmap->b_ops->bop_check_insert != NULL) {
112 ret = bmap->b_ops->bop_check_insert(bmap, key);
114 n = bmap->b_ops->bop_gather_data(
115 bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
118 ret = nilfs_btree_convert_and_insert(
119 bmap, key, ptr, keys, ptrs, n);
121 bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
128 return bmap->b_ops->bop_insert(bmap, key, ptr);
132 * nilfs_bmap_insert - insert a new key-record pair into a bmap
137 * Description: nilfs_bmap_insert() inserts the new key-record pair specified
138 * by @key and @rec into @bmap.
140 * Return Value: On success, 0 is returned. On error, one of the following
141 * negative error codes is returned.
145 * %-ENOMEM - Insufficient amount of memory available.
147 * %-EEXIST - A record associated with @key already exist.
149 int nilfs_bmap_insert(struct nilfs_bmap *bmap,
155 down_write(&bmap->b_sem);
156 ret = nilfs_bmap_do_insert(bmap, key, rec);
157 up_write(&bmap->b_sem);
161 static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
163 __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
164 __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
167 if (bmap->b_ops->bop_check_delete != NULL) {
168 ret = bmap->b_ops->bop_check_delete(bmap, key);
170 n = bmap->b_ops->bop_gather_data(
171 bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
174 ret = nilfs_direct_delete_and_convert(
175 bmap, key, keys, ptrs, n);
177 bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
184 return bmap->b_ops->bop_delete(bmap, key);
187 int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key)
192 down_read(&bmap->b_sem);
193 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
196 up_read(&bmap->b_sem);
201 * nilfs_bmap_delete - delete a key-record pair from a bmap
205 * Description: nilfs_bmap_delete() deletes the key-record pair specified by
208 * Return Value: On success, 0 is returned. On error, one of the following
209 * negative error codes is returned.
213 * %-ENOMEM - Insufficient amount of memory available.
215 * %-ENOENT - A record associated with @key does not exist.
217 int nilfs_bmap_delete(struct nilfs_bmap *bmap, unsigned long key)
221 down_write(&bmap->b_sem);
222 ret = nilfs_bmap_do_delete(bmap, key);
223 up_write(&bmap->b_sem);
227 static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key)
232 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
239 while (key <= lastkey) {
240 ret = nilfs_bmap_do_delete(bmap, lastkey);
243 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
254 * nilfs_bmap_truncate - truncate a bmap to a specified key
258 * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
259 * greater than or equal to @key from @bmap.
261 * Return Value: On success, 0 is returned. On error, one of the following
262 * negative error codes is returned.
266 * %-ENOMEM - Insufficient amount of memory available.
268 int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key)
272 down_write(&bmap->b_sem);
273 ret = nilfs_bmap_do_truncate(bmap, key);
274 up_write(&bmap->b_sem);
279 * nilfs_bmap_clear - free resources a bmap holds
282 * Description: nilfs_bmap_clear() frees resources associated with @bmap.
284 void nilfs_bmap_clear(struct nilfs_bmap *bmap)
286 down_write(&bmap->b_sem);
287 if (bmap->b_ops->bop_clear != NULL)
288 bmap->b_ops->bop_clear(bmap);
289 up_write(&bmap->b_sem);
293 * nilfs_bmap_propagate - propagate dirty state
297 * Description: nilfs_bmap_propagate() marks the buffers that directly or
298 * indirectly refer to the block specified by @bh dirty.
300 * Return Value: On success, 0 is returned. On error, one of the following
301 * negative error codes is returned.
305 * %-ENOMEM - Insufficient amount of memory available.
307 int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
311 down_write(&bmap->b_sem);
312 ret = bmap->b_ops->bop_propagate(bmap, bh);
313 up_write(&bmap->b_sem);
318 * nilfs_bmap_lookup_dirty_buffers -
320 * @listp: pointer to buffer head list
322 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
323 struct list_head *listp)
325 if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
326 bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
330 * nilfs_bmap_assign - assign a new block number to a block
332 * @bhp: pointer to buffer head
333 * @blocknr: block number
334 * @binfo: block information
336 * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
337 * buffer specified by @bh.
339 * Return Value: On success, 0 is returned and the buffer head of a newly
340 * create buffer and the block information associated with the buffer are
341 * stored in the place pointed by @bh and @binfo, respectively. On error, one
342 * of the following negative error codes is returned.
346 * %-ENOMEM - Insufficient amount of memory available.
348 int nilfs_bmap_assign(struct nilfs_bmap *bmap,
349 struct buffer_head **bh,
350 unsigned long blocknr,
351 union nilfs_binfo *binfo)
355 down_write(&bmap->b_sem);
356 ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
357 up_write(&bmap->b_sem);
362 * nilfs_bmap_mark - mark block dirty
367 * Description: nilfs_bmap_mark() marks the block specified by @key and @level
370 * Return Value: On success, 0 is returned. On error, one of the following
371 * negative error codes is returned.
375 * %-ENOMEM - Insufficient amount of memory available.
377 int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
381 if (bmap->b_ops->bop_mark == NULL)
384 down_write(&bmap->b_sem);
385 ret = bmap->b_ops->bop_mark(bmap, key, level);
386 up_write(&bmap->b_sem);
391 * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
394 * Description: nilfs_test_and_clear() is the atomic operation to test and
395 * clear the dirty state of @bmap.
397 * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
399 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
403 down_write(&bmap->b_sem);
404 ret = nilfs_bmap_dirty(bmap);
405 nilfs_bmap_clear_dirty(bmap);
406 up_write(&bmap->b_sem);
415 void nilfs_bmap_add_blocks(const struct nilfs_bmap *bmap, int n)
417 inode_add_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
418 if (NILFS_MDT(bmap->b_inode))
419 nilfs_mdt_mark_dirty(bmap->b_inode);
421 mark_inode_dirty(bmap->b_inode);
424 void nilfs_bmap_sub_blocks(const struct nilfs_bmap *bmap, int n)
426 inode_sub_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
427 if (NILFS_MDT(bmap->b_inode))
428 nilfs_mdt_mark_dirty(bmap->b_inode);
430 mark_inode_dirty(bmap->b_inode);
433 __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
434 const struct buffer_head *bh)
436 struct buffer_head *pbh;
439 key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT -
440 bmap->b_inode->i_blkbits);
441 for (pbh = page_buffers(bh->b_page); pbh != bh;
442 pbh = pbh->b_this_page, key++);
447 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
451 diff = key - bmap->b_last_allocated_key;
452 if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
453 (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
454 (bmap->b_last_allocated_ptr + diff > 0))
455 return bmap->b_last_allocated_ptr + diff;
457 return NILFS_BMAP_INVALID_PTR;
460 #define NILFS_BMAP_GROUP_DIV 8
461 __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
463 struct inode *dat = nilfs_bmap_get_dat(bmap);
464 unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
465 unsigned long group = bmap->b_inode->i_ino / entries_per_group;
467 return group * entries_per_group +
468 (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
469 (entries_per_group / NILFS_BMAP_GROUP_DIV);
472 int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *bmap,
473 union nilfs_bmap_ptr_req *req)
475 return nilfs_dat_prepare_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
478 void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *bmap,
479 union nilfs_bmap_ptr_req *req)
481 nilfs_dat_commit_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
484 void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *bmap,
485 union nilfs_bmap_ptr_req *req)
487 nilfs_dat_abort_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
490 int nilfs_bmap_start_v(struct nilfs_bmap *bmap, union nilfs_bmap_ptr_req *req,
493 struct inode *dat = nilfs_bmap_get_dat(bmap);
496 ret = nilfs_dat_prepare_start(dat, &req->bpr_req);
498 nilfs_dat_commit_start(dat, &req->bpr_req, blocknr);
502 int nilfs_bmap_prepare_end_v(struct nilfs_bmap *bmap,
503 union nilfs_bmap_ptr_req *req)
505 return nilfs_dat_prepare_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
508 void nilfs_bmap_commit_end_v(struct nilfs_bmap *bmap,
509 union nilfs_bmap_ptr_req *req)
511 nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req,
512 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
515 void nilfs_bmap_abort_end_v(struct nilfs_bmap *bmap,
516 union nilfs_bmap_ptr_req *req)
518 nilfs_dat_abort_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
521 int nilfs_bmap_move_v(const struct nilfs_bmap *bmap, __u64 vblocknr,
524 return nilfs_dat_move(nilfs_bmap_get_dat(bmap), vblocknr, blocknr);
527 int nilfs_bmap_mark_dirty(const struct nilfs_bmap *bmap, __u64 vblocknr)
529 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), vblocknr);
532 int nilfs_bmap_prepare_update_v(struct nilfs_bmap *bmap,
533 union nilfs_bmap_ptr_req *oldreq,
534 union nilfs_bmap_ptr_req *newreq)
536 struct inode *dat = nilfs_bmap_get_dat(bmap);
539 ret = nilfs_dat_prepare_end(dat, &oldreq->bpr_req);
542 ret = nilfs_dat_prepare_alloc(dat, &newreq->bpr_req);
544 nilfs_dat_abort_end(dat, &oldreq->bpr_req);
549 void nilfs_bmap_commit_update_v(struct nilfs_bmap *bmap,
550 union nilfs_bmap_ptr_req *oldreq,
551 union nilfs_bmap_ptr_req *newreq)
553 struct inode *dat = nilfs_bmap_get_dat(bmap);
555 nilfs_dat_commit_end(dat, &oldreq->bpr_req,
556 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
557 nilfs_dat_commit_alloc(dat, &newreq->bpr_req);
560 void nilfs_bmap_abort_update_v(struct nilfs_bmap *bmap,
561 union nilfs_bmap_ptr_req *oldreq,
562 union nilfs_bmap_ptr_req *newreq)
564 struct inode *dat = nilfs_bmap_get_dat(bmap);
566 nilfs_dat_abort_end(dat, &oldreq->bpr_req);
567 nilfs_dat_abort_alloc(dat, &newreq->bpr_req);
570 static struct lock_class_key nilfs_bmap_dat_lock_key;
573 * nilfs_bmap_read - read a bmap from an inode
575 * @raw_inode: on-disk inode
577 * Description: nilfs_bmap_read() initializes the bmap @bmap.
579 * Return Value: On success, 0 is returned. On error, the following negative
580 * error code is returned.
582 * %-ENOMEM - Insufficient amount of memory available.
584 int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
586 if (raw_inode == NULL)
587 memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
589 memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
591 init_rwsem(&bmap->b_sem);
593 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
594 switch (bmap->b_inode->i_ino) {
596 bmap->b_ptr_type = NILFS_BMAP_PTR_P;
597 bmap->b_last_allocated_key = 0;
598 bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
599 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
601 case NILFS_CPFILE_INO:
602 case NILFS_SUFILE_INO:
603 bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
604 bmap->b_last_allocated_key = 0;
605 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
608 bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
609 bmap->b_last_allocated_key = 0;
610 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
614 return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
615 nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
619 * nilfs_bmap_write - write back a bmap to an inode
621 * @raw_inode: on-disk inode
623 * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
625 void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
627 down_write(&bmap->b_sem);
628 memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
629 NILFS_INODE_BMAP_SIZE * sizeof(__le64));
630 if (bmap->b_inode->i_ino == NILFS_DAT_INO)
631 bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
633 up_write(&bmap->b_sem);
636 void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
638 memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
639 init_rwsem(&bmap->b_sem);
640 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
641 bmap->b_ptr_type = NILFS_BMAP_PTR_U;
642 bmap->b_last_allocated_key = 0;
643 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
645 nilfs_btree_init_gc(bmap);
648 void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
650 memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
651 init_rwsem(&gcbmap->b_sem);
652 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
653 gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
656 void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
658 memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
659 init_rwsem(&bmap->b_sem);
660 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
661 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;