direct.c 8.9 KB
Newer Older
K
Koji Sato 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * direct.c - NILFS direct block pointer.
 *
 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Written by Koji Sato <koji@osrg.net>.
 */

#include <linux/errno.h>
#include "nilfs.h"
#include "page.h"
#include "direct.h"
#include "alloc.h"
28
#include "dat.h"
K
Koji Sato 已提交
29

30
static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
K
Koji Sato 已提交
31 32
{
	return (__le64 *)
33
		((struct nilfs_direct_node *)direct->b_u.u_data + 1);
K
Koji Sato 已提交
34 35 36
}

static inline __u64
37
nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
K
Koji Sato 已提交
38
{
39
	return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
K
Koji Sato 已提交
40 41
}

42
static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
K
Koji Sato 已提交
43 44
					__u64 key, __u64 ptr)
{
45
	*(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
K
Koji Sato 已提交
46 47
}

48
static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
K
Koji Sato 已提交
49 50 51 52
			       __u64 key, int level, __u64 *ptrp)
{
	__u64 ptr;

J
Jiro SEKIBA 已提交
53 54 55 56
	if (key > NILFS_DIRECT_KEY_MAX || level != 1)
		return -ENOENT;
	ptr = nilfs_direct_get_ptr(direct, key);
	if (ptr == NILFS_BMAP_INVALID_PTR)
K
Koji Sato 已提交
57 58
		return -ENOENT;

59
	*ptrp = ptr;
K
Koji Sato 已提交
60 61 62
	return 0;
}

