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 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);
119 if (!nilfs_bmap_dirty(&direct->d_bmap))
120 nilfs_bmap_set_dirty(&direct->d_bmap);
122 if (direct->d_ops->dop_set_target != NULL)
123 direct->d_ops->dop_set_target(direct, key, req->bpr_ptr);
126 static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
128 struct nilfs_direct *direct;
129 union nilfs_bmap_ptr_req req;
130 struct nilfs_bmap_stats stats;
133 direct = (struct nilfs_direct *)bmap;
134 if (key > NILFS_DIRECT_KEY_MAX)
136 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
139 ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
142 nilfs_direct_commit_insert(direct, &req, key, ptr);
143 nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
148 static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
149 union nilfs_bmap_ptr_req *req,
151 struct nilfs_bmap_stats *stats)
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);
163 stats->bs_nblocks = 1;
167 static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
168 union nilfs_bmap_ptr_req *req,
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);
177 static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
179 struct nilfs_direct *direct;
180 union nilfs_bmap_ptr_req req;
181 struct nilfs_bmap_stats stats;
184 direct = (struct nilfs_direct *)bmap;
185 if ((key > NILFS_DIRECT_KEY_MAX) ||
186 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
189 ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
192 nilfs_direct_commit_delete(direct, &req, key);
193 nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
198 static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
200 struct nilfs_direct *direct;
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)
210 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
213 BUG_ON(keyp == NULL);
219 static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
221 return key > NILFS_DIRECT_KEY_MAX;
224 static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
225 __u64 *keys, __u64 *ptrs, int nitems)
227 struct nilfs_direct *direct;
232 direct = (struct nilfs_direct *)bmap;
233 if (nitems > NILFS_DIRECT_NBLOCKS)
234 nitems = NILFS_DIRECT_NBLOCKS;
236 for (key = 0; key < nitems; key++) {
237 ptr = nilfs_direct_get_ptr(direct, key);
238 if (ptr != NILFS_BMAP_INVALID_PTR) {
247 int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
248 __u64 key, __u64 *keys, __u64 *ptrs,
249 int n, __u64 low, __u64 high)
251 struct nilfs_direct *direct;
255 /* no need to allocate any resource for conversion */
258 ret = bmap->b_ops->bop_delete(bmap, key);
263 if (bmap->b_ops->bop_clear != NULL)
264 bmap->b_ops->bop_clear(bmap);
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;
276 dptrs[i] = NILFS_BMAP_INVALID_PTR;
279 nilfs_direct_init(bmap, low, high);
284 static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
285 struct buffer_head *bh)
287 union nilfs_bmap_ptr_req oldreq, newreq;
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,
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);
305 ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
310 static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
311 struct buffer_head *bh)
313 struct nilfs_direct *direct;
315 direct = (struct nilfs_direct *)bmap;
316 return (direct->d_ops->dop_propagate != NULL) ?
317 direct->d_ops->dop_propagate(direct, bh) :
321 static int nilfs_direct_assign_v(struct nilfs_direct *direct,
322 __u64 key, __u64 ptr,
323 struct buffer_head **bh,
325 union nilfs_binfo *binfo)
327 union nilfs_bmap_ptr_req req;
331 ret = direct->d_bmap.b_pops->bpop_prepare_start_ptr(
332 &direct->d_bmap, &req);
335 direct->d_bmap.b_pops->bpop_commit_start_ptr(&direct->d_bmap,
338 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
339 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
344 static int nilfs_direct_assign_p(struct nilfs_direct *direct,
345 __u64 key, __u64 ptr,
346 struct buffer_head **bh,
348 union nilfs_binfo *binfo)
350 nilfs_direct_set_ptr(direct, key, blocknr);
352 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
353 binfo->bi_dat.bi_level = 0;
358 static int nilfs_direct_assign(struct nilfs_bmap *bmap,
359 struct buffer_head **bh,
361 union nilfs_binfo *binfo)
363 struct nilfs_direct *direct;
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);
373 return direct->d_ops->dop_assign(direct, key, ptr, bh,
377 static 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,
383 .bop_propagate = nilfs_direct_propagate,
385 .bop_lookup_dirty_buffers = NULL,
387 .bop_assign = nilfs_direct_assign,
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,
397 static 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,
404 static 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,
411 int nilfs_direct_init(struct nilfs_bmap *bmap, __u64 low, __u64 high)
413 struct nilfs_direct *direct;
415 direct = (struct nilfs_direct *)bmap;
416 bmap->b_ops = &nilfs_direct_ops;
419 switch (bmap->b_inode->i_ino) {
421 direct->d_ops = &nilfs_direct_ops_p;
424 direct->d_ops = &nilfs_direct_ops_v;