nilfs2: clean up old e-mail addresses
[deliverable/linux.git] / fs / nilfs2 / direct.c
CommitLineData
36a580eb
KS
1/*
2 * direct.c - NILFS direct block pointer.
3 *
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
5 *
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.
10 *
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.
15 *
4b420ab4 16 * Written by Koji Sato.
36a580eb
KS
17 */
18
19#include <linux/errno.h>
20#include "nilfs.h"
21#include "page.h"
22#include "direct.h"
23#include "alloc.h"
c3a7abf0 24#include "dat.h"
36a580eb 25
10ff885b 26static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
36a580eb
KS
27{
28 return (__le64 *)
10ff885b 29 ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
36a580eb
KS
30}
31
32static inline __u64
10ff885b 33nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
36a580eb 34{
25b8d7de 35 return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
36a580eb
KS
36}
37
10ff885b 38static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
36a580eb
KS
39 __u64 key, __u64 ptr)
40{
25b8d7de 41 *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
36a580eb
KS
42}
43
10ff885b 44static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
36a580eb
KS
45 __u64 key, int level, __u64 *ptrp)
46{
36a580eb
KS
47 __u64 ptr;
48
5ee58148
JS
49 if (key > NILFS_DIRECT_KEY_MAX || level != 1)
50 return -ENOENT;
51 ptr = nilfs_direct_get_ptr(direct, key);
52 if (ptr == NILFS_BMAP_INVALID_PTR)
36a580eb
KS
53 return -ENOENT;
54
364ec2d7 55 *ptrp = ptr;
36a580eb
KS
56 return 0;
57}
58
10ff885b 59static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
c3a7abf0
RK
60 __u64 key, __u64 *ptrp,
61 unsigned maxblocks)
62{
c3a7abf0
RK
63 struct inode *dat = NULL;
64 __u64 ptr, ptr2;
65 sector_t blocknr;
66 int ret, cnt;
67
5ee58148
JS
68 if (key > NILFS_DIRECT_KEY_MAX)
69 return -ENOENT;
70 ptr = nilfs_direct_get_ptr(direct, key);
71 if (ptr == NILFS_BMAP_INVALID_PTR)
c3a7abf0
RK
72 return -ENOENT;
73
10ff885b
RK
74 if (NILFS_BMAP_USE_VBN(direct)) {
75 dat = nilfs_bmap_get_dat(direct);
c3a7abf0
RK
76 ret = nilfs_dat_translate(dat, ptr, &blocknr);
77 if (ret < 0)
78 return ret;
79 ptr = blocknr;
80 }
81
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;
86 cnt++) {
87 if (dat) {
88 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
89 if (ret < 0)
90 return ret;
91 ptr2 = blocknr;
92 }
93 if (ptr2 != ptr + cnt)
94 break;
95 }
96 *ptrp = ptr;
97 return cnt;
98}
99
36a580eb 100static __u64
10ff885b 101nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
36a580eb
KS
102{
103 __u64 ptr;
104
10ff885b 105 ptr = nilfs_bmap_find_target_seq(direct, key);
36a580eb
KS
106 if (ptr != NILFS_BMAP_INVALID_PTR)
107 /* sequential access */
108 return ptr;
109 else
110 /* block group */
10ff885b 111 return nilfs_bmap_find_target_in_group(direct);
36a580eb
KS
112}
113
36a580eb
KS
114static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
115{
36a580eb 116 union nilfs_bmap_ptr_req req;
2e0c2c73
RK
117 struct inode *dat = NULL;
118 struct buffer_head *bh;
36a580eb
KS
119 int ret;
120
36a580eb
KS
121 if (key > NILFS_DIRECT_KEY_MAX)
122 return -ENOENT;
10ff885b 123 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
36a580eb
KS
124 return -EEXIST;
125
2e0c2c73 126 if (NILFS_BMAP_USE_VBN(bmap)) {
10ff885b 127 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
2e0c2c73
RK
128 dat = nilfs_bmap_get_dat(bmap);
129 }
130 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
131 if (!ret) {
132 /* ptr must be a pointer to a buffer head. */
133 bh = (struct buffer_head *)((unsigned long)ptr);
134 set_buffer_nilfs_volatile(bh);
36a580eb 135
2e0c2c73 136 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
10ff885b 137 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
36a580eb 138
2e0c2c73
RK
139 if (!nilfs_bmap_dirty(bmap))
140 nilfs_bmap_set_dirty(bmap);
36a580eb 141
2e0c2c73 142 if (NILFS_BMAP_USE_VBN(bmap))
dc935be2 143 nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
36a580eb 144
be667377 145 nilfs_inode_add_blocks(bmap->b_inode, 1);
2e0c2c73
RK
146 }
147 return ret;
36a580eb
KS
148}
149
150static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
151{
36a580eb 152 union nilfs_bmap_ptr_req req;
2e0c2c73 153 struct inode *dat;
36a580eb
KS
154 int ret;
155
2e0c2c73 156 if (key > NILFS_DIRECT_KEY_MAX ||
10ff885b 157 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
36a580eb
KS
158 return -ENOENT;
159
2e0c2c73 160 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
10ff885b 161 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
36a580eb 162
2e0c2c73
RK
163 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
164 if (!ret) {
165 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
10ff885b 166 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
be667377 167 nilfs_inode_sub_blocks(bmap->b_inode, 1);
2e0c2c73
RK
168 }
169 return ret;
36a580eb
KS
170}
171
5b20384f
RK
172static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start,
173 __u64 *keyp)
174{
175 __u64 key;
176
177 for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) {
178 if (nilfs_direct_get_ptr(direct, key) !=
179 NILFS_BMAP_INVALID_PTR) {
180 *keyp = key;
181 return 0;
182 }
183 }
184 return -ENOENT;
185}
186
10ff885b 187static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
36a580eb 188{
36a580eb
KS
189 __u64 key, lastkey;
190
36a580eb
KS
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)
195 lastkey = key;
196
197 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
198 return -ENOENT;
199
36a580eb
KS
200 *keyp = lastkey;
201
202 return 0;
203}
204
205static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
206{
207 return key > NILFS_DIRECT_KEY_MAX;
208}
209
10ff885b 210static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
36a580eb
KS
211 __u64 *keys, __u64 *ptrs, int nitems)
212{
36a580eb
KS
213 __u64 key;
214 __u64 ptr;
215 int n;
216
36a580eb
KS
217 if (nitems > NILFS_DIRECT_NBLOCKS)
218 nitems = NILFS_DIRECT_NBLOCKS;
219 n = 0;
220 for (key = 0; key < nitems; key++) {
221 ptr = nilfs_direct_get_ptr(direct, key);
222 if (ptr != NILFS_BMAP_INVALID_PTR) {
223 keys[n] = key;
224 ptrs[n] = ptr;
225 n++;
226 }
227 }
228 return n;
229}
230
231int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
3033342a 232 __u64 key, __u64 *keys, __u64 *ptrs, int n)
36a580eb 233{
36a580eb
KS
234 __le64 *dptrs;
235 int ret, i, j;
236
237 /* no need to allocate any resource for conversion */
238
239 /* delete */
8acfbf09 240 ret = bmap->b_ops->bop_delete(bmap, key);
36a580eb
KS
241 if (ret < 0)
242 return ret;
243
244 /* free resources */
245 if (bmap->b_ops->bop_clear != NULL)
8acfbf09 246 bmap->b_ops->bop_clear(bmap);
36a580eb
KS
247
248 /* convert */
10ff885b 249 dptrs = nilfs_direct_dptrs(bmap);
36a580eb
KS
250 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
251 if ((j < n) && (i == keys[j])) {
252 dptrs[i] = (i != key) ?
25b8d7de 253 cpu_to_le64(ptrs[j]) :
36a580eb
KS
254 NILFS_BMAP_INVALID_PTR;
255 j++;
256 } else
257 dptrs[i] = NILFS_BMAP_INVALID_PTR;
258 }
259
3033342a 260 nilfs_direct_init(bmap);
36a580eb
KS
261 return 0;
262}
263
583ada47 264static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
2e0c2c73 265 struct buffer_head *bh)
36a580eb 266{
2e0c2c73
RK
267 struct nilfs_palloc_req oldreq, newreq;
268 struct inode *dat;
36a580eb
KS
269 __u64 key;
270 __u64 ptr;
271 int ret;
272
2e0c2c73
RK
273 if (!NILFS_BMAP_USE_VBN(bmap))
274 return 0;
275
276 dat = nilfs_bmap_get_dat(bmap);
277 key = nilfs_bmap_data_get_key(bmap, bh);
10ff885b 278 ptr = nilfs_direct_get_ptr(bmap, key);
36a580eb 279 if (!buffer_nilfs_volatile(bh)) {
2e0c2c73
RK
280 oldreq.pr_entry_nr = ptr;
281 newreq.pr_entry_nr = ptr;
282 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
36a580eb
KS
283 if (ret < 0)
284 return ret;
2e0c2c73
RK
285 nilfs_dat_commit_update(dat, &oldreq, &newreq,
286 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
36a580eb 287 set_buffer_nilfs_volatile(bh);
10ff885b 288 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
36a580eb 289 } else
2e0c2c73 290 ret = nilfs_dat_mark_dirty(dat, ptr);
36a580eb
KS
291
292 return ret;
293}
294
10ff885b 295static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
36a580eb
KS
296 __u64 key, __u64 ptr,
297 struct buffer_head **bh,
298 sector_t blocknr,
299 union nilfs_binfo *binfo)
300{
10ff885b 301 struct inode *dat = nilfs_bmap_get_dat(direct);
36a580eb
KS
302 union nilfs_bmap_ptr_req req;
303 int ret;
304
305 req.bpr_ptr = ptr;
2e0c2c73
RK
306 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
307 if (!ret) {
308 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
25b8d7de
RK
309 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
310 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
2e0c2c73
RK
311 }
312 return ret;
36a580eb
KS
313}
314
10ff885b 315static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
36a580eb
KS
316 __u64 key, __u64 ptr,
317 struct buffer_head **bh,
318 sector_t blocknr,
319 union nilfs_binfo *binfo)
320{
321 nilfs_direct_set_ptr(direct, key, blocknr);
322
25b8d7de 323 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
36a580eb
KS
324 binfo->bi_dat.bi_level = 0;
325
326 return 0;
327}
328
329static int nilfs_direct_assign(struct nilfs_bmap *bmap,
330 struct buffer_head **bh,
331 sector_t blocknr,
332 union nilfs_binfo *binfo)
333{
36a580eb
KS
334 __u64 key;
335 __u64 ptr;
336
36a580eb 337 key = nilfs_bmap_data_get_key(bmap, *bh);
1f5abe7e
RK
338 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
339 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
340 (unsigned long long)key);
341 return -EINVAL;
342 }
10ff885b 343 ptr = nilfs_direct_get_ptr(bmap, key);
1f5abe7e
RK
344 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
345 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
346 (unsigned long long)ptr);
347 return -EINVAL;
348 }
36a580eb 349
355c6b61 350 return NILFS_BMAP_USE_VBN(bmap) ?
10ff885b
RK
351 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
352 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
36a580eb
KS
353}
354
355static const struct nilfs_bmap_operations nilfs_direct_ops = {
356 .bop_lookup = nilfs_direct_lookup,
c3a7abf0 357 .bop_lookup_contig = nilfs_direct_lookup_contig,
36a580eb
KS
358 .bop_insert = nilfs_direct_insert,
359 .bop_delete = nilfs_direct_delete,
360 .bop_clear = NULL,
361
362 .bop_propagate = nilfs_direct_propagate,
363
364 .bop_lookup_dirty_buffers = NULL,
365
366 .bop_assign = nilfs_direct_assign,
367 .bop_mark = NULL,
368
5b20384f 369 .bop_seek_key = nilfs_direct_seek_key,
36a580eb 370 .bop_last_key = nilfs_direct_last_key,
5b20384f 371
36a580eb
KS
372 .bop_check_insert = nilfs_direct_check_insert,
373 .bop_check_delete = NULL,
374 .bop_gather_data = nilfs_direct_gather_data,
375};
376
377
3033342a 378int nilfs_direct_init(struct nilfs_bmap *bmap)
36a580eb 379{
36a580eb 380 bmap->b_ops = &nilfs_direct_ops;
36a580eb
KS
381 return 0;
382}
This page took 0.413396 seconds and 5 git commands to generate.