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 * Written by Koji Sato.
19 #include <linux/errno.h>
26 static inline __le64
*nilfs_direct_dptrs(const struct nilfs_bmap
*direct
)
29 ((struct nilfs_direct_node
*)direct
->b_u
.u_data
+ 1);
33 nilfs_direct_get_ptr(const struct nilfs_bmap
*direct
, __u64 key
)
35 return le64_to_cpu(*(nilfs_direct_dptrs(direct
) + key
));
38 static inline void nilfs_direct_set_ptr(struct nilfs_bmap
*direct
,
41 *(nilfs_direct_dptrs(direct
) + key
) = cpu_to_le64(ptr
);
44 static int nilfs_direct_lookup(const struct nilfs_bmap
*direct
,
45 __u64 key
, int level
, __u64
*ptrp
)
49 if (key
> NILFS_DIRECT_KEY_MAX
|| level
!= 1)
51 ptr
= nilfs_direct_get_ptr(direct
, key
);
52 if (ptr
== NILFS_BMAP_INVALID_PTR
)
59 static int nilfs_direct_lookup_contig(const struct nilfs_bmap
*direct
,
60 __u64 key
, __u64
*ptrp
,
63 struct inode
*dat
= NULL
;
68 if (key
> NILFS_DIRECT_KEY_MAX
)
70 ptr
= nilfs_direct_get_ptr(direct
, key
);
71 if (ptr
== NILFS_BMAP_INVALID_PTR
)
74 if (NILFS_BMAP_USE_VBN(direct
)) {
75 dat
= nilfs_bmap_get_dat(direct
);
76 ret
= nilfs_dat_translate(dat
, ptr
, &blocknr
);
82 maxblocks
= min_t(unsigned, maxblocks
, NILFS_DIRECT_KEY_MAX
- key
+ 1);
83 for (cnt
= 1; cnt
< maxblocks
&&
84 (ptr2
= nilfs_direct_get_ptr(direct
, key
+ cnt
)) !=
85 NILFS_BMAP_INVALID_PTR
;
88 ret
= nilfs_dat_translate(dat
, ptr2
, &blocknr
);
93 if (ptr2
!= ptr
+ cnt
)
101 nilfs_direct_find_target_v(const struct nilfs_bmap
*direct
, __u64 key
)
105 ptr
= nilfs_bmap_find_target_seq(direct
, key
);
106 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
107 /* sequential access */
111 return nilfs_bmap_find_target_in_group(direct
);
114 static int nilfs_direct_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
116 union nilfs_bmap_ptr_req req
;
117 struct inode
*dat
= NULL
;
118 struct buffer_head
*bh
;
121 if (key
> NILFS_DIRECT_KEY_MAX
)
123 if (nilfs_direct_get_ptr(bmap
, key
) != NILFS_BMAP_INVALID_PTR
)
126 if (NILFS_BMAP_USE_VBN(bmap
)) {
127 req
.bpr_ptr
= nilfs_direct_find_target_v(bmap
, key
);
128 dat
= nilfs_bmap_get_dat(bmap
);
130 ret
= nilfs_bmap_prepare_alloc_ptr(bmap
, &req
, dat
);
132 /* ptr must be a pointer to a buffer head. */
133 bh
= (struct buffer_head
*)((unsigned long)ptr
);
134 set_buffer_nilfs_volatile(bh
);
136 nilfs_bmap_commit_alloc_ptr(bmap
, &req
, dat
);
137 nilfs_direct_set_ptr(bmap
, key
, req
.bpr_ptr
);
139 if (!nilfs_bmap_dirty(bmap
))
140 nilfs_bmap_set_dirty(bmap
);
142 if (NILFS_BMAP_USE_VBN(bmap
))
143 nilfs_bmap_set_target_v(bmap
, key
, req
.bpr_ptr
);
145 nilfs_inode_add_blocks(bmap
->b_inode
, 1);
150 static int nilfs_direct_delete(struct nilfs_bmap
*bmap
, __u64 key
)
152 union nilfs_bmap_ptr_req req
;
156 if (key
> NILFS_DIRECT_KEY_MAX
||
157 nilfs_direct_get_ptr(bmap
, key
) == NILFS_BMAP_INVALID_PTR
)
160 dat
= NILFS_BMAP_USE_VBN(bmap
) ? nilfs_bmap_get_dat(bmap
) : NULL
;
161 req
.bpr_ptr
= nilfs_direct_get_ptr(bmap
, key
);
163 ret
= nilfs_bmap_prepare_end_ptr(bmap
, &req
, dat
);
165 nilfs_bmap_commit_end_ptr(bmap
, &req
, dat
);
166 nilfs_direct_set_ptr(bmap
, key
, NILFS_BMAP_INVALID_PTR
);
167 nilfs_inode_sub_blocks(bmap
->b_inode
, 1);
172 static int nilfs_direct_seek_key(const struct nilfs_bmap
*direct
, __u64 start
,
177 for (key
= start
; key
<= NILFS_DIRECT_KEY_MAX
; key
++) {
178 if (nilfs_direct_get_ptr(direct
, key
) !=
179 NILFS_BMAP_INVALID_PTR
) {
187 static int nilfs_direct_last_key(const struct nilfs_bmap
*direct
, __u64
*keyp
)
191 lastkey
= NILFS_DIRECT_KEY_MAX
+ 1;
192 for (key
= NILFS_DIRECT_KEY_MIN
; key
<= NILFS_DIRECT_KEY_MAX
; key
++)
193 if (nilfs_direct_get_ptr(direct
, key
) !=
194 NILFS_BMAP_INVALID_PTR
)
197 if (lastkey
== NILFS_DIRECT_KEY_MAX
+ 1)
205 static int nilfs_direct_check_insert(const struct nilfs_bmap
*bmap
, __u64 key
)
207 return key
> NILFS_DIRECT_KEY_MAX
;
210 static int nilfs_direct_gather_data(struct nilfs_bmap
*direct
,
211 __u64
*keys
, __u64
*ptrs
, int nitems
)
217 if (nitems
> NILFS_DIRECT_NBLOCKS
)
218 nitems
= NILFS_DIRECT_NBLOCKS
;
220 for (key
= 0; key
< nitems
; key
++) {
221 ptr
= nilfs_direct_get_ptr(direct
, key
);
222 if (ptr
!= NILFS_BMAP_INVALID_PTR
) {
231 int nilfs_direct_delete_and_convert(struct nilfs_bmap
*bmap
,
232 __u64 key
, __u64
*keys
, __u64
*ptrs
, int n
)
237 /* no need to allocate any resource for conversion */
240 ret
= bmap
->b_ops
->bop_delete(bmap
, key
);
245 if (bmap
->b_ops
->bop_clear
!= NULL
)
246 bmap
->b_ops
->bop_clear(bmap
);
249 dptrs
= nilfs_direct_dptrs(bmap
);
250 for (i
= 0, j
= 0; i
< NILFS_DIRECT_NBLOCKS
; i
++) {
251 if ((j
< n
) && (i
== keys
[j
])) {
252 dptrs
[i
] = (i
!= key
) ?
253 cpu_to_le64(ptrs
[j
]) :
254 NILFS_BMAP_INVALID_PTR
;
257 dptrs
[i
] = NILFS_BMAP_INVALID_PTR
;
260 nilfs_direct_init(bmap
);
264 static int nilfs_direct_propagate(struct nilfs_bmap
*bmap
,
265 struct buffer_head
*bh
)
267 struct nilfs_palloc_req oldreq
, newreq
;
273 if (!NILFS_BMAP_USE_VBN(bmap
))
276 dat
= nilfs_bmap_get_dat(bmap
);
277 key
= nilfs_bmap_data_get_key(bmap
, bh
);
278 ptr
= nilfs_direct_get_ptr(bmap
, key
);
279 if (!buffer_nilfs_volatile(bh
)) {
280 oldreq
.pr_entry_nr
= ptr
;
281 newreq
.pr_entry_nr
= ptr
;
282 ret
= nilfs_dat_prepare_update(dat
, &oldreq
, &newreq
);
285 nilfs_dat_commit_update(dat
, &oldreq
, &newreq
,
286 bmap
->b_ptr_type
== NILFS_BMAP_PTR_VS
);
287 set_buffer_nilfs_volatile(bh
);
288 nilfs_direct_set_ptr(bmap
, key
, newreq
.pr_entry_nr
);
290 ret
= nilfs_dat_mark_dirty(dat
, ptr
);
295 static int nilfs_direct_assign_v(struct nilfs_bmap
*direct
,
296 __u64 key
, __u64 ptr
,
297 struct buffer_head
**bh
,
299 union nilfs_binfo
*binfo
)
301 struct inode
*dat
= nilfs_bmap_get_dat(direct
);
302 union nilfs_bmap_ptr_req req
;
306 ret
= nilfs_dat_prepare_start(dat
, &req
.bpr_req
);
308 nilfs_dat_commit_start(dat
, &req
.bpr_req
, blocknr
);
309 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64(ptr
);
310 binfo
->bi_v
.bi_blkoff
= cpu_to_le64(key
);
315 static int nilfs_direct_assign_p(struct nilfs_bmap
*direct
,
316 __u64 key
, __u64 ptr
,
317 struct buffer_head
**bh
,
319 union nilfs_binfo
*binfo
)
321 nilfs_direct_set_ptr(direct
, key
, blocknr
);
323 binfo
->bi_dat
.bi_blkoff
= cpu_to_le64(key
);
324 binfo
->bi_dat
.bi_level
= 0;
329 static int nilfs_direct_assign(struct nilfs_bmap
*bmap
,
330 struct buffer_head
**bh
,
332 union nilfs_binfo
*binfo
)
337 key
= nilfs_bmap_data_get_key(bmap
, *bh
);
338 if (unlikely(key
> NILFS_DIRECT_KEY_MAX
)) {
339 printk(KERN_CRIT
"%s: invalid key: %llu\n", __func__
,
340 (unsigned long long)key
);
343 ptr
= nilfs_direct_get_ptr(bmap
, key
);
344 if (unlikely(ptr
== NILFS_BMAP_INVALID_PTR
)) {
345 printk(KERN_CRIT
"%s: invalid pointer: %llu\n", __func__
,
346 (unsigned long long)ptr
);
350 return NILFS_BMAP_USE_VBN(bmap
) ?
351 nilfs_direct_assign_v(bmap
, key
, ptr
, bh
, blocknr
, binfo
) :
352 nilfs_direct_assign_p(bmap
, key
, ptr
, bh
, blocknr
, binfo
);
355 static const struct nilfs_bmap_operations nilfs_direct_ops
= {
356 .bop_lookup
= nilfs_direct_lookup
,
357 .bop_lookup_contig
= nilfs_direct_lookup_contig
,
358 .bop_insert
= nilfs_direct_insert
,
359 .bop_delete
= nilfs_direct_delete
,
362 .bop_propagate
= nilfs_direct_propagate
,
364 .bop_lookup_dirty_buffers
= NULL
,
366 .bop_assign
= nilfs_direct_assign
,
369 .bop_seek_key
= nilfs_direct_seek_key
,
370 .bop_last_key
= nilfs_direct_last_key
,
372 .bop_check_insert
= nilfs_direct_check_insert
,
373 .bop_check_delete
= NULL
,
374 .bop_gather_data
= nilfs_direct_gather_data
,
378 int nilfs_direct_init(struct nilfs_bmap
*bmap
)
380 bmap
->b_ops
= &nilfs_direct_ops
;