aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nilfs2/mdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/nilfs2/mdt.c')
-rw-r--r--fs/nilfs2/mdt.c313
1 files changed, 166 insertions, 147 deletions
diff --git a/fs/nilfs2/mdt.c b/fs/nilfs2/mdt.c
index d01aff4957d9..39a5b84e2c9f 100644
--- a/fs/nilfs2/mdt.c
+++ b/fs/nilfs2/mdt.c
@@ -36,7 +36,6 @@
36 36
37#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1) 37#define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
38 38
39#define INIT_UNUSED_INODE_FIELDS
40 39
41static int 40static int
42nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block, 41nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
@@ -78,25 +77,11 @@ static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
78 struct buffer_head *, 77 struct buffer_head *,
79 void *)) 78 void *))
80{ 79{
81 struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
82 struct super_block *sb = inode->i_sb; 80 struct super_block *sb = inode->i_sb;
83 struct nilfs_transaction_info ti; 81 struct nilfs_transaction_info ti;
84 struct buffer_head *bh; 82 struct buffer_head *bh;
85 int err; 83 int err;
86 84
87 if (!sb) {
88 /*
89 * Make sure this function is not called from any
90 * read-only context.
91 */
92 if (!nilfs->ns_writer) {
93 WARN_ON(1);
94 err = -EROFS;
95 goto out;
96 }
97 sb = nilfs->ns_writer->s_super;
98 }
99
100 nilfs_transaction_begin(sb, &ti, 0); 85 nilfs_transaction_begin(sb, &ti, 0);
101 86
102 err = -ENOMEM; 87 err = -ENOMEM;
@@ -112,7 +97,7 @@ static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
112 if (buffer_uptodate(bh)) 97 if (buffer_uptodate(bh))
113 goto failed_bh; 98 goto failed_bh;
114 99
115 bh->b_bdev = nilfs->ns_bdev; 100 bh->b_bdev = sb->s_bdev;
116 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block); 101 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
117 if (likely(!err)) { 102 if (likely(!err)) {
118 get_bh(bh); 103 get_bh(bh);
@@ -129,7 +114,7 @@ static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
129 err = nilfs_transaction_commit(sb); 114 err = nilfs_transaction_commit(sb);
130 else 115 else
131 nilfs_transaction_abort(sb); 116 nilfs_transaction_abort(sb);
132 out: 117
133 return err; 118 return err;
134} 119}
135 120
@@ -167,9 +152,7 @@ nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
167 unlock_buffer(bh); 152 unlock_buffer(bh);
168 goto failed_bh; 153 goto failed_bh;
169 } 154 }
170 bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev; 155 map_bh(bh, inode->i_sb, (sector_t)blknum);
171 bh->b_blocknr = (sector_t)blknum;
172 set_buffer_mapped(bh);
173 156
174 bh->b_end_io = end_buffer_read_sync; 157 bh->b_end_io = end_buffer_read_sync;
175 get_bh(bh); 158 get_bh(bh);
@@ -398,35 +381,24 @@ int nilfs_mdt_fetch_dirty(struct inode *inode)
398static int 381static int
399nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc) 382nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
400{ 383{
401 struct inode *inode = container_of(page->mapping, 384 struct inode *inode;
402 struct inode, i_data); 385 struct super_block *sb;
403 struct super_block *sb = inode->i_sb;
404 struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
405 struct nilfs_sb_info *writer = NULL;
406 int err = 0; 386 int err = 0;
407 387
408 redirty_page_for_writepage(wbc, page); 388 redirty_page_for_writepage(wbc, page);
409 unlock_page(page); 389 unlock_page(page);
410 390
411 if (page->mapping->assoc_mapping) 391 inode = page->mapping->host;
412 return 0; /* Do not request flush for shadow page cache */ 392 if (!inode)
413 if (!sb) { 393 return 0;
414 down_read(&nilfs->ns_writer_sem); 394
415 writer = nilfs->ns_writer; 395 sb = inode->i_sb;
416 if (!writer) {
417 up_read(&nilfs->ns_writer_sem);
418 return -EROFS;
419 }
420 sb = writer->s_super;
421 }
422 396
423 if (wbc->sync_mode == WB_SYNC_ALL) 397 if (wbc->sync_mode == WB_SYNC_ALL)
424 err = nilfs_construct_segment(sb); 398 err = nilfs_construct_segment(sb);
425 else if (wbc->for_reclaim) 399 else if (wbc->for_reclaim)
426 nilfs_flush_segment(sb, inode->i_ino); 400 nilfs_flush_segment(sb, inode->i_ino);
427 401
428 if (writer)
429 up_read(&nilfs->ns_writer_sem);
430 return err; 402 return err;
431} 403}
432 404
@@ -439,105 +411,27 @@ static const struct address_space_operations def_mdt_aops = {
439static const struct inode_operations def_mdt_iops; 411static const struct inode_operations def_mdt_iops;
440static const struct file_operations def_mdt_fops; 412static const struct file_operations def_mdt_fops;
441 413
442/* 414
443 * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile, 415int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
444 * ifile, or gcinodes. This allows the B-tree code and segment constructor
445 * to treat them like regular files, and this helps to simplify the
446 * implementation.
447 * On the other hand, some of the pseudo inodes have an irregular point:
448 * They don't have valid inode->i_sb pointer because their lifetimes are
449 * longer than those of the super block structs; they may continue for
450 * several consecutive mounts/umounts. This would need discussions.
451 */
452/**
453 * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
454 * @nilfs: nilfs object
455 * @sb: super block instance the metadata file belongs to
456 * @ino: inode number
457 * @gfp_mask: gfp mask for data pages
458 * @objsz: size of the private object attached to inode->i_private
459 */
460struct inode *
461nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
462 ino_t ino, gfp_t gfp_mask, size_t objsz)
463{ 416{
464 struct inode *inode = nilfs_alloc_inode_common(nilfs); 417 struct nilfs_mdt_info *mi;
465 418
466 if (!inode) 419 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
467 return NULL; 420 if (!mi)
468 else { 421 return -ENOMEM;
469 struct address_space * const mapping = &inode->i_data;
470 struct nilfs_mdt_info *mi;
471
472 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
473 if (!mi) {
474 nilfs_destroy_inode(inode);
475 return NULL;
476 }
477 mi->mi_nilfs = nilfs;
478 init_rwsem(&mi->mi_sem);
479
480 inode->i_sb = sb; /* sb may be NULL for some meta data files */
481 inode->i_blkbits = nilfs->ns_blocksize_bits;
482 inode->i_flags = 0;
483 atomic_set(&inode->i_count, 1);
484 inode->i_nlink = 1;
485 inode->i_ino = ino;
486 inode->i_mode = S_IFREG;
487 inode->i_private = mi;
488
489#ifdef INIT_UNUSED_INODE_FIELDS
490 atomic_set(&inode->i_writecount, 0);
491 inode->i_size = 0;
492 inode->i_blocks = 0;
493 inode->i_bytes = 0;
494 inode->i_generation = 0;
495#ifdef CONFIG_QUOTA
496 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
497#endif
498 inode->i_pipe = NULL;
499 inode->i_bdev = NULL;
500 inode->i_cdev = NULL;
501 inode->i_rdev = 0;
502#ifdef CONFIG_SECURITY
503 inode->i_security = NULL;
504#endif
505 inode->dirtied_when = 0;
506
507 INIT_LIST_HEAD(&inode->i_list);
508 INIT_LIST_HEAD(&inode->i_sb_list);
509 inode->i_state = 0;
510#endif
511
512 spin_lock_init(&inode->i_lock);
513 mutex_init(&inode->i_mutex);
514 init_rwsem(&inode->i_alloc_sem);
515
516 mapping->host = NULL; /* instead of inode */
517 mapping->flags = 0;
518 mapping_set_gfp_mask(mapping, gfp_mask);
519 mapping->assoc_mapping = NULL;
520 mapping->backing_dev_info = nilfs->ns_bdi;
521
522 inode->i_mapping = mapping;
523 }
524 422
525 return inode; 423 init_rwsem(&mi->mi_sem);
526} 424 inode->i_private = mi;
527 425
528struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb, 426 inode->i_mode = S_IFREG;
529 ino_t ino, size_t objsz) 427 mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
530{ 428 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
531 struct inode *inode;
532
533 inode = nilfs_mdt_new_common(nilfs, sb, ino, NILFS_MDT_GFP, objsz);
534 if (!inode)
535 return NULL;
536 429
537 inode->i_op = &def_mdt_iops; 430 inode->i_op = &def_mdt_iops;
538 inode->i_fop = &def_mdt_fops; 431 inode->i_fop = &def_mdt_fops;
539 inode->i_mapping->a_ops = &def_mdt_aops; 432 inode->i_mapping->a_ops = &def_mdt_aops;
540 return inode; 433
434 return 0;
541} 435}
542 436
543void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size, 437void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
@@ -550,34 +444,159 @@ void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
550 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size); 444 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
551} 445}
552 446
553void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow) 447static const struct address_space_operations shadow_map_aops = {
448 .sync_page = block_sync_page,
449};
450
451/**
452 * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
453 * @inode: inode of the metadata file
454 * @shadow: shadow mapping
455 */
456int nilfs_mdt_setup_shadow_map(struct inode *inode,
457 struct nilfs_shadow_map *shadow)
554{ 458{
555 shadow->i_mapping->assoc_mapping = orig->i_mapping; 459 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
556 NILFS_I(shadow)->i_btnode_cache.assoc_mapping = 460 struct backing_dev_info *bdi = inode->i_sb->s_bdi;
557 &NILFS_I(orig)->i_btnode_cache; 461
462 INIT_LIST_HEAD(&shadow->frozen_buffers);
463 nilfs_mapping_init_once(&shadow->frozen_data);
464 nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
465 nilfs_mapping_init_once(&shadow->frozen_btnodes);
466 nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
467 mi->mi_shadow = shadow;
468 return 0;
558} 469}
559 470
560static void nilfs_mdt_clear(struct inode *inode) 471/**
472 * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
473 * @inode: inode of the metadata file
474 */
475int nilfs_mdt_save_to_shadow_map(struct inode *inode)
561{ 476{
477 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
562 struct nilfs_inode_info *ii = NILFS_I(inode); 478 struct nilfs_inode_info *ii = NILFS_I(inode);
479 struct nilfs_shadow_map *shadow = mi->mi_shadow;
480 int ret;
563 481
564 invalidate_mapping_pages(inode->i_mapping, 0, -1); 482 ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
565 truncate_inode_pages(inode->i_mapping, 0); 483 if (ret)
484 goto out;
485
486 ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
487 &ii->i_btnode_cache);
488 if (ret)
489 goto out;
566 490
567 if (test_bit(NILFS_I_BMAP, &ii->i_state)) 491 nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
568 nilfs_bmap_clear(ii->i_bmap); 492 out:
569 nilfs_btnode_cache_clear(&ii->i_btnode_cache); 493 return ret;
570} 494}
571 495
572void nilfs_mdt_destroy(struct inode *inode) 496int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
573{ 497{
574 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 498 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
499 struct buffer_head *bh_frozen;
500 struct page *page;
501 int blkbits = inode->i_blkbits;
502 int ret = -ENOMEM;
503
504 page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
505 if (!page)
506 return ret;
507
508 if (!page_has_buffers(page))
509 create_empty_buffers(page, 1 << blkbits, 0);
510
511 bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
512 if (bh_frozen) {
513 if (!buffer_uptodate(bh_frozen))
514 nilfs_copy_buffer(bh_frozen, bh);
515 if (list_empty(&bh_frozen->b_assoc_buffers)) {
516 list_add_tail(&bh_frozen->b_assoc_buffers,
517 &shadow->frozen_buffers);
518 set_buffer_nilfs_redirected(bh);
519 } else {
520 brelse(bh_frozen); /* already frozen */
521 }
522 ret = 0;
523 }
524 unlock_page(page);
525 page_cache_release(page);
526 return ret;
527}
528
529struct buffer_head *
530nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
531{
532 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
533 struct buffer_head *bh_frozen = NULL;
534 struct page *page;
535 int n;
536
537 page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
538 if (page) {
539 if (page_has_buffers(page)) {
540 n = bh_offset(bh) >> inode->i_blkbits;
541 bh_frozen = nilfs_page_get_nth_block(page, n);
542 }
543 unlock_page(page);
544 page_cache_release(page);
545 }
546 return bh_frozen;
547}
548
549static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
550{
551 struct list_head *head = &shadow->frozen_buffers;
552 struct buffer_head *bh;
553
554 while (!list_empty(head)) {
555 bh = list_first_entry(head, struct buffer_head,
556 b_assoc_buffers);
557 list_del_init(&bh->b_assoc_buffers);
558 brelse(bh); /* drop ref-count to make it releasable */
559 }
560}
561
562/**
563 * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
564 * @inode: inode of the metadata file
565 */
566void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
567{
568 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
569 struct nilfs_inode_info *ii = NILFS_I(inode);
570 struct nilfs_shadow_map *shadow = mi->mi_shadow;
571
572 down_write(&mi->mi_sem);
575 573
576 if (mdi->mi_palloc_cache) 574 if (mi->mi_palloc_cache)
577 nilfs_palloc_destroy_cache(inode); 575 nilfs_palloc_clear_cache(inode);
578 nilfs_mdt_clear(inode); 576
577 nilfs_clear_dirty_pages(inode->i_mapping);
578 nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
579
580 nilfs_clear_dirty_pages(&ii->i_btnode_cache);
581 nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
582
583 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
584
585 up_write(&mi->mi_sem);
586}
587
588/**
589 * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
590 * @inode: inode of the metadata file
591 */
592void nilfs_mdt_clear_shadow_map(struct inode *inode)
593{
594 struct nilfs_mdt_info *mi = NILFS_MDT(inode);
595 struct nilfs_shadow_map *shadow = mi->mi_shadow;
579 596
580 kfree(mdi->mi_bgl); /* kfree(NULL) is safe */ 597 down_write(&mi->mi_sem);
581 kfree(mdi); 598 nilfs_release_frozen_buffers(shadow);
582 nilfs_destroy_inode(inode); 599 truncate_inode_pages(&shadow->frozen_data, 0);
600 truncate_inode_pages(&shadow->frozen_btnodes, 0);
601 up_write(&mi->mi_sem);
583} 602}