mtd: nand_base: use __func__ instead of typing names
[linux/fpc-iii.git] / fs / nilfs2 / bmap.c
blob99d58a028b94c3c9698661bac527e14ed2812a72
1 /*
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>.
23 #include <linux/fs.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include "nilfs.h"
27 #include "bmap.h"
28 #include "sb.h"
29 #include "btnode.h"
30 #include "mdt.h"
31 #include "dat.h"
32 #include "alloc.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,
40 __u64 *ptrp)
42 sector_t blocknr;
43 int ret;
45 down_read(&bmap->b_sem);
46 ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
47 if (ret < 0)
48 goto out;
49 if (NILFS_BMAP_USE_VBN(bmap)) {
50 ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp,
51 &blocknr);
52 if (!ret)
53 *ptrp = blocknr;
56 out:
57 up_read(&bmap->b_sem);
58 return ret;
61 int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp,
62 unsigned maxblocks)
64 int ret;
66 down_read(&bmap->b_sem);
67 ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks);
68 up_read(&bmap->b_sem);
69 return ret;
72 /**
73 * nilfs_bmap_lookup - find a record
74 * @bmap: bmap
75 * @key: key
76 * @recp: pointer to record
78 * Description: nilfs_bmap_lookup() finds a record whose key matches @key in
79 * @bmap.
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.
85 * %-EIO - I/O error.
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,
92 unsigned long key,
93 unsigned long *recp)
95 __u64 ptr;
96 int ret;
98 /* XXX: use macro for level 1 */
99 ret = nilfs_bmap_lookup_at_level(bmap, key, 1, &ptr);
100 if (recp != NULL)
101 *recp = ptr;
102 return ret;
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];
109 int ret, n;
111 if (bmap->b_ops->bop_check_insert != NULL) {
112 ret = bmap->b_ops->bop_check_insert(bmap, key);
113 if (ret > 0) {
114 n = bmap->b_ops->bop_gather_data(
115 bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
116 if (n < 0)
117 return n;
118 ret = nilfs_btree_convert_and_insert(
119 bmap, key, ptr, keys, ptrs, n);
120 if (ret == 0)
121 bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
123 return ret;
124 } else if (ret < 0)
125 return ret;
128 return bmap->b_ops->bop_insert(bmap, key, ptr);
132 * nilfs_bmap_insert - insert a new key-record pair into a bmap
133 * @bmap: bmap
134 * @key: key
135 * @rec: record
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.
143 * %-EIO - I/O error.
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,
150 unsigned long key,
151 unsigned long rec)
153 int ret;
155 down_write(&bmap->b_sem);
156 ret = nilfs_bmap_do_insert(bmap, key, rec);
157 up_write(&bmap->b_sem);
158 return ret;
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];
165 int ret, n;
167 if (bmap->b_ops->bop_check_delete != NULL) {
168 ret = bmap->b_ops->bop_check_delete(bmap, key);
169 if (ret > 0) {
170 n = bmap->b_ops->bop_gather_data(
171 bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
172 if (n < 0)
173 return n;
174 ret = nilfs_direct_delete_and_convert(
175 bmap, key, keys, ptrs, n);
176 if (ret == 0)
177 bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
179 return ret;
180 } else if (ret < 0)
181 return ret;
184 return bmap->b_ops->bop_delete(bmap, key);
187 int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key)
189 __u64 lastkey;
190 int ret;
192 down_read(&bmap->b_sem);
193 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
194 if (!ret)
195 *key = lastkey;
196 up_read(&bmap->b_sem);
197 return ret;
201 * nilfs_bmap_delete - delete a key-record pair from a bmap
202 * @bmap: bmap
203 * @key: key
205 * Description: nilfs_bmap_delete() deletes the key-record pair specified by
206 * @key from @bmap.
208 * Return Value: On success, 0 is returned. On error, one of the following
209 * negative error codes is returned.
211 * %-EIO - I/O error.
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)
219 int ret;
221 down_write(&bmap->b_sem);
222 ret = nilfs_bmap_do_delete(bmap, key);
223 up_write(&bmap->b_sem);
224 return ret;
227 static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key)
229 __u64 lastkey;
230 int ret;
232 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
233 if (ret < 0) {
234 if (ret == -ENOENT)
235 ret = 0;
236 return ret;
239 while (key <= lastkey) {
240 ret = nilfs_bmap_do_delete(bmap, lastkey);
241 if (ret < 0)
242 return ret;
243 ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
244 if (ret < 0) {
245 if (ret == -ENOENT)
246 ret = 0;
247 return ret;
250 return 0;
254 * nilfs_bmap_truncate - truncate a bmap to a specified key
255 * @bmap: bmap
256 * @key: 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.
264 * %-EIO - I/O error.
266 * %-ENOMEM - Insufficient amount of memory available.
268 int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key)
270 int ret;
272 down_write(&bmap->b_sem);
273 ret = nilfs_bmap_do_truncate(bmap, key);
274 up_write(&bmap->b_sem);
275 return ret;
279 * nilfs_bmap_clear - free resources a bmap holds
280 * @bmap: bmap
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
294 * @bmap: bmap
295 * @bh: buffer head
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.
303 * %-EIO - I/O error.
305 * %-ENOMEM - Insufficient amount of memory available.
307 int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
309 int ret;
311 down_write(&bmap->b_sem);
312 ret = bmap->b_ops->bop_propagate(bmap, bh);
313 up_write(&bmap->b_sem);
314 return ret;
318 * nilfs_bmap_lookup_dirty_buffers -
319 * @bmap: bmap
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
331 * @bmap: bmap
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.
344 * %-EIO - I/O error.
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)
353 int ret;
355 down_write(&bmap->b_sem);
356 ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
357 up_write(&bmap->b_sem);
358 return ret;
362 * nilfs_bmap_mark - mark block dirty
363 * @bmap: bmap
364 * @key: key
365 * @level: level
367 * Description: nilfs_bmap_mark() marks the block specified by @key and @level
368 * as dirty.
370 * Return Value: On success, 0 is returned. On error, one of the following
371 * negative error codes is returned.
373 * %-EIO - I/O error.
375 * %-ENOMEM - Insufficient amount of memory available.
377 int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
379 int ret;
381 if (bmap->b_ops->bop_mark == NULL)
382 return 0;
384 down_write(&bmap->b_sem);
385 ret = bmap->b_ops->bop_mark(bmap, key, level);
386 up_write(&bmap->b_sem);
387 return ret;
391 * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
392 * @bmap: bmap
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)
401 int ret;
403 down_write(&bmap->b_sem);
404 ret = nilfs_bmap_dirty(bmap);
405 nilfs_bmap_clear_dirty(bmap);
406 up_write(&bmap->b_sem);
407 return ret;
412 * Internal use only
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);
420 else
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);
429 else
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;
437 __u64 key;
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++);
444 return key;
447 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
449 __s64 diff;
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;
456 else
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,
491 sector_t blocknr)
493 struct inode *dat = nilfs_bmap_get_dat(bmap);
494 int ret;
496 ret = nilfs_dat_prepare_start(dat, &req->bpr_req);
497 if (likely(!ret))
498 nilfs_dat_commit_start(dat, &req->bpr_req, blocknr);
499 return ret;
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,
522 sector_t blocknr)
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);
537 int ret;
539 ret = nilfs_dat_prepare_end(dat, &oldreq->bpr_req);
540 if (ret < 0)
541 return ret;
542 ret = nilfs_dat_prepare_alloc(dat, &newreq->bpr_req);
543 if (ret < 0)
544 nilfs_dat_abort_end(dat, &oldreq->bpr_req);
546 return ret;
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;
571 static struct lock_class_key nilfs_bmap_mdt_lock_key;
574 * nilfs_bmap_read - read a bmap from an inode
575 * @bmap: bmap
576 * @raw_inode: on-disk inode
578 * Description: nilfs_bmap_read() initializes the bmap @bmap.
580 * Return Value: On success, 0 is returned. On error, the following negative
581 * error code is returned.
583 * %-ENOMEM - Insufficient amount of memory available.
585 int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
587 if (raw_inode == NULL)
588 memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
589 else
590 memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
592 init_rwsem(&bmap->b_sem);
593 bmap->b_state = 0;
594 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
595 switch (bmap->b_inode->i_ino) {
596 case NILFS_DAT_INO:
597 bmap->b_ptr_type = NILFS_BMAP_PTR_P;
598 bmap->b_last_allocated_key = 0;
599 bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
600 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
601 break;
602 case NILFS_CPFILE_INO:
603 case NILFS_SUFILE_INO:
604 bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
605 bmap->b_last_allocated_key = 0;
606 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
607 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
608 break;
609 case NILFS_IFILE_INO:
610 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
611 /* Fall through */
612 default:
613 bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
614 bmap->b_last_allocated_key = 0;
615 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
616 break;
619 return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
620 nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
624 * nilfs_bmap_write - write back a bmap to an inode
625 * @bmap: bmap
626 * @raw_inode: on-disk inode
628 * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
630 void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
632 down_write(&bmap->b_sem);
633 memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
634 NILFS_INODE_BMAP_SIZE * sizeof(__le64));
635 if (bmap->b_inode->i_ino == NILFS_DAT_INO)
636 bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
638 up_write(&bmap->b_sem);
641 void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
643 memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
644 init_rwsem(&bmap->b_sem);
645 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
646 bmap->b_ptr_type = NILFS_BMAP_PTR_U;
647 bmap->b_last_allocated_key = 0;
648 bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
649 bmap->b_state = 0;
650 nilfs_btree_init_gc(bmap);
653 void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
655 memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
656 init_rwsem(&gcbmap->b_sem);
657 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
658 gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
661 void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
663 memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
664 init_rwsem(&bmap->b_sem);
665 lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
666 bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;