switch hugetlbfs to ->evict_inode()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nilfs2 / direct.c
blob236753df5cdf9db2362494aa44d8b20105cfc5fb
1 /*
2 * direct.c - NILFS direct block pointer.
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Koji Sato <koji@osrg.net>.
23 #include <linux/errno.h>
24 #include "nilfs.h"
25 #include "page.h"
26 #include "direct.h"
27 #include "alloc.h"
28 #include "dat.h"
30 static inline __le64 *nilfs_direct_dptrs(const struct nilfs_direct *direct)
32 return (__le64 *)
33 ((struct nilfs_direct_node *)direct->d_bmap.b_u.u_data + 1);
36 static inline __u64
37 nilfs_direct_get_ptr(const struct nilfs_direct *direct, __u64 key)
39 return nilfs_bmap_dptr_to_ptr(*(nilfs_direct_dptrs(direct) + key));
42 static inline void nilfs_direct_set_ptr(struct nilfs_direct *direct,
43 __u64 key, __u64 ptr)
45 *(nilfs_direct_dptrs(direct) + key) = nilfs_bmap_ptr_to_dptr(ptr);
48 static int nilfs_direct_lookup(const struct nilfs_bmap *bmap,
49 __u64 key, int level, __u64 *ptrp)
51 struct nilfs_direct *direct;
52 __u64 ptr;
54 direct = (struct nilfs_direct *)bmap; /* XXX: use macro for level 1 */
55 if (key > NILFS_DIRECT_KEY_MAX || level != 1)
56 return -ENOENT;
57 ptr = nilfs_direct_get_ptr(direct, key);
58 if (ptr == NILFS_BMAP_INVALID_PTR)
59 return -ENOENT;
61 if (ptrp != NULL)
62 *ptrp = ptr;
63 return 0;
66 static int nilfs_direct_lookup_contig(const struct nilfs_bmap *bmap,
67 __u64 key, __u64 *ptrp,
68 unsigned maxblocks)
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;
76 if (key > NILFS_DIRECT_KEY_MAX)
77 return -ENOENT;
78 ptr = nilfs_direct_get_ptr(direct, key);
79 if (ptr == NILFS_BMAP_INVALID_PTR)
80 return -ENOENT;
82 if (NILFS_BMAP_USE_VBN(bmap)) {
83 dat = nilfs_bmap_get_dat(bmap);
84 ret = nilfs_dat_translate(dat, ptr, &blocknr);
85 if (ret < 0)
86 return ret;
87 ptr = blocknr;
90 maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
91 for (cnt = 1; cnt < maxblocks &&
92 (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
93 NILFS_BMAP_INVALID_PTR;
94 cnt++) {
95 if (dat) {
96 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
97 if (ret < 0)
98 return ret;
99 ptr2 = blocknr;
101 if (ptr2 != ptr + cnt)
102 break;
104 *ptrp = ptr;
105 return cnt;
108 static __u64
109 nilfs_direct_find_target_v(const struct nilfs_direct *direct, __u64 key)
111 __u64 ptr;
113 ptr = nilfs_bmap_find_target_seq(&direct->d_bmap, key);
114 if (ptr != NILFS_BMAP_INVALID_PTR)
115 /* sequential access */
116 return ptr;
117 else
118 /* block group */
119 return nilfs_bmap_find_target_in_group(&direct->d_bmap);
122 static void nilfs_direct_set_target_v(struct nilfs_direct *direct,
123 __u64 key, __u64 ptr)
125 direct->d_bmap.b_last_allocated_key = key;
126 direct->d_bmap.b_last_allocated_ptr = ptr;
129 static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
131 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
132 union nilfs_bmap_ptr_req req;
133 struct inode *dat = NULL;
134 struct buffer_head *bh;
135 int ret;
137 if (key > NILFS_DIRECT_KEY_MAX)
138 return -ENOENT;
139 if (nilfs_direct_get_ptr(direct, key) != NILFS_BMAP_INVALID_PTR)
140 return -EEXIST;
142 if (NILFS_BMAP_USE_VBN(bmap)) {
143 req.bpr_ptr = nilfs_direct_find_target_v(direct, key);
144 dat = nilfs_bmap_get_dat(bmap);
146 ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
147 if (!ret) {
148 /* ptr must be a pointer to a buffer head. */
149 bh = (struct buffer_head *)((unsigned long)ptr);
150 set_buffer_nilfs_volatile(bh);
152 nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
153 nilfs_direct_set_ptr(direct, key, req.bpr_ptr);
155 if (!nilfs_bmap_dirty(bmap))
156 nilfs_bmap_set_dirty(bmap);
158 if (NILFS_BMAP_USE_VBN(bmap))
159 nilfs_direct_set_target_v(direct, key, req.bpr_ptr);
161 nilfs_bmap_add_blocks(bmap, 1);
163 return ret;
166 static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
168 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
169 union nilfs_bmap_ptr_req req;
170 struct inode *dat;
171 int ret;
173 if (key > NILFS_DIRECT_KEY_MAX ||
174 nilfs_direct_get_ptr(direct, key) == NILFS_BMAP_INVALID_PTR)
175 return -ENOENT;
177 dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
178 req.bpr_ptr = nilfs_direct_get_ptr(direct, key);
180 ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
181 if (!ret) {
182 nilfs_bmap_commit_end_ptr(bmap, &req, dat);
183 nilfs_direct_set_ptr(direct, key, NILFS_BMAP_INVALID_PTR);
184 nilfs_bmap_sub_blocks(bmap, 1);
186 return ret;
189 static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
191 struct nilfs_direct *direct;
192 __u64 key, lastkey;
194 direct = (struct nilfs_direct *)bmap;
195 lastkey = NILFS_DIRECT_KEY_MAX + 1;
196 for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
197 if (nilfs_direct_get_ptr(direct, key) !=
198 NILFS_BMAP_INVALID_PTR)
199 lastkey = key;
201 if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
202 return -ENOENT;
204 *keyp = lastkey;
206 return 0;
209 static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
211 return key > NILFS_DIRECT_KEY_MAX;
214 static int nilfs_direct_gather_data(struct nilfs_bmap *bmap,
215 __u64 *keys, __u64 *ptrs, int nitems)
217 struct nilfs_direct *direct;
218 __u64 key;
219 __u64 ptr;
220 int n;
222 direct = (struct nilfs_direct *)bmap;
223 if (nitems > NILFS_DIRECT_NBLOCKS)
224 nitems = NILFS_DIRECT_NBLOCKS;
225 n = 0;
226 for (key = 0; key < nitems; key++) {
227 ptr = nilfs_direct_get_ptr(direct, key);
228 if (ptr != NILFS_BMAP_INVALID_PTR) {
229 keys[n] = key;
230 ptrs[n] = ptr;
231 n++;
234 return n;
237 int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
238 __u64 key, __u64 *keys, __u64 *ptrs, int n)
240 struct nilfs_direct *direct;
241 __le64 *dptrs;
242 int ret, i, j;
244 /* no need to allocate any resource for conversion */
246 /* delete */
247 ret = bmap->b_ops->bop_delete(bmap, key);
248 if (ret < 0)
249 return ret;
251 /* free resources */
252 if (bmap->b_ops->bop_clear != NULL)
253 bmap->b_ops->bop_clear(bmap);
255 /* convert */
256 direct = (struct nilfs_direct *)bmap;
257 dptrs = nilfs_direct_dptrs(direct);
258 for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
259 if ((j < n) && (i == keys[j])) {
260 dptrs[i] = (i != key) ?
261 nilfs_bmap_ptr_to_dptr(ptrs[j]) :
262 NILFS_BMAP_INVALID_PTR;
263 j++;
264 } else
265 dptrs[i] = NILFS_BMAP_INVALID_PTR;
268 nilfs_direct_init(bmap);
269 return 0;
272 static int nilfs_direct_propagate(const struct nilfs_bmap *bmap,
273 struct buffer_head *bh)
275 struct nilfs_direct *direct = (struct nilfs_direct *)bmap;
276 struct nilfs_palloc_req oldreq, newreq;
277 struct inode *dat;
278 __u64 key;
279 __u64 ptr;
280 int ret;
282 if (!NILFS_BMAP_USE_VBN(bmap))
283 return 0;
285 dat = nilfs_bmap_get_dat(bmap);
286 key = nilfs_bmap_data_get_key(bmap, bh);
287 ptr = nilfs_direct_get_ptr(direct, key);
288 if (!buffer_nilfs_volatile(bh)) {
289 oldreq.pr_entry_nr = ptr;
290 newreq.pr_entry_nr = ptr;
291 ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
292 if (ret < 0)
293 return ret;
294 nilfs_dat_commit_update(dat, &oldreq, &newreq,
295 bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
296 set_buffer_nilfs_volatile(bh);
297 nilfs_direct_set_ptr(direct, key, newreq.pr_entry_nr);
298 } else
299 ret = nilfs_dat_mark_dirty(dat, ptr);
301 return ret;
304 static int nilfs_direct_assign_v(struct nilfs_direct *direct,
305 __u64 key, __u64 ptr,
306 struct buffer_head **bh,
307 sector_t blocknr,
308 union nilfs_binfo *binfo)
310 struct inode *dat = nilfs_bmap_get_dat(&direct->d_bmap);
311 union nilfs_bmap_ptr_req req;
312 int ret;
314 req.bpr_ptr = ptr;
315 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
316 if (!ret) {
317 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
318 binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
319 binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);
321 return ret;
324 static int nilfs_direct_assign_p(struct nilfs_direct *direct,
325 __u64 key, __u64 ptr,
326 struct buffer_head **bh,
327 sector_t blocknr,
328 union nilfs_binfo *binfo)
330 nilfs_direct_set_ptr(direct, key, blocknr);
332 binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
333 binfo->bi_dat.bi_level = 0;
335 return 0;
338 static int nilfs_direct_assign(struct nilfs_bmap *bmap,
339 struct buffer_head **bh,
340 sector_t blocknr,
341 union nilfs_binfo *binfo)
343 struct nilfs_direct *direct;
344 __u64 key;
345 __u64 ptr;
347 direct = (struct nilfs_direct *)bmap;
348 key = nilfs_bmap_data_get_key(bmap, *bh);
349 if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
350 printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
351 (unsigned long long)key);
352 return -EINVAL;
354 ptr = nilfs_direct_get_ptr(direct, key);
355 if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
356 printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
357 (unsigned long long)ptr);
358 return -EINVAL;
361 return NILFS_BMAP_USE_VBN(bmap) ?
362 nilfs_direct_assign_v(direct, key, ptr, bh, blocknr, binfo) :
363 nilfs_direct_assign_p(direct, key, ptr, bh, blocknr, binfo);
366 static const struct nilfs_bmap_operations nilfs_direct_ops = {
367 .bop_lookup = nilfs_direct_lookup,
368 .bop_lookup_contig = nilfs_direct_lookup_contig,
369 .bop_insert = nilfs_direct_insert,
370 .bop_delete = nilfs_direct_delete,
371 .bop_clear = NULL,
373 .bop_propagate = nilfs_direct_propagate,
375 .bop_lookup_dirty_buffers = NULL,
377 .bop_assign = nilfs_direct_assign,
378 .bop_mark = NULL,
380 .bop_last_key = nilfs_direct_last_key,
381 .bop_check_insert = nilfs_direct_check_insert,
382 .bop_check_delete = NULL,
383 .bop_gather_data = nilfs_direct_gather_data,
387 int nilfs_direct_init(struct nilfs_bmap *bmap)
389 bmap->b_ops = &nilfs_direct_ops;
390 return 0;