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 (NILFS_BMAP_USE_VBN(&direct
->d_bmap
))
94 req
->bpr_ptr
= nilfs_direct_find_target_v(direct
, key
);
95 ret
= nilfs_bmap_prepare_alloc_ptr(&direct
->d_bmap
, req
);
99 stats
->bs_nblocks
= 1;
103 static void nilfs_direct_commit_insert(struct nilfs_direct
*direct
,
104 union nilfs_bmap_ptr_req
*req
,
105 __u64 key
, __u64 ptr
)
107 struct buffer_head
*bh
;
109 /* ptr must be a pointer to a buffer head. */
110 bh
= (struct buffer_head
*)((unsigned long)ptr
);
111 set_buffer_nilfs_volatile(bh
);
113 nilfs_bmap_commit_alloc_ptr(&direct
->d_bmap
, req
);
114 nilfs_direct_set_ptr(direct
, key
, req
->bpr_ptr
);
116 if (!nilfs_bmap_dirty(&direct
->d_bmap
))
117 nilfs_bmap_set_dirty(&direct
->d_bmap
);
119 if (NILFS_BMAP_USE_VBN(&direct
->d_bmap
))
120 nilfs_direct_set_target_v(direct
, key
, req
->bpr_ptr
);
123 static int nilfs_direct_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
125 struct nilfs_direct
*direct
;
126 union nilfs_bmap_ptr_req req
;
127 struct nilfs_bmap_stats stats
;
130 direct
= (struct nilfs_direct
*)bmap
;
131 if (key
> NILFS_DIRECT_KEY_MAX
)
133 if (nilfs_direct_get_ptr(direct
, key
) != NILFS_BMAP_INVALID_PTR
)
136 ret
= nilfs_direct_prepare_insert(direct
, key
, &req
, &stats
);
139 nilfs_direct_commit_insert(direct
, &req
, key
, ptr
);
140 nilfs_bmap_add_blocks(bmap
, stats
.bs_nblocks
);
145 static int nilfs_direct_prepare_delete(struct nilfs_direct
*direct
,
146 union nilfs_bmap_ptr_req
*req
,
148 struct nilfs_bmap_stats
*stats
)
152 req
->bpr_ptr
= nilfs_direct_get_ptr(direct
, key
);
153 ret
= nilfs_bmap_prepare_end_ptr(&direct
->d_bmap
, req
);
155 stats
->bs_nblocks
= 1;
159 static void nilfs_direct_commit_delete(struct nilfs_direct
*direct
,
160 union nilfs_bmap_ptr_req
*req
,
163 nilfs_bmap_commit_end_ptr(&direct
->d_bmap
, req
);
164 nilfs_direct_set_ptr(direct
, key
, NILFS_BMAP_INVALID_PTR
);
167 static int nilfs_direct_delete(struct nilfs_bmap
*bmap
, __u64 key
)
169 struct nilfs_direct
*direct
;
170 union nilfs_bmap_ptr_req req
;
171 struct nilfs_bmap_stats stats
;
174 direct
= (struct nilfs_direct
*)bmap
;
175 if ((key
> NILFS_DIRECT_KEY_MAX
) ||
176 nilfs_direct_get_ptr(direct
, key
) == NILFS_BMAP_INVALID_PTR
)
179 ret
= nilfs_direct_prepare_delete(direct
, &req
, key
, &stats
);
182 nilfs_direct_commit_delete(direct
, &req
, key
);
183 nilfs_bmap_sub_blocks(bmap
, stats
.bs_nblocks
);
188 static int nilfs_direct_last_key(const struct nilfs_bmap
*bmap
, __u64
*keyp
)
190 struct nilfs_direct
*direct
;
193 direct
= (struct nilfs_direct
*)bmap
;
194 lastkey
= NILFS_DIRECT_KEY_MAX
+ 1;
195 for (key
= NILFS_DIRECT_KEY_MIN
; key
<= NILFS_DIRECT_KEY_MAX
; key
++)
196 if (nilfs_direct_get_ptr(direct
, key
) !=
197 NILFS_BMAP_INVALID_PTR
)
200 if (lastkey
== NILFS_DIRECT_KEY_MAX
+ 1)
208 static int nilfs_direct_check_insert(const struct nilfs_bmap
*bmap
, __u64 key
)
210 return key
> NILFS_DIRECT_KEY_MAX
;
213 static int nilfs_direct_gather_data(struct nilfs_bmap
*bmap
,
214 __u64
*keys
, __u64
*ptrs
, int nitems
)
216 struct nilfs_direct
*direct
;
221 direct
= (struct nilfs_direct
*)bmap
;
222 if (nitems
> NILFS_DIRECT_NBLOCKS
)
223 nitems
= NILFS_DIRECT_NBLOCKS
;
225 for (key
= 0; key
< nitems
; key
++) {
226 ptr
= nilfs_direct_get_ptr(direct
, key
);
227 if (ptr
!= NILFS_BMAP_INVALID_PTR
) {
236 int nilfs_direct_delete_and_convert(struct nilfs_bmap
*bmap
,
237 __u64 key
, __u64
*keys
, __u64
*ptrs
, int n
)
239 struct nilfs_direct
*direct
;
243 /* no need to allocate any resource for conversion */
246 ret
= bmap
->b_ops
->bop_delete(bmap
, key
);
251 if (bmap
->b_ops
->bop_clear
!= NULL
)
252 bmap
->b_ops
->bop_clear(bmap
);
255 direct
= (struct nilfs_direct
*)bmap
;
256 dptrs
= nilfs_direct_dptrs(direct
);
257 for (i
= 0, j
= 0; i
< NILFS_DIRECT_NBLOCKS
; i
++) {
258 if ((j
< n
) && (i
== keys
[j
])) {
259 dptrs
[i
] = (i
!= key
) ?
260 nilfs_bmap_ptr_to_dptr(ptrs
[j
]) :
261 NILFS_BMAP_INVALID_PTR
;
264 dptrs
[i
] = NILFS_BMAP_INVALID_PTR
;
267 nilfs_direct_init(bmap
);
271 static int nilfs_direct_propagate_v(struct nilfs_direct
*direct
,
272 struct buffer_head
*bh
)
274 union nilfs_bmap_ptr_req oldreq
, newreq
;
279 key
= nilfs_bmap_data_get_key(&direct
->d_bmap
, bh
);
280 ptr
= nilfs_direct_get_ptr(direct
, key
);
281 if (!buffer_nilfs_volatile(bh
)) {
282 oldreq
.bpr_ptr
= ptr
;
283 newreq
.bpr_ptr
= ptr
;
284 ret
= nilfs_bmap_prepare_update_v(&direct
->d_bmap
, &oldreq
,
288 nilfs_bmap_commit_update_v(&direct
->d_bmap
, &oldreq
, &newreq
);
289 set_buffer_nilfs_volatile(bh
);
290 nilfs_direct_set_ptr(direct
, key
, newreq
.bpr_ptr
);
292 ret
= nilfs_bmap_mark_dirty(&direct
->d_bmap
, ptr
);
297 static int nilfs_direct_propagate(const struct nilfs_bmap
*bmap
,
298 struct buffer_head
*bh
)
300 struct nilfs_direct
*direct
= (struct nilfs_direct
*)bmap
;
302 return NILFS_BMAP_USE_VBN(bmap
) ?
303 nilfs_direct_propagate_v(direct
, bh
) : 0;
306 static int nilfs_direct_assign_v(struct nilfs_direct
*direct
,
307 __u64 key
, __u64 ptr
,
308 struct buffer_head
**bh
,
310 union nilfs_binfo
*binfo
)
312 union nilfs_bmap_ptr_req req
;
316 ret
= nilfs_bmap_start_v(&direct
->d_bmap
, &req
, blocknr
);
317 if (unlikely(ret
< 0))
320 binfo
->bi_v
.bi_vblocknr
= nilfs_bmap_ptr_to_dptr(ptr
);
321 binfo
->bi_v
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
326 static int nilfs_direct_assign_p(struct nilfs_direct
*direct
,
327 __u64 key
, __u64 ptr
,
328 struct buffer_head
**bh
,
330 union nilfs_binfo
*binfo
)
332 nilfs_direct_set_ptr(direct
, key
, blocknr
);
334 binfo
->bi_dat
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
335 binfo
->bi_dat
.bi_level
= 0;
340 static int nilfs_direct_assign(struct nilfs_bmap
*bmap
,
341 struct buffer_head
**bh
,
343 union nilfs_binfo
*binfo
)
345 struct nilfs_direct
*direct
;
349 direct
= (struct nilfs_direct
*)bmap
;
350 key
= nilfs_bmap_data_get_key(bmap
, *bh
);
351 if (unlikely(key
> NILFS_DIRECT_KEY_MAX
)) {
352 printk(KERN_CRIT
"%s: invalid key: %llu\n", __func__
,
353 (unsigned long long)key
);
356 ptr
= nilfs_direct_get_ptr(direct
, key
);
357 if (unlikely(ptr
== NILFS_BMAP_INVALID_PTR
)) {
358 printk(KERN_CRIT
"%s: invalid pointer: %llu\n", __func__
,
359 (unsigned long long)ptr
);
363 return NILFS_BMAP_USE_VBN(bmap
) ?
364 nilfs_direct_assign_v(direct
, key
, ptr
, bh
, blocknr
, binfo
) :
365 nilfs_direct_assign_p(direct
, key
, ptr
, bh
, blocknr
, binfo
);
368 static const struct nilfs_bmap_operations nilfs_direct_ops
= {
369 .bop_lookup
= nilfs_direct_lookup
,
370 .bop_insert
= nilfs_direct_insert
,
371 .bop_delete
= nilfs_direct_delete
,
374 .bop_propagate
= nilfs_direct_propagate
,
376 .bop_lookup_dirty_buffers
= NULL
,
378 .bop_assign
= nilfs_direct_assign
,
381 .bop_last_key
= nilfs_direct_last_key
,
382 .bop_check_insert
= nilfs_direct_check_insert
,
383 .bop_check_delete
= NULL
,
384 .bop_gather_data
= nilfs_direct_gather_data
,
388 int nilfs_direct_init(struct nilfs_bmap
*bmap
)
390 bmap
->b_ops
= &nilfs_direct_ops
;