blob: cfc7218914d6b8a36c7919a36227fa298f99a671 [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"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090028#include "dat.h"
Koji Sato36a580e2009-04-06 19:01:25 -070029
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090030static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
Koji Sato36a580e2009-04-06 19:01:25 -070031{
32 return (__le64 *)
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090033 ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
Koji Sato36a580e2009-04-06 19:01:25 -070034}
35
36static inline __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090037nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -070038{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090039 return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
Koji Sato36a580e2009-04-06 19:01:25 -070040}
41
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090042static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070043 __u64 key, __u64 ptr)
44{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +090045 *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
Koji Sato36a580e2009-04-06 19:01:25 -070046}
47
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090048static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -070049 __u64 key, int level, __u64 *ptrp)
50{
Koji Sato36a580e2009-04-06 19:01:25 -070051 __u64 ptr;
52
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090053 if (key > NILFS_DIRECT_KEY_MAX || level != 1)
54 return -ENOENT;
55 ptr = nilfs_direct_get_ptr(direct, key);
56 if (ptr == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -070057 return -ENOENT;
58
59 if (ptrp != NULL)
60 *ptrp = ptr;
61 return 0;
62}
63
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090064static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090065 __u64 key, __u64 *ptrp,
66 unsigned maxblocks)
67{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090068 struct inode *dat = NULL;
69 __u64 ptr, ptr2;
70 sector_t blocknr;
71 int ret, cnt;
72
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090073 if (key > NILFS_DIRECT_KEY_MAX)
74 return -ENOENT;
75 ptr = nilfs_direct_get_ptr(direct, key);
76 if (ptr == NILFS_BMAP_INVALID_PTR)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090077 return -ENOENT;
78
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090079 if (NILFS_BMAP_USE_VBN(direct)) {
80 dat = nilfs_bmap_get_dat(direct);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090081 ret = nilfs_dat_translate(dat, ptr, &blocknr);
82 if (ret < 0)
83 return ret;
84 ptr = blocknr;
85 }
86
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;
91 cnt++) {
92 if (dat) {
93 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
94 if (ret < 0)
95 return ret;
96 ptr2 = blocknr;
97 }
98 if (ptr2 != ptr + cnt)
99 break;
100 }
101 *ptrp = ptr;
102 return cnt;
103}
104
Koji Sato36a580e2009-04-06 19:01:25 -0700105static __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900106nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -0700107{
108 __u64 ptr;
109
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900110 ptr = nilfs_bmap_find_target_seq(direct, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700111 if (ptr != NILFS_BMAP_INVALID_PTR)
112 /* sequential access */
113 return ptr;
114 else
115 /* block group */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900116 return nilfs_bmap_find_target_in_group(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700117}
118
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900119static void nilfs_direct_set_target_v(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700120 __u64 key, __u64 ptr)
121{
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900122 direct->b_last_allocated_key = key;
123 direct->b_last_allocated_ptr = ptr;
Koji Sato36a580e2009-04-06 19:01:25 -0700124}
125
Koji Sato36a580e2009-04-06 19:01:25 -0700126static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
127{
Koji Sato36a580e2009-04-06 19:01:25 -0700128 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900129 struct inode *dat = NULL;
130 struct buffer_head *bh;
Koji Sato36a580e2009-04-06 19:01:25 -0700131 int ret;
132
Koji Sato36a580e2009-04-06 19:01:25 -0700133 if (key > NILFS_DIRECT_KEY_MAX)
134 return -ENOENT;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900135 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700136 return -EEXIST;
137
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900138 if (NILFS_BMAP_USE_VBN(bmap)) {
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900139 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900140 dat = nilfs_bmap_get_dat(bmap);
141 }
142 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
143 if (!ret) {
144 /* ptr must be a pointer to a buffer head. */
145 bh = (struct buffer_head *)((unsigned long)ptr);
146 set_buffer_nilfs_volatile(bh);
Koji Sato36a580e2009-04-06 19:01:25 -0700147
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900148 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900149 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700150
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900151 if (!nilfs_bmap_dirty(bmap))
152 nilfs_bmap_set_dirty(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700153
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900154 if (NILFS_BMAP_USE_VBN(bmap))
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900155 nilfs_direct_set_target_v(bmap, key, req.bpr_ptr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900156
157 nilfs_bmap_add_blocks(bmap, 1);
158 }
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900159 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700160}
161
Koji Sato36a580e2009-04-06 19:01:25 -0700162static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
163{
Koji Sato36a580e2009-04-06 19:01:25 -0700164 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900165 struct inode *dat;
Koji Sato36a580e2009-04-06 19:01:25 -0700166 int ret;
167
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900168 if (key > NILFS_DIRECT_KEY_MAX ||
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900169 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700170 return -ENOENT;
171
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900172 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900173 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700174
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900175 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
176 if (!ret) {
177 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900178 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900179 nilfs_bmap_sub_blocks(bmap, 1);
180 }
181 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700182}
183
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900184static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
Koji Sato36a580e2009-04-06 19:01:25 -0700185{
Koji Sato36a580e2009-04-06 19:01:25 -0700186 __u64 key, lastkey;
187
Koji Sato36a580e2009-04-06 19:01:25 -0700188 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)
192 lastkey = key;
193
194 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
195 return -ENOENT;
196
Koji Sato36a580e2009-04-06 19:01:25 -0700197 *keyp = lastkey;
198
199 return 0;
200}
201
202static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
203{
204 return key > NILFS_DIRECT_KEY_MAX;
205}
206
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900207static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700208 __u64 *keys, __u64 *ptrs, int nitems)
209{
Koji Sato36a580e2009-04-06 19:01:25 -0700210 __u64 key;
211 __u64 ptr;
212 int n;
213
Koji Sato36a580e2009-04-06 19:01:25 -0700214 if (nitems > NILFS_DIRECT_NBLOCKS)
215 nitems = NILFS_DIRECT_NBLOCKS;
216 n = 0;
217 for (key = 0; key < nitems; key++) {
218 ptr = nilfs_direct_get_ptr(direct, key);
219 if (ptr != NILFS_BMAP_INVALID_PTR) {
220 keys[n] = key;
221 ptrs[n] = ptr;
222 n++;
223 }
224 }
225 return n;
226}
227
228int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900229 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700230{
Koji Sato36a580e2009-04-06 19:01:25 -0700231 __le64 *dptrs;
232 int ret, i, j;
233
234 /* no need to allocate any resource for conversion */
235
236 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700237 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700238 if (ret < 0)
239 return ret;
240
241 /* free resources */
242 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700243 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700244
245 /* convert */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900246 dptrs = nilfs_direct_dptrs(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700247 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
248 if ((j < n) && (i == keys[j])) {
249 dptrs[i] = (i != key) ?
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900250 cpu_to_le64(ptrs[j]) :
Koji Sato36a580e2009-04-06 19:01:25 -0700251 NILFS_BMAP_INVALID_PTR;
252 j++;
253 } else
254 dptrs[i] = NILFS_BMAP_INVALID_PTR;
255 }
256
Ryusuke Konishi30333422009-05-24 00:09:44 +0900257 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700258 return 0;
259}
260
Ryusuke Konishi583ada42010-07-10 21:37:47 +0900261static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
Koji Sato36a580e2009-04-06 19:01:25 -0700262 struct buffer_head *bh)
263{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900264 struct nilfs_palloc_req oldreq, newreq;
265 struct inode *dat;
266 __u64 key;
267 __u64 ptr;
268 int ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700269
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900270 if (!NILFS_BMAP_USE_VBN(bmap))
271 return 0;
272
273 dat = nilfs_bmap_get_dat(bmap);
274 key = nilfs_bmap_data_get_key(bmap, bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900275 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900276 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);
280 if (ret < 0)
281 return ret;
282 nilfs_dat_commit_update(dat, &oldreq, &newreq,
283 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
284 set_buffer_nilfs_volatile(bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900285 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900286 } else
287 ret = nilfs_dat_mark_dirty(dat, ptr);
288
289 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700290}
291
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900292static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700293 __u64 key, __u64 ptr,
294 struct buffer_head **bh,
295 sector_t blocknr,
296 union nilfs_binfo *binfo)
297{
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900298 struct inode *dat = nilfs_bmap_get_dat(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700299 union nilfs_bmap_ptr_req req;
300 int ret;
301
302 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900303 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
304 if (!ret) {
305 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900306 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
307 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900308 }
309 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700310}
311
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900312static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700313 __u64 key, __u64 ptr,
314 struct buffer_head **bh,
315 sector_t blocknr,
316 union nilfs_binfo *binfo)
317{
318 nilfs_direct_set_ptr(direct, key, blocknr);
319
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900320 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato36a580e2009-04-06 19:01:25 -0700321 binfo->bi_dat.bi_level = 0;
322
323 return 0;
324}
325
326static int nilfs_direct_assign(struct nilfs_bmap *bmap,
327 struct buffer_head **bh,
328 sector_t blocknr,
329 union nilfs_binfo *binfo)
330{
Koji Sato36a580e2009-04-06 19:01:25 -0700331 __u64 key;
332 __u64 ptr;
333
Koji Sato36a580e2009-04-06 19:01:25 -0700334 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700335 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
336 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
337 (unsigned long long)key);
338 return -EINVAL;
339 }
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900340 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700341 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
342 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
343 (unsigned long long)ptr);
344 return -EINVAL;
345 }
Koji Sato36a580e2009-04-06 19:01:25 -0700346
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900347 return NILFS_BMAP_USE_VBN(bmap) ?
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900348 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
349 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700350}
351
352static const struct nilfs_bmap_operations nilfs_direct_ops = {
353 .bop_lookup = nilfs_direct_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900354 .bop_lookup_contig = nilfs_direct_lookup_contig,
Koji Sato36a580e2009-04-06 19:01:25 -0700355 .bop_insert = nilfs_direct_insert,
356 .bop_delete = nilfs_direct_delete,
357 .bop_clear = NULL,
358
359 .bop_propagate = nilfs_direct_propagate,
360
361 .bop_lookup_dirty_buffers = NULL,
362
363 .bop_assign = nilfs_direct_assign,
364 .bop_mark = NULL,
365
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,
370};
371
372
Ryusuke Konishi30333422009-05-24 00:09:44 +0900373int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700374{
Koji Sato36a580e2009-04-06 19:01:25 -0700375 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700376 return 0;
377}