nilfs2: add sync_page method to page caches of meta data
[deliverable/linux.git] / fs / nilfs2 / direct.c
CommitLineData
36a580eb
KS
1/*
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
355c6b61
RK
93 if (NILFS_BMAP_USE_VBN(&direct->d_bmap))
94 req->bpr_ptr = nilfs_direct_find_target_v(direct, key);
d4b96157 95 ret = nilfs_bmap_prepare_alloc_ptr(&direct->d_bmap, req);
36a580eb
KS
96 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
d4b96157 113 nilfs_bmap_commit_alloc_ptr(&direct->d_bmap, req);
36a580eb
KS
114 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
355c6b61
RK
119 if (NILFS_BMAP_USE_VBN(&direct->d_bmap))
120 nilfs_direct_set_target_v(direct, key, req->bpr_ptr);
36a580eb
KS
121}
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
d4b96157
RK
152 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;
36a580eb
KS
157}
158
159static void nilfs_direct_commit_delete(struct nilfs_direct *direct,
160 union nilfs_bmap_ptr_req *req,
161 __u64 key)
162{
d4b96157 163 nilfs_bmap_commit_end_ptr(&direct->d_bmap, req);
36a580eb
KS
164 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
36a580eb
KS
203 *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,
3033342a 237 __u64 key, __u64 *keys, __u64 *ptrs, int n)
36a580eb
KS
238{
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 */
8acfbf09 246 ret = bmap->b_ops->bop_delete(bmap, key);
36a580eb
KS
247 if (ret < 0)
248 return ret;
249
250 /* free resources */
251 if (bmap->b_ops->bop_clear != NULL)
8acfbf09 252 bmap->b_ops->bop_clear(bmap);
36a580eb
KS
253
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
3033342a 267 nilfs_direct_init(bmap);
36a580eb
KS
268 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;
d4b96157
RK
284 ret = nilfs_bmap_prepare_update_v(&direct->d_bmap, &oldreq,
285 &newreq);
36a580eb
KS
286 if (ret < 0)
287 return ret;
d4b96157 288 nilfs_bmap_commit_update_v(&direct->d_bmap, &oldreq, &newreq);
36a580eb
KS
289 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{
355c6b61 300 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
36a580eb 301
355c6b61
RK
302 return NILFS_BMAP_USE_VBN(bmap) ?
303 nilfs_direct_propagate_v(direct, bh) : 0;
36a580eb
KS
304}
305
306static int nilfs_direct_assign_v(struct nilfs_direct *direct,
307 __u64 key, __u64 ptr,
308 struct buffer_head **bh,
309 sector_t blocknr,
310 union nilfs_binfo *binfo)
311{
312 union nilfs_bmap_ptr_req req;
313 int ret;
314
315 req.bpr_ptr = ptr;
d97a51a7
RK
316 ret = nilfs_bmap_start_v(&direct->d_bmap, &req, blocknr);
317 if (unlikely(ret < 0))
36a580eb 318 return ret;
36a580eb
KS
319
320 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
321 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
322
323 return 0;
324}
325
326static int nilfs_direct_assign_p(struct nilfs_direct *direct,
327 __u64 key, __u64 ptr,
328 struct buffer_head **bh,
329 sector_t blocknr,
330 union nilfs_binfo *binfo)
331{
332 nilfs_direct_set_ptr(direct, key, blocknr);
333
334 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
335 binfo->bi_dat.bi_level = 0;
336
337 return 0;
338}
339
340static int nilfs_direct_assign(struct nilfs_bmap *bmap,
341 struct buffer_head **bh,
342 sector_t blocknr,
343 union nilfs_binfo *binfo)
344{
345 struct nilfs_direct *direct;
346 __u64 key;
347 __u64 ptr;
348
349 direct = (struct nilfs_direct *)bmap;
350 key = nilfs_bmap_data_get_key(bmap, *bh);
1f5abe7e
RK
351 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
352 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
353 (unsigned long long)key);
354 return -EINVAL;
355 }
36a580eb 356 ptr = nilfs_direct_get_ptr(direct, key);
1f5abe7e
RK
357 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
358 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
359 (unsigned long long)ptr);
360 return -EINVAL;
361 }
36a580eb 362
355c6b61
RK
363 return NILFS_BMAP_USE_VBN(bmap) ?
364 nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
365 nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
36a580eb
KS
366}
367
368static const struct nilfs_bmap_operations nilfs_direct_ops = {
369 .bop_lookup = nilfs_direct_lookup,
370 .bop_insert = nilfs_direct_insert,
371 .bop_delete = nilfs_direct_delete,
372 .bop_clear = NULL,
373
374 .bop_propagate = nilfs_direct_propagate,
375
376 .bop_lookup_dirty_buffers = NULL,
377
378 .bop_assign = nilfs_direct_assign,
379 .bop_mark = NULL,
380
381 .bop_last_key = nilfs_direct_last_key,
382 .bop_check_insert = nilfs_direct_check_insert,
383 .bop_check_delete = NULL,
384 .bop_gather_data = nilfs_direct_gather_data,
385};
386
387
3033342a 388int nilfs_direct_init(struct nilfs_bmap *bmap)
36a580eb 389{
36a580eb 390 bmap->b_ops = &nilfs_direct_ops;
36a580eb
KS
391 return 0;
392}
This page took 0.053471 seconds and 5 git commands to generate.