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_bmap *direct)
33 ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
37 nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
39 return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
42 static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
45 *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
48 static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
49 __u64 key, int level, __u64 *ptrp)
53 if (key > NILFS_DIRECT_KEY_MAX || level != 1)
55 ptr = nilfs_direct_get_ptr(direct, key);
56 if (ptr == NILFS_BMAP_INVALID_PTR)
64 static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
65 __u64 key, __u64 *ptrp,
68 struct inode *dat = NULL;
73 if (key > NILFS_DIRECT_KEY_MAX)
75 ptr = nilfs_direct_get_ptr(direct, key);
76 if (ptr == NILFS_BMAP_INVALID_PTR)
79 if (NILFS_BMAP_USE_VBN(direct)) {
80 dat = nilfs_bmap_get_dat(direct);
81 ret = nilfs_dat_translate(dat, ptr, &blocknr);
87 maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
88 for (cnt = 1; cnt < maxblocks &&
89 (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
90 NILFS_BMAP_INVALID_PTR;
93 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
98 if (ptr2 != ptr + cnt)
106 nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
110 ptr = nilfs_bmap_find_target_seq(direct, key);
111 if (ptr != NILFS_BMAP_INVALID_PTR)
112 /* sequential access */
116 return nilfs_bmap_find_target_in_group(direct);
119 static void nilfs_direct_set_target_v(struct nilfs_bmap *direct,
120 __u64 key, __u64 ptr)
122 direct->b_last_allocated_key = key;
123 direct->b_last_allocated_ptr = ptr;
126 static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
128 union nilfs_bmap_ptr_req req;
129 struct inode *dat = NULL;
130 struct buffer_head *bh;
133 if (key > NILFS_DIRECT_KEY_MAX)
135 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
138 if (NILFS_BMAP_USE_VBN(bmap)) {
139 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
140 dat = nilfs_bmap_get_dat(bmap);
142 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
144 /* ptr must be a pointer to a buffer head. */
145 bh = (struct buffer_head *)((unsigned long)ptr);
146 set_buffer_nilfs_volatile(bh);
148 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
149 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
151 if (!nilfs_bmap_dirty(bmap))
152 nilfs_bmap_set_dirty(bmap);
154 if (NILFS_BMAP_USE_VBN(bmap))
155 nilfs_direct_set_target_v(bmap, key, req.bpr_ptr);
157 nilfs_bmap_add_blocks(bmap, 1);
162 static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
164 union nilfs_bmap_ptr_req req;
168 if (key > NILFS_DIRECT_KEY_MAX ||
169 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
172 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
173 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
175 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
177 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
178 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
179 nilfs_bmap_sub_blocks(bmap, 1);
184 static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
188 lastkey = NILFS_DIRECT_KEY_MAX + 1;
189 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
190 if (nilfs_direct_get_ptr(direct, key) !=
191 NILFS_BMAP_INVALID_PTR)
194 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
202 static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
204 return key > NILFS_DIRECT_KEY_MAX;
207 static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
208 __u64 *keys, __u64 *ptrs, int nitems)
214 if (nitems > NILFS_DIRECT_NBLOCKS)
215 nitems = NILFS_DIRECT_NBLOCKS;
217 for (key = 0; key < nitems; key++) {
218 ptr = nilfs_direct_get_ptr(direct, key);
219 if (ptr != NILFS_BMAP_INVALID_PTR) {
228 int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
229 __u64 key, __u64 *keys, __u64 *ptrs, int n)
234 /* no need to allocate any resource for conversion */
237 ret = bmap->b_ops->bop_delete(bmap, key);
242 if (bmap->b_ops->bop_clear != NULL)
243 bmap->b_ops->bop_clear(bmap);
246 dptrs = nilfs_direct_dptrs(bmap);
247 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
248 if ((j < n) && (i == keys[j])) {
249 dptrs[i] = (i != key) ?
250 cpu_to_le64(ptrs[j]) :
251 NILFS_BMAP_INVALID_PTR;
254 dptrs[i] = NILFS_BMAP_INVALID_PTR;
257 nilfs_direct_init(bmap);
261 static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
262 struct buffer_head *bh)
264 struct nilfs_palloc_req oldreq, newreq;
270 if (!NILFS_BMAP_USE_VBN(bmap))
273 dat = nilfs_bmap_get_dat(bmap);
274 key = nilfs_bmap_data_get_key(bmap, bh);
275 ptr = nilfs_direct_get_ptr(bmap, key);
276 if (!buffer_nilfs_volatile(bh)) {
277 oldreq.pr_entry_nr = ptr;
278 newreq.pr_entry_nr = ptr;
279 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
282 nilfs_dat_commit_update(dat, &oldreq, &newreq,
283 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
284 set_buffer_nilfs_volatile(bh);
285 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
287 ret = nilfs_dat_mark_dirty(dat, ptr);
292 static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
293 __u64 key, __u64 ptr,
294 struct buffer_head **bh,
296 union nilfs_binfo *binfo)
298 struct inode *dat = nilfs_bmap_get_dat(direct);
299 union nilfs_bmap_ptr_req req;
303 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
305 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
306 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
307 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
312 static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
313 __u64 key, __u64 ptr,
314 struct buffer_head **bh,
316 union nilfs_binfo *binfo)
318 nilfs_direct_set_ptr(direct, key, blocknr);
320 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
321 binfo->bi_dat.bi_level = 0;
326 static int nilfs_direct_assign(struct nilfs_bmap *bmap,
327 struct buffer_head **bh,
329 union nilfs_binfo *binfo)
334 key = nilfs_bmap_data_get_key(bmap, *bh);
335 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
336 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
337 (unsigned long long)key);
340 ptr = nilfs_direct_get_ptr(bmap, key);
341 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
342 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
343 (unsigned long long)ptr);
347 return NILFS_BMAP_USE_VBN(bmap) ?
348 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
349 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
352 static const struct nilfs_bmap_operations nilfs_direct_ops = {
353 .bop_lookup = nilfs_direct_lookup,
354 .bop_lookup_contig = nilfs_direct_lookup_contig,
355 .bop_insert = nilfs_direct_insert,
356 .bop_delete = nilfs_direct_delete,
359 .bop_propagate = nilfs_direct_propagate,
361 .bop_lookup_dirty_buffers = NULL,
363 .bop_assign = nilfs_direct_assign,
366 .bop_last_key = nilfs_direct_last_key,
367 .bop_check_insert = nilfs_direct_check_insert,
368 .bop_check_delete = NULL,
369 .bop_gather_data = nilfs_direct_gather_data,
373 int nilfs_direct_init(struct nilfs_bmap *bmap)
375 bmap->b_ops = &nilfs_direct_ops;