]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/nilfs2/bmap.h
nilfs2: unify bmap operations starting use of indirect block address
[karo-tx-linux.git] / fs / nilfs2 / bmap.h
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_insert)(struct nilfs_bmap *, __u64, __u64);
68         int (*bop_delete)(struct nilfs_bmap *, __u64);
69         void (*bop_clear)(struct nilfs_bmap *);
70
71         int (*bop_propagate)(const struct nilfs_bmap *, struct buffer_head *);
72         void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
73                                          struct list_head *);
74
75         int (*bop_assign)(struct nilfs_bmap *,
76                           struct buffer_head **,
77                           sector_t,
78                           union nilfs_binfo *);
79         int (*bop_mark)(struct nilfs_bmap *, __u64, int);
80
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);
86 };
87
88
89 /**
90  * struct nilfs_bmap_ptr_operations - bmap ptr operation table
91  */
92 struct nilfs_bmap_ptr_operations {
93         int (*bpop_prepare_alloc_ptr)(struct nilfs_bmap *,
94                                       union nilfs_bmap_ptr_req *);
95         void (*bpop_commit_alloc_ptr)(struct nilfs_bmap *,
96                                       union nilfs_bmap_ptr_req *);
97         void (*bpop_abort_alloc_ptr)(struct nilfs_bmap *,
98                                      union nilfs_bmap_ptr_req *);
99         int (*bpop_prepare_end_ptr)(struct nilfs_bmap *,
100                                     union nilfs_bmap_ptr_req *);
101         void (*bpop_commit_end_ptr)(struct nilfs_bmap *,
102                                     union nilfs_bmap_ptr_req *);
103         void (*bpop_abort_end_ptr)(struct nilfs_bmap *,
104                                    union nilfs_bmap_ptr_req *);
105
106         int (*bpop_translate)(const struct nilfs_bmap *, __u64, __u64 *);
107 };
108
109
110 #define NILFS_BMAP_SIZE         (NILFS_INODE_BMAP_SIZE * sizeof(__le64))
111 #define NILFS_BMAP_KEY_BIT      (sizeof(unsigned long) * 8 /* CHAR_BIT */)
112 #define NILFS_BMAP_NEW_PTR_INIT \
113         (1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
114
115 static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
116 {
117         return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
118 }
119
120
121 /**
122  * struct nilfs_bmap - bmap structure
123  * @b_u: raw data
124  * @b_sem: semaphore
125  * @b_inode: owner of bmap
126  * @b_ops: bmap operation table
127  * @b_pops: bmap ptr operation table
128  * @b_low: low watermark of conversion
129  * @b_high: high watermark of conversion
130  * @b_last_allocated_key: last allocated key for data block
131  * @b_last_allocated_ptr: last allocated ptr for data block
132  * @b_state: state
133  */
134 struct nilfs_bmap {
135         union {
136                 __u8 u_flags;
137                 __le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
138         } b_u;
139         struct rw_semaphore b_sem;
140         struct inode *b_inode;
141         const struct nilfs_bmap_operations *b_ops;
142         const struct nilfs_bmap_ptr_operations *b_pops;
143         __u64 b_low;
144         __u64 b_high;
145         __u64 b_last_allocated_key;
146         __u64 b_last_allocated_ptr;
147         int b_state;
148 };
149
150 /* state */
151 #define NILFS_BMAP_DIRTY        0x00000001
152
153
154 int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
155 int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
156 void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
157 int nilfs_bmap_lookup(struct nilfs_bmap *, unsigned long, unsigned long *);
158 int nilfs_bmap_insert(struct nilfs_bmap *, unsigned long, unsigned long);
159 int nilfs_bmap_delete(struct nilfs_bmap *, unsigned long);
160 int nilfs_bmap_last_key(struct nilfs_bmap *, unsigned long *);
161 int nilfs_bmap_truncate(struct nilfs_bmap *, unsigned long);
162 void nilfs_bmap_clear(struct nilfs_bmap *);
163 int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
164 void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
165 int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
166                       unsigned long, union nilfs_binfo *);
167 int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
168 int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
169
170 void nilfs_bmap_init_gc(struct nilfs_bmap *);
171 void nilfs_bmap_init_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
172 void nilfs_bmap_commit_gcdat(struct nilfs_bmap *, struct nilfs_bmap *);
173
174
175 /*
176  * Internal use only
177  */
178
179 int nilfs_bmap_start_v(struct nilfs_bmap *, union nilfs_bmap_ptr_req *,
180                        sector_t);
181 int nilfs_bmap_move_v(const struct nilfs_bmap *, __u64, sector_t);
182 int nilfs_bmap_mark_dirty(const struct nilfs_bmap *, __u64);
183
184
185 __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
186                               const struct buffer_head *);
187
188 __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
189 __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
190
191 int nilfs_bmap_prepare_update(struct nilfs_bmap *,
192                               union nilfs_bmap_ptr_req *,
193                               union nilfs_bmap_ptr_req *);
194 void nilfs_bmap_commit_update(struct nilfs_bmap *,
195                               union nilfs_bmap_ptr_req *,
196                               union nilfs_bmap_ptr_req *);
197 void nilfs_bmap_abort_update(struct nilfs_bmap *,
198                              union nilfs_bmap_ptr_req *,
199                              union nilfs_bmap_ptr_req *);
200
201 void nilfs_bmap_add_blocks(const struct nilfs_bmap *, int);
202 void nilfs_bmap_sub_blocks(const struct nilfs_bmap *, int);
203
204
205 int nilfs_bmap_get_block(const struct nilfs_bmap *, __u64,
206                          struct buffer_head **);
207 void nilfs_bmap_put_block(const struct nilfs_bmap *, struct buffer_head *);
208 int nilfs_bmap_get_new_block(const struct nilfs_bmap *, __u64,
209                              struct buffer_head **);
210 void nilfs_bmap_delete_block(const struct nilfs_bmap *, struct buffer_head *);
211
212
213 /* Assume that bmap semaphore is locked. */
214 static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
215 {
216         return !!(bmap->b_state & NILFS_BMAP_DIRTY);
217 }
218
219 /* Assume that bmap semaphore is locked. */
220 static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
221 {
222         bmap->b_state |= NILFS_BMAP_DIRTY;
223 }
224
225 /* Assume that bmap semaphore is locked. */
226 static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
227 {
228         bmap->b_state &= ~NILFS_BMAP_DIRTY;
229 }
230
231
232 #define NILFS_BMAP_LARGE        0x1
233
234 #define NILFS_BMAP_SMALL_LOW    NILFS_DIRECT_KEY_MIN
235 #define NILFS_BMAP_SMALL_HIGH   NILFS_DIRECT_KEY_MAX
236 #define NILFS_BMAP_LARGE_LOW    NILFS_BTREE_ROOT_NCHILDREN_MAX
237 #define NILFS_BMAP_LARGE_HIGH   NILFS_BTREE_KEY_MAX
238
239 #endif  /* _NILFS_BMAP_H */