2 * direct.c - NILFS direct block pointer.
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/errno.h>
29 static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
32 ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
36 nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
38 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
41 static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
44 *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
47 static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
48 __u64 key, int level, __u64 *ptrp)
50 struct nilfs_direct *direct;
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))
66 nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
70 ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
71 if (ptr != NILFS_BMAP_INVALID_PTR)
72 /* sequential access */
76 return nilfs_bmap_find_target_in_group(&direct->d_bmap);
79 static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
82 direct->d_bmap.b_last_allocated_key = key;
83 direct->d_bmap.b_last_allocated_ptr = ptr;
86 static int nilfs_direct_prepare_insert(struct nilfs_direct *direct,
88 union nilfs_bmap_ptr_req *req,
89 struct nilfs_bmap_stats *stats)
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,
100 stats->bs_nblocks = 1;
104 static void nilfs_direct_commit_insert(struct nilfs_direct *direct,
105 union nilfs_bmap_ptr_req *req,
106 __u64 key, __u64 ptr)
108 struct buffer_head *bh;
110 /* ptr must be a pointer to a buffer head. */
111 bh = (struct buffer_head *)((unsigned long)ptr);
112 set_buffer_nilfs_volatile(bh);
114 direct->d_bmap.b_pops->bpop_commit_alloc_ptr(&direct->d_bmap, req);
115 nilfs_direct_set_ptr(direct, key, req->bpr_ptr);
117 if (!nilfs_bmap_dirty(&direct->d_bmap))
118 nilfs_bmap_set_dirty(&direct->d_bmap);
120 if (direct->d_ops->dop_set_target != NULL)
121 direct->d_ops->dop_set_target(direct, key, req->bpr_ptr);
124 static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
126 struct nilfs_direct *direct;
127 union nilfs_bmap_ptr_req req;
128 struct nilfs_bmap_stats stats;
131 direct = (struct nilfs_direct *)bmap;
132 if (key > NILFS_DIRECT_KEY_MAX)
134 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
137 ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
140 nilfs_direct_commit_insert(direct, &req, key, ptr);
141 nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
146 static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
147 union nilfs_bmap_ptr_req *req,
149 struct nilfs_bmap_stats *stats)
153 if (direct->d_bmap.b_pops->bpop_prepare_end_ptr != NULL) {
154 req->bpr_ptr = nilfs_direct_get_ptr(direct, key);
155 ret = direct->d_bmap.b_pops->bpop_prepare_end_ptr(
156 &direct->d_bmap, req);
161 stats->bs_nblocks = 1;
165 static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
166 union nilfs_bmap_ptr_req *req,
169 if (direct->d_bmap.b_pops->bpop_commit_end_ptr != NULL)
170 direct->d_bmap.b_pops->bpop_commit_end_ptr(
171 &direct->d_bmap, req);
172 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
175 static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
177 struct nilfs_direct *direct;
178 union nilfs_bmap_ptr_req req;
179 struct nilfs_bmap_stats stats;
182 direct = (struct nilfs_direct *)bmap;
183 if ((key > NILFS_DIRECT_KEY_MAX) ||
184 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
187 ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
190 nilfs_direct_commit_delete(direct, &req, key);
191 nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
196 static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
198 struct nilfs_direct *direct;
201 direct = (struct nilfs_direct *)bmap;
202 lastkey = NILFS_DIRECT_KEY_MAX + 1;
203 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
204 if (nilfs_direct_get_ptr(direct, key) !=
205 NILFS_BMAP_INVALID_PTR)
208 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
216 static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
218 return key > NILFS_DIRECT_KEY_MAX;
221 static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
222 __u64 *keys, __u64 *ptrs, int nitems)
224 struct nilfs_direct *direct;
229 direct = (struct nilfs_direct *)bmap;
230 if (nitems > NILFS_DIRECT_NBLOCKS)
231 nitems = NILFS_DIRECT_NBLOCKS;
233 for (key = 0; key < nitems; key++) {
234 ptr = nilfs_direct_get_ptr(direct, key);
235 if (ptr != NILFS_BMAP_INVALID_PTR) {
244 int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
245 __u64 key, __u64 *keys, __u64 *ptrs,
246 int n, __u64 low, __u64 high)
248 struct nilfs_direct *direct;
252 /* no need to allocate any resource for conversion */
255 ret = bmap->b_ops->bop_delete(bmap, key);
260 if (bmap->b_ops->bop_clear != NULL)
261 bmap->b_ops->bop_clear(bmap);
264 direct = (struct nilfs_direct *)bmap;
265 dptrs = nilfs_direct_dptrs(direct);
266 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
267 if ((j < n) && (i == keys[j])) {
268 dptrs[i] = (i != key) ?
269 nilfs_bmap_ptr_to_dptr(ptrs[j]) :
270 NILFS_BMAP_INVALID_PTR;
273 dptrs[i] = NILFS_BMAP_INVALID_PTR;
276 nilfs_direct_init(bmap, low, high);
281 static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
282 struct buffer_head *bh)
284 union nilfs_bmap_ptr_req oldreq, newreq;
289 key = nilfs_bmap_data_get_key(&direct->d_bmap, bh);
290 ptr = nilfs_direct_get_ptr(direct, key);
291 if (!buffer_nilfs_volatile(bh)) {
292 oldreq.bpr_ptr = ptr;
293 newreq.bpr_ptr = ptr;
294 ret = nilfs_bmap_prepare_update(&direct->d_bmap, &oldreq,
298 nilfs_bmap_commit_update(&direct->d_bmap, &oldreq, &newreq);
299 set_buffer_nilfs_volatile(bh);
300 nilfs_direct_set_ptr(direct, key, newreq.bpr_ptr);
302 ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
307 static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
308 struct buffer_head *bh)
310 struct nilfs_direct *direct;
312 direct = (struct nilfs_direct *)bmap;
313 return (direct->d_ops->dop_propagate != NULL) ?
314 direct->d_ops->dop_propagate(direct, bh) :
318 static int nilfs_direct_assign_v(struct nilfs_direct *direct,
319 __u64 key, __u64 ptr,
320 struct buffer_head **bh,
322 union nilfs_binfo *binfo)
324 union nilfs_bmap_ptr_req req;
328 ret = nilfs_bmap_start_v(&direct->d_bmap, &req, blocknr);
329 if (unlikely(ret < 0))
332 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
333 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
338 static int nilfs_direct_assign_p(struct nilfs_direct *direct,
339 __u64 key, __u64 ptr,
340 struct buffer_head **bh,
342 union nilfs_binfo *binfo)
344 nilfs_direct_set_ptr(direct, key, blocknr);
346 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
347 binfo->bi_dat.bi_level = 0;
352 static int nilfs_direct_assign(struct nilfs_bmap *bmap,
353 struct buffer_head **bh,
355 union nilfs_binfo *binfo)
357 struct nilfs_direct *direct;
361 direct = (struct nilfs_direct *)bmap;
362 key = nilfs_bmap_data_get_key(bmap, *bh);
363 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
364 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
365 (unsigned long long)key);
368 ptr = nilfs_direct_get_ptr(direct, key);
369 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
370 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
371 (unsigned long long)ptr);
375 return direct->d_ops->dop_assign(direct, key, ptr, bh,
379 static const struct nilfs_bmap_operations nilfs_direct_ops = {
380 .bop_lookup = nilfs_direct_lookup,
381 .bop_insert = nilfs_direct_insert,
382 .bop_delete = nilfs_direct_delete,
385 .bop_propagate = nilfs_direct_propagate,
387 .bop_lookup_dirty_buffers = NULL,
389 .bop_assign = nilfs_direct_assign,
392 .bop_last_key = nilfs_direct_last_key,
393 .bop_check_insert = nilfs_direct_check_insert,
394 .bop_check_delete = NULL,
395 .bop_gather_data = nilfs_direct_gather_data,
399 static const struct nilfs_direct_operations nilfs_direct_ops_v = {
400 .dop_find_target = nilfs_direct_find_target_v,
401 .dop_set_target = nilfs_direct_set_target_v,
402 .dop_propagate = nilfs_direct_propagate_v,
403 .dop_assign = nilfs_direct_assign_v,
406 static const struct nilfs_direct_operations nilfs_direct_ops_p = {
407 .dop_find_target = NULL,
408 .dop_set_target = NULL,
409 .dop_propagate = NULL,
410 .dop_assign = nilfs_direct_assign_p,
413 int nilfs_direct_init(struct nilfs_bmap *bmap, __u64 low, __u64 high)
415 struct nilfs_direct *direct;
417 direct = (struct nilfs_direct *)bmap;
418 bmap->b_ops = &nilfs_direct_ops;
421 switch (bmap->b_inode->i_ino) {
423 direct->d_ops = &nilfs_direct_ops_p;
426 direct->d_ops = &nilfs_direct_ops_v;