blob: 324d80c57518ad09a83cab70ee1ca6abfae4d644 [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
Ryusuke Konishi364ec2d2010-07-13 23:33:51 +090059 *ptrp = ptr;
Koji Sato36a580e2009-04-06 19:01:25 -070060 return 0;
61}
62
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090063static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090064 __u64 key, __u64 *ptrp,
65 unsigned maxblocks)
66{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090067 struct inode *dat = NULL;
68 __u64 ptr, ptr2;
69 sector_t blocknr;
70 int ret, cnt;
71
Jiro SEKIBA5ee58142009-12-06 15:43:56 +090072 if (key > NILFS_DIRECT_KEY_MAX)
73 return -ENOENT;
74 ptr = nilfs_direct_get_ptr(direct, key);
75 if (ptr == NILFS_BMAP_INVALID_PTR)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090076 return -ENOENT;
77
Ryusuke Konishi10ff8852010-07-10 18:07:04 +090078 if (NILFS_BMAP_USE_VBN(direct)) {
79 dat = nilfs_bmap_get_dat(direct);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090080 ret = nilfs_dat_translate(dat, ptr, &blocknr);
81 if (ret < 0)
82 return ret;
83 ptr = blocknr;
84 }
85
86 maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
87 for (cnt = 1; cnt < maxblocks &&
88 (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
89 NILFS_BMAP_INVALID_PTR;
90 cnt++) {
91 if (dat) {
92 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
93 if (ret < 0)
94 return ret;
95 ptr2 = blocknr;
96 }
97 if (ptr2 != ptr + cnt)
98 break;
99 }
100 *ptrp = ptr;
101 return cnt;
102}
103
Koji Sato36a580e2009-04-06 19:01:25 -0700104static __u64
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900105nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
Koji Sato36a580e2009-04-06 19:01:25 -0700106{
107 __u64 ptr;
108
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900109 ptr = nilfs_bmap_find_target_seq(direct, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700110 if (ptr != NILFS_BMAP_INVALID_PTR)
111 /* sequential access */
112 return ptr;
113 else
114 /* block group */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900115 return nilfs_bmap_find_target_in_group(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700116}
117
Koji Sato36a580e2009-04-06 19:01:25 -0700118static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
119{
Koji Sato36a580e2009-04-06 19:01:25 -0700120 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900121 struct inode *dat = NULL;
122 struct buffer_head *bh;
Koji Sato36a580e2009-04-06 19:01:25 -0700123 int ret;
124
Koji Sato36a580e2009-04-06 19:01:25 -0700125 if (key > NILFS_DIRECT_KEY_MAX)
126 return -ENOENT;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900127 if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700128 return -EEXIST;
129
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900130 if (NILFS_BMAP_USE_VBN(bmap)) {
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900131 req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900132 dat = nilfs_bmap_get_dat(bmap);
133 }
134 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
135 if (!ret) {
136 /* ptr must be a pointer to a buffer head. */
137 bh = (struct buffer_head *)((unsigned long)ptr);
138 set_buffer_nilfs_volatile(bh);
Koji Sato36a580e2009-04-06 19:01:25 -0700139
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900140 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900141 nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700142
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900143 if (!nilfs_bmap_dirty(bmap))
144 nilfs_bmap_set_dirty(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700145
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900146 if (NILFS_BMAP_USE_VBN(bmap))
Ryusuke Konishidc935be2010-07-10 22:21:54 +0900147 nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900148
149 nilfs_bmap_add_blocks(bmap, 1);
150 }
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900151 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700152}
153
Koji Sato36a580e2009-04-06 19:01:25 -0700154static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
155{
Koji Sato36a580e2009-04-06 19:01:25 -0700156 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900157 struct inode *dat;
Koji Sato36a580e2009-04-06 19:01:25 -0700158 int ret;
159
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900160 if (key > NILFS_DIRECT_KEY_MAX ||
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900161 nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
Koji Sato36a580e2009-04-06 19:01:25 -0700162 return -ENOENT;
163
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900164 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900165 req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700166
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900167 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
168 if (!ret) {
169 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900170 nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900171 nilfs_bmap_sub_blocks(bmap, 1);
172 }
173 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700174}
175
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900176static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
Koji Sato36a580e2009-04-06 19:01:25 -0700177{
Koji Sato36a580e2009-04-06 19:01:25 -0700178 __u64 key, lastkey;
179
Koji Sato36a580e2009-04-06 19:01:25 -0700180 lastkey = NILFS_DIRECT_KEY_MAX + 1;
181 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
182 if (nilfs_direct_get_ptr(direct, key) !=
183 NILFS_BMAP_INVALID_PTR)
184 lastkey = key;
185
186 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
187 return -ENOENT;
188
Koji Sato36a580e2009-04-06 19:01:25 -0700189 *keyp = lastkey;
190
191 return 0;
192}
193
194static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
195{
196 return key > NILFS_DIRECT_KEY_MAX;
197}
198
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900199static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700200 __u64 *keys, __u64 *ptrs, int nitems)
201{
Koji Sato36a580e2009-04-06 19:01:25 -0700202 __u64 key;
203 __u64 ptr;
204 int n;
205
Koji Sato36a580e2009-04-06 19:01:25 -0700206 if (nitems > NILFS_DIRECT_NBLOCKS)
207 nitems = NILFS_DIRECT_NBLOCKS;
208 n = 0;
209 for (key = 0; key < nitems; key++) {
210 ptr = nilfs_direct_get_ptr(direct, key);
211 if (ptr != NILFS_BMAP_INVALID_PTR) {
212 keys[n] = key;
213 ptrs[n] = ptr;
214 n++;
215 }
216 }
217 return n;
218}
219
220int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
Ryusuke Konishi30333422009-05-24 00:09:44 +0900221 __u64 key, __u64 *keys, __u64 *ptrs, int n)
Koji Sato36a580e2009-04-06 19:01:25 -0700222{
Koji Sato36a580e2009-04-06 19:01:25 -0700223 __le64 *dptrs;
224 int ret, i, j;
225
226 /* no need to allocate any resource for conversion */
227
228 /* delete */
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700229 ret = bmap->b_ops->bop_delete(bmap, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700230 if (ret < 0)
231 return ret;
232
233 /* free resources */
234 if (bmap->b_ops->bop_clear != NULL)
Pekka Enberg8acfbf02009-04-06 19:01:49 -0700235 bmap->b_ops->bop_clear(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700236
237 /* convert */
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900238 dptrs = nilfs_direct_dptrs(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700239 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
240 if ((j < n) && (i == keys[j])) {
241 dptrs[i] = (i != key) ?
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900242 cpu_to_le64(ptrs[j]) :
Koji Sato36a580e2009-04-06 19:01:25 -0700243 NILFS_BMAP_INVALID_PTR;
244 j++;
245 } else
246 dptrs[i] = NILFS_BMAP_INVALID_PTR;
247 }
248
Ryusuke Konishi30333422009-05-24 00:09:44 +0900249 nilfs_direct_init(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700250 return 0;
251}
252
Ryusuke Konishi583ada42010-07-10 21:37:47 +0900253static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
Koji Sato36a580e2009-04-06 19:01:25 -0700254 struct buffer_head *bh)
255{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900256 struct nilfs_palloc_req oldreq, newreq;
257 struct inode *dat;
258 __u64 key;
259 __u64 ptr;
260 int ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700261
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900262 if (!NILFS_BMAP_USE_VBN(bmap))
263 return 0;
264
265 dat = nilfs_bmap_get_dat(bmap);
266 key = nilfs_bmap_data_get_key(bmap, bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900267 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900268 if (!buffer_nilfs_volatile(bh)) {
269 oldreq.pr_entry_nr = ptr;
270 newreq.pr_entry_nr = ptr;
271 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
272 if (ret < 0)
273 return ret;
274 nilfs_dat_commit_update(dat, &oldreq, &newreq,
275 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
276 set_buffer_nilfs_volatile(bh);
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900277 nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900278 } else
279 ret = nilfs_dat_mark_dirty(dat, ptr);
280
281 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700282}
283
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900284static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700285 __u64 key, __u64 ptr,
286 struct buffer_head **bh,
287 sector_t blocknr,
288 union nilfs_binfo *binfo)
289{
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900290 struct inode *dat = nilfs_bmap_get_dat(direct);
Koji Sato36a580e2009-04-06 19:01:25 -0700291 union nilfs_bmap_ptr_req req;
292 int ret;
293
294 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900295 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
296 if (!ret) {
297 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900298 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
299 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900300 }
301 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700302}
303
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900304static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
Koji Sato36a580e2009-04-06 19:01:25 -0700305 __u64 key, __u64 ptr,
306 struct buffer_head **bh,
307 sector_t blocknr,
308 union nilfs_binfo *binfo)
309{
310 nilfs_direct_set_ptr(direct, key, blocknr);
311
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900312 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato36a580e2009-04-06 19:01:25 -0700313 binfo->bi_dat.bi_level = 0;
314
315 return 0;
316}
317
318static int nilfs_direct_assign(struct nilfs_bmap *bmap,
319 struct buffer_head **bh,
320 sector_t blocknr,
321 union nilfs_binfo *binfo)
322{
Koji Sato36a580e2009-04-06 19:01:25 -0700323 __u64 key;
324 __u64 ptr;
325
Koji Sato36a580e2009-04-06 19:01:25 -0700326 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700327 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
328 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
329 (unsigned long long)key);
330 return -EINVAL;
331 }
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900332 ptr = nilfs_direct_get_ptr(bmap, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700333 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
334 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
335 (unsigned long long)ptr);
336 return -EINVAL;
337 }
Koji Sato36a580e2009-04-06 19:01:25 -0700338
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900339 return NILFS_BMAP_USE_VBN(bmap) ?
Ryusuke Konishi10ff8852010-07-10 18:07:04 +0900340 nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
341 nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700342}
343
344static const struct nilfs_bmap_operations nilfs_direct_ops = {
345 .bop_lookup = nilfs_direct_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900346 .bop_lookup_contig = nilfs_direct_lookup_contig,
Koji Sato36a580e2009-04-06 19:01:25 -0700347 .bop_insert = nilfs_direct_insert,
348 .bop_delete = nilfs_direct_delete,
349 .bop_clear = NULL,
350
351 .bop_propagate = nilfs_direct_propagate,
352
353 .bop_lookup_dirty_buffers = NULL,
354
355 .bop_assign = nilfs_direct_assign,
356 .bop_mark = NULL,
357
358 .bop_last_key = nilfs_direct_last_key,
359 .bop_check_insert = nilfs_direct_check_insert,
360 .bop_check_delete = NULL,
361 .bop_gather_data = nilfs_direct_gather_data,
362};
363
364
Ryusuke Konishi30333422009-05-24 00:09:44 +0900365int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700366{
Koji Sato36a580e2009-04-06 19:01:25 -0700367 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700368 return 0;
369}