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>
30 static inline __le64
*nilfs_direct_dptrs(const struct nilfs_direct
*direct
)
33 ((struct nilfs_direct_node
*)direct
->d_bmap
.b_u
.u_data
+ 1);
37 nilfs_direct_get_ptr(const struct nilfs_direct
*direct
, __u64 key
)
39 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct
) + key
));
42 static inline void nilfs_direct_set_ptr(struct nilfs_direct
*direct
,
45 *(nilfs_direct_dptrs(direct
) + key
) = nilfs_bmap_ptr_to_dptr(ptr
);
48 static int nilfs_direct_lookup(const struct nilfs_bmap
*bmap
,
49 __u64 key
, int level
, __u64
*ptrp
)
51 struct nilfs_direct
*direct
;
54 direct
= (struct nilfs_direct
*)bmap
; /* XXX: use macro for level 1 */
55 if (key
> NILFS_DIRECT_KEY_MAX
|| level
!= 1)
57 ptr
= nilfs_direct_get_ptr(direct
, key
);
58 if (ptr
== NILFS_BMAP_INVALID_PTR
)
66 static int nilfs_direct_lookup_contig(const struct nilfs_bmap
*bmap
,
67 __u64 key
, __u64
*ptrp
,
70 struct nilfs_direct
*direct
= (struct nilfs_direct
*)bmap
;
71 struct inode
*dat
= NULL
;
76 if (key
> NILFS_DIRECT_KEY_MAX
)
78 ptr
= nilfs_direct_get_ptr(direct
, key
);
79 if (ptr
== NILFS_BMAP_INVALID_PTR
)
82 if (NILFS_BMAP_USE_VBN(bmap
)) {
83 dat
= nilfs_bmap_get_dat(bmap
);
84 ret
= nilfs_dat_translate(dat
, ptr
, &blocknr
);
90 maxblocks
= min_t(unsigned, maxblocks
, NILFS_DIRECT_KEY_MAX
- key
+ 1);
91 for (cnt
= 1; cnt
< maxblocks
&&
92 (ptr2
= nilfs_direct_get_ptr(direct
, key
+ cnt
)) !=
93 NILFS_BMAP_INVALID_PTR
;
96 ret
= nilfs_dat_translate(dat
, ptr2
, &blocknr
);
101 if (ptr2
!= ptr
+ cnt
)
109 nilfs_direct_find_target_v(const struct nilfs_direct
*direct
, __u64 key
)
113 ptr
= nilfs_bmap_find_target_seq(&direct
->d_bmap
, key
);
114 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
115 /* sequential access */
119 return nilfs_bmap_find_target_in_group(&direct
->d_bmap
);
122 static void nilfs_direct_set_target_v(struct nilfs_direct
*direct
,
123 __u64 key
, __u64 ptr
)
125 direct
->d_bmap
.b_last_allocated_key
= key
;
126 direct
->d_bmap
.b_last_allocated_ptr
= ptr
;
129 static int nilfs_direct_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
131 struct nilfs_direct
*direct
= (struct nilfs_direct
*)bmap
;
132 union nilfs_bmap_ptr_req req
;
133 struct inode
*dat
= NULL
;
134 struct buffer_head
*bh
;
137 if (key
> NILFS_DIRECT_KEY_MAX
)
139 if (nilfs_direct_get_ptr(direct
, key
) != NILFS_BMAP_INVALID_PTR
)
142 if (NILFS_BMAP_USE_VBN(bmap
)) {
143 req
.bpr_ptr
= nilfs_direct_find_target_v(direct
, key
);
144 dat
= nilfs_bmap_get_dat(bmap
);
146 ret
= nilfs_bmap_prepare_alloc_ptr(bmap
, &req
, dat
);
148 /* ptr must be a pointer to a buffer head. */
149 bh
= (struct buffer_head
*)((unsigned long)ptr
);
150 set_buffer_nilfs_volatile(bh
);
152 nilfs_bmap_commit_alloc_ptr(bmap
, &req
, dat
);
153 nilfs_direct_set_ptr(direct
, key
, req
.bpr_ptr
);
155 if (!nilfs_bmap_dirty(bmap
))
156 nilfs_bmap_set_dirty(bmap
);
158 if (NILFS_BMAP_USE_VBN(bmap
))
159 nilfs_direct_set_target_v(direct
, key
, req
.bpr_ptr
);
161 nilfs_bmap_add_blocks(bmap
, 1);
166 static int nilfs_direct_delete(struct nilfs_bmap
*bmap
, __u64 key
)
168 struct nilfs_direct
*direct
= (struct nilfs_direct
*)bmap
;
169 union nilfs_bmap_ptr_req req
;
173 if (key
> NILFS_DIRECT_KEY_MAX
||
174 nilfs_direct_get_ptr(direct
, key
) == NILFS_BMAP_INVALID_PTR
)
177 dat
= NILFS_BMAP_USE_VBN(bmap
) ? nilfs_bmap_get_dat(bmap
) : NULL
;
178 req
.bpr_ptr
= nilfs_direct_get_ptr(direct
, key
);
180 ret
= nilfs_bmap_prepare_end_ptr(bmap
, &req
, dat
);
182 nilfs_bmap_commit_end_ptr(bmap
, &req
, dat
);
183 nilfs_direct_set_ptr(direct
, key
, NILFS_BMAP_INVALID_PTR
);
184 nilfs_bmap_sub_blocks(bmap
, 1);
189 static int nilfs_direct_last_key(const struct nilfs_bmap
*bmap
, __u64
*keyp
)
191 struct nilfs_direct
*direct
;
194 direct
= (struct nilfs_direct
*)bmap
;
195 lastkey
= NILFS_DIRECT_KEY_MAX
+ 1;
196 for (key
= NILFS_DIRECT_KEY_MIN
; key
<= NILFS_DIRECT_KEY_MAX
; key
++)
197 if (nilfs_direct_get_ptr(direct
, key
) !=
198 NILFS_BMAP_INVALID_PTR
)
201 if (lastkey
== NILFS_DIRECT_KEY_MAX
+ 1)
209 static int nilfs_direct_check_insert(const struct nilfs_bmap
*bmap
, __u64 key
)
211 return key
> NILFS_DIRECT_KEY_MAX
;
214 static int nilfs_direct_gather_data(struct nilfs_bmap
*bmap
,
215 __u64
*keys
, __u64
*ptrs
, int nitems
)
217 struct nilfs_direct
*direct
;
222 direct
= (struct nilfs_direct
*)bmap
;
223 if (nitems
> NILFS_DIRECT_NBLOCKS
)
224 nitems
= NILFS_DIRECT_NBLOCKS
;
226 for (key
= 0; key
< nitems
; key
++) {
227 ptr
= nilfs_direct_get_ptr(direct
, key
);
228 if (ptr
!= NILFS_BMAP_INVALID_PTR
) {
237 int nilfs_direct_delete_and_convert(struct nilfs_bmap
*bmap
,
238 __u64 key
, __u64
*keys
, __u64
*ptrs
, int n
)
240 struct nilfs_direct
*direct
;
244 /* no need to allocate any resource for conversion */
247 ret
= bmap
->b_ops
->bop_delete(bmap
, key
);
252 if (bmap
->b_ops
->bop_clear
!= NULL
)
253 bmap
->b_ops
->bop_clear(bmap
);
256 direct
= (struct nilfs_direct
*)bmap
;
257 dptrs
= nilfs_direct_dptrs(direct
);
258 for (i
= 0, j
= 0; i
< NILFS_DIRECT_NBLOCKS
; i
++) {
259 if ((j
< n
) && (i
== keys
[j
])) {
260 dptrs
[i
] = (i
!= key
) ?
261 nilfs_bmap_ptr_to_dptr(ptrs
[j
]) :
262 NILFS_BMAP_INVALID_PTR
;
265 dptrs
[i
] = NILFS_BMAP_INVALID_PTR
;
268 nilfs_direct_init(bmap
);
272 static int nilfs_direct_propagate(const struct nilfs_bmap
*bmap
,
273 struct buffer_head
*bh
)
275 struct nilfs_direct
*direct
= (struct nilfs_direct
*)bmap
;
276 struct nilfs_palloc_req oldreq
, newreq
;
282 if (!NILFS_BMAP_USE_VBN(bmap
))
285 dat
= nilfs_bmap_get_dat(bmap
);
286 key
= nilfs_bmap_data_get_key(bmap
, bh
);
287 ptr
= nilfs_direct_get_ptr(direct
, key
);
288 if (!buffer_nilfs_volatile(bh
)) {
289 oldreq
.pr_entry_nr
= ptr
;
290 newreq
.pr_entry_nr
= ptr
;
291 ret
= nilfs_dat_prepare_update(dat
, &oldreq
, &newreq
);
294 nilfs_dat_commit_update(dat
, &oldreq
, &newreq
,
295 bmap
->b_ptr_type
== NILFS_BMAP_PTR_VS
);
296 set_buffer_nilfs_volatile(bh
);
297 nilfs_direct_set_ptr(direct
, key
, newreq
.pr_entry_nr
);
299 ret
= nilfs_dat_mark_dirty(dat
, ptr
);
304 static int nilfs_direct_assign_v(struct nilfs_direct
*direct
,
305 __u64 key
, __u64 ptr
,
306 struct buffer_head
**bh
,
308 union nilfs_binfo
*binfo
)
310 struct inode
*dat
= nilfs_bmap_get_dat(&direct
->d_bmap
);
311 union nilfs_bmap_ptr_req req
;
315 ret
= nilfs_dat_prepare_start(dat
, &req
.bpr_req
);
317 nilfs_dat_commit_start(dat
, &req
.bpr_req
, blocknr
);
318 binfo
->bi_v
.bi_vblocknr
= nilfs_bmap_ptr_to_dptr(ptr
);
319 binfo
->bi_v
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
324 static int nilfs_direct_assign_p(struct nilfs_direct
*direct
,
325 __u64 key
, __u64 ptr
,
326 struct buffer_head
**bh
,
328 union nilfs_binfo
*binfo
)
330 nilfs_direct_set_ptr(direct
, key
, blocknr
);
332 binfo
->bi_dat
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
333 binfo
->bi_dat
.bi_level
= 0;
338 static int nilfs_direct_assign(struct nilfs_bmap
*bmap
,
339 struct buffer_head
**bh
,
341 union nilfs_binfo
*binfo
)
343 struct nilfs_direct
*direct
;
347 direct
= (struct nilfs_direct
*)bmap
;
348 key
= nilfs_bmap_data_get_key(bmap
, *bh
);
349 if (unlikely(key
> NILFS_DIRECT_KEY_MAX
)) {
350 printk(KERN_CRIT
"%s: invalid key: %llu\n", __func__
,
351 (unsigned long long)key
);
354 ptr
= nilfs_direct_get_ptr(direct
, key
);
355 if (unlikely(ptr
== NILFS_BMAP_INVALID_PTR
)) {
356 printk(KERN_CRIT
"%s: invalid pointer: %llu\n", __func__
,
357 (unsigned long long)ptr
);
361 return NILFS_BMAP_USE_VBN(bmap
) ?
362 nilfs_direct_assign_v(direct
, key
, ptr
, bh
, blocknr
, binfo
) :
363 nilfs_direct_assign_p(direct
, key
, ptr
, bh
, blocknr
, binfo
);
366 static const struct nilfs_bmap_operations nilfs_direct_ops
= {
367 .bop_lookup
= nilfs_direct_lookup
,
368 .bop_lookup_contig
= nilfs_direct_lookup_contig
,
369 .bop_insert
= nilfs_direct_insert
,
370 .bop_delete
= nilfs_direct_delete
,
373 .bop_propagate
= nilfs_direct_propagate
,
375 .bop_lookup_dirty_buffers
= NULL
,
377 .bop_assign
= nilfs_direct_assign
,
380 .bop_last_key
= nilfs_direct_last_key
,
381 .bop_check_insert
= nilfs_direct_check_insert
,
382 .bop_check_delete
= NULL
,
383 .bop_gather_data
= nilfs_direct_gather_data
,
387 int nilfs_direct_init(struct nilfs_bmap
*bmap
)
389 bmap
->b_ops
= &nilfs_direct_ops
;