blob: 8e8095c2e1bb3c24912236abe36fb627d9b0c86b [file] [log] [blame]
Koji Sato36a580e2009-04-06 19:01:25 -07001/*
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 *
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
19 *
20 * Written by Koji Sato <koji@osrg.net>.
21 */
22
23#include <linux/errno.h>
24#include "nilfs.h"
25#include "page.h"
26#include "direct.h"
27#include "alloc.h"
28
29static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
30{
31 return (__le64 *)
32 ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
33}
34
35static inline __u64
36nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
37{
38 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
39}
40
41static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
42 __u64 key, __u64 ptr)
43{
44 *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
45}
46
47static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
48 __u64 key, int level, __u64 *ptrp)
49{
50 struct nilfs_direct *direct;
51 __u64 ptr;
52
53 direct = (struct nilfs_direct *)bmap;
54 if ((key > NILFS_DIRECT_KEY_MAX) ||
55 (level != 1) || /* XXX: use macro for level 1 */
56 ((ptr = nilfs_direct_get_ptr(direct, key)) ==
57 NILFS_BMAP_INVALID_PTR))
58 return -ENOENT;
59
60 if (ptrp != NULL)
61 *ptrp = ptr;
62 return 0;
63}
64
65static __u64
66nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
67{
68 __u64 ptr;
69
70 ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
71 if (ptr != NILFS_BMAP_INVALID_PTR)
72 /* sequential access */
73 return ptr;
74 else
75 /* block group */
76 return nilfs_bmap_find_target_in_group(&direct->d_bmap);
77}
78
79static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
80 __u64 key, __u64 ptr)
81{
82 direct->d_bmap.b_last_allocated_key = key;
83 direct->d_bmap.b_last_allocated_ptr = ptr;
84}
85
86static int nilfs_direct_prepare_insert(struct nilfs_direct *direct,
87 __u64 key,
88 union nilfs_bmap_ptr_req *req,
89 struct nilfs_bmap_stats *stats)
90{
91 int ret;
92
93 if (direct->d_ops->dop_find_target != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -070094 req->bpr_ptr = direct->d_ops->dop_find_target(direct, key);
95 ret = direct->d_bmap.b_pops->bpop_prepare_alloc_ptr(&direct->d_bmap,
Koji Sato36a580e2009-04-06 19:01:25 -070096 req);
97 if (ret < 0)
98 return ret;
99
100 stats->bs_nblocks = 1;
101 return 0;
102}
103
104static void nilfs_direct_commit_insert(struct nilfs_direct *direct,
105 union nilfs_bmap_ptr_req *req,
106 __u64 key, __u64 ptr)
107{
108 struct buffer_head *bh;
109
110 /* ptr must be a pointer to a buffer head. */
111 bh = (struct buffer_head *)((unsigned long)ptr);
112 set_buffer_nilfs_volatile(bh);
113
Ryusuke Konishie473c1f2009-05-22 02:18:36 +0900114 direct->d_bmap.b_pops->bpop_commit_alloc_ptr(&direct->d_bmap, req);
Koji Sato36a580e2009-04-06 19:01:25 -0700115 nilfs_direct_set_ptr(direct, key, req->bpr_ptr);
116
117 if (!nilfs_bmap_dirty(&direct->d_bmap))
118 nilfs_bmap_set_dirty(&direct->d_bmap);
119
120 if (direct->d_ops->dop_set_target != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700121 direct->d_ops->dop_set_target(direct, key, req->bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700122}
123
124static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
125{
126 struct nilfs_direct *direct;
127 union nilfs_bmap_ptr_req req;
128 struct nilfs_bmap_stats stats;
129 int ret;
130
131 direct = (struct nilfs_direct *)bmap;
132 if (key > NILFS_DIRECT_KEY_MAX)
133 return -ENOENT;
134 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
135 return -EEXIST;
136
137 ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
138 if (ret < 0)
139 return ret;
140 nilfs_direct_commit_insert(direct, &req, key, ptr);
141 nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
142
143 return 0;
144}
145
146static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
147 union nilfs_bmap_ptr_req *req,
148 __u64 key,
149 struct nilfs_bmap_stats *stats)
150{
151 int ret;
152
153 if (direct->d_bmap.b_pops->bpop_prepare_end_ptr != NULL) {
154 req->bpr_ptr = nilfs_direct_get_ptr(direct, key);
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700155 ret = direct->d_bmap.b_pops->bpop_prepare_end_ptr(
Koji Sato36a580e2009-04-06 19:01:25 -0700156 &direct->d_bmap, req);
157 if (ret < 0)
158 return ret;
159 }
160
161 stats->bs_nblocks = 1;
162 return 0;
163}
164
165static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
166 union nilfs_bmap_ptr_req *req,
167 __u64 key)
168{
169 if (direct->d_bmap.b_pops->bpop_commit_end_ptr != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700170 direct->d_bmap.b_pops->bpop_commit_end_ptr(
Koji Sato36a580e2009-04-06 19:01:25 -0700171 &direct->d_bmap, req);
172 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
173}
174
175static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
176{
177 struct nilfs_direct *direct;
178 union nilfs_bmap_ptr_req req;
179 struct nilfs_bmap_stats stats;
180 int ret;
181
182 direct = (struct nilfs_direct *)bmap;
183 if ((key > NILFS_DIRECT_KEY_MAX) ||
184 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
185 return -ENOENT;
186
187 ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
188 if (ret < 0)
189 return ret;
190 nilfs_direct_commit_delete(direct, &req, key);
191 nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
192
193 return 0;
194}
195
196static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
197{
198 struct nilfs_direct *direct;
199 __u64 key, lastkey;
200
201 direct = (struct nilfs_direct *)bmap;
202 lastkey = NILFS_DIRECT_KEY_MAX + 1;
203 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
204 if (nilfs_direct_get_ptr(direct, key) !=
205 NILFS_BMAP_INVALID_PTR)
206 lastkey = key;
207
208 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
209 return -ENOENT;
210
Koji Sato36a580e2009-04-06 19:01:25 -0700211 *keyp = lastkey;
212
213 return 0;
214}
215
216static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
217{
218 return key > NILFS_DIRECT_KEY_MAX;
219}
220
221static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
222 __u64 *keys, __u64 *ptrs, int nitems)
223{
224 struct nilfs_direct *direct;
225 __u64 key;
226 __u64 ptr;
227 int n;
228
229 direct = (struct nilfs_direct *)bmap;
230 if (nitems > NILFS_DIRECT_NBLOCKS)
231 nitems = NILFS_DIRECT_NBLOCKS;
232 n = 0;
233 for (key = 0; key < nitems; key++) {
234 ptr = nilfs_direct_get_ptr(direct, key);
235 if (ptr != NILFS_BMAP_INVALID_PTR) {
236 keys[n] = key;
237 ptrs[n] = ptr;
238 n++;
239 }
240 }
241 return n;
242}
243
244int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900245 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700246{
247 struct nilfs_direct *direct;
248 __le64 *dptrs;
249 int ret, i, j;
250
251 /* no need to allocate any resource for conversion */
252
253 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700254 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700255 if (ret < 0)
256 return ret;
257
258 /* free resources */
259 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700260 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700261
262 /* convert */
263 direct = (struct nilfs_direct *)bmap;
264 dptrs = nilfs_direct_dptrs(direct);
265 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
266 if ((j < n) && (i == keys[j])) {
267 dptrs[i] = (i != key) ?
268 nilfs_bmap_ptr_to_dptr(ptrs[j]) :
269 NILFS_BMAP_INVALID_PTR;
270 j++;
271 } else
272 dptrs[i] = NILFS_BMAP_INVALID_PTR;
273 }
274
Ryusuke Konishi30333422009-05-24 00:09:44 +0900275 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700276 return 0;
277}
278
279static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
280 struct buffer_head *bh)
281{
282 union nilfs_bmap_ptr_req oldreq, newreq;
283 __u64 key;
284 __u64 ptr;
285 int ret;
286
287 key = nilfs_bmap_data_get_key(&direct->d_bmap, bh);
288 ptr = nilfs_direct_get_ptr(direct, key);
289 if (!buffer_nilfs_volatile(bh)) {
290 oldreq.bpr_ptr = ptr;
291 newreq.bpr_ptr = ptr;
292 ret = nilfs_bmap_prepare_update(&direct->d_bmap, &oldreq,
293 &newreq);
294 if (ret < 0)
295 return ret;
296 nilfs_bmap_commit_update(&direct->d_bmap, &oldreq, &newreq);
297 set_buffer_nilfs_volatile(bh);
298 nilfs_direct_set_ptr(direct, key, newreq.bpr_ptr);
299 } else
300 ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
301
302 return ret;
303}
304
305static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
306 struct buffer_head *bh)
307{
308 struct nilfs_direct *direct;
309
310 direct = (struct nilfs_direct *)bmap;
311 return (direct->d_ops->dop_propagate != NULL) ?
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700312 direct->d_ops->dop_propagate(direct, bh) :
Koji Sato36a580e2009-04-06 19:01:25 -0700313 0;
314}
315
316static int nilfs_direct_assign_v(struct nilfs_direct *direct,
317 __u64 key, __u64 ptr,
318 struct buffer_head **bh,
319 sector_t blocknr,
320 union nilfs_binfo *binfo)
321{
322 union nilfs_bmap_ptr_req req;
323 int ret;
324
325 req.bpr_ptr = ptr;
Ryusuke Konishid97a51a2009-05-03 21:43:01 +0900326 ret = nilfs_bmap_start_v(&direct->d_bmap, &req, blocknr);
327 if (unlikely(ret < 0))
Koji Sato36a580e2009-04-06 19:01:25 -0700328 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700329
330 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
331 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
332
333 return 0;
334}
335
336static int nilfs_direct_assign_p(struct nilfs_direct *direct,
337 __u64 key, __u64 ptr,
338 struct buffer_head **bh,
339 sector_t blocknr,
340 union nilfs_binfo *binfo)
341{
342 nilfs_direct_set_ptr(direct, key, blocknr);
343
344 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
345 binfo->bi_dat.bi_level = 0;
346
347 return 0;
348}
349
350static int nilfs_direct_assign(struct nilfs_bmap *bmap,
351 struct buffer_head **bh,
352 sector_t blocknr,
353 union nilfs_binfo *binfo)
354{
355 struct nilfs_direct *direct;
356 __u64 key;
357 __u64 ptr;
358
359 direct = (struct nilfs_direct *)bmap;
360 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700361 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
362 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
363 (unsigned long long)key);
364 return -EINVAL;
365 }
Koji Sato36a580e2009-04-06 19:01:25 -0700366 ptr = nilfs_direct_get_ptr(direct, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700367 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
368 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
369 (unsigned long long)ptr);
370 return -EINVAL;
371 }
Koji Sato36a580e2009-04-06 19:01:25 -0700372
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700373 return direct->d_ops->dop_assign(direct, key, ptr, bh,
374 blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700375}
376
377static const struct nilfs_bmap_operations nilfs_direct_ops = {
378 .bop_lookup = nilfs_direct_lookup,
379 .bop_insert = nilfs_direct_insert,
380 .bop_delete = nilfs_direct_delete,
381 .bop_clear = NULL,
382
383 .bop_propagate = nilfs_direct_propagate,
384
385 .bop_lookup_dirty_buffers = NULL,
386
387 .bop_assign = nilfs_direct_assign,
388 .bop_mark = NULL,
389
390 .bop_last_key = nilfs_direct_last_key,
391 .bop_check_insert = nilfs_direct_check_insert,
392 .bop_check_delete = NULL,
393 .bop_gather_data = nilfs_direct_gather_data,
394};
395
396
397static const struct nilfs_direct_operations nilfs_direct_ops_v = {
398 .dop_find_target = nilfs_direct_find_target_v,
399 .dop_set_target = nilfs_direct_set_target_v,
400 .dop_propagate = nilfs_direct_propagate_v,
401 .dop_assign = nilfs_direct_assign_v,
402};
403
404static const struct nilfs_direct_operations nilfs_direct_ops_p = {
405 .dop_find_target = NULL,
406 .dop_set_target = NULL,
407 .dop_propagate = NULL,
408 .dop_assign = nilfs_direct_assign_p,
409};
410
Ryusuke Konishi30333422009-05-24 00:09:44 +0900411int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700412{
Ryusuke Konishi30333422009-05-24 00:09:44 +0900413 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
Koji Sato36a580e2009-04-06 19:01:25 -0700414
Koji Sato36a580e2009-04-06 19:01:25 -0700415 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700416 switch (bmap->b_inode->i_ino) {
417 case NILFS_DAT_INO:
418 direct->d_ops = &nilfs_direct_ops_p;
419 break;
420 default:
421 direct->d_ops = &nilfs_direct_ops_v;
422 break;
423 }
424
425 return 0;
426}