63
static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
64 65 66 67 68 69 70 71
				      __u64 key, __u64 *ptrp,
				      unsigned maxblocks)
{
	struct inode *dat = NULL;
	__u64 ptr, ptr2;
	sector_t blocknr;
	int ret, cnt;

J
Jiro SEKIBA 已提交
72 73 74 75
	if (key > NILFS_DIRECT_KEY_MAX)
		return -ENOENT;
	ptr = nilfs_direct_get_ptr(direct, key);
	if (ptr == NILFS_BMAP_INVALID_PTR)
76 77
		return -ENOENT;

78 79
	if (NILFS_BMAP_USE_VBN(direct)) {
		dat = nilfs_bmap_get_dat(direct);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		ret = nilfs_dat_translate(dat, ptr, &blocknr);
		if (ret < 0)
			return ret;
		ptr = blocknr;
	}

	maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
	for (cnt = 1; cnt < maxblocks &&
		     (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
		     NILFS_BMAP_INVALID_PTR;
	     cnt++) {
		if (dat) {
			ret = nilfs_dat_translate(dat, ptr2, &blocknr);
			if (ret < 0)
				return ret;
			ptr2 = blocknr;
		}
		if (ptr2 != ptr + cnt)
			break;
	}
	*ptrp = ptr;
	return cnt;
}

K
Koji Sato 已提交
104
static __u64
105
nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
K
Koji Sato 已提交
106 107 108
{
	__u64 ptr;

109
	ptr = nilfs_bmap_find_target_seq(direct, key);
K
Koji Sato 已提交
110 111 112 113 114
	if (ptr != NILFS_BMAP_INVALID_PTR)
		/* sequential access */
		return ptr;
	else
		/* block group */
115
		return nilfs_bmap_find_target_in_group(direct);
K
Koji Sato 已提交
116 117 118 119 120
}

static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
{
	union nilfs_bmap_ptr_req req;
121 122
	struct inode *dat = NULL;
	struct buffer_head *bh;
K
Koji Sato 已提交
123 124 125 126
	int ret;

	if (key > NILFS_DIRECT_KEY_MAX)
		return -ENOENT;
127
	if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
K
Koji Sato 已提交
128 129
		return -EEXIST;

130
	if (NILFS_BMAP_USE_VBN(bmap)) {
131
		req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
132 133 134 135 136 137 138
		dat = nilfs_bmap_get_dat(bmap);
	}
	ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
	if (!ret) {
		/* ptr must be a pointer to a buffer head. */
		bh = (struct buffer_head *)((unsigned long)ptr);
		set_buffer_nilfs_volatile(bh);
K
Koji Sato 已提交
139

140
		nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
141
		nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
K
Koji Sato 已提交
142

143 144
		if (!nilfs_bmap_dirty(bmap))
			nilfs_bmap_set_dirty(bmap);
K
Koji Sato 已提交
145

146
		if (NILFS_BMAP_USE_VBN(bmap))
147
			nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
K
Koji Sato 已提交
148

149 150 151
		nilfs_bmap_add_blocks(bmap, 1);
	}
	return ret;
K
Koji Sato 已提交
152 153 154 155 156
}

static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
{
	union nilfs_bmap_ptr_req req;
157
	struct inode *dat;
K
Koji Sato 已提交
158 159
	int ret;

160
	if (key > NILFS_DIRECT_KEY_MAX ||
161
	    nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
K
Koji Sato 已提交
162 163
		return -ENOENT;

164
	dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
165
	req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
K
Koji Sato 已提交
166

167 168 169
	ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
	if (!ret) {
		nilfs_bmap_commit_end_ptr(bmap, &req, dat);
170
		nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
171 172 173
		nilfs_bmap_sub_blocks(bmap, 1);
	}
	return ret;
K
Koji Sato 已提交
174 175
}

176
static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
K
Koji Sato 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
	__u64 key, lastkey;

	lastkey = NILFS_DIRECT_KEY_MAX + 1;
	for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
		if (nilfs_direct_get_ptr(direct, key) !=
		    NILFS_BMAP_INVALID_PTR)
			lastkey = key;

	if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
		return -ENOENT;

	*keyp = lastkey;

	return 0;
}

static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
{
	return key > NILFS_DIRECT_KEY_MAX;
}

199
static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
K
Koji Sato 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
				    __u64 *keys, __u64 *ptrs, int nitems)
{
	__u64 key;
	__u64 ptr;
	int n;

	if (nitems > NILFS_DIRECT_NBLOCKS)
		nitems = NILFS_DIRECT_NBLOCKS;
	n = 0;
	for (key = 0; key < nitems; key++) {
		ptr = nilfs_direct_get_ptr(direct, key);
		if (ptr != NILFS_BMAP_INVALID_PTR) {
			keys[n] = key;
			ptrs[n] = ptr;
			n++;
		}
	}
	return n;
}

int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
221
				    __u64 key, __u64 *keys, __u64 *ptrs, int n)
K
Koji Sato 已提交
222 223 224 225 226 227 228
{
	__le64 *dptrs;
	int ret, i, j;

	/* no need to allocate any resource for conversion */

	/* delete */
229
	ret = bmap->b_ops->bop_delete(bmap, key);
K
Koji Sato 已提交
230 231 232 233 234
	if (ret < 0)
		return ret;

	/* free resources */
	if (bmap->b_ops->bop_clear != NULL)
235
		bmap->b_ops->bop_clear(bmap);
K
Koji Sato 已提交
236 237

	/* convert */
238
	dptrs = nilfs_direct_dptrs(bmap);
K
Koji Sato 已提交
239 240 241
	for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
		if ((j < n) && (i == keys[j])) {
			dptrs[i] = (i != key) ?
242
				cpu_to_le64(ptrs[j]) :
K
Koji Sato 已提交
243 244 245 246 247 248
				NILFS_BMAP_INVALID_PTR;
			j++;
		} else
			dptrs[i] = NILFS_BMAP_INVALID_PTR;
	}

249
	nilfs_direct_init(bmap);
K
Koji Sato 已提交
250 251 252
	return 0;
}

253
static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
254
				  struct buffer_head *bh)
