]>
Commit | Line | Data |
---|---|---|
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 | * | |
4b420ab4 | 16 | * Written by Koji Sato. |
36a580eb KS |
17 | */ |
18 | ||
19 | #include <linux/errno.h> | |
20 | #include "nilfs.h" | |
21 | #include "page.h" | |
22 | #include "direct.h" | |
23 | #include "alloc.h" | |
c3a7abf0 | 24 | #include "dat.h" |
36a580eb | 25 | |
10ff885b | 26 | static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct) |
36a580eb KS |
27 | { |
28 | return (__le64 *) | |
10ff885b | 29 | ((struct nilfs_direct_node *)direct->b_u.u_data + 1); |
36a580eb KS |
30 | } |
31 | ||
32 | static inline __u64 | |
10ff885b | 33 | nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key) |
36a580eb | 34 | { |
25b8d7de | 35 | return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key)); |
36a580eb KS |
36 | } |
37 | ||
10ff885b | 38 | static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct, |
36a580eb KS |
39 | __u64 key, __u64 ptr) |
40 | { | |
25b8d7de | 41 | *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr); |
36a580eb KS |
42 | } |
43 | ||
10ff885b | 44 | static int nilfs_direct_lookup(const struct nilfs_bmap *direct, |
36a580eb KS |
45 | __u64 key, int level, __u64 *ptrp) |
46 | { | |
36a580eb KS |
47 | __u64 ptr; |
48 | ||
5ee58148 JS |
49 | if (key > NILFS_DIRECT_KEY_MAX || level != 1) |
50 | return -ENOENT; | |
51 | ptr = nilfs_direct_get_ptr(direct, key); | |
52 | if (ptr == NILFS_BMAP_INVALID_PTR) | |
36a580eb KS |
53 | return -ENOENT; |
54 | ||
364ec2d7 | 55 | *ptrp = ptr; |
36a580eb KS |
56 | return 0; |
57 | } | |
58 | ||
10ff885b | 59 | static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct, |
c3a7abf0 | 60 | __u64 key, __u64 *ptrp, |
0c6c44cb | 61 | unsigned int maxblocks) |
c3a7abf0 | 62 | { |
c3a7abf0 RK |
63 | struct inode *dat = NULL; |
64 | __u64 ptr, ptr2; | |
65 | sector_t blocknr; | |
66 | int ret, cnt; | |
67 | ||
5ee58148 JS |
68 | if (key > NILFS_DIRECT_KEY_MAX) |
69 | return -ENOENT; | |
70 | ptr = nilfs_direct_get_ptr(direct, key); | |
71 | if (ptr == NILFS_BMAP_INVALID_PTR) | |
c3a7abf0 RK |
72 | return -ENOENT; |
73 | ||
10ff885b RK |
74 | if (NILFS_BMAP_USE_VBN(direct)) { |
75 | dat = nilfs_bmap_get_dat(direct); | |
c3a7abf0 RK |
76 | ret = nilfs_dat_translate(dat, ptr, &blocknr); |
77 | if (ret < 0) | |
78 | return ret; | |
79 | ptr = blocknr; | |
80 | } | |
81 | ||
0c6c44cb RK |
82 | maxblocks = min_t(unsigned int, maxblocks, |
83 | NILFS_DIRECT_KEY_MAX - key + 1); | |
c3a7abf0 RK |
84 | for (cnt = 1; cnt < maxblocks && |
85 | (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) != | |
86 | NILFS_BMAP_INVALID_PTR; | |
87 | cnt++) { | |
88 | if (dat) { | |
89 | ret = nilfs_dat_translate(dat, ptr2, &blocknr); | |
90 | if (ret < 0) | |
91 | return ret; | |
92 | ptr2 = blocknr; | |
93 | } | |
94 | if (ptr2 != ptr + cnt) | |
95 | break; | |
96 | } | |
97 | *ptrp = ptr; | |
98 | return cnt; | |
99 | } | |
100 | ||
36a580eb | 101 | static __u64 |
10ff885b | 102 | nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key) |
36a580eb KS |
103 | { |
104 | __u64 ptr; | |
105 | ||
10ff885b | 106 | ptr = nilfs_bmap_find_target_seq(direct, key); |
36a580eb KS |
107 | if (ptr != NILFS_BMAP_INVALID_PTR) |
108 | /* sequential access */ | |
109 | return ptr; | |
7f00184e RK |
110 | |
111 | /* block group */ | |
112 | return nilfs_bmap_find_target_in_group(direct); | |
36a580eb KS |
113 | } |
114 | ||
36a580eb KS |
115 | static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr) |
116 | { | |
36a580eb | 117 | union nilfs_bmap_ptr_req req; |
2e0c2c73 RK |
118 | struct inode *dat = NULL; |
119 | struct buffer_head *bh; | |
36a580eb KS |
120 | int ret; |
121 | ||
36a580eb KS |
122 | if (key > NILFS_DIRECT_KEY_MAX) |
123 | return -ENOENT; | |
10ff885b | 124 | if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR) |
36a580eb KS |
125 | return -EEXIST; |
126 | ||
2e0c2c73 | 127 | if (NILFS_BMAP_USE_VBN(bmap)) { |
10ff885b | 128 | req.bpr_ptr = nilfs_direct_find_target_v(bmap, key); |
2e0c2c73 RK |
129 | dat = nilfs_bmap_get_dat(bmap); |
130 | } | |
131 | ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat); | |
132 | if (!ret) { | |
133 | /* ptr must be a pointer to a buffer head. */ | |
134 | bh = (struct buffer_head *)((unsigned long)ptr); | |
135 | set_buffer_nilfs_volatile(bh); | |
36a580eb | 136 | |
2e0c2c73 | 137 | nilfs_bmap_commit_alloc_ptr(bmap, &req, dat); |
10ff885b | 138 | nilfs_direct_set_ptr(bmap, key, req.bpr_ptr); |
36a580eb | 139 | |
2e0c2c73 RK |
140 | if (!nilfs_bmap_dirty(bmap)) |
141 | nilfs_bmap_set_dirty(bmap); | |
36a580eb | 142 | |
2e0c2c73 | 143 | if (NILFS_BMAP_USE_VBN(bmap)) |
dc935be2 | 144 | nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr); |
36a580eb | 145 | |
be667377 | 146 | nilfs_inode_add_blocks(bmap->b_inode, 1); |
2e0c2c73 RK |
147 | } |
148 | return ret; | |
36a580eb KS |
149 | } |
150 | ||
151 | static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key) | |
152 | { | |
36a580eb | 153 | union nilfs_bmap_ptr_req req; |
2e0c2c73 | 154 | struct inode *dat; |
36a580eb KS |
155 | int ret; |
156 | ||
2e0c2c73 | 157 | if (key > NILFS_DIRECT_KEY_MAX || |
10ff885b | 158 | nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR) |
36a580eb KS |
159 | return -ENOENT; |
160 | ||
2e0c2c73 | 161 | dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL; |
10ff885b | 162 | req.bpr_ptr = nilfs_direct_get_ptr(bmap, key); |
36a580eb | 163 | |
2e0c2c73 RK |
164 | ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat); |
165 | if (!ret) { | |
166 | nilfs_bmap_commit_end_ptr(bmap, &req, dat); | |
10ff885b | 167 | nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR); |
be667377 | 168 | nilfs_inode_sub_blocks(bmap->b_inode, 1); |
2e0c2c73 RK |
169 | } |
170 | return ret; | |
36a580eb KS |
171 | } |
172 | ||
5b20384f RK |
173 | static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start, |
174 | __u64 *keyp) | |
175 | { | |
176 | __u64 key; | |
177 | ||
178 | for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) { | |
179 | if (nilfs_direct_get_ptr(direct, key) != | |
180 | NILFS_BMAP_INVALID_PTR) { | |
181 | *keyp = key; | |
182 | return 0; | |
183 | } | |
184 | } | |
185 | return -ENOENT; | |
186 | } | |
187 | ||
10ff885b | 188 | static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp) |
36a580eb | 189 | { |
36a580eb KS |
190 | __u64 key, lastkey; |
191 | ||
36a580eb KS |
192 | lastkey = NILFS_DIRECT_KEY_MAX + 1; |
193 | for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++) | |
194 | if (nilfs_direct_get_ptr(direct, key) != | |
195 | NILFS_BMAP_INVALID_PTR) | |
196 | lastkey = key; | |
197 | ||
198 | if (lastkey == NILFS_DIRECT_KEY_MAX + 1) | |
199 | return -ENOENT; | |
200 | ||
36a580eb KS |
201 | *keyp = lastkey; |
202 | ||
203 | return 0; | |
204 | } | |
205 | ||
206 | static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key) | |
207 | { | |
208 | return key > NILFS_DIRECT_KEY_MAX; | |
209 | } | |
210 | ||
10ff885b | 211 | static int nilfs_direct_gather_data(struct nilfs_bmap *direct, |
36a580eb KS |
212 | __u64 *keys, __u64 *ptrs, int nitems) |
213 | { | |
36a580eb KS |
214 | __u64 key; |
215 | __u64 ptr; | |
216 | int n; | |
217 | ||
36a580eb KS |
218 | if (nitems > NILFS_DIRECT_NBLOCKS) |
219 | nitems = NILFS_DIRECT_NBLOCKS; | |
220 | n = 0; | |
221 | for (key = 0; key < nitems; key++) { | |
222 | ptr = nilfs_direct_get_ptr(direct, key); | |
223 | if (ptr != NILFS_BMAP_INVALID_PTR) { | |
224 | keys[n] = key; | |
225 | ptrs[n] = ptr; | |
226 | n++; | |
227 | } | |
228 | } | |
229 | return n; | |
230 | } | |
231 | ||
232 | int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap, | |
3033342a | 233 | __u64 key, __u64 *keys, __u64 *ptrs, int n) |
36a580eb | 234 | { |
36a580eb KS |
235 | __le64 *dptrs; |
236 | int ret, i, j; | |
237 | ||
238 | /* no need to allocate any resource for conversion */ | |
239 | ||
240 | /* delete */ | |
8acfbf09 | 241 | ret = bmap->b_ops->bop_delete(bmap, key); |
36a580eb KS |
242 | if (ret < 0) |
243 | return ret; | |
244 | ||
245 | /* free resources */ | |
246 | if (bmap->b_ops->bop_clear != NULL) | |
8acfbf09 | 247 | bmap->b_ops->bop_clear(bmap); |
36a580eb KS |
248 | |
249 | /* convert */ | |
10ff885b | 250 | dptrs = nilfs_direct_dptrs(bmap); |
36a580eb KS |
251 | for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) { |
252 | if ((j < n) && (i == keys[j])) { | |
253 | dptrs[i] = (i != key) ? | |
25b8d7de | 254 | cpu_to_le64(ptrs[j]) : |
36a580eb KS |
255 | NILFS_BMAP_INVALID_PTR; |
256 | j++; | |
257 | } else | |
258 | dptrs[i] = NILFS_BMAP_INVALID_PTR; | |
259 | } | |
260 | ||
3033342a | 261 | nilfs_direct_init(bmap); |
36a580eb KS |
262 | return 0; |
263 | } | |
264 | ||
583ada47 | 265 | static int nilfs_direct_propagate(struct nilfs_bmap *bmap, |
2e0c2c73 | 266 | struct buffer_head *bh) |
36a580eb | 267 | { |
2e0c2c73 RK |
268 | struct nilfs_palloc_req oldreq, newreq; |
269 | struct inode *dat; | |
36a580eb KS |
270 | __u64 key; |
271 | __u64 ptr; | |
272 | int ret; | |
273 | ||
2e0c2c73 RK |
274 | if (!NILFS_BMAP_USE_VBN(bmap)) |
275 | return 0; | |
276 | ||
277 | dat = nilfs_bmap_get_dat(bmap); | |
278 | key = nilfs_bmap_data_get_key(bmap, bh); | |
10ff885b | 279 | ptr = nilfs_direct_get_ptr(bmap, key); |
36a580eb | 280 | if (!buffer_nilfs_volatile(bh)) { |
2e0c2c73 RK |
281 | oldreq.pr_entry_nr = ptr; |
282 | newreq.pr_entry_nr = ptr; | |
283 | ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq); | |
36a580eb KS |
284 | if (ret < 0) |
285 | return ret; | |
2e0c2c73 RK |
286 | nilfs_dat_commit_update(dat, &oldreq, &newreq, |
287 | bmap->b_ptr_type == NILFS_BMAP_PTR_VS); | |
36a580eb | 288 | set_buffer_nilfs_volatile(bh); |
10ff885b | 289 | nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr); |
36a580eb | 290 | } else |
2e0c2c73 | 291 | ret = nilfs_dat_mark_dirty(dat, ptr); |
36a580eb KS |
292 | |
293 | return ret; | |
294 | } | |
295 | ||
10ff885b | 296 | static int nilfs_direct_assign_v(struct nilfs_bmap *direct, |
36a580eb KS |
297 | __u64 key, __u64 ptr, |
298 | struct buffer_head **bh, | |
299 | sector_t blocknr, | |
300 | union nilfs_binfo *binfo) | |
301 | { | |
10ff885b | 302 | struct inode *dat = nilfs_bmap_get_dat(direct); |
36a580eb KS |
303 | union nilfs_bmap_ptr_req req; |
304 | int ret; | |
305 | ||
306 | req.bpr_ptr = ptr; | |
2e0c2c73 RK |
307 | ret = nilfs_dat_prepare_start(dat, &req.bpr_req); |
308 | if (!ret) { | |
309 | nilfs_dat_commit_start(dat, &req.bpr_req, blocknr); | |
25b8d7de RK |
310 | binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr); |
311 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); | |
2e0c2c73 RK |
312 | } |
313 | return ret; | |
36a580eb KS |
314 | } |
315 | ||
10ff885b | 316 | static int nilfs_direct_assign_p(struct nilfs_bmap *direct, |
36a580eb KS |
317 | __u64 key, __u64 ptr, |
318 | struct buffer_head **bh, | |
319 | sector_t blocknr, | |
320 | union nilfs_binfo *binfo) | |
321 | { | |
322 | nilfs_direct_set_ptr(direct, key, blocknr); | |
323 | ||
25b8d7de | 324 | binfo->bi_dat.bi_blkoff = cpu_to_le64(key); |
36a580eb KS |
325 | binfo->bi_dat.bi_level = 0; |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | static int nilfs_direct_assign(struct nilfs_bmap *bmap, | |
331 | struct buffer_head **bh, | |
332 | sector_t blocknr, | |
333 | union nilfs_binfo *binfo) | |
334 | { | |
36a580eb KS |
335 | __u64 key; |
336 | __u64 ptr; | |
337 | ||
36a580eb | 338 | key = nilfs_bmap_data_get_key(bmap, *bh); |
1f5abe7e | 339 | if (unlikely(key > NILFS_DIRECT_KEY_MAX)) { |
feee880f RK |
340 | nilfs_msg(bmap->b_inode->i_sb, KERN_CRIT, |
341 | "%s (ino=%lu): invalid key: %llu", __func__, | |
342 | bmap->b_inode->i_ino, (unsigned long long)key); | |
1f5abe7e RK |
343 | return -EINVAL; |
344 | } | |
10ff885b | 345 | ptr = nilfs_direct_get_ptr(bmap, key); |
1f5abe7e | 346 | if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) { |
feee880f RK |
347 | nilfs_msg(bmap->b_inode->i_sb, KERN_CRIT, |
348 | "%s (ino=%lu): invalid pointer: %llu", __func__, | |
349 | bmap->b_inode->i_ino, (unsigned long long)ptr); | |
1f5abe7e RK |
350 | return -EINVAL; |
351 | } | |
36a580eb | 352 | |
355c6b61 | 353 | return NILFS_BMAP_USE_VBN(bmap) ? |
10ff885b RK |
354 | nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) : |
355 | nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo); | |
36a580eb KS |
356 | } |
357 | ||
358 | static const struct nilfs_bmap_operations nilfs_direct_ops = { | |
359 | .bop_lookup = nilfs_direct_lookup, | |
c3a7abf0 | 360 | .bop_lookup_contig = nilfs_direct_lookup_contig, |
36a580eb KS |
361 | .bop_insert = nilfs_direct_insert, |
362 | .bop_delete = nilfs_direct_delete, | |
363 | .bop_clear = NULL, | |
364 | ||
365 | .bop_propagate = nilfs_direct_propagate, | |
366 | ||
367 | .bop_lookup_dirty_buffers = NULL, | |
368 | ||
369 | .bop_assign = nilfs_direct_assign, | |
370 | .bop_mark = NULL, | |
371 | ||
5b20384f | 372 | .bop_seek_key = nilfs_direct_seek_key, |
36a580eb | 373 | .bop_last_key = nilfs_direct_last_key, |
5b20384f | 374 | |
36a580eb KS |
375 | .bop_check_insert = nilfs_direct_check_insert, |
376 | .bop_check_delete = NULL, | |
377 | .bop_gather_data = nilfs_direct_gather_data, | |
378 | }; | |
379 | ||
380 | ||
3033342a | 381 | int nilfs_direct_init(struct nilfs_bmap *bmap) |
36a580eb | 382 | { |
36a580eb | 383 | bmap->b_ops = &nilfs_direct_ops; |
36a580eb KS |
384 | return 0; |
385 | } |