2 * bmap.h - 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>.
26 #include <linux/types.h>
28 #include <linux/buffer_head.h>
29 #include <linux/nilfs2_fs.h>
32 #define NILFS_BMAP_INVALID_PTR 0
34 #define nilfs_bmap_dkey_to_key(dkey) le64_to_cpu(dkey)
35 #define nilfs_bmap_key_to_dkey(key) cpu_to_le64(key)
36 #define nilfs_bmap_dptr_to_ptr(dptr) le64_to_cpu(dptr)
37 #define nilfs_bmap_ptr_to_dptr(ptr) cpu_to_le64(ptr)
39 #define nilfs_bmap_keydiff_abs(diff) ((diff) < 0 ? -(diff) : (diff))
45 * union nilfs_bmap_ptr_req - request for bmap ptr
46 * @bpr_ptr: bmap pointer
47 * @bpr_req: request for persistent allocator
49 union nilfs_bmap_ptr_req {
51 struct nilfs_palloc_req bpr_req;
55 * struct nilfs_bmap_stats - bmap statistics
56 * @bs_nblocks: number of blocks created or deleted
58 struct nilfs_bmap_stats {
59 unsigned int bs_nblocks;
63 * struct nilfs_bmap_operations - bmap operation table
65 struct nilfs_bmap_operations {
66 int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
67 int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
68 int (*bop_delete)(struct nilfs_bmap *, __u64);
69 void (*bop_clear)(struct nilfs_bmap *);
71 int (*bop_propagate)(const struct nilfs_bmap *, struct buffer_head *);
72 void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
75 int (*bop_assign)(struct nilfs_bmap *,
76 struct buffer_head **,
79 int (*bop_mark)(struct nilfs_bmap *, __u64, int);
81 /* The following functions are internal use only. */
82 int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
83 int (*bop_check_insert)(const struct nilfs_bmap *, __u64);
84 int (*bop_check_delete)(struct nilfs_bmap *, __u64);
85 int (*bop_gather_data)(struct nilfs_bmap *, __u64 *, __u64 *, int);
89 #define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(__le64))
90 #define NILFS_BMAP_KEY_BIT (sizeof(unsigned long) * 8 /* CHAR_BIT */)
91 #define NILFS_BMAP_NEW_PTR_INIT \
92 (1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
94 static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
96 return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
101 * struct nilfs_bmap - bmap structure
104 * @b_inode: owner of bmap
105 * @b_ops: bmap operation table
106 * @b_last_allocated_key: last allocated key for data block
107 * @b_last_allocated_ptr: last allocated ptr for data block
108 * @b_ptr_type: pointer type
114 __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
116 struct rw_semaphore b_sem;
117 struct inode *b_inode;
118 const struct nilfs_bmap_operations *b_ops;
119 __u64 b_last_allocated_key;
120 __u64 b_last_allocated_ptr;
126 #define NILFS_BMAP_PTR_P 0 /* physical block number (i.e. LBN) */
127 #define NILFS_BMAP_PTR_VS 1 /* virtual block number (single
129 #define NILFS_BMAP_PTR_VM 2 /* virtual block number (has multiple
131 #define NILFS_BMAP_PTR_U (-1) /* never perform pointer operations */
133 #define NILFS_BMAP_USE_VBN(bmap) ((bmap)->b_ptr_type > 0)
136 #define NILFS_BMAP_DIRTY 0x00000001
139 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
140 int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
141 void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
142 int nilfs_bmap_lookup(struct nilfs_bmap *, unsigned long, unsigned long *);
143 int nilfs_bmap_insert(struct nilfs_bmap *, unsigned long, unsigned long);
144 int nilfs_bmap_delete(struct nilfs_bmap *, unsigned long);
145 int nilfs_bmap_last_key(struct nilfs_bmap *, unsigned long *);
146 int nilfs_bmap_truncate(struct nilfs_bmap *, unsigned long);
147 void nilfs_bmap_clear(struct nilfs_bmap *);
148 int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
149 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
150 int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
151 unsigned long, union nilfs_binfo *);
152 int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
153 int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
155 void nilfs_bmap_init_gc(struct nilfs_bmap *);
156 void nilfs_bmap_init_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
157 void nilfs_bmap_commit_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
163 int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *,
164 union nilfs_bmap_ptr_req *);
165 void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *,
166 union nilfs_bmap_ptr_req *);
167 void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *,
168 union nilfs_bmap_ptr_req *);
170 static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
171 union nilfs_bmap_ptr_req *req)
173 if (NILFS_BMAP_USE_VBN(bmap))
174 return nilfs_bmap_prepare_alloc_v(bmap, req);
175 /* ignore target ptr */
176 req->bpr_ptr = bmap->b_last_allocated_ptr++;
180 static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
181 union nilfs_bmap_ptr_req *req)
183 if (NILFS_BMAP_USE_VBN(bmap))
184 nilfs_bmap_commit_alloc_v(bmap, req);
187 static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
188 union nilfs_bmap_ptr_req *req)
190 if (NILFS_BMAP_USE_VBN(bmap))
191 nilfs_bmap_abort_alloc_v(bmap, req);
193 bmap->b_last_allocated_ptr--;
196 int nilfs_bmap_prepare_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
197 void nilfs_bmap_commit_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
198 void nilfs_bmap_abort_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
200 static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
201 union nilfs_bmap_ptr_req *req)
203 return NILFS_BMAP_USE_VBN(bmap) ?
204 nilfs_bmap_prepare_end_v(bmap, req) : 0;
207 static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
208 union nilfs_bmap_ptr_req *req)
210 if (NILFS_BMAP_USE_VBN(bmap))
211 nilfs_bmap_commit_end_v(bmap, req);
214 static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
215 union nilfs_bmap_ptr_req *req)
217 if (NILFS_BMAP_USE_VBN(bmap))
218 nilfs_bmap_abort_end_v(bmap, req);
221 int nilfs_bmap_start_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *,
223 int nilfs_bmap_move_v(const struct nilfs_bmap *, __u64, sector_t);
224 int nilfs_bmap_mark_dirty(const struct nilfs_bmap *, __u64);
227 __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
228 const struct buffer_head *);
230 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
231 __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
233 int nilfs_bmap_prepare_update_v(struct nilfs_bmap *,
234 union nilfs_bmap_ptr_req *,
235 union nilfs_bmap_ptr_req *);
236 void nilfs_bmap_commit_update_v(struct nilfs_bmap *,
237 union nilfs_bmap_ptr_req *,
238 union nilfs_bmap_ptr_req *);
239 void nilfs_bmap_abort_update_v(struct nilfs_bmap *,
240 union nilfs_bmap_ptr_req *,
241 union nilfs_bmap_ptr_req *);
243 void nilfs_bmap_add_blocks(const struct nilfs_bmap *, int);
244 void nilfs_bmap_sub_blocks(const struct nilfs_bmap *, int);
247 /* Assume that bmap semaphore is locked. */
248 static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
250 return !!(bmap->b_state & NILFS_BMAP_DIRTY);
253 /* Assume that bmap semaphore is locked. */
254 static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
256 bmap->b_state |= NILFS_BMAP_DIRTY;
259 /* Assume that bmap semaphore is locked. */
260 static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
262 bmap->b_state &= ~NILFS_BMAP_DIRTY;
266 #define NILFS_BMAP_LARGE 0x1
268 #define NILFS_BMAP_SMALL_LOW NILFS_DIRECT_KEY_MIN
269 #define NILFS_BMAP_SMALL_HIGH NILFS_DIRECT_KEY_MAX
270 #define NILFS_BMAP_LARGE_LOW NILFS_BTREE_ROOT_NCHILDREN_MAX
271 #define NILFS_BMAP_LARGE_HIGH NILFS_BTREE_KEY_MAX
273 #endif /* _NILFS_BMAP_H */