blob: 2a546c8f784e0d2bcd85152679c7715f07d5eeb3 [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);
Ryusuke Konishid4b96152009-05-24 03:25:44 +090095 ret = nilfs_bmap_prepare_alloc_ptr(&direct->d_bmap, req);
Koji Sato36a580e2009-04-06 19:01:25 -070096 if (ret < 0)
97 return ret;
98
99 stats->bs_nblocks = 1;
100 return 0;
101}
102
103static void nilfs_direct_commit_insert(struct nilfs_direct *direct,
104 union nilfs_bmap_ptr_req *req,
105 __u64 key, __u64 ptr)
106{
107 struct buffer_head *bh;
108
109 /* ptr must be a pointer to a buffer head. */
110 bh = (struct buffer_head *)((unsigned long)ptr);
111 set_buffer_nilfs_volatile(bh);
112
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900113 nilfs_bmap_commit_alloc_ptr(&direct->d_bmap, req);
Koji Sato36a580e2009-04-06 19:01:25 -0700114 nilfs_direct_set_ptr(direct, key, req->bpr_ptr);
115
116 if (!nilfs_bmap_dirty(&direct->d_bmap))
117 nilfs_bmap_set_dirty(&direct->d_bmap);
118
119 if (direct->d_ops->dop_set_target != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700120 direct->d_ops->dop_set_target(direct, key, req->bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700121}
122
123static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
124{
125 struct nilfs_direct *direct;
126 union nilfs_bmap_ptr_req req;
127 struct nilfs_bmap_stats stats;
128 int ret;
129
130 direct = (struct nilfs_direct *)bmap;
131 if (key > NILFS_DIRECT_KEY_MAX)
132 return -ENOENT;
133 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
134 return -EEXIST;
135
136 ret = nilfs_direct_prepare_insert(direct, key, &req, &stats);
137 if (ret < 0)
138 return ret;
139 nilfs_direct_commit_insert(direct, &req, key, ptr);
140 nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
141
142 return 0;
143}
144
145static int nilfs_direct_prepare_delete(struct nilfs_direct *direct,
146 union nilfs_bmap_ptr_req *req,
147 __u64 key,
148 struct nilfs_bmap_stats *stats)
149{
150 int ret;
151
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900152 req->bpr_ptr = nilfs_direct_get_ptr(direct, key);
153 ret = nilfs_bmap_prepare_end_ptr(&direct->d_bmap, req);
154 if (!ret)
155 stats->bs_nblocks = 1;
156 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700157}
158
159static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
160 union nilfs_bmap_ptr_req *req,
161 __u64 key)
162{
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900163 nilfs_bmap_commit_end_ptr(&direct->d_bmap, req);
Koji Sato36a580e2009-04-06 19:01:25 -0700164 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
165}
166
167static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
168{
169 struct nilfs_direct *direct;
170 union nilfs_bmap_ptr_req req;
171 struct nilfs_bmap_stats stats;
172 int ret;
173
174 direct = (struct nilfs_direct *)bmap;
175 if ((key > NILFS_DIRECT_KEY_MAX) ||
176 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
177 return -ENOENT;
178
179 ret = nilfs_direct_prepare_delete(direct, &req, key, &stats);
180 if (ret < 0)
181 return ret;
182 nilfs_direct_commit_delete(direct, &req, key);
183 nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);
184
185 return 0;
186}
187
188static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
189{
190 struct nilfs_direct *direct;
191 __u64 key, lastkey;
192
193 direct = (struct nilfs_direct *)bmap;
194 lastkey = NILFS_DIRECT_KEY_MAX + 1;
195 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
196 if (nilfs_direct_get_ptr(direct, key) !=
197 NILFS_BMAP_INVALID_PTR)
198 lastkey = key;
199
200 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
201 return -ENOENT;
202
Koji Sato36a580e2009-04-06 19:01:25 -0700203 *keyp = lastkey;
204
205 return 0;
206}
207
208static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
209{
210 return key > NILFS_DIRECT_KEY_MAX;
211}
212
213static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
214 __u64 *keys, __u64 *ptrs, int nitems)
215{
216 struct nilfs_direct *direct;
217 __u64 key;
218 __u64 ptr;
219 int n;
220
221 direct = (struct nilfs_direct *)bmap;
222 if (nitems > NILFS_DIRECT_NBLOCKS)
223 nitems = NILFS_DIRECT_NBLOCKS;
224 n = 0;
225 for (key = 0; key < nitems; key++) {
226 ptr = nilfs_direct_get_ptr(direct, key);
227 if (ptr != NILFS_BMAP_INVALID_PTR) {
228 keys[n] = key;
229 ptrs[n] = ptr;
230 n++;
231 }
232 }
233 return n;
234}
235
236int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900237 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700238{
239 struct nilfs_direct *direct;
240 __le64 *dptrs;
241 int ret, i, j;
242
243 /* no need to allocate any resource for conversion */
244
245 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700246 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700247 if (ret < 0)
248 return ret;
249
250 /* free resources */
251 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700252 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700253
254 /* convert */
255 direct = (struct nilfs_direct *)bmap;
256 dptrs = nilfs_direct_dptrs(direct);
257 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
258 if ((j < n) && (i == keys[j])) {
259 dptrs[i] = (i != key) ?
260 nilfs_bmap_ptr_to_dptr(ptrs[j]) :
261 NILFS_BMAP_INVALID_PTR;
262 j++;
263 } else
264 dptrs[i] = NILFS_BMAP_INVALID_PTR;
265 }
266
Ryusuke Konishi30333422009-05-24 00:09:44 +0900267 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700268 return 0;
269}
270
271static int nilfs_direct_propagate_v(struct nilfs_direct *direct,
272 struct buffer_head *bh)
273{
274 union nilfs_bmap_ptr_req oldreq, newreq;
275 __u64 key;
276 __u64 ptr;
277 int ret;
278
279 key = nilfs_bmap_data_get_key(&direct->d_bmap, bh);
280 ptr = nilfs_direct_get_ptr(direct, key);
281 if (!buffer_nilfs_volatile(bh)) {
282 oldreq.bpr_ptr = ptr;
283 newreq.bpr_ptr = ptr;
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900284 ret = nilfs_bmap_prepare_update_v(&direct->d_bmap, &oldreq,
285 &newreq);
Koji Sato36a580e2009-04-06 19:01:25 -0700286 if (ret < 0)
287 return ret;
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900288 nilfs_bmap_commit_update_v(&direct->d_bmap, &oldreq, &newreq);
Koji Sato36a580e2009-04-06 19:01:25 -0700289 set_buffer_nilfs_volatile(bh);
290 nilfs_direct_set_ptr(direct, key, newreq.bpr_ptr);
291 } else
292 ret = nilfs_bmap_mark_dirty(&direct->d_bmap, ptr);
293
294 return ret;
295}
296
297static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
298 struct buffer_head *bh)
299{
300 struct nilfs_direct *direct;
301
302 direct = (struct nilfs_direct *)bmap;
303 return (direct->d_ops->dop_propagate != NULL) ?
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700304 direct->d_ops->dop_propagate(direct, bh) :
Koji Sato36a580e2009-04-06 19:01:25 -0700305 0;
306}
307
308static int nilfs_direct_assign_v(struct nilfs_direct *direct,
309 __u64 key, __u64 ptr,
310 struct buffer_head **bh,
311 sector_t blocknr,
312 union nilfs_binfo *binfo)
313{
314 union nilfs_bmap_ptr_req req;
315 int ret;
316
317 req.bpr_ptr = ptr;
Ryusuke Konishid97a51a2009-05-03 21:43:01 +0900318 ret = nilfs_bmap_start_v(&direct->d_bmap, &req, blocknr);
319 if (unlikely(ret < 0))
Koji Sato36a580e2009-04-06 19:01:25 -0700320 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700321
322 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
323 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
324
325 return 0;
326}
327
328static int nilfs_direct_assign_p(struct nilfs_direct *direct,
329 __u64 key, __u64 ptr,
330 struct buffer_head **bh,
331 sector_t blocknr,
332 union nilfs_binfo *binfo)
333{
334 nilfs_direct_set_ptr(direct, key, blocknr);
335
336 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
337 binfo->bi_dat.bi_level = 0;
338
339 return 0;
340}
341
342static int nilfs_direct_assign(struct nilfs_bmap *bmap,
343 struct buffer_head **bh,
344 sector_t blocknr,
345 union nilfs_binfo *binfo)
346{
347 struct nilfs_direct *direct;
348 __u64 key;
349 __u64 ptr;
350
351 direct = (struct nilfs_direct *)bmap;
352 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700353 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
354 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
355 (unsigned long long)key);
356 return -EINVAL;
357 }
Koji Sato36a580e2009-04-06 19:01:25 -0700358 ptr = nilfs_direct_get_ptr(direct, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700359 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
360 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
361 (unsigned long long)ptr);
362 return -EINVAL;
363 }
Koji Sato36a580e2009-04-06 19:01:25 -0700364
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700365 return direct->d_ops->dop_assign(direct, key, ptr, bh,
366 blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700367}
368
369static const struct nilfs_bmap_operations nilfs_direct_ops = {
370 .bop_lookup = nilfs_direct_lookup,
371 .bop_insert = nilfs_direct_insert,
372 .bop_delete = nilfs_direct_delete,
373 .bop_clear = NULL,
374
375 .bop_propagate = nilfs_direct_propagate,
376
377 .bop_lookup_dirty_buffers = NULL,
378
379 .bop_assign = nilfs_direct_assign,
380 .bop_mark = NULL,
381
382 .bop_last_key = nilfs_direct_last_key,
383 .bop_check_insert = nilfs_direct_check_insert,
384 .bop_check_delete = NULL,
385 .bop_gather_data = nilfs_direct_gather_data,
386};
387
388
389static const struct nilfs_direct_operations nilfs_direct_ops_v = {
390 .dop_find_target = nilfs_direct_find_target_v,
391 .dop_set_target = nilfs_direct_set_target_v,
392 .dop_propagate = nilfs_direct_propagate_v,
393 .dop_assign = nilfs_direct_assign_v,
394};
395
396static const struct nilfs_direct_operations nilfs_direct_ops_p = {
397 .dop_find_target = NULL,
398 .dop_set_target = NULL,
399 .dop_propagate = NULL,
400 .dop_assign = nilfs_direct_assign_p,
401};
402
Ryusuke Konishi30333422009-05-24 00:09:44 +0900403int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700404{
Ryusuke Konishi30333422009-05-24 00:09:44 +0900405 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
Koji Sato36a580e2009-04-06 19:01:25 -0700406
Koji Sato36a580e2009-04-06 19:01:25 -0700407 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700408 switch (bmap->b_inode->i_ino) {
409 case NILFS_DAT_INO:
410 direct->d_ops = &nilfs_direct_ops_p;
411 break;
412 default:
413 direct->d_ops = &nilfs_direct_ops_v;
414 break;
415 }
416
417 return 0;
418}