xref: /linux/fs/nilfs2/bmap.h (revision 5bdef865eb358b6f3760e25e591ae115e9eeddef)
1 /*
2  * bmap.h - NILFS block mapping.
3  *
4  * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Written by Koji Sato <koji@osrg.net>.
21  */
22 
23 #ifndef _NILFS_BMAP_H
24 #define _NILFS_BMAP_H
25 
26 #include <linux/types.h>
27 #include <linux/fs.h>
28 #include <linux/buffer_head.h>
29 #include <linux/nilfs2_fs.h>
30 #include "alloc.h"
31 
32 #define NILFS_BMAP_INVALID_PTR	0
33 
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)
38 
39 #define nilfs_bmap_keydiff_abs(diff)	((diff) < 0 ? -(diff) : (diff))
40 
41 
42 struct nilfs_bmap;
43 
44 /**
45  * union nilfs_bmap_ptr_req - request for bmap ptr
46  * @bpr_ptr: bmap pointer
47  * @bpr_req: request for persistent allocator
48  */
49 union nilfs_bmap_ptr_req {
50 	__u64 bpr_ptr;
51 	struct nilfs_palloc_req bpr_req;
52 };
53 
54 /**
55  * struct nilfs_bmap_stats - bmap statistics
56  * @bs_nblocks: number of blocks created or deleted
57  */
58 struct nilfs_bmap_stats {
59 	unsigned int bs_nblocks;
60 };
61 
62 /**
63  * struct nilfs_bmap_operations - bmap operation table
64  */
65 struct nilfs_bmap_operations {
66 	int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
67 	int (*bop_lookup_contig)(const struct nilfs_bmap *, __u64, __u64 *,
68 				 unsigned);
69 	int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
70 	int (*bop_delete)(struct nilfs_bmap *, __u64);
71 	void (*bop_clear)(struct nilfs_bmap *);
72 
73 	int (*bop_propagate)(const struct nilfs_bmap *, struct buffer_head *);
74 	void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
75 					 struct list_head *);
76 
77 	int (*bop_assign)(struct nilfs_bmap *,
78 			  struct buffer_head **,
79 			  sector_t,
80 			  union nilfs_binfo *);
81 	int (*bop_mark)(struct nilfs_bmap *, __u64, int);
82 
83 	/* The following functions are internal use only. */
84 	int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
85 	int (*bop_check_insert)(const struct nilfs_bmap *, __u64);
86 	int (*bop_check_delete)(struct nilfs_bmap *, __u64);
87 	int (*bop_gather_data)(struct nilfs_bmap *, __u64 *, __u64 *, int);
88 };
89 
90 
91 #define NILFS_BMAP_SIZE		(NILFS_INODE_BMAP_SIZE * sizeof(__le64))
92 #define NILFS_BMAP_KEY_BIT	(sizeof(unsigned long) * 8 /* CHAR_BIT */)
93 #define NILFS_BMAP_NEW_PTR_INIT	\
94 	(1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
95 
96 static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
97 {
98 	return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
99 }
100 
101 
102 /**
103  * struct nilfs_bmap - bmap structure
104  * @b_u: raw data
105  * @b_sem: semaphore
106  * @b_inode: owner of bmap
107  * @b_ops: bmap operation table
108  * @b_last_allocated_key: last allocated key for data block
109  * @b_last_allocated_ptr: last allocated ptr for data block
110  * @b_ptr_type: pointer type
111  * @b_state: state
112  */
113 struct nilfs_bmap {
114 	union {
115 		__u8 u_flags;
116 		__le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
117 	} b_u;
118 	struct rw_semaphore b_sem;
119 	struct inode *b_inode;
120 	const struct nilfs_bmap_operations *b_ops;
121 	__u64 b_last_allocated_key;
122 	__u64 b_last_allocated_ptr;
123 	int b_ptr_type;
124 	int b_state;
125 };
126 
127 /* pointer type */
128 #define NILFS_BMAP_PTR_P	0	/* physical block number (i.e. LBN) */
129 #define NILFS_BMAP_PTR_VS	1	/* virtual block number (single
130 					   version) */
131 #define NILFS_BMAP_PTR_VM	2	/* virtual block number (has multiple
132 					   versions) */
133 #define NILFS_BMAP_PTR_U	(-1)	/* never perform pointer operations */
134 
135 #define NILFS_BMAP_USE_VBN(bmap)	((bmap)->b_ptr_type > 0)
136 
137 /* state */
138 #define NILFS_BMAP_DIRTY	0x00000001
139 
140 
141 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
142 int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
143 void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
144 int nilfs_bmap_lookup(struct nilfs_bmap *, unsigned long, unsigned long *);
145 int nilfs_bmap_lookup_contig(struct nilfs_bmap *, __u64, __u64 *, unsigned);
146 int nilfs_bmap_insert(struct nilfs_bmap *, unsigned long, unsigned long);
147 int nilfs_bmap_delete(struct nilfs_bmap *, unsigned long);
148 int nilfs_bmap_last_key(struct nilfs_bmap *, unsigned long *);
149 int nilfs_bmap_truncate(struct nilfs_bmap *, unsigned long);
150 void nilfs_bmap_clear(struct nilfs_bmap *);
151 int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
152 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
153 int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
154 		      unsigned long, union nilfs_binfo *);
155 int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
156 int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
157 
158 void nilfs_bmap_init_gc(struct nilfs_bmap *);
159 void nilfs_bmap_init_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
160 void nilfs_bmap_commit_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
161 
162 
163 /*
164  * Internal use only
165  */
166 struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *);
167 int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *,
168 			       union nilfs_bmap_ptr_req *);
169 void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *,
170 			       union nilfs_bmap_ptr_req *);
171 void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *,
172 			      union nilfs_bmap_ptr_req *);
173 
174 static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
175 					       union nilfs_bmap_ptr_req *req)
176 {
177 	if (NILFS_BMAP_USE_VBN(bmap))
178 		return nilfs_bmap_prepare_alloc_v(bmap, req);
179 	/* ignore target ptr */
180 	req->bpr_ptr = bmap->b_last_allocated_ptr++;
181 	return 0;
182 }
183 
184 static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
185 					       union nilfs_bmap_ptr_req *req)
186 {
187 	if (NILFS_BMAP_USE_VBN(bmap))
188 		nilfs_bmap_commit_alloc_v(bmap, req);
189 }
190 
191 static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
192 					      union nilfs_bmap_ptr_req *req)
193 {
194 	if (NILFS_BMAP_USE_VBN(bmap))
195 		nilfs_bmap_abort_alloc_v(bmap, req);
196 	else
197 		bmap->b_last_allocated_ptr--;
198 }
199 
200 int nilfs_bmap_prepare_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
201 void nilfs_bmap_commit_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
202 void nilfs_bmap_abort_end_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *);
203 
204 static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
205 					     union nilfs_bmap_ptr_req *req)
206 {
207 	return NILFS_BMAP_USE_VBN(bmap) ?
208 		nilfs_bmap_prepare_end_v(bmap, req) : 0;
209 }
210 
211 static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
212 					     union nilfs_bmap_ptr_req *req)
213 {
214 	if (NILFS_BMAP_USE_VBN(bmap))
215 		nilfs_bmap_commit_end_v(bmap, req);
216 }
217 
218 static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
219 					    union nilfs_bmap_ptr_req *req)
220 {
221 	if (NILFS_BMAP_USE_VBN(bmap))
222 		nilfs_bmap_abort_end_v(bmap, req);
223 }
224 
225 int nilfs_bmap_start_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *,
226 		       sector_t);
227 int nilfs_bmap_move_v(const struct nilfs_bmap *, __u64, sector_t);
228 int nilfs_bmap_mark_dirty(const struct nilfs_bmap *, __u64);
229 
230 
231 __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
232 			      const struct buffer_head *);
233 
234 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
235 __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
236 
237 int nilfs_bmap_prepare_update_v(struct nilfs_bmap *,
238 				union nilfs_bmap_ptr_req *,
239 				union nilfs_bmap_ptr_req *);
240 void nilfs_bmap_commit_update_v(struct nilfs_bmap *,
241 				union nilfs_bmap_ptr_req *,
242 				union nilfs_bmap_ptr_req *);
243 void nilfs_bmap_abort_update_v(struct nilfs_bmap *,
244 			       union nilfs_bmap_ptr_req *,
245 			       union nilfs_bmap_ptr_req *);
246 
247 void nilfs_bmap_add_blocks(const struct nilfs_bmap *, int);
248 void nilfs_bmap_sub_blocks(const struct nilfs_bmap *, int);
249 
250 
251 /* Assume that bmap semaphore is locked. */
252 static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
253 {
254 	return !!(bmap->b_state & NILFS_BMAP_DIRTY);
255 }
256 
257 /* Assume that bmap semaphore is locked. */
258 static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
259 {
260 	bmap->b_state |= NILFS_BMAP_DIRTY;
261 }
262 
263 /* Assume that bmap semaphore is locked. */
264 static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
265 {
266 	bmap->b_state &= ~NILFS_BMAP_DIRTY;
267 }
268 
269 
270 #define NILFS_BMAP_LARGE	0x1
271 
272 #define NILFS_BMAP_SMALL_LOW	NILFS_DIRECT_KEY_MIN
273 #define NILFS_BMAP_SMALL_HIGH	NILFS_DIRECT_KEY_MAX
274 #define NILFS_BMAP_LARGE_LOW	NILFS_BTREE_ROOT_NCHILDREN_MAX
275 #define NILFS_BMAP_LARGE_HIGH	NILFS_BTREE_KEY_MAX
276 
277 #endif	/* _NILFS_BMAP_H */
278