blob: d369ac7182772b359bc148bd5dc7d7a30d3285d9 [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
30static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
31{
32 return (__le64 *)
33 ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
34}
35
36static inline __u64
37nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
38{
39 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
40}
41
42static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
43 __u64 key, __u64 ptr)
44{
45 *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
46}
47
48static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
49 __u64 key, int level, __u64 *ptrp)
50{
51 struct nilfs_direct *direct;
52 __u64 ptr;
53
54 direct = (struct nilfs_direct *)bmap;
55 if ((key > NILFS_DIRECT_KEY_MAX) ||
56 (level != 1) || /* XXX: use macro for level 1 */
57 ((ptr = nilfs_direct_get_ptr(direct, key)) ==
58 NILFS_BMAP_INVALID_PTR))
59 return -ENOENT;
60
61 if (ptrp != NULL)
62 *ptrp = ptr;
63 return 0;
64}
65
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090066static int nilfs_direct_lookup_contig(const struct nilfs_bmap *bmap,
67 __u64 key, __u64 *ptrp,
68 unsigned maxblocks)
69{
70 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
71 struct inode *dat = NULL;
72 __u64 ptr, ptr2;
73 sector_t blocknr;
74 int ret, cnt;
75
76 if (key > NILFS_DIRECT_KEY_MAX ||
77 (ptr = nilfs_direct_get_ptr(direct, key)) ==
78 NILFS_BMAP_INVALID_PTR)
79 return -ENOENT;
80
81 if (NILFS_BMAP_USE_VBN(bmap)) {
82 dat = nilfs_bmap_get_dat(bmap);
83 ret = nilfs_dat_translate(dat, ptr, &blocknr);
84 if (ret < 0)
85 return ret;
86 ptr = blocknr;
87 }
88
89 maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
90 for (cnt = 1; cnt < maxblocks &&
91 (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
92 NILFS_BMAP_INVALID_PTR;
93 cnt++) {
94 if (dat) {
95 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
96 if (ret < 0)
97 return ret;
98 ptr2 = blocknr;
99 }
100 if (ptr2 != ptr + cnt)
101 break;
102 }
103 *ptrp = ptr;
104 return cnt;
105}
106
Koji Sato36a580e2009-04-06 19:01:25 -0700107static __u64
108nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
109{
110 __u64 ptr;
111
112 ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
113 if (ptr != NILFS_BMAP_INVALID_PTR)
114 /* sequential access */
115 return ptr;
116 else
117 /* block group */
118 return nilfs_bmap_find_target_in_group(&direct->d_bmap);
119}
120
121static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
122 __u64 key, __u64 ptr)
123{
124 direct->d_bmap.b_last_allocated_key = key;
125 direct->d_bmap.b_last_allocated_ptr = ptr;
126}
127
Koji Sato36a580e2009-04-06 19:01:25 -0700128static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
129{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900130 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
Koji Sato36a580e2009-04-06 19:01:25 -0700131 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900132 struct inode *dat = NULL;
133 struct buffer_head *bh;
Koji Sato36a580e2009-04-06 19:01:25 -0700134 int ret;
135
Koji Sato36a580e2009-04-06 19:01:25 -0700136 if (key > NILFS_DIRECT_KEY_MAX)
137 return -ENOENT;
138 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
139 return -EEXIST;
140
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900141 if (NILFS_BMAP_USE_VBN(bmap)) {
142 req.bpr_ptr = nilfs_direct_find_target_v(direct, key);
143 dat = nilfs_bmap_get_dat(bmap);
144 }
145 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
146 if (!ret) {
147 /* ptr must be a pointer to a buffer head. */
148 bh = (struct buffer_head *)((unsigned long)ptr);
149 set_buffer_nilfs_volatile(bh);
Koji Sato36a580e2009-04-06 19:01:25 -0700150
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900151 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
152 nilfs_direct_set_ptr(direct, key, req.bpr_ptr);
Koji Sato36a580e2009-04-06 19:01:25 -0700153
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900154 if (!nilfs_bmap_dirty(bmap))
155 nilfs_bmap_set_dirty(bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700156
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900157 if (NILFS_BMAP_USE_VBN(bmap))
158 nilfs_direct_set_target_v(direct, key, req.bpr_ptr);
159
160 nilfs_bmap_add_blocks(bmap, 1);
161 }
Ryusuke Konishid4b96152009-05-24 03:25:44 +0900162 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700163}
164
Koji Sato36a580e2009-04-06 19:01:25 -0700165static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
166{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900167 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
Koji Sato36a580e2009-04-06 19:01:25 -0700168 union nilfs_bmap_ptr_req req;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900169 struct inode *dat;
Koji Sato36a580e2009-04-06 19:01:25 -0700170 int ret;
171
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900172 if (key > NILFS_DIRECT_KEY_MAX ||
Koji Sato36a580e2009-04-06 19:01:25 -0700173 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
174 return -ENOENT;
175
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900176 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
177 req.bpr_ptr = nilfs_direct_get_ptr(direct, key);
Koji Sato36a580e2009-04-06 19:01:25 -0700178
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900179 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
180 if (!ret) {
181 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
182 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
183 nilfs_bmap_sub_blocks(bmap, 1);
184 }
185 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700186}
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
Koji Sato36a580e2009-04-06 19:01:25 -0700271static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
272 struct buffer_head *bh)
273{
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900274 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900275 struct nilfs_palloc_req oldreq, newreq;
276 struct inode *dat;
277 __u64 key;
278 __u64 ptr;
279 int ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700280
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900281 if (!NILFS_BMAP_USE_VBN(bmap))
282 return 0;
283
284 dat = nilfs_bmap_get_dat(bmap);
285 key = nilfs_bmap_data_get_key(bmap, bh);
286 ptr = nilfs_direct_get_ptr(direct, key);
287 if (!buffer_nilfs_volatile(bh)) {
288 oldreq.pr_entry_nr = ptr;
289 newreq.pr_entry_nr = ptr;
290 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
291 if (ret < 0)
292 return ret;
293 nilfs_dat_commit_update(dat, &oldreq, &newreq,
294 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
295 set_buffer_nilfs_volatile(bh);
296 nilfs_direct_set_ptr(direct, key, newreq.pr_entry_nr);
297 } else
298 ret = nilfs_dat_mark_dirty(dat, ptr);
299
300 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700301}
302
303static int nilfs_direct_assign_v(struct nilfs_direct *direct,
304 __u64 key, __u64 ptr,
305 struct buffer_head **bh,
306 sector_t blocknr,
307 union nilfs_binfo *binfo)
308{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900309 struct inode *dat = nilfs_bmap_get_dat(&direct->d_bmap);
Koji Sato36a580e2009-04-06 19:01:25 -0700310 union nilfs_bmap_ptr_req req;
311 int ret;
312
313 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900314 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
315 if (!ret) {
316 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
317 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
318 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
319 }
320 return ret;
Koji Sato36a580e2009-04-06 19:01:25 -0700321}
322
323static int nilfs_direct_assign_p(struct nilfs_direct *direct,
324 __u64 key, __u64 ptr,
325 struct buffer_head **bh,
326 sector_t blocknr,
327 union nilfs_binfo *binfo)
328{
329 nilfs_direct_set_ptr(direct, key, blocknr);
330
331 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
332 binfo->bi_dat.bi_level = 0;
333
334 return 0;
335}
336
337static int nilfs_direct_assign(struct nilfs_bmap *bmap,
338 struct buffer_head **bh,
339 sector_t blocknr,
340 union nilfs_binfo *binfo)
341{
342 struct nilfs_direct *direct;
343 __u64 key;
344 __u64 ptr;
345
346 direct = (struct nilfs_direct *)bmap;
347 key = nilfs_bmap_data_get_key(bmap, *bh);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700348 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
349 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
350 (unsigned long long)key);
351 return -EINVAL;
352 }
Koji Sato36a580e2009-04-06 19:01:25 -0700353 ptr = nilfs_direct_get_ptr(direct, key);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700354 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
355 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
356 (unsigned long long)ptr);
357 return -EINVAL;
358 }
Koji Sato36a580e2009-04-06 19:01:25 -0700359
Ryusuke Konishi355c6b62009-05-24 16:46:37 +0900360 return NILFS_BMAP_USE_VBN(bmap) ?
361 nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
362 nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
Koji Sato36a580e2009-04-06 19:01:25 -0700363}
364
365static const struct nilfs_bmap_operations nilfs_direct_ops = {
366 .bop_lookup = nilfs_direct_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900367 .bop_lookup_contig = nilfs_direct_lookup_contig,
Koji Sato36a580e2009-04-06 19:01:25 -0700368 .bop_insert = nilfs_direct_insert,
369 .bop_delete = nilfs_direct_delete,
370 .bop_clear = NULL,
371
372 .bop_propagate = nilfs_direct_propagate,
373
374 .bop_lookup_dirty_buffers = NULL,
375
376 .bop_assign = nilfs_direct_assign,
377 .bop_mark = NULL,
378
379 .bop_last_key = nilfs_direct_last_key,
380 .bop_check_insert = nilfs_direct_check_insert,
381 .bop_check_delete = NULL,
382 .bop_gather_data = nilfs_direct_gather_data,
383};
384
385
Ryusuke Konishi30333422009-05-24 00:09:44 +0900386int nilfs_direct_init(struct nilfs_bmap *bmap)
Koji Sato36a580e2009-04-06 19:01:25 -0700387{
Koji Sato36a580e2009-04-06 19:01:25 -0700388 bmap->b_ops = &nilfs_direct_ops;
Koji Sato36a580e2009-04-06 19:01:25 -0700389 return 0;
390}