]>
Commit | Line | Data |
---|---|---|
ae98043f | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bdb265ea | 2 | /* |
94ee1d91 | 3 | * NILFS block mapping. |
bdb265ea KS |
4 | * |
5 | * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation. | |
6 | * | |
4b420ab4 | 7 | * Written by Koji Sato. |
bdb265ea KS |
8 | */ |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/errno.h> | |
13 | #include "nilfs.h" | |
14 | #include "bmap.h" | |
05d0e94b RK |
15 | #include "btree.h" |
16 | #include "direct.h" | |
bdb265ea KS |
17 | #include "btnode.h" |
18 | #include "mdt.h" | |
19 | #include "dat.h" | |
20 | #include "alloc.h" | |
21 | ||
c3a7abf0 | 22 | struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap) |
d4b96157 | 23 | { |
0ef28f9a RK |
24 | struct the_nilfs *nilfs = bmap->b_inode->i_sb->s_fs_info; |
25 | ||
26 | return nilfs->ns_dat; | |
d4b96157 RK |
27 | } |
28 | ||
e828949e RK |
29 | static int nilfs_bmap_convert_error(struct nilfs_bmap *bmap, |
30 | const char *fname, int err) | |
31 | { | |
32 | struct inode *inode = bmap->b_inode; | |
33 | ||
34 | if (err == -EINVAL) { | |
cae3d4ca RK |
35 | __nilfs_error(inode->i_sb, fname, |
36 | "broken bmap (inode number=%lu)", inode->i_ino); | |
e828949e RK |
37 | err = -EIO; |
38 | } | |
39 | return err; | |
40 | } | |
41 | ||
0f3fe33b RK |
42 | /** |
43 | * nilfs_bmap_lookup_at_level - find a data block or node block | |
44 | * @bmap: bmap | |
45 | * @key: key | |
46 | * @level: level | |
47 | * @ptrp: place to store the value associated to @key | |
48 | * | |
49 | * Description: nilfs_bmap_lookup_at_level() finds a record whose key | |
50 | * matches @key in the block at @level of the bmap. | |
51 | * | |
52 | * Return Value: On success, 0 is returned and the record associated with @key | |
53 | * is stored in the place pointed by @ptrp. On error, one of the following | |
54 | * negative error codes is returned. | |
55 | * | |
56 | * %-EIO - I/O error. | |
57 | * | |
58 | * %-ENOMEM - Insufficient amount of memory available. | |
59 | * | |
60 | * %-ENOENT - A record associated with @key does not exist. | |
61 | */ | |
bdb265ea KS |
62 | int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level, |
63 | __u64 *ptrp) | |
64 | { | |
d4b96157 | 65 | sector_t blocknr; |
bdb265ea KS |
66 | int ret; |
67 | ||
68 | down_read(&bmap->b_sem); | |
8acfbf09 | 69 | ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp); |
a6a491c0 | 70 | if (ret < 0) |
bdb265ea | 71 | goto out; |
a6a491c0 | 72 | |
d4b96157 RK |
73 | if (NILFS_BMAP_USE_VBN(bmap)) { |
74 | ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp, | |
75 | &blocknr); | |
76 | if (!ret) | |
77 | *ptrp = blocknr; | |
a6a491c0 RK |
78 | else if (ret == -ENOENT) { |
79 | /* | |
80 | * If there was no valid entry in DAT for the block | |
81 | * address obtained by b_ops->bop_lookup, then pass | |
82 | * internal code -EINVAL to nilfs_bmap_convert_error | |
83 | * to treat it as metadata corruption. | |
84 | */ | |
85 | ret = -EINVAL; | |
86 | } | |
bdb265ea KS |
87 | } |
88 | ||
89 | out: | |
90 | up_read(&bmap->b_sem); | |
a6a491c0 | 91 | return nilfs_bmap_convert_error(bmap, __func__, ret); |
bdb265ea KS |
92 | } |
93 | ||
c3a7abf0 | 94 | int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp, |
0c6c44cb | 95 | unsigned int maxblocks) |
c3a7abf0 RK |
96 | { |
97 | int ret; | |
98 | ||
99 | down_read(&bmap->b_sem); | |
100 | ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks); | |
101 | up_read(&bmap->b_sem); | |
e828949e RK |
102 | |
103 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
c3a7abf0 | 104 | } |
bdb265ea | 105 | |
bdb265ea KS |
106 | static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr) |
107 | { | |
108 | __u64 keys[NILFS_BMAP_SMALL_HIGH + 1]; | |
109 | __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1]; | |
110 | int ret, n; | |
111 | ||
112 | if (bmap->b_ops->bop_check_insert != NULL) { | |
8acfbf09 | 113 | ret = bmap->b_ops->bop_check_insert(bmap, key); |
bdb265ea | 114 | if (ret > 0) { |
8acfbf09 | 115 | n = bmap->b_ops->bop_gather_data( |
bdb265ea KS |
116 | bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1); |
117 | if (n < 0) | |
118 | return n; | |
119 | ret = nilfs_btree_convert_and_insert( | |
3033342a | 120 | bmap, key, ptr, keys, ptrs, n); |
bdb265ea KS |
121 | if (ret == 0) |
122 | bmap->b_u.u_flags |= NILFS_BMAP_LARGE; | |
123 | ||
124 | return ret; | |
125 | } else if (ret < 0) | |
126 | return ret; | |
127 | } | |
128 | ||
8acfbf09 | 129 | return bmap->b_ops->bop_insert(bmap, key, ptr); |
bdb265ea KS |
130 | } |
131 | ||
132 | /** | |
133 | * nilfs_bmap_insert - insert a new key-record pair into a bmap | |
134 | * @bmap: bmap | |
135 | * @key: key | |
136 | * @rec: record | |
137 | * | |
138 | * Description: nilfs_bmap_insert() inserts the new key-record pair specified | |
139 | * by @key and @rec into @bmap. | |
140 | * | |
141 | * Return Value: On success, 0 is returned. On error, one of the following | |
142 | * negative error codes is returned. | |
143 | * | |
144 | * %-EIO - I/O error. | |
145 | * | |
146 | * %-ENOMEM - Insufficient amount of memory available. | |
147 | * | |
148 | * %-EEXIST - A record associated with @key already exist. | |
149 | */ | |
3568a13f | 150 | int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec) |
bdb265ea KS |
151 | { |
152 | int ret; | |
153 | ||
154 | down_write(&bmap->b_sem); | |
155 | ret = nilfs_bmap_do_insert(bmap, key, rec); | |
156 | up_write(&bmap->b_sem); | |
e828949e RK |
157 | |
158 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
159 | } |
160 | ||
161 | static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key) | |
162 | { | |
163 | __u64 keys[NILFS_BMAP_LARGE_LOW + 1]; | |
164 | __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1]; | |
165 | int ret, n; | |
166 | ||
167 | if (bmap->b_ops->bop_check_delete != NULL) { | |
8acfbf09 | 168 | ret = bmap->b_ops->bop_check_delete(bmap, key); |
bdb265ea | 169 | if (ret > 0) { |
8acfbf09 | 170 | n = bmap->b_ops->bop_gather_data( |
bdb265ea KS |
171 | bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1); |
172 | if (n < 0) | |
173 | return n; | |
174 | ret = nilfs_direct_delete_and_convert( | |
3033342a | 175 | bmap, key, keys, ptrs, n); |
bdb265ea KS |
176 | if (ret == 0) |
177 | bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE; | |
178 | ||
179 | return ret; | |
180 | } else if (ret < 0) | |
181 | return ret; | |
182 | } | |
183 | ||
8acfbf09 | 184 | return bmap->b_ops->bop_delete(bmap, key); |
bdb265ea KS |
185 | } |
186 | ||
5b20384f RK |
187 | /** |
188 | * nilfs_bmap_seek_key - seek a valid entry and return its key | |
189 | * @bmap: bmap struct | |
190 | * @start: start key number | |
191 | * @keyp: place to store valid key | |
192 | * | |
193 | * Description: nilfs_bmap_seek_key() seeks a valid key on @bmap | |
194 | * starting from @start, and stores it to @keyp if found. | |
195 | * | |
196 | * Return Value: On success, 0 is returned. On error, one of the following | |
197 | * negative error codes is returned. | |
198 | * | |
199 | * %-EIO - I/O error. | |
200 | * | |
201 | * %-ENOMEM - Insufficient amount of memory available. | |
202 | * | |
203 | * %-ENOENT - No valid entry was found | |
204 | */ | |
205 | int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp) | |
206 | { | |
207 | int ret; | |
208 | ||
209 | down_read(&bmap->b_sem); | |
210 | ret = bmap->b_ops->bop_seek_key(bmap, start, keyp); | |
211 | up_read(&bmap->b_sem); | |
212 | ||
213 | if (ret < 0) | |
214 | ret = nilfs_bmap_convert_error(bmap, __func__, ret); | |
215 | return ret; | |
216 | } | |
217 | ||
3568a13f | 218 | int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp) |
bdb265ea | 219 | { |
bdb265ea KS |
220 | int ret; |
221 | ||
222 | down_read(&bmap->b_sem); | |
3568a13f | 223 | ret = bmap->b_ops->bop_last_key(bmap, keyp); |
bdb265ea | 224 | up_read(&bmap->b_sem); |
e828949e RK |
225 | |
226 | if (ret < 0) | |
227 | ret = nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
228 | return ret; |
229 | } | |
230 | ||
231 | /** | |
232 | * nilfs_bmap_delete - delete a key-record pair from a bmap | |
233 | * @bmap: bmap | |
234 | * @key: key | |
235 | * | |
236 | * Description: nilfs_bmap_delete() deletes the key-record pair specified by | |
237 | * @key from @bmap. | |
238 | * | |
239 | * Return Value: On success, 0 is returned. On error, one of the following | |
240 | * negative error codes is returned. | |
241 | * | |
242 | * %-EIO - I/O error. | |
243 | * | |
244 | * %-ENOMEM - Insufficient amount of memory available. | |
245 | * | |
246 | * %-ENOENT - A record associated with @key does not exist. | |
247 | */ | |
3568a13f | 248 | int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key) |
bdb265ea KS |
249 | { |
250 | int ret; | |
251 | ||
252 | down_write(&bmap->b_sem); | |
253 | ret = nilfs_bmap_do_delete(bmap, key); | |
254 | up_write(&bmap->b_sem); | |
e828949e RK |
255 | |
256 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
257 | } |
258 | ||
3568a13f | 259 | static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, __u64 key) |
bdb265ea KS |
260 | { |
261 | __u64 lastkey; | |
262 | int ret; | |
263 | ||
8acfbf09 | 264 | ret = bmap->b_ops->bop_last_key(bmap, &lastkey); |
bdb265ea KS |
265 | if (ret < 0) { |
266 | if (ret == -ENOENT) | |
267 | ret = 0; | |
268 | return ret; | |
269 | } | |
270 | ||
271 | while (key <= lastkey) { | |
272 | ret = nilfs_bmap_do_delete(bmap, lastkey); | |
273 | if (ret < 0) | |
274 | return ret; | |
8acfbf09 | 275 | ret = bmap->b_ops->bop_last_key(bmap, &lastkey); |
bdb265ea KS |
276 | if (ret < 0) { |
277 | if (ret == -ENOENT) | |
278 | ret = 0; | |
279 | return ret; | |
280 | } | |
281 | } | |
282 | return 0; | |
283 | } | |
284 | ||
285 | /** | |
286 | * nilfs_bmap_truncate - truncate a bmap to a specified key | |
287 | * @bmap: bmap | |
288 | * @key: key | |
289 | * | |
290 | * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are | |
291 | * greater than or equal to @key from @bmap. | |
292 | * | |
293 | * Return Value: On success, 0 is returned. On error, one of the following | |
294 | * negative error codes is returned. | |
295 | * | |
296 | * %-EIO - I/O error. | |
297 | * | |
298 | * %-ENOMEM - Insufficient amount of memory available. | |
299 | */ | |
3568a13f | 300 | int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key) |
bdb265ea KS |
301 | { |
302 | int ret; | |
303 | ||
304 | down_write(&bmap->b_sem); | |
305 | ret = nilfs_bmap_do_truncate(bmap, key); | |
306 | up_write(&bmap->b_sem); | |
e828949e RK |
307 | |
308 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
309 | } |
310 | ||
311 | /** | |
312 | * nilfs_bmap_clear - free resources a bmap holds | |
313 | * @bmap: bmap | |
314 | * | |
315 | * Description: nilfs_bmap_clear() frees resources associated with @bmap. | |
316 | */ | |
317 | void nilfs_bmap_clear(struct nilfs_bmap *bmap) | |
318 | { | |
319 | down_write(&bmap->b_sem); | |
320 | if (bmap->b_ops->bop_clear != NULL) | |
8acfbf09 | 321 | bmap->b_ops->bop_clear(bmap); |
bdb265ea KS |
322 | up_write(&bmap->b_sem); |
323 | } | |
324 | ||
325 | /** | |
326 | * nilfs_bmap_propagate - propagate dirty state | |
327 | * @bmap: bmap | |
328 | * @bh: buffer head | |
329 | * | |
330 | * Description: nilfs_bmap_propagate() marks the buffers that directly or | |
331 | * indirectly refer to the block specified by @bh dirty. | |
332 | * | |
333 | * Return Value: On success, 0 is returned. On error, one of the following | |
334 | * negative error codes is returned. | |
335 | * | |
336 | * %-EIO - I/O error. | |
337 | * | |
338 | * %-ENOMEM - Insufficient amount of memory available. | |
339 | */ | |
340 | int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh) | |
341 | { | |
342 | int ret; | |
343 | ||
344 | down_write(&bmap->b_sem); | |
8acfbf09 | 345 | ret = bmap->b_ops->bop_propagate(bmap, bh); |
bdb265ea | 346 | up_write(&bmap->b_sem); |
e828949e RK |
347 | |
348 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
349 | } |
350 | ||
351 | /** | |
352 | * nilfs_bmap_lookup_dirty_buffers - | |
353 | * @bmap: bmap | |
354 | * @listp: pointer to buffer head list | |
355 | */ | |
356 | void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap, | |
357 | struct list_head *listp) | |
358 | { | |
359 | if (bmap->b_ops->bop_lookup_dirty_buffers != NULL) | |
8acfbf09 | 360 | bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp); |
bdb265ea KS |
361 | } |
362 | ||
363 | /** | |
364 | * nilfs_bmap_assign - assign a new block number to a block | |
365 | * @bmap: bmap | |
64ead520 | 366 | * @bh: pointer to buffer head |
bdb265ea KS |
367 | * @blocknr: block number |
368 | * @binfo: block information | |
369 | * | |
370 | * Description: nilfs_bmap_assign() assigns the block number @blocknr to the | |
371 | * buffer specified by @bh. | |
372 | * | |
373 | * Return Value: On success, 0 is returned and the buffer head of a newly | |
374 | * create buffer and the block information associated with the buffer are | |
375 | * stored in the place pointed by @bh and @binfo, respectively. On error, one | |
376 | * of the following negative error codes is returned. | |
377 | * | |
378 | * %-EIO - I/O error. | |
379 | * | |
380 | * %-ENOMEM - Insufficient amount of memory available. | |
381 | */ | |
382 | int nilfs_bmap_assign(struct nilfs_bmap *bmap, | |
383 | struct buffer_head **bh, | |
384 | unsigned long blocknr, | |
385 | union nilfs_binfo *binfo) | |
386 | { | |
387 | int ret; | |
388 | ||
389 | down_write(&bmap->b_sem); | |
8acfbf09 | 390 | ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo); |
bdb265ea | 391 | up_write(&bmap->b_sem); |
e828949e RK |
392 | |
393 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
394 | } |
395 | ||
396 | /** | |
397 | * nilfs_bmap_mark - mark block dirty | |
398 | * @bmap: bmap | |
399 | * @key: key | |
400 | * @level: level | |
401 | * | |
402 | * Description: nilfs_bmap_mark() marks the block specified by @key and @level | |
403 | * as dirty. | |
404 | * | |
405 | * Return Value: On success, 0 is returned. On error, one of the following | |
406 | * negative error codes is returned. | |
407 | * | |
408 | * %-EIO - I/O error. | |
409 | * | |
410 | * %-ENOMEM - Insufficient amount of memory available. | |
411 | */ | |
412 | int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level) | |
413 | { | |
414 | int ret; | |
415 | ||
416 | if (bmap->b_ops->bop_mark == NULL) | |
417 | return 0; | |
418 | ||
419 | down_write(&bmap->b_sem); | |
8acfbf09 | 420 | ret = bmap->b_ops->bop_mark(bmap, key, level); |
bdb265ea | 421 | up_write(&bmap->b_sem); |
e828949e RK |
422 | |
423 | return nilfs_bmap_convert_error(bmap, __func__, ret); | |
bdb265ea KS |
424 | } |
425 | ||
426 | /** | |
427 | * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state | |
428 | * @bmap: bmap | |
429 | * | |
430 | * Description: nilfs_test_and_clear() is the atomic operation to test and | |
431 | * clear the dirty state of @bmap. | |
432 | * | |
433 | * Return Value: 1 is returned if @bmap is dirty, or 0 if clear. | |
434 | */ | |
435 | int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap) | |
436 | { | |
437 | int ret; | |
438 | ||
439 | down_write(&bmap->b_sem); | |
440 | ret = nilfs_bmap_dirty(bmap); | |
441 | nilfs_bmap_clear_dirty(bmap); | |
442 | up_write(&bmap->b_sem); | |
443 | return ret; | |
444 | } | |
445 | ||
446 | ||
447 | /* | |
448 | * Internal use only | |
449 | */ | |
bdb265ea KS |
450 | __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap, |
451 | const struct buffer_head *bh) | |
452 | { | |
453 | struct buffer_head *pbh; | |
454 | __u64 key; | |
455 | ||
09cbfeaf | 456 | key = page_index(bh->b_page) << (PAGE_SHIFT - |
bdb265ea | 457 | bmap->b_inode->i_blkbits); |
5ee58148 JS |
458 | for (pbh = page_buffers(bh->b_page); pbh != bh; pbh = pbh->b_this_page) |
459 | key++; | |
bdb265ea KS |
460 | |
461 | return key; | |
462 | } | |
463 | ||
464 | __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key) | |
465 | { | |
466 | __s64 diff; | |
467 | ||
468 | diff = key - bmap->b_last_allocated_key; | |
469 | if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) && | |
470 | (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) && | |
471 | (bmap->b_last_allocated_ptr + diff > 0)) | |
472 | return bmap->b_last_allocated_ptr + diff; | |
473 | else | |
474 | return NILFS_BMAP_INVALID_PTR; | |
475 | } | |
476 | ||
bdb265ea KS |
477 | #define NILFS_BMAP_GROUP_DIV 8 |
478 | __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap) | |
479 | { | |
480 | struct inode *dat = nilfs_bmap_get_dat(bmap); | |
481 | unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat); | |
482 | unsigned long group = bmap->b_inode->i_ino / entries_per_group; | |
483 | ||
484 | return group * entries_per_group + | |
485 | (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) * | |
486 | (entries_per_group / NILFS_BMAP_GROUP_DIV); | |
487 | } | |
488 | ||
bcb48891 | 489 | static struct lock_class_key nilfs_bmap_dat_lock_key; |
ff54de36 | 490 | static struct lock_class_key nilfs_bmap_mdt_lock_key; |
bcb48891 | 491 | |
bdb265ea KS |
492 | /** |
493 | * nilfs_bmap_read - read a bmap from an inode | |
494 | * @bmap: bmap | |
495 | * @raw_inode: on-disk inode | |
496 | * | |
497 | * Description: nilfs_bmap_read() initializes the bmap @bmap. | |
498 | * | |
499 | * Return Value: On success, 0 is returned. On error, the following negative | |
500 | * error code is returned. | |
501 | * | |
502 | * %-ENOMEM - Insufficient amount of memory available. | |
503 | */ | |
504 | int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode) | |
505 | { | |
506 | if (raw_inode == NULL) | |
507 | memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE); | |
508 | else | |
509 | memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE); | |
510 | ||
511 | init_rwsem(&bmap->b_sem); | |
512 | bmap->b_state = 0; | |
513 | bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode; | |
514 | switch (bmap->b_inode->i_ino) { | |
515 | case NILFS_DAT_INO: | |
d4b96157 RK |
516 | bmap->b_ptr_type = NILFS_BMAP_PTR_P; |
517 | bmap->b_last_allocated_key = 0; | |
bdb265ea | 518 | bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT; |
bcb48891 | 519 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key); |
bdb265ea KS |
520 | break; |
521 | case NILFS_CPFILE_INO: | |
522 | case NILFS_SUFILE_INO: | |
d4b96157 RK |
523 | bmap->b_ptr_type = NILFS_BMAP_PTR_VS; |
524 | bmap->b_last_allocated_key = 0; | |
bdb265ea | 525 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; |
ff54de36 | 526 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key); |
bdb265ea | 527 | break; |
ff54de36 RK |
528 | case NILFS_IFILE_INO: |
529 | lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key); | |
df561f66 | 530 | fallthrough; |
bdb265ea | 531 | default: |
d4b96157 RK |
532 | bmap->b_ptr_type = NILFS_BMAP_PTR_VM; |
533 | bmap->b_last_allocated_key = 0; | |
bdb265ea KS |
534 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; |
535 | break; | |
536 | } | |
537 | ||
538 | return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ? | |
3033342a | 539 | nilfs_btree_init(bmap) : nilfs_direct_init(bmap); |
bdb265ea KS |
540 | } |
541 | ||
542 | /** | |
543 | * nilfs_bmap_write - write back a bmap to an inode | |
544 | * @bmap: bmap | |
545 | * @raw_inode: on-disk inode | |
546 | * | |
547 | * Description: nilfs_bmap_write() stores @bmap in @raw_inode. | |
548 | */ | |
549 | void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode) | |
550 | { | |
551 | down_write(&bmap->b_sem); | |
552 | memcpy(raw_inode->i_bmap, bmap->b_u.u_data, | |
553 | NILFS_INODE_BMAP_SIZE * sizeof(__le64)); | |
554 | if (bmap->b_inode->i_ino == NILFS_DAT_INO) | |
555 | bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT; | |
556 | ||
557 | up_write(&bmap->b_sem); | |
558 | } | |
559 | ||
560 | void nilfs_bmap_init_gc(struct nilfs_bmap *bmap) | |
561 | { | |
562 | memset(&bmap->b_u, 0, NILFS_BMAP_SIZE); | |
563 | init_rwsem(&bmap->b_sem); | |
564 | bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode; | |
d4b96157 | 565 | bmap->b_ptr_type = NILFS_BMAP_PTR_U; |
bdb265ea KS |
566 | bmap->b_last_allocated_key = 0; |
567 | bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR; | |
568 | bmap->b_state = 0; | |
569 | nilfs_btree_init_gc(bmap); | |
570 | } | |
571 | ||
a8070dd3 RK |
572 | void nilfs_bmap_save(const struct nilfs_bmap *bmap, |
573 | struct nilfs_bmap_store *store) | |
574 | { | |
575 | memcpy(store->data, bmap->b_u.u_data, sizeof(store->data)); | |
576 | store->last_allocated_key = bmap->b_last_allocated_key; | |
577 | store->last_allocated_ptr = bmap->b_last_allocated_ptr; | |
578 | store->state = bmap->b_state; | |
579 | } | |
580 | ||
581 | void nilfs_bmap_restore(struct nilfs_bmap *bmap, | |
582 | const struct nilfs_bmap_store *store) | |
583 | { | |
584 | memcpy(bmap->b_u.u_data, store->data, sizeof(store->data)); | |
585 | bmap->b_last_allocated_key = store->last_allocated_key; | |
586 | bmap->b_last_allocated_ptr = store->last_allocated_ptr; | |
587 | bmap->b_state = store->state; | |
588 | } |