K
Koji Sato 已提交
255
{
256 257
	struct nilfs_palloc_req oldreq, newreq;
	struct inode *dat;
K
Koji Sato 已提交
258 259 260 261
	__u64 key;
	__u64 ptr;
	int ret;

262 263 264 265 266
	if (!NILFS_BMAP_USE_VBN(bmap))
		return 0;

	dat = nilfs_bmap_get_dat(bmap);
	key = nilfs_bmap_data_get_key(bmap, bh);
267
	ptr = nilfs_direct_get_ptr(bmap, key);
K
Koji Sato 已提交
268
	if (!buffer_nilfs_volatile(bh)) {
269 270 271
		oldreq.pr_entry_nr = ptr;
		newreq.pr_entry_nr = ptr;
		ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
K
Koji Sato 已提交
272 273
		if (ret < 0)
			return ret;
274 275
		nilfs_dat_commit_update(dat, &oldreq, &newreq,
					bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
K
Koji Sato 已提交
276
		set_buffer_nilfs_volatile(bh);
277
		nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
K
Koji Sato 已提交
278
	} else
279
		ret = nilfs_dat_mark_dirty(dat, ptr);
K
Koji Sato 已提交
280 281 282 283

	return ret;
}

284
static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
K
Koji Sato 已提交
285 286 287 288 289
				 __u64 key, __u64 ptr,
				 struct buffer_head **bh,
				 sector_t blocknr,
				 union nilfs_binfo *binfo)
{
290
	struct inode *dat = nilfs_bmap_get_dat(direct);
K
Koji Sato 已提交
291 292 293 294
	union nilfs_bmap_ptr_req req;
	int ret;

	req.bpr_ptr = ptr;
295 296 297
	ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
	if (!ret) {
		nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
298 299
		binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
		binfo->bi_v.bi_blkoff = cpu_to_le64(key);
300 301
	}
	return ret;
K
Koji Sato 已提交
302 303
}

304
static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
K
Koji Sato 已提交
305 306 307 308 309 310 311
				 __u64 key, __u64 ptr,
				 struct buffer_head **bh,
				 sector_t blocknr,
				 union nilfs_binfo *binfo)
{
	nilfs_direct_set_ptr(direct, key, blocknr);

312
	binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
	binfo->bi_dat.bi_level = 0;

	return 0;
}

static int nilfs_direct_assign(struct nilfs_bmap *bmap,
			       struct buffer_head **bh,
			       sector_t blocknr,
			       union nilfs_binfo *binfo)
{
	__u64 key;
	__u64 ptr;

	key = nilfs_bmap_data_get_key(bmap, *bh);
327 328 329 330 331
	if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
		printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
		       (unsigned long long)key);
		return -EINVAL;
	}
332
	ptr = nilfs_direct_get_ptr(bmap, key);
333 334 335 336 337
	if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
		printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
		       (unsigned long long)ptr);
		return -EINVAL;
	}
K
Koji Sato 已提交
338

339
	return NILFS_BMAP_USE_VBN(bmap) ?
340 341
		nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
		nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
K
Koji Sato 已提交
342 343 344 345
}

static const struct nilfs_bmap_operations nilfs_direct_ops = {
	.bop_lookup		=	nilfs_direct_lookup,
346
	.bop_lookup_contig	=	nilfs_direct_lookup_contig,
K
Koji Sato 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	.bop_insert		=	nilfs_direct_insert,
	.bop_delete		=	nilfs_direct_delete,
	.bop_clear		=	NULL,

	.bop_propagate		=	nilfs_direct_propagate,

	.bop_lookup_dirty_buffers	=	NULL,

	.bop_assign		=	nilfs_direct_assign,
	.bop_mark		=	NULL,

	.bop_last_key		=	nilfs_direct_last_key,
	.bop_check_insert	=	nilfs_direct_check_insert,
	.bop_check_delete	=	NULL,
	.bop_gather_data	=	nilfs_direct_gather_data,
};


365
int nilfs_direct_init(struct nilfs_bmap *bmap)
K
Koji Sato 已提交
366 367 368 369
{
	bmap->b_ops = &nilfs_direct_ops;
	return 0;
}