aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoji Sato <sato.koji@lab.ntt.co.jp>2009-04-06 22:01:25 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-04-07 11:31:13 -0400
commit36a580eb489f54d81a0534974962e732a314b999 (patch)
treebf28d0b13c4187b11c58822bc49a87453d83f877
parent17c76b0104e4a6513983777e1a17e0297a12b0c4 (diff)
nilfs2: direct block mapping
This adds block mappings using direct pointers which are stored in the i_bmap array of inode. Signed-off-by: Koji Sato <sato.koji@lab.ntt.co.jp> Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/nilfs2/direct.c429
-rw-r--r--fs/nilfs2/direct.h78
2 files changed, 507 insertions, 0 deletions
diff --git a/fs/nilfs2/direct.c b/fs/nilfs2/direct.c
new file mode 100644
index 000000000000..303d7f1982f9
--- /dev/null
+++ b/fs/nilfs2/direct.c
@@ -0,0 +1,429 @@
1/*
2 * direct.c - NILFS direct block pointer.
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#include <linux/errno.h>
24#include "nilfs.h"
25#include "page.h"
26#include "direct.h"
27#include "alloc.h"
28
29static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
30{
31 return (__le64 *)
32 ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
33}
34
35static inline __u64
36nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
37{
38 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
39}
40
41static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
42 __u64 key, __u64 ptr)
43{
44 *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
45}
46
47static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
48 __u64 key, int level, __u64 *ptrp)
49{
50 struct nilfs_direct *direct;
51 __u64 ptr;
52
53 direct = (struct nilfs_direct *)bmap;
54 if ((key > NILFS_DIRECT_KEY_MAX) ||
55 (level != 1) || /* XXX: use macro for level 1 */
56 ((ptr = nilfs_direct_get_ptr(direct, key)) ==
57 NILFS_BMAP_INVALID_PTR))
58 return -ENOENT;
59
60 if (ptrp != NULL)
61 *ptrp = ptr;
62 return 0;
63}
64
65static __u64
66nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
67{
68 __u64 ptr;
69
70 ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
71 if (ptr != NILFS_BMAP_INVALID_PTR)
72 /* sequential access */
73 return ptr;
74 else
75 /* block group */
76 return nilfs_bmap_find_target_in_group(&direct->d_bmap);
77}
78
79static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
80 __u64 key, __u64 ptr)
81{
82 direct->d_bmap.b_last_allocated_key = key;
83 direct->d_bmap.b_last_allocated_ptr = ptr;
84}
85
86static int nilfs_direct_prepare_insert(struct nilfs_direct *direct,
87 __u64 key,
88 union nilfs_bmap_ptr_req *req,
89 struct nilfs_bmap_stats *stats)
90{
91 int ret;
92
93 if (direct->d_ops->dop_find_target != NULL)
94 req->bpr_ptr = (*direct->d_ops->dop_find_target)(direct, key);
95 ret = (*direct->d_bmap.b_pops->bpop_prepare_alloc_ptr)(&direct->d_bmap,
96 req);
97 if (ret < 0)
98 return ret;
99
100 stats->bs_nblocks = 1;
101 return 0;
102}
103
104static void nilfs_direct_commit_insert(struct nilfs_direct *direct,
105 union nilfs_bmap_ptr_req *req,
106 __u64 key, __u64 ptr)
107{
108 struct buffer_head *bh;
109
110 /* ptr must be a pointer to a buffer head. */
111 bh = (struct buffer_head *)((unsigned long)ptr);
112 set_buffer_nilfs_volatile(bh);
113
114 if (direct->d_bmap.b_pops->bpop_commit_alloc_ptr != NULL)
115 (*direct->d_bmap.b_pops->bpop_commit_alloc_ptr)(
116 &direct->d_bmap, req);
117 nilfs_direct_set_ptr(direct, key, req->bpr_ptr);
118
119 if (!nilfs_bmap_dirty(&direct->d_bmap))
120 nilfs_bmap_set_dirty(&direct->d_bmap);
121
122 if (direct->d_ops->dop_set_target != NULL)
123 (*direct->d_ops->dop_set_target)(direct, key, req->bpr_ptr);
124}
125
126static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
127{
128 struct nilfs_direct *direct;
129 union nilfs_bmap_ptr_req req;
130 struct nilfs_bmap_stats stats;
131 int ret;
132
133 direct = (struct nilfs_direct *)bmap;
134 if (key > NILFS_DIRECT_KEY_MAX)
135 return -ENOENT;
136 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
137 return -EEXIST;
138
139 ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
140 if (ret < 0)
141 return ret;
142 nilfs_direct_commit_insert(direct, &req, key, ptr);
143 nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
144
145 return 0;
146}
147
148static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
149 union nilfs_bmap_ptr_req *req,
150 __u64 key,
151 struct nilfs_bmap_stats *stats)
152{
153 int ret;
154
155 if (direct->d_bmap.b_pops->bpop_prepare_end_ptr != NULL) {
156 req->bpr_ptr = nilfs_direct_get_ptr(direct, key);
157 ret = (*direct->d_bmap.b_pops->bpop_prepare_end_ptr)(
158 &direct->d_bmap, req);
159 if (ret < 0)
160 return ret;
161 }
162
163 stats->bs_nblocks = 1;
164 return 0;
165}
166
167static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
168 union nilfs_bmap_ptr_req *req,
169 __u64 key)
170{
171 if (direct->d_bmap.b_pops->bpop_commit_end_ptr != NULL)
172 (*direct->d_bmap.b_pops->bpop_commit_end_ptr)(
173 &direct->d_bmap, req);
174 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
175}
176
177static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
178{
179 struct nilfs_direct *direct;
180 union nilfs_bmap_ptr_req req;
181 struct nilfs_bmap_stats stats;
182 int ret;
183
184 direct = (struct nilfs_direct *)bmap;
185 if ((key > NILFS_DIRECT_KEY_MAX) ||
186 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
187 return -ENOENT;
188
189 ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
190 if (ret < 0)
191 return ret;
192 nilfs_direct_commit_delete(direct, &req, key);
193 nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
194
195 return 0;
196}
197
198static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
199{
200 struct nilfs_direct *direct;
201 __u64 key, lastkey;
202
203 direct = (struct nilfs_direct *)bmap;
204 lastkey = NILFS_DIRECT_KEY_MAX + 1;
205 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
206 if (nilfs_direct_get_ptr(direct, key) !=
207 NILFS_BMAP_INVALID_PTR)
208 lastkey = key;
209
210 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
211 return -ENOENT;
212
213 BUG_ON(keyp == NULL);
214 *keyp = lastkey;
215
216 return 0;
217}
218
219static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
220{
221 return key > NILFS_DIRECT_KEY_MAX;
222}
223
224static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
225 __u64 *keys, __u64 *ptrs, int nitems)
226{
227 struct nilfs_direct *direct;
228 __u64 key;
229 __u64 ptr;
230 int n;
231
232 direct = (struct nilfs_direct *)bmap;
233 if (nitems > NILFS_DIRECT_NBLOCKS)
234 nitems = NILFS_DIRECT_NBLOCKS;
235 n = 0;
236 for (key = 0; key < nitems; key++) {
237 ptr = nilfs_direct_get_ptr(direct, key);
238 if (ptr != NILFS_BMAP_INVALID_PTR) {
239 keys[n] = key;
240 ptrs[n] = ptr;
241 n++;
242 }
243 }
244 return n;
245}
246
247int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
248 __u64 key, __u64 *keys, __u64 *ptrs,
249 int n, __u64 low, __u64 high)
250{
251 struct nilfs_direct *direct;
252 __le64 *dptrs;
253 int ret, i, j;
254
255 /* no need to allocate any resource for conversion */
256
257 /* delete */
258 ret = (*bmap->b_ops->bop_delete)(bmap, key);
259 if (ret < 0)
260 return ret;
261
262 /* free resources */
263 if (bmap->b_ops->bop_clear != NULL)
264 (*bmap->b_ops->bop_clear)(bmap);
265
266 /* convert */
267 direct = (struct nilfs_direct *)bmap;
268 dptrs = nilfs_direct_dptrs(direct);
269 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
270 if ((j < n) && (i == keys[j])) {
271 dptrs[i] = (i != key) ?
272 nilfs_bmap_ptr_to_dptr(ptrs[j]) :
273 NILFS_BMAP_INVALID_PTR;
274 j++;
275 } else
276 dptrs[i] = NILFS_BMAP_INVALID_PTR;
277 }
278
279 nilfs_direct_init(bmap, low, high);
280
281 return 0;
282}
283
284static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
285 struct buffer_head *bh)
286{
287 union nilfs_bmap_ptr_req oldreq, newreq;
288 __u64 key;
289 __u64 ptr;
290 int ret;
291
292 key = nilfs_bmap_data_get_key(&direct->d_bmap, bh);
293 ptr = nilfs_direct_get_ptr(direct, key);
294 if (!buffer_nilfs_volatile(bh)) {
295 oldreq.bpr_ptr = ptr;
296 newreq.bpr_ptr = ptr;
297 ret = nilfs_bmap_prepare_update(&direct->d_bmap, &oldreq,
298 &newreq);
299 if (ret < 0)
300 return ret;
301 nilfs_bmap_commit_update(&direct->d_bmap, &oldreq, &newreq);
302 set_buffer_nilfs_volatile(bh);
303 nilfs_direct_set_ptr(direct, key, newreq.bpr_ptr);
304 } else
305 ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
306
307 return ret;
308}
309
310static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
311 struct buffer_head *bh)
312{
313 struct nilfs_direct *direct;
314
315 direct = (struct nilfs_direct *)bmap;
316 return (direct->d_ops->dop_propagate != NULL) ?
317 (*direct->d_ops->dop_propagate)(direct, bh) :
318 0;
319}
320
321static int nilfs_direct_assign_v(struct nilfs_direct *direct,
322 __u64 key, __u64 ptr,
323 struct buffer_head **bh,
324 sector_t blocknr,
325 union nilfs_binfo *binfo)
326{
327 union nilfs_bmap_ptr_req req;
328 int ret;
329
330 req.bpr_ptr = ptr;
331 ret = (*direct->d_bmap.b_pops->bpop_prepare_start_ptr)(
332 &direct->d_bmap, &req);
333 if (ret < 0)
334 return ret;
335 (*direct->d_bmap.b_pops->bpop_commit_start_ptr)(&direct->d_bmap,
336 &req, blocknr);
337
338 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
339 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
340
341 return 0;
342}
343
344static int nilfs_direct_assign_p(struct nilfs_direct *direct,
345 __u64 key, __u64 ptr,
346 struct buffer_head **bh,
347 sector_t blocknr,
348 union nilfs_binfo *binfo)
349{
350 nilfs_direct_set_ptr(direct, key, blocknr);
351
352 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
353 binfo->bi_dat.bi_level = 0;
354
355 return 0;
356}
357
358static int nilfs_direct_assign(struct nilfs_bmap *bmap,
359 struct buffer_head **bh,
360 sector_t blocknr,
361 union nilfs_binfo *binfo)
362{
363 struct nilfs_direct *direct;
364 __u64 key;
365 __u64 ptr;
366
367 direct = (struct nilfs_direct *)bmap;
368 key = nilfs_bmap_data_get_key(bmap, *bh);
369 BUG_ON(key > NILFS_DIRECT_KEY_MAX);
370 ptr = nilfs_direct_get_ptr(direct, key);
371 BUG_ON(ptr == NILFS_BMAP_INVALID_PTR);
372
373 return (*direct->d_ops->dop_assign)(direct, key, ptr, bh,
374 blocknr, binfo);
375}
376
377static const struct nilfs_bmap_operations nilfs_direct_ops = {
378 .bop_lookup = nilfs_direct_lookup,
379 .bop_insert = nilfs_direct_insert,
380 .bop_delete = nilfs_direct_delete,
381 .bop_clear = NULL,
382
383 .bop_propagate = nilfs_direct_propagate,
384
385 .bop_lookup_dirty_buffers = NULL,
386
387 .bop_assign = nilfs_direct_assign,
388 .bop_mark = NULL,
389
390 .bop_last_key = nilfs_direct_last_key,
391 .bop_check_insert = nilfs_direct_check_insert,
392 .bop_check_delete = NULL,
393 .bop_gather_data = nilfs_direct_gather_data,
394};
395
396
397static const struct nilfs_direct_operations nilfs_direct_ops_v = {
398 .dop_find_target = nilfs_direct_find_target_v,
399 .dop_set_target = nilfs_direct_set_target_v,
400 .dop_propagate = nilfs_direct_propagate_v,
401 .dop_assign = nilfs_direct_assign_v,
402};
403
404static const struct nilfs_direct_operations nilfs_direct_ops_p = {
405 .dop_find_target = NULL,
406 .dop_set_target = NULL,
407 .dop_propagate = NULL,
408 .dop_assign = nilfs_direct_assign_p,
409};
410
411int nilfs_direct_init(struct nilfs_bmap *bmap, __u64 low, __u64 high)
412{
413 struct nilfs_direct *direct;
414
415 direct = (struct nilfs_direct *)bmap;
416 bmap->b_ops = &nilfs_direct_ops;
417 bmap->b_low = low;
418 bmap->b_high = high;
419 switch (bmap->b_inode->i_ino) {
420 case NILFS_DAT_INO:
421 direct->d_ops = &nilfs_direct_ops_p;
422 break;
423 default:
424 direct->d_ops = &nilfs_direct_ops_v;
425 break;
426 }
427
428 return 0;
429}
diff --git a/fs/nilfs2/direct.h b/fs/nilfs2/direct.h
new file mode 100644
index 000000000000..45d2c5cda812
--- /dev/null
+++ b/fs/nilfs2/direct.h
@@ -0,0 +1,78 @@
1/*
2 * direct.h - NILFS direct block pointer.
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_DIRECT_H
24#define _NILFS_DIRECT_H
25
26#include <linux/types.h>
27#include <linux/buffer_head.h>
28#include "bmap.h"
29
30
31struct nilfs_direct;
32
33/**
34 * struct nilfs_direct_operations - direct mapping operation table
35 */
36struct nilfs_direct_operations {
37 __u64 (*dop_find_target)(const struct nilfs_direct *, __u64);
38 void (*dop_set_target)(struct nilfs_direct *, __u64, __u64);
39 int (*dop_propagate)(struct nilfs_direct *, struct buffer_head *);
40 int (*dop_assign)(struct nilfs_direct *, __u64, __u64,
41 struct buffer_head **, sector_t,
42 union nilfs_binfo *);
43};
44
45/**
46 * struct nilfs_direct_node - direct node
47 * @dn_flags: flags
48 * @dn_pad: padding
49 */
50struct nilfs_direct_node {
51 __u8 dn_flags;
52 __u8 pad[7];
53};
54
55/**
56 * struct nilfs_direct - direct mapping
57 * @d_bmap: bmap structure
58 * @d_ops: direct mapping operation table
59 */
60struct nilfs_direct {
61 struct nilfs_bmap d_bmap;
62
63 /* direct-mapping-specific members */
64 const struct nilfs_direct_operations *d_ops;
65};
66
67
68#define NILFS_DIRECT_NBLOCKS (NILFS_BMAP_SIZE / sizeof(__le64) - 1)
69#define NILFS_DIRECT_KEY_MIN 0
70#define NILFS_DIRECT_KEY_MAX (NILFS_DIRECT_NBLOCKS - 1)
71
72
73int nilfs_direct_init(struct nilfs_bmap *, __u64, __u64);
74int nilfs_direct_delete_and_convert(struct nilfs_bmap *, __u64, __u64 *,
75 __u64 *, int, __u64, __u64);
76
77
78#endif /* _NILFS_DIRECT_H */