diff options
Diffstat (limited to 'fs/btrfs/free-space-cache.c')
-rw-r--r-- | fs/btrfs/free-space-cache.c | 926 |
1 files changed, 564 insertions, 362 deletions
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 41ac927401d0..7a15fcfb3e1f 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c | |||
@@ -20,6 +20,7 @@ | |||
20 | #include <linux/sched.h> | 20 | #include <linux/sched.h> |
21 | #include <linux/slab.h> | 21 | #include <linux/slab.h> |
22 | #include <linux/math64.h> | 22 | #include <linux/math64.h> |
23 | #include <linux/ratelimit.h> | ||
23 | #include "ctree.h" | 24 | #include "ctree.h" |
24 | #include "free-space-cache.h" | 25 | #include "free-space-cache.h" |
25 | #include "transaction.h" | 26 | #include "transaction.h" |
@@ -84,6 +85,7 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root, | |||
84 | *block_group, struct btrfs_path *path) | 85 | *block_group, struct btrfs_path *path) |
85 | { | 86 | { |
86 | struct inode *inode = NULL; | 87 | struct inode *inode = NULL; |
88 | u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; | ||
87 | 89 | ||
88 | spin_lock(&block_group->lock); | 90 | spin_lock(&block_group->lock); |
89 | if (block_group->inode) | 91 | if (block_group->inode) |
@@ -98,13 +100,14 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root, | |||
98 | return inode; | 100 | return inode; |
99 | 101 | ||
100 | spin_lock(&block_group->lock); | 102 | spin_lock(&block_group->lock); |
101 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) { | 103 | if (!((BTRFS_I(inode)->flags & flags) == flags)) { |
102 | printk(KERN_INFO "Old style space inode found, converting.\n"); | 104 | printk(KERN_INFO "Old style space inode found, converting.\n"); |
103 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NODATASUM; | 105 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM | |
106 | BTRFS_INODE_NODATACOW; | ||
104 | block_group->disk_cache_state = BTRFS_DC_CLEAR; | 107 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
105 | } | 108 | } |
106 | 109 | ||
107 | if (!btrfs_fs_closing(root->fs_info)) { | 110 | if (!block_group->iref) { |
108 | block_group->inode = igrab(inode); | 111 | block_group->inode = igrab(inode); |
109 | block_group->iref = 1; | 112 | block_group->iref = 1; |
110 | } | 113 | } |
@@ -122,12 +125,17 @@ int __create_free_space_inode(struct btrfs_root *root, | |||
122 | struct btrfs_free_space_header *header; | 125 | struct btrfs_free_space_header *header; |
123 | struct btrfs_inode_item *inode_item; | 126 | struct btrfs_inode_item *inode_item; |
124 | struct extent_buffer *leaf; | 127 | struct extent_buffer *leaf; |
128 | u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC; | ||
125 | int ret; | 129 | int ret; |
126 | 130 | ||
127 | ret = btrfs_insert_empty_inode(trans, root, path, ino); | 131 | ret = btrfs_insert_empty_inode(trans, root, path, ino); |
128 | if (ret) | 132 | if (ret) |
129 | return ret; | 133 | return ret; |
130 | 134 | ||
135 | /* We inline crc's for the free disk space cache */ | ||
136 | if (ino != BTRFS_FREE_INO_OBJECTID) | ||
137 | flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; | ||
138 | |||
131 | leaf = path->nodes[0]; | 139 | leaf = path->nodes[0]; |
132 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | 140 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
133 | struct btrfs_inode_item); | 141 | struct btrfs_inode_item); |
@@ -140,8 +148,7 @@ int __create_free_space_inode(struct btrfs_root *root, | |||
140 | btrfs_set_inode_uid(leaf, inode_item, 0); | 148 | btrfs_set_inode_uid(leaf, inode_item, 0); |
141 | btrfs_set_inode_gid(leaf, inode_item, 0); | 149 | btrfs_set_inode_gid(leaf, inode_item, 0); |
142 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); | 150 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); |
143 | btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS | | 151 | btrfs_set_inode_flags(leaf, inode_item, flags); |
144 | BTRFS_INODE_PREALLOC); | ||
145 | btrfs_set_inode_nlink(leaf, inode_item, 1); | 152 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
146 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); | 153 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); |
147 | btrfs_set_inode_block_group(leaf, inode_item, offset); | 154 | btrfs_set_inode_block_group(leaf, inode_item, offset); |
@@ -191,16 +198,24 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root, | |||
191 | struct inode *inode) | 198 | struct inode *inode) |
192 | { | 199 | { |
193 | struct btrfs_block_rsv *rsv; | 200 | struct btrfs_block_rsv *rsv; |
201 | u64 needed_bytes; | ||
194 | loff_t oldsize; | 202 | loff_t oldsize; |
195 | int ret = 0; | 203 | int ret = 0; |
196 | 204 | ||
197 | rsv = trans->block_rsv; | 205 | rsv = trans->block_rsv; |
198 | trans->block_rsv = root->orphan_block_rsv; | 206 | trans->block_rsv = &root->fs_info->global_block_rsv; |
199 | ret = btrfs_block_rsv_check(trans, root, | 207 | |
200 | root->orphan_block_rsv, | 208 | /* 1 for slack space, 1 for updating the inode */ |
201 | 0, 5); | 209 | needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) + |
202 | if (ret) | 210 | btrfs_calc_trans_metadata_size(root, 1); |
203 | return ret; | 211 | |
212 | spin_lock(&trans->block_rsv->lock); | ||
213 | if (trans->block_rsv->reserved < needed_bytes) { | ||
214 | spin_unlock(&trans->block_rsv->lock); | ||
215 | trans->block_rsv = rsv; | ||
216 | return -ENOSPC; | ||
217 | } | ||
218 | spin_unlock(&trans->block_rsv->lock); | ||
204 | 219 | ||
205 | oldsize = i_size_read(inode); | 220 | oldsize = i_size_read(inode); |
206 | btrfs_i_size_write(inode, 0); | 221 | btrfs_i_size_write(inode, 0); |
@@ -213,13 +228,15 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root, | |||
213 | ret = btrfs_truncate_inode_items(trans, root, inode, | 228 | ret = btrfs_truncate_inode_items(trans, root, inode, |
214 | 0, BTRFS_EXTENT_DATA_KEY); | 229 | 0, BTRFS_EXTENT_DATA_KEY); |
215 | 230 | ||
216 | trans->block_rsv = rsv; | ||
217 | if (ret) { | 231 | if (ret) { |
232 | trans->block_rsv = rsv; | ||
218 | WARN_ON(1); | 233 | WARN_ON(1); |
219 | return ret; | 234 | return ret; |
220 | } | 235 | } |
221 | 236 | ||
222 | ret = btrfs_update_inode(trans, root, inode); | 237 | ret = btrfs_update_inode(trans, root, inode); |
238 | trans->block_rsv = rsv; | ||
239 | |||
223 | return ret; | 240 | return ret; |
224 | } | 241 | } |
225 | 242 | ||
@@ -242,26 +259,342 @@ static int readahead_cache(struct inode *inode) | |||
242 | return 0; | 259 | return 0; |
243 | } | 260 | } |
244 | 261 | ||
262 | struct io_ctl { | ||
263 | void *cur, *orig; | ||
264 | struct page *page; | ||
265 | struct page **pages; | ||
266 | struct btrfs_root *root; | ||
267 | unsigned long size; | ||
268 | int index; | ||
269 | int num_pages; | ||
270 | unsigned check_crcs:1; | ||
271 | }; | ||
272 | |||
273 | static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, | ||
274 | struct btrfs_root *root) | ||
275 | { | ||
276 | memset(io_ctl, 0, sizeof(struct io_ctl)); | ||
277 | io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | ||
278 | PAGE_CACHE_SHIFT; | ||
279 | io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages, | ||
280 | GFP_NOFS); | ||
281 | if (!io_ctl->pages) | ||
282 | return -ENOMEM; | ||
283 | io_ctl->root = root; | ||
284 | if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID) | ||
285 | io_ctl->check_crcs = 1; | ||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | static void io_ctl_free(struct io_ctl *io_ctl) | ||
290 | { | ||
291 | kfree(io_ctl->pages); | ||
292 | } | ||
293 | |||
294 | static void io_ctl_unmap_page(struct io_ctl *io_ctl) | ||
295 | { | ||
296 | if (io_ctl->cur) { | ||
297 | kunmap(io_ctl->page); | ||
298 | io_ctl->cur = NULL; | ||
299 | io_ctl->orig = NULL; | ||
300 | } | ||
301 | } | ||
302 | |||
303 | static void io_ctl_map_page(struct io_ctl *io_ctl, int clear) | ||
304 | { | ||
305 | WARN_ON(io_ctl->cur); | ||
306 | BUG_ON(io_ctl->index >= io_ctl->num_pages); | ||
307 | io_ctl->page = io_ctl->pages[io_ctl->index++]; | ||
308 | io_ctl->cur = kmap(io_ctl->page); | ||
309 | io_ctl->orig = io_ctl->cur; | ||
310 | io_ctl->size = PAGE_CACHE_SIZE; | ||
311 | if (clear) | ||
312 | memset(io_ctl->cur, 0, PAGE_CACHE_SIZE); | ||
313 | } | ||
314 | |||
315 | static void io_ctl_drop_pages(struct io_ctl *io_ctl) | ||
316 | { | ||
317 | int i; | ||
318 | |||
319 | io_ctl_unmap_page(io_ctl); | ||
320 | |||
321 | for (i = 0; i < io_ctl->num_pages; i++) { | ||
322 | ClearPageChecked(io_ctl->pages[i]); | ||
323 | unlock_page(io_ctl->pages[i]); | ||
324 | page_cache_release(io_ctl->pages[i]); | ||
325 | } | ||
326 | } | ||
327 | |||
328 | static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode, | ||
329 | int uptodate) | ||
330 | { | ||
331 | struct page *page; | ||
332 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); | ||
333 | int i; | ||
334 | |||
335 | for (i = 0; i < io_ctl->num_pages; i++) { | ||
336 | page = find_or_create_page(inode->i_mapping, i, mask); | ||
337 | if (!page) { | ||
338 | io_ctl_drop_pages(io_ctl); | ||
339 | return -ENOMEM; | ||
340 | } | ||
341 | io_ctl->pages[i] = page; | ||
342 | if (uptodate && !PageUptodate(page)) { | ||
343 | btrfs_readpage(NULL, page); | ||
344 | lock_page(page); | ||
345 | if (!PageUptodate(page)) { | ||
346 | printk(KERN_ERR "btrfs: error reading free " | ||
347 | "space cache\n"); | ||
348 | io_ctl_drop_pages(io_ctl); | ||
349 | return -EIO; | ||
350 | } | ||
351 | } | ||
352 | } | ||
353 | |||
354 | return 0; | ||
355 | } | ||
356 | |||
357 | static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation) | ||
358 | { | ||
359 | u64 *val; | ||
360 | |||
361 | io_ctl_map_page(io_ctl, 1); | ||
362 | |||
363 | /* | ||
364 | * Skip the csum areas. If we don't check crcs then we just have a | ||
365 | * 64bit chunk at the front of the first page. | ||
366 | */ | ||
367 | if (io_ctl->check_crcs) { | ||
368 | io_ctl->cur += (sizeof(u32) * io_ctl->num_pages); | ||
369 | io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages); | ||
370 | } else { | ||
371 | io_ctl->cur += sizeof(u64); | ||
372 | io_ctl->size -= sizeof(u64) * 2; | ||
373 | } | ||
374 | |||
375 | val = io_ctl->cur; | ||
376 | *val = cpu_to_le64(generation); | ||
377 | io_ctl->cur += sizeof(u64); | ||
378 | } | ||
379 | |||
380 | static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation) | ||
381 | { | ||
382 | u64 *gen; | ||
383 | |||
384 | /* | ||
385 | * Skip the crc area. If we don't check crcs then we just have a 64bit | ||
386 | * chunk at the front of the first page. | ||
387 | */ | ||
388 | if (io_ctl->check_crcs) { | ||
389 | io_ctl->cur += sizeof(u32) * io_ctl->num_pages; | ||
390 | io_ctl->size -= sizeof(u64) + | ||
391 | (sizeof(u32) * io_ctl->num_pages); | ||
392 | } else { | ||
393 | io_ctl->cur += sizeof(u64); | ||
394 | io_ctl->size -= sizeof(u64) * 2; | ||
395 | } | ||
396 | |||
397 | gen = io_ctl->cur; | ||
398 | if (le64_to_cpu(*gen) != generation) { | ||
399 | printk_ratelimited(KERN_ERR "btrfs: space cache generation " | ||
400 | "(%Lu) does not match inode (%Lu)\n", *gen, | ||
401 | generation); | ||
402 | io_ctl_unmap_page(io_ctl); | ||
403 | return -EIO; | ||
404 | } | ||
405 | io_ctl->cur += sizeof(u64); | ||
406 | return 0; | ||
407 | } | ||
408 | |||
409 | static void io_ctl_set_crc(struct io_ctl *io_ctl, int index) | ||
410 | { | ||
411 | u32 *tmp; | ||
412 | u32 crc = ~(u32)0; | ||
413 | unsigned offset = 0; | ||
414 | |||
415 | if (!io_ctl->check_crcs) { | ||
416 | io_ctl_unmap_page(io_ctl); | ||
417 | return; | ||
418 | } | ||
419 | |||
420 | if (index == 0) | ||
421 | offset = sizeof(u32) * io_ctl->num_pages;; | ||
422 | |||
423 | crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc, | ||
424 | PAGE_CACHE_SIZE - offset); | ||
425 | btrfs_csum_final(crc, (char *)&crc); | ||
426 | io_ctl_unmap_page(io_ctl); | ||
427 | tmp = kmap(io_ctl->pages[0]); | ||
428 | tmp += index; | ||
429 | *tmp = crc; | ||
430 | kunmap(io_ctl->pages[0]); | ||
431 | } | ||
432 | |||
433 | static int io_ctl_check_crc(struct io_ctl *io_ctl, int index) | ||
434 | { | ||
435 | u32 *tmp, val; | ||
436 | u32 crc = ~(u32)0; | ||
437 | unsigned offset = 0; | ||
438 | |||
439 | if (!io_ctl->check_crcs) { | ||
440 | io_ctl_map_page(io_ctl, 0); | ||
441 | return 0; | ||
442 | } | ||
443 | |||
444 | if (index == 0) | ||
445 | offset = sizeof(u32) * io_ctl->num_pages; | ||
446 | |||
447 | tmp = kmap(io_ctl->pages[0]); | ||
448 | tmp += index; | ||
449 | val = *tmp; | ||
450 | kunmap(io_ctl->pages[0]); | ||
451 | |||
452 | io_ctl_map_page(io_ctl, 0); | ||
453 | crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc, | ||
454 | PAGE_CACHE_SIZE - offset); | ||
455 | btrfs_csum_final(crc, (char *)&crc); | ||
456 | if (val != crc) { | ||
457 | printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free " | ||
458 | "space cache\n"); | ||
459 | io_ctl_unmap_page(io_ctl); | ||
460 | return -EIO; | ||
461 | } | ||
462 | |||
463 | return 0; | ||
464 | } | ||
465 | |||
466 | static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes, | ||
467 | void *bitmap) | ||
468 | { | ||
469 | struct btrfs_free_space_entry *entry; | ||
470 | |||
471 | if (!io_ctl->cur) | ||
472 | return -ENOSPC; | ||
473 | |||
474 | entry = io_ctl->cur; | ||
475 | entry->offset = cpu_to_le64(offset); | ||
476 | entry->bytes = cpu_to_le64(bytes); | ||
477 | entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP : | ||
478 | BTRFS_FREE_SPACE_EXTENT; | ||
479 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); | ||
480 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); | ||
481 | |||
482 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) | ||
483 | return 0; | ||
484 | |||
485 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); | ||
486 | |||
487 | /* No more pages to map */ | ||
488 | if (io_ctl->index >= io_ctl->num_pages) | ||
489 | return 0; | ||
490 | |||
491 | /* map the next page */ | ||
492 | io_ctl_map_page(io_ctl, 1); | ||
493 | return 0; | ||
494 | } | ||
495 | |||
496 | static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap) | ||
497 | { | ||
498 | if (!io_ctl->cur) | ||
499 | return -ENOSPC; | ||
500 | |||
501 | /* | ||
502 | * If we aren't at the start of the current page, unmap this one and | ||
503 | * map the next one if there is any left. | ||
504 | */ | ||
505 | if (io_ctl->cur != io_ctl->orig) { | ||
506 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); | ||
507 | if (io_ctl->index >= io_ctl->num_pages) | ||
508 | return -ENOSPC; | ||
509 | io_ctl_map_page(io_ctl, 0); | ||
510 | } | ||
511 | |||
512 | memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE); | ||
513 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); | ||
514 | if (io_ctl->index < io_ctl->num_pages) | ||
515 | io_ctl_map_page(io_ctl, 0); | ||
516 | return 0; | ||
517 | } | ||
518 | |||
519 | static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl) | ||
520 | { | ||
521 | /* | ||
522 | * If we're not on the boundary we know we've modified the page and we | ||
523 | * need to crc the page. | ||
524 | */ | ||
525 | if (io_ctl->cur != io_ctl->orig) | ||
526 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); | ||
527 | else | ||
528 | io_ctl_unmap_page(io_ctl); | ||
529 | |||
530 | while (io_ctl->index < io_ctl->num_pages) { | ||
531 | io_ctl_map_page(io_ctl, 1); | ||
532 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); | ||
533 | } | ||
534 | } | ||
535 | |||
536 | static int io_ctl_read_entry(struct io_ctl *io_ctl, | ||
537 | struct btrfs_free_space *entry, u8 *type) | ||
538 | { | ||
539 | struct btrfs_free_space_entry *e; | ||
540 | |||
541 | e = io_ctl->cur; | ||
542 | entry->offset = le64_to_cpu(e->offset); | ||
543 | entry->bytes = le64_to_cpu(e->bytes); | ||
544 | *type = e->type; | ||
545 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); | ||
546 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); | ||
547 | |||
548 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) | ||
549 | return 0; | ||
550 | |||
551 | io_ctl_unmap_page(io_ctl); | ||
552 | |||
553 | if (io_ctl->index >= io_ctl->num_pages) | ||
554 | return 0; | ||
555 | |||
556 | return io_ctl_check_crc(io_ctl, io_ctl->index); | ||
557 | } | ||
558 | |||
559 | static int io_ctl_read_bitmap(struct io_ctl *io_ctl, | ||
560 | struct btrfs_free_space *entry) | ||
561 | { | ||
562 | int ret; | ||
563 | |||
564 | if (io_ctl->cur && io_ctl->cur != io_ctl->orig) | ||
565 | io_ctl_unmap_page(io_ctl); | ||
566 | |||
567 | ret = io_ctl_check_crc(io_ctl, io_ctl->index); | ||
568 | if (ret) | ||
569 | return ret; | ||
570 | |||
571 | memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE); | ||
572 | io_ctl_unmap_page(io_ctl); | ||
573 | |||
574 | return 0; | ||
575 | } | ||
576 | |||
245 | int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | 577 | int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, |
246 | struct btrfs_free_space_ctl *ctl, | 578 | struct btrfs_free_space_ctl *ctl, |
247 | struct btrfs_path *path, u64 offset) | 579 | struct btrfs_path *path, u64 offset) |
248 | { | 580 | { |
249 | struct btrfs_free_space_header *header; | 581 | struct btrfs_free_space_header *header; |
250 | struct extent_buffer *leaf; | 582 | struct extent_buffer *leaf; |
251 | struct page *page; | 583 | struct io_ctl io_ctl; |
252 | struct btrfs_key key; | 584 | struct btrfs_key key; |
585 | struct btrfs_free_space *e, *n; | ||
253 | struct list_head bitmaps; | 586 | struct list_head bitmaps; |
254 | u64 num_entries; | 587 | u64 num_entries; |
255 | u64 num_bitmaps; | 588 | u64 num_bitmaps; |
256 | u64 generation; | 589 | u64 generation; |
257 | pgoff_t index = 0; | 590 | u8 type; |
258 | int ret = 0; | 591 | int ret = 0; |
259 | 592 | ||
260 | INIT_LIST_HEAD(&bitmaps); | 593 | INIT_LIST_HEAD(&bitmaps); |
261 | 594 | ||
262 | /* Nothing in the space cache, goodbye */ | 595 | /* Nothing in the space cache, goodbye */ |
263 | if (!i_size_read(inode)) | 596 | if (!i_size_read(inode)) |
264 | goto out; | 597 | return 0; |
265 | 598 | ||
266 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | 599 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
267 | key.offset = offset; | 600 | key.offset = offset; |
@@ -269,11 +602,10 @@ int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | |||
269 | 602 | ||
270 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 603 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
271 | if (ret < 0) | 604 | if (ret < 0) |
272 | goto out; | 605 | return 0; |
273 | else if (ret > 0) { | 606 | else if (ret > 0) { |
274 | btrfs_release_path(path); | 607 | btrfs_release_path(path); |
275 | ret = 0; | 608 | return 0; |
276 | goto out; | ||
277 | } | 609 | } |
278 | 610 | ||
279 | ret = -1; | 611 | ret = -1; |
@@ -291,169 +623,100 @@ int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, | |||
291 | " not match free space cache generation (%llu)\n", | 623 | " not match free space cache generation (%llu)\n", |
292 | (unsigned long long)BTRFS_I(inode)->generation, | 624 | (unsigned long long)BTRFS_I(inode)->generation, |
293 | (unsigned long long)generation); | 625 | (unsigned long long)generation); |
294 | goto out; | 626 | return 0; |
295 | } | 627 | } |
296 | 628 | ||
297 | if (!num_entries) | 629 | if (!num_entries) |
298 | goto out; | 630 | return 0; |
299 | 631 | ||
632 | io_ctl_init(&io_ctl, inode, root); | ||
300 | ret = readahead_cache(inode); | 633 | ret = readahead_cache(inode); |
301 | if (ret) | 634 | if (ret) |
302 | goto out; | 635 | goto out; |
303 | 636 | ||
304 | while (1) { | 637 | ret = io_ctl_prepare_pages(&io_ctl, inode, 1); |
305 | struct btrfs_free_space_entry *entry; | 638 | if (ret) |
306 | struct btrfs_free_space *e; | 639 | goto out; |
307 | void *addr; | ||
308 | unsigned long offset = 0; | ||
309 | int need_loop = 0; | ||
310 | 640 | ||
311 | if (!num_entries && !num_bitmaps) | 641 | ret = io_ctl_check_crc(&io_ctl, 0); |
312 | break; | 642 | if (ret) |
643 | goto free_cache; | ||
313 | 644 | ||
314 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | 645 | ret = io_ctl_check_generation(&io_ctl, generation); |
315 | if (!page) | 646 | if (ret) |
647 | goto free_cache; | ||
648 | |||
649 | while (num_entries) { | ||
650 | e = kmem_cache_zalloc(btrfs_free_space_cachep, | ||
651 | GFP_NOFS); | ||
652 | if (!e) | ||
316 | goto free_cache; | 653 | goto free_cache; |
317 | 654 | ||
318 | if (!PageUptodate(page)) { | 655 | ret = io_ctl_read_entry(&io_ctl, e, &type); |
319 | btrfs_readpage(NULL, page); | 656 | if (ret) { |
320 | lock_page(page); | 657 | kmem_cache_free(btrfs_free_space_cachep, e); |
321 | if (!PageUptodate(page)) { | 658 | goto free_cache; |
322 | unlock_page(page); | ||
323 | page_cache_release(page); | ||
324 | printk(KERN_ERR "btrfs: error reading free " | ||
325 | "space cache\n"); | ||
326 | goto free_cache; | ||
327 | } | ||
328 | } | 659 | } |
329 | addr = kmap(page); | ||
330 | 660 | ||
331 | if (index == 0) { | 661 | if (!e->bytes) { |
332 | u64 *gen; | 662 | kmem_cache_free(btrfs_free_space_cachep, e); |
663 | goto free_cache; | ||
664 | } | ||
333 | 665 | ||
334 | /* | 666 | if (type == BTRFS_FREE_SPACE_EXTENT) { |
335 | * We put a bogus crc in the front of the first page in | 667 | spin_lock(&ctl->tree_lock); |
336 | * case old kernels try to mount a fs with the new | 668 | ret = link_free_space(ctl, e); |
337 | * format to make sure they discard the cache. | 669 | spin_unlock(&ctl->tree_lock); |
338 | */ | 670 | if (ret) { |
339 | addr += sizeof(u64); | 671 | printk(KERN_ERR "Duplicate entries in " |
340 | offset += sizeof(u64); | 672 | "free space cache, dumping\n"); |
341 | 673 | kmem_cache_free(btrfs_free_space_cachep, e); | |
342 | gen = addr; | ||
343 | if (*gen != BTRFS_I(inode)->generation) { | ||
344 | printk(KERN_ERR "btrfs: space cache generation" | ||
345 | " (%llu) does not match inode (%llu)\n", | ||
346 | (unsigned long long)*gen, | ||
347 | (unsigned long long) | ||
348 | BTRFS_I(inode)->generation); | ||
349 | kunmap(page); | ||
350 | unlock_page(page); | ||
351 | page_cache_release(page); | ||
352 | goto free_cache; | 674 | goto free_cache; |
353 | } | 675 | } |
354 | addr += sizeof(u64); | 676 | } else { |
355 | offset += sizeof(u64); | 677 | BUG_ON(!num_bitmaps); |
356 | } | 678 | num_bitmaps--; |
357 | entry = addr; | 679 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
358 | 680 | if (!e->bitmap) { | |
359 | while (1) { | 681 | kmem_cache_free( |
360 | if (!num_entries) | 682 | btrfs_free_space_cachep, e); |
361 | break; | ||
362 | |||
363 | need_loop = 1; | ||
364 | e = kmem_cache_zalloc(btrfs_free_space_cachep, | ||
365 | GFP_NOFS); | ||
366 | if (!e) { | ||
367 | kunmap(page); | ||
368 | unlock_page(page); | ||
369 | page_cache_release(page); | ||
370 | goto free_cache; | 683 | goto free_cache; |
371 | } | 684 | } |
372 | 685 | spin_lock(&ctl->tree_lock); | |
373 | e->offset = le64_to_cpu(entry->offset); | 686 | ret = link_free_space(ctl, e); |
374 | e->bytes = le64_to_cpu(entry->bytes); | 687 | ctl->total_bitmaps++; |
375 | if (!e->bytes) { | 688 | ctl->op->recalc_thresholds(ctl); |
376 | kunmap(page); | 689 | spin_unlock(&ctl->tree_lock); |
690 | if (ret) { | ||
691 | printk(KERN_ERR "Duplicate entries in " | ||
692 | "free space cache, dumping\n"); | ||
377 | kmem_cache_free(btrfs_free_space_cachep, e); | 693 | kmem_cache_free(btrfs_free_space_cachep, e); |
378 | unlock_page(page); | ||
379 | page_cache_release(page); | ||
380 | goto free_cache; | 694 | goto free_cache; |
381 | } | 695 | } |
382 | 696 | list_add_tail(&e->list, &bitmaps); | |
383 | if (entry->type == BTRFS_FREE_SPACE_EXTENT) { | ||
384 | spin_lock(&ctl->tree_lock); | ||
385 | ret = link_free_space(ctl, e); | ||
386 | spin_unlock(&ctl->tree_lock); | ||
387 | if (ret) { | ||
388 | printk(KERN_ERR "Duplicate entries in " | ||
389 | "free space cache, dumping\n"); | ||
390 | kunmap(page); | ||
391 | unlock_page(page); | ||
392 | page_cache_release(page); | ||
393 | goto free_cache; | ||
394 | } | ||
395 | } else { | ||
396 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); | ||
397 | if (!e->bitmap) { | ||
398 | kunmap(page); | ||
399 | kmem_cache_free( | ||
400 | btrfs_free_space_cachep, e); | ||
401 | unlock_page(page); | ||
402 | page_cache_release(page); | ||
403 | goto free_cache; | ||
404 | } | ||
405 | spin_lock(&ctl->tree_lock); | ||
406 | ret = link_free_space(ctl, e); | ||
407 | ctl->total_bitmaps++; | ||
408 | ctl->op->recalc_thresholds(ctl); | ||
409 | spin_unlock(&ctl->tree_lock); | ||
410 | if (ret) { | ||
411 | printk(KERN_ERR "Duplicate entries in " | ||
412 | "free space cache, dumping\n"); | ||
413 | kunmap(page); | ||
414 | unlock_page(page); | ||
415 | page_cache_release(page); | ||
416 | goto free_cache; | ||
417 | } | ||
418 | list_add_tail(&e->list, &bitmaps); | ||
419 | } | ||
420 | |||
421 | num_entries--; | ||
422 | offset += sizeof(struct btrfs_free_space_entry); | ||
423 | if (offset + sizeof(struct btrfs_free_space_entry) >= | ||
424 | PAGE_CACHE_SIZE) | ||
425 | break; | ||
426 | entry++; | ||
427 | } | 697 | } |
428 | 698 | ||
429 | /* | 699 | num_entries--; |
430 | * We read an entry out of this page, we need to move on to the | 700 | } |
431 | * next page. | ||
432 | */ | ||
433 | if (need_loop) { | ||
434 | kunmap(page); | ||
435 | goto next; | ||
436 | } | ||
437 | 701 | ||
438 | /* | 702 | /* |
439 | * We add the bitmaps at the end of the entries in order that | 703 | * We add the bitmaps at the end of the entries in order that |
440 | * the bitmap entries are added to the cache. | 704 | * the bitmap entries are added to the cache. |
441 | */ | 705 | */ |
442 | e = list_entry(bitmaps.next, struct btrfs_free_space, list); | 706 | list_for_each_entry_safe(e, n, &bitmaps, list) { |
443 | list_del_init(&e->list); | 707 | list_del_init(&e->list); |
444 | memcpy(e->bitmap, addr, PAGE_CACHE_SIZE); | 708 | ret = io_ctl_read_bitmap(&io_ctl, e); |
445 | kunmap(page); | 709 | if (ret) |
446 | num_bitmaps--; | 710 | goto free_cache; |
447 | next: | ||
448 | unlock_page(page); | ||
449 | page_cache_release(page); | ||
450 | index++; | ||
451 | } | 711 | } |
452 | 712 | ||
713 | io_ctl_drop_pages(&io_ctl); | ||
453 | ret = 1; | 714 | ret = 1; |
454 | out: | 715 | out: |
716 | io_ctl_free(&io_ctl); | ||
455 | return ret; | 717 | return ret; |
456 | free_cache: | 718 | free_cache: |
719 | io_ctl_drop_pages(&io_ctl); | ||
457 | __btrfs_remove_free_space_cache(ctl); | 720 | __btrfs_remove_free_space_cache(ctl); |
458 | goto out; | 721 | goto out; |
459 | } | 722 | } |
@@ -465,7 +728,7 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info, | |||
465 | struct btrfs_root *root = fs_info->tree_root; | 728 | struct btrfs_root *root = fs_info->tree_root; |
466 | struct inode *inode; | 729 | struct inode *inode; |
467 | struct btrfs_path *path; | 730 | struct btrfs_path *path; |
468 | int ret; | 731 | int ret = 0; |
469 | bool matched; | 732 | bool matched; |
470 | u64 used = btrfs_block_group_used(&block_group->item); | 733 | u64 used = btrfs_block_group_used(&block_group->item); |
471 | 734 | ||
@@ -497,6 +760,14 @@ int load_free_space_cache(struct btrfs_fs_info *fs_info, | |||
497 | return 0; | 760 | return 0; |
498 | } | 761 | } |
499 | 762 | ||
763 | /* We may have converted the inode and made the cache invalid. */ | ||
764 | spin_lock(&block_group->lock); | ||
765 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { | ||
766 | spin_unlock(&block_group->lock); | ||
767 | goto out; | ||
768 | } | ||
769 | spin_unlock(&block_group->lock); | ||
770 | |||
500 | ret = __load_free_space_cache(fs_info->tree_root, inode, ctl, | 771 | ret = __load_free_space_cache(fs_info->tree_root, inode, ctl, |
501 | path, block_group->key.objectid); | 772 | path, block_group->key.objectid); |
502 | btrfs_free_path(path); | 773 | btrfs_free_path(path); |
@@ -530,6 +801,19 @@ out: | |||
530 | return ret; | 801 | return ret; |
531 | } | 802 | } |
532 | 803 | ||
804 | /** | ||
805 | * __btrfs_write_out_cache - write out cached info to an inode | ||
806 | * @root - the root the inode belongs to | ||
807 | * @ctl - the free space cache we are going to write out | ||
808 | * @block_group - the block_group for this cache if it belongs to a block_group | ||
809 | * @trans - the trans handle | ||
810 | * @path - the path to use | ||
811 | * @offset - the offset for the key we'll insert | ||
812 | * | ||
813 | * This function writes out a free space cache struct to disk for quick recovery | ||
814 | * on mount. This will return 0 if it was successfull in writing the cache out, | ||
815 | * and -1 if it was not. | ||
816 | */ | ||
533 | int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | 817 | int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, |
534 | struct btrfs_free_space_ctl *ctl, | 818 | struct btrfs_free_space_ctl *ctl, |
535 | struct btrfs_block_group_cache *block_group, | 819 | struct btrfs_block_group_cache *block_group, |
@@ -540,42 +824,24 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
540 | struct extent_buffer *leaf; | 824 | struct extent_buffer *leaf; |
541 | struct rb_node *node; | 825 | struct rb_node *node; |
542 | struct list_head *pos, *n; | 826 | struct list_head *pos, *n; |
543 | struct page **pages; | ||
544 | struct page *page; | ||
545 | struct extent_state *cached_state = NULL; | 827 | struct extent_state *cached_state = NULL; |
546 | struct btrfs_free_cluster *cluster = NULL; | 828 | struct btrfs_free_cluster *cluster = NULL; |
547 | struct extent_io_tree *unpin = NULL; | 829 | struct extent_io_tree *unpin = NULL; |
830 | struct io_ctl io_ctl; | ||
548 | struct list_head bitmap_list; | 831 | struct list_head bitmap_list; |
549 | struct btrfs_key key; | 832 | struct btrfs_key key; |
550 | u64 start, end, len; | 833 | u64 start, end, len; |
551 | u64 bytes = 0; | ||
552 | u32 crc = ~(u32)0; | ||
553 | int index = 0, num_pages = 0; | ||
554 | int entries = 0; | 834 | int entries = 0; |
555 | int bitmaps = 0; | 835 | int bitmaps = 0; |
556 | int ret = -1; | 836 | int ret; |
557 | bool next_page = false; | 837 | int err = -1; |
558 | bool out_of_space = false; | ||
559 | 838 | ||
560 | INIT_LIST_HEAD(&bitmap_list); | 839 | INIT_LIST_HEAD(&bitmap_list); |
561 | 840 | ||
562 | node = rb_first(&ctl->free_space_offset); | ||
563 | if (!node) | ||
564 | return 0; | ||
565 | |||
566 | if (!i_size_read(inode)) | 841 | if (!i_size_read(inode)) |
567 | return -1; | 842 | return -1; |
568 | 843 | ||
569 | num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | 844 | io_ctl_init(&io_ctl, inode, root); |
570 | PAGE_CACHE_SHIFT; | ||
571 | |||
572 | filemap_write_and_wait(inode->i_mapping); | ||
573 | btrfs_wait_ordered_range(inode, inode->i_size & | ||
574 | ~(root->sectorsize - 1), (u64)-1); | ||
575 | |||
576 | pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS); | ||
577 | if (!pages) | ||
578 | return -1; | ||
579 | 845 | ||
580 | /* Get the cluster for this block_group if it exists */ | 846 | /* Get the cluster for this block_group if it exists */ |
581 | if (block_group && !list_empty(&block_group->cluster_list)) | 847 | if (block_group && !list_empty(&block_group->cluster_list)) |
@@ -589,30 +855,9 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
589 | */ | 855 | */ |
590 | unpin = root->fs_info->pinned_extents; | 856 | unpin = root->fs_info->pinned_extents; |
591 | 857 | ||
592 | /* | 858 | /* Lock all pages first so we can lock the extent safely. */ |
593 | * Lock all pages first so we can lock the extent safely. | 859 | io_ctl_prepare_pages(&io_ctl, inode, 0); |
594 | * | ||
595 | * NOTE: Because we hold the ref the entire time we're going to write to | ||
596 | * the page find_get_page should never fail, so we don't do a check | ||
597 | * after find_get_page at this point. Just putting this here so people | ||
598 | * know and don't freak out. | ||
599 | */ | ||
600 | while (index < num_pages) { | ||
601 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | ||
602 | if (!page) { | ||
603 | int i; | ||
604 | |||
605 | for (i = 0; i < num_pages; i++) { | ||
606 | unlock_page(pages[i]); | ||
607 | page_cache_release(pages[i]); | ||
608 | } | ||
609 | goto out; | ||
610 | } | ||
611 | pages[index] = page; | ||
612 | index++; | ||
613 | } | ||
614 | 860 | ||
615 | index = 0; | ||
616 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, | 861 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
617 | 0, &cached_state, GFP_NOFS); | 862 | 0, &cached_state, GFP_NOFS); |
618 | 863 | ||
@@ -623,189 +868,111 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
623 | if (block_group) | 868 | if (block_group) |
624 | start = block_group->key.objectid; | 869 | start = block_group->key.objectid; |
625 | 870 | ||
626 | /* Write out the extent entries */ | 871 | node = rb_first(&ctl->free_space_offset); |
627 | do { | 872 | if (!node && cluster) { |
628 | struct btrfs_free_space_entry *entry; | 873 | node = rb_first(&cluster->root); |
629 | void *addr, *orig; | 874 | cluster = NULL; |
630 | unsigned long offset = 0; | 875 | } |
631 | 876 | ||
632 | next_page = false; | 877 | /* Make sure we can fit our crcs into the first page */ |
878 | if (io_ctl.check_crcs && | ||
879 | (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) { | ||
880 | WARN_ON(1); | ||
881 | goto out_nospc; | ||
882 | } | ||
633 | 883 | ||
634 | if (index >= num_pages) { | 884 | io_ctl_set_generation(&io_ctl, trans->transid); |
635 | out_of_space = true; | ||
636 | break; | ||
637 | } | ||
638 | 885 | ||
639 | page = pages[index]; | 886 | /* Write out the extent entries */ |
887 | while (node) { | ||
888 | struct btrfs_free_space *e; | ||
640 | 889 | ||
641 | orig = addr = kmap(page); | 890 | e = rb_entry(node, struct btrfs_free_space, offset_index); |
642 | if (index == 0) { | 891 | entries++; |
643 | u64 *gen; | ||
644 | 892 | ||
645 | /* | 893 | ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes, |
646 | * We're going to put in a bogus crc for this page to | 894 | e->bitmap); |
647 | * make sure that old kernels who aren't aware of this | 895 | if (ret) |
648 | * format will be sure to discard the cache. | 896 | goto out_nospc; |
649 | */ | ||
650 | addr += sizeof(u64); | ||
651 | offset += sizeof(u64); | ||
652 | 897 | ||
653 | gen = addr; | 898 | if (e->bitmap) { |
654 | *gen = trans->transid; | 899 | list_add_tail(&e->list, &bitmap_list); |
655 | addr += sizeof(u64); | 900 | bitmaps++; |
656 | offset += sizeof(u64); | ||
657 | } | 901 | } |
658 | entry = addr; | 902 | node = rb_next(node); |
659 | 903 | if (!node && cluster) { | |
660 | memset(addr, 0, PAGE_CACHE_SIZE - offset); | 904 | node = rb_first(&cluster->root); |
661 | while (node && !next_page) { | 905 | cluster = NULL; |
662 | struct btrfs_free_space *e; | ||
663 | |||
664 | e = rb_entry(node, struct btrfs_free_space, offset_index); | ||
665 | entries++; | ||
666 | |||
667 | entry->offset = cpu_to_le64(e->offset); | ||
668 | entry->bytes = cpu_to_le64(e->bytes); | ||
669 | if (e->bitmap) { | ||
670 | entry->type = BTRFS_FREE_SPACE_BITMAP; | ||
671 | list_add_tail(&e->list, &bitmap_list); | ||
672 | bitmaps++; | ||
673 | } else { | ||
674 | entry->type = BTRFS_FREE_SPACE_EXTENT; | ||
675 | } | ||
676 | node = rb_next(node); | ||
677 | if (!node && cluster) { | ||
678 | node = rb_first(&cluster->root); | ||
679 | cluster = NULL; | ||
680 | } | ||
681 | offset += sizeof(struct btrfs_free_space_entry); | ||
682 | if (offset + sizeof(struct btrfs_free_space_entry) >= | ||
683 | PAGE_CACHE_SIZE) | ||
684 | next_page = true; | ||
685 | entry++; | ||
686 | } | 906 | } |
907 | } | ||
687 | 908 | ||
688 | /* | 909 | /* |
689 | * We want to add any pinned extents to our free space cache | 910 | * We want to add any pinned extents to our free space cache |
690 | * so we don't leak the space | 911 | * so we don't leak the space |
691 | */ | 912 | */ |
692 | while (block_group && !next_page && | 913 | while (block_group && (start < block_group->key.objectid + |
693 | (start < block_group->key.objectid + | 914 | block_group->key.offset)) { |
694 | block_group->key.offset)) { | 915 | ret = find_first_extent_bit(unpin, start, &start, &end, |
695 | ret = find_first_extent_bit(unpin, start, &start, &end, | 916 | EXTENT_DIRTY); |
696 | EXTENT_DIRTY); | 917 | if (ret) { |
697 | if (ret) { | 918 | ret = 0; |
698 | ret = 0; | 919 | break; |
699 | break; | ||
700 | } | ||
701 | |||
702 | /* This pinned extent is out of our range */ | ||
703 | if (start >= block_group->key.objectid + | ||
704 | block_group->key.offset) | ||
705 | break; | ||
706 | |||
707 | len = block_group->key.objectid + | ||
708 | block_group->key.offset - start; | ||
709 | len = min(len, end + 1 - start); | ||
710 | |||
711 | entries++; | ||
712 | entry->offset = cpu_to_le64(start); | ||
713 | entry->bytes = cpu_to_le64(len); | ||
714 | entry->type = BTRFS_FREE_SPACE_EXTENT; | ||
715 | |||
716 | start = end + 1; | ||
717 | offset += sizeof(struct btrfs_free_space_entry); | ||
718 | if (offset + sizeof(struct btrfs_free_space_entry) >= | ||
719 | PAGE_CACHE_SIZE) | ||
720 | next_page = true; | ||
721 | entry++; | ||
722 | } | 920 | } |
723 | 921 | ||
724 | /* Generate bogus crc value */ | 922 | /* This pinned extent is out of our range */ |
725 | if (index == 0) { | 923 | if (start >= block_group->key.objectid + |
726 | u32 *tmp; | 924 | block_group->key.offset) |
727 | crc = btrfs_csum_data(root, orig + sizeof(u64), crc, | 925 | break; |
728 | PAGE_CACHE_SIZE - sizeof(u64)); | ||
729 | btrfs_csum_final(crc, (char *)&crc); | ||
730 | crc++; | ||
731 | tmp = orig; | ||
732 | *tmp = crc; | ||
733 | } | ||
734 | 926 | ||
735 | kunmap(page); | 927 | len = block_group->key.objectid + |
928 | block_group->key.offset - start; | ||
929 | len = min(len, end + 1 - start); | ||
736 | 930 | ||
737 | bytes += PAGE_CACHE_SIZE; | 931 | entries++; |
932 | ret = io_ctl_add_entry(&io_ctl, start, len, NULL); | ||
933 | if (ret) | ||
934 | goto out_nospc; | ||
738 | 935 | ||
739 | index++; | 936 | start = end + 1; |
740 | } while (node || next_page); | 937 | } |
741 | 938 | ||
742 | /* Write out the bitmaps */ | 939 | /* Write out the bitmaps */ |
743 | list_for_each_safe(pos, n, &bitmap_list) { | 940 | list_for_each_safe(pos, n, &bitmap_list) { |
744 | void *addr; | ||
745 | struct btrfs_free_space *entry = | 941 | struct btrfs_free_space *entry = |
746 | list_entry(pos, struct btrfs_free_space, list); | 942 | list_entry(pos, struct btrfs_free_space, list); |
747 | 943 | ||
748 | if (index >= num_pages) { | 944 | ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap); |
749 | out_of_space = true; | 945 | if (ret) |
750 | break; | 946 | goto out_nospc; |
751 | } | ||
752 | page = pages[index]; | ||
753 | |||
754 | addr = kmap(page); | ||
755 | memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE); | ||
756 | kunmap(page); | ||
757 | bytes += PAGE_CACHE_SIZE; | ||
758 | |||
759 | list_del_init(&entry->list); | 947 | list_del_init(&entry->list); |
760 | index++; | ||
761 | } | ||
762 | |||
763 | if (out_of_space) { | ||
764 | btrfs_drop_pages(pages, num_pages); | ||
765 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | ||
766 | i_size_read(inode) - 1, &cached_state, | ||
767 | GFP_NOFS); | ||
768 | ret = 0; | ||
769 | goto out; | ||
770 | } | 948 | } |
771 | 949 | ||
772 | /* Zero out the rest of the pages just to make sure */ | 950 | /* Zero out the rest of the pages just to make sure */ |
773 | while (index < num_pages) { | 951 | io_ctl_zero_remaining_pages(&io_ctl); |
774 | void *addr; | ||
775 | |||
776 | page = pages[index]; | ||
777 | addr = kmap(page); | ||
778 | memset(addr, 0, PAGE_CACHE_SIZE); | ||
779 | kunmap(page); | ||
780 | bytes += PAGE_CACHE_SIZE; | ||
781 | index++; | ||
782 | } | ||
783 | 952 | ||
784 | ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0, | 953 | ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages, |
785 | bytes, &cached_state); | 954 | 0, i_size_read(inode), &cached_state); |
786 | btrfs_drop_pages(pages, num_pages); | 955 | io_ctl_drop_pages(&io_ctl); |
787 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | 956 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
788 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); | 957 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); |
789 | 958 | ||
790 | if (ret) { | 959 | if (ret) |
791 | ret = 0; | ||
792 | goto out; | 960 | goto out; |
793 | } | ||
794 | 961 | ||
795 | BTRFS_I(inode)->generation = trans->transid; | ||
796 | 962 | ||
797 | filemap_write_and_wait(inode->i_mapping); | 963 | ret = filemap_write_and_wait(inode->i_mapping); |
964 | if (ret) | ||
965 | goto out; | ||
798 | 966 | ||
799 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | 967 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
800 | key.offset = offset; | 968 | key.offset = offset; |
801 | key.type = 0; | 969 | key.type = 0; |
802 | 970 | ||
803 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); | 971 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
804 | if (ret < 0) { | 972 | if (ret < 0) { |
805 | ret = -1; | 973 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
806 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1, | 974 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, |
807 | EXTENT_DIRTY | EXTENT_DELALLOC | | 975 | GFP_NOFS); |
808 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS); | ||
809 | goto out; | 976 | goto out; |
810 | } | 977 | } |
811 | leaf = path->nodes[0]; | 978 | leaf = path->nodes[0]; |
@@ -816,15 +983,16 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
816 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | 983 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
817 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || | 984 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || |
818 | found_key.offset != offset) { | 985 | found_key.offset != offset) { |
819 | ret = -1; | 986 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, |
820 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1, | 987 | inode->i_size - 1, |
821 | EXTENT_DIRTY | EXTENT_DELALLOC | | 988 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, |
822 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, | 989 | NULL, GFP_NOFS); |
823 | GFP_NOFS); | ||
824 | btrfs_release_path(path); | 990 | btrfs_release_path(path); |
825 | goto out; | 991 | goto out; |
826 | } | 992 | } |
827 | } | 993 | } |
994 | |||
995 | BTRFS_I(inode)->generation = trans->transid; | ||
828 | header = btrfs_item_ptr(leaf, path->slots[0], | 996 | header = btrfs_item_ptr(leaf, path->slots[0], |
829 | struct btrfs_free_space_header); | 997 | struct btrfs_free_space_header); |
830 | btrfs_set_free_space_entries(leaf, header, entries); | 998 | btrfs_set_free_space_entries(leaf, header, entries); |
@@ -833,16 +1001,26 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, | |||
833 | btrfs_mark_buffer_dirty(leaf); | 1001 | btrfs_mark_buffer_dirty(leaf); |
834 | btrfs_release_path(path); | 1002 | btrfs_release_path(path); |
835 | 1003 | ||
836 | ret = 1; | 1004 | err = 0; |
837 | |||
838 | out: | 1005 | out: |
839 | kfree(pages); | 1006 | io_ctl_free(&io_ctl); |
840 | if (ret != 1) { | 1007 | if (err) { |
841 | invalidate_inode_pages2_range(inode->i_mapping, 0, index); | 1008 | invalidate_inode_pages2(inode->i_mapping); |
842 | BTRFS_I(inode)->generation = 0; | 1009 | BTRFS_I(inode)->generation = 0; |
843 | } | 1010 | } |
844 | btrfs_update_inode(trans, root, inode); | 1011 | btrfs_update_inode(trans, root, inode); |
845 | return ret; | 1012 | return err; |
1013 | |||
1014 | out_nospc: | ||
1015 | list_for_each_safe(pos, n, &bitmap_list) { | ||
1016 | struct btrfs_free_space *entry = | ||
1017 | list_entry(pos, struct btrfs_free_space, list); | ||
1018 | list_del_init(&entry->list); | ||
1019 | } | ||
1020 | io_ctl_drop_pages(&io_ctl); | ||
1021 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, | ||
1022 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); | ||
1023 | goto out; | ||
846 | } | 1024 | } |
847 | 1025 | ||
848 | int btrfs_write_out_cache(struct btrfs_root *root, | 1026 | int btrfs_write_out_cache(struct btrfs_root *root, |
@@ -869,14 +1047,15 @@ int btrfs_write_out_cache(struct btrfs_root *root, | |||
869 | 1047 | ||
870 | ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans, | 1048 | ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans, |
871 | path, block_group->key.objectid); | 1049 | path, block_group->key.objectid); |
872 | if (ret < 0) { | 1050 | if (ret) { |
873 | spin_lock(&block_group->lock); | 1051 | spin_lock(&block_group->lock); |
874 | block_group->disk_cache_state = BTRFS_DC_ERROR; | 1052 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
875 | spin_unlock(&block_group->lock); | 1053 | spin_unlock(&block_group->lock); |
876 | ret = 0; | 1054 | ret = 0; |
877 | 1055 | #ifdef DEBUG | |
878 | printk(KERN_ERR "btrfs: failed to write free space cace " | 1056 | printk(KERN_ERR "btrfs: failed to write free space cace " |
879 | "for block group %llu\n", block_group->key.objectid); | 1057 | "for block group %llu\n", block_group->key.objectid); |
1058 | #endif | ||
880 | } | 1059 | } |
881 | 1060 | ||
882 | iput(inode); | 1061 | iput(inode); |
@@ -1701,6 +1880,7 @@ again: | |||
1701 | ctl->total_bitmaps--; | 1880 | ctl->total_bitmaps--; |
1702 | } | 1881 | } |
1703 | kmem_cache_free(btrfs_free_space_cachep, info); | 1882 | kmem_cache_free(btrfs_free_space_cachep, info); |
1883 | ret = 0; | ||
1704 | goto out_lock; | 1884 | goto out_lock; |
1705 | } | 1885 | } |
1706 | 1886 | ||
@@ -1708,7 +1888,8 @@ again: | |||
1708 | unlink_free_space(ctl, info); | 1888 | unlink_free_space(ctl, info); |
1709 | info->offset += bytes; | 1889 | info->offset += bytes; |
1710 | info->bytes -= bytes; | 1890 | info->bytes -= bytes; |
1711 | link_free_space(ctl, info); | 1891 | ret = link_free_space(ctl, info); |
1892 | WARN_ON(ret); | ||
1712 | goto out_lock; | 1893 | goto out_lock; |
1713 | } | 1894 | } |
1714 | 1895 | ||
@@ -2472,9 +2653,19 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, | |||
2472 | spin_unlock(&ctl->tree_lock); | 2653 | spin_unlock(&ctl->tree_lock); |
2473 | 2654 | ||
2474 | if (bytes >= minlen) { | 2655 | if (bytes >= minlen) { |
2475 | int update_ret; | 2656 | struct btrfs_space_info *space_info; |
2476 | update_ret = btrfs_update_reserved_bytes(block_group, | 2657 | int update = 0; |
2477 | bytes, 1, 1); | 2658 | |
2659 | space_info = block_group->space_info; | ||
2660 | spin_lock(&space_info->lock); | ||
2661 | spin_lock(&block_group->lock); | ||
2662 | if (!block_group->ro) { | ||
2663 | block_group->reserved += bytes; | ||
2664 | space_info->bytes_reserved += bytes; | ||
2665 | update = 1; | ||
2666 | } | ||
2667 | spin_unlock(&block_group->lock); | ||
2668 | spin_unlock(&space_info->lock); | ||
2478 | 2669 | ||
2479 | ret = btrfs_error_discard_extent(fs_info->extent_root, | 2670 | ret = btrfs_error_discard_extent(fs_info->extent_root, |
2480 | start, | 2671 | start, |
@@ -2482,9 +2673,16 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, | |||
2482 | &actually_trimmed); | 2673 | &actually_trimmed); |
2483 | 2674 | ||
2484 | btrfs_add_free_space(block_group, start, bytes); | 2675 | btrfs_add_free_space(block_group, start, bytes); |
2485 | if (!update_ret) | 2676 | if (update) { |
2486 | btrfs_update_reserved_bytes(block_group, | 2677 | spin_lock(&space_info->lock); |
2487 | bytes, 0, 1); | 2678 | spin_lock(&block_group->lock); |
2679 | if (block_group->ro) | ||
2680 | space_info->bytes_readonly += bytes; | ||
2681 | block_group->reserved -= bytes; | ||
2682 | space_info->bytes_reserved -= bytes; | ||
2683 | spin_unlock(&space_info->lock); | ||
2684 | spin_unlock(&block_group->lock); | ||
2685 | } | ||
2488 | 2686 | ||
2489 | if (ret) | 2687 | if (ret) |
2490 | break; | 2688 | break; |
@@ -2643,9 +2841,13 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root, | |||
2643 | return 0; | 2841 | return 0; |
2644 | 2842 | ||
2645 | ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0); | 2843 | ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0); |
2646 | if (ret < 0) | 2844 | if (ret) { |
2845 | btrfs_delalloc_release_metadata(inode, inode->i_size); | ||
2846 | #ifdef DEBUG | ||
2647 | printk(KERN_ERR "btrfs: failed to write free ino cache " | 2847 | printk(KERN_ERR "btrfs: failed to write free ino cache " |
2648 | "for root %llu\n", root->root_key.objectid); | 2848 | "for root %llu\n", root->root_key.objectid); |
2849 | #endif | ||
2850 | } | ||
2649 | 2851 | ||
2650 | iput(inode); | 2852 | iput(inode); |
2651 | return ret; | 2853 | return ret; |