nilfs2: add routines to roll back state of DAT file
[deliverable/linux.git] / fs / nilfs2 / mdt.c
index d01aff4957d94bf95b2148cb45d647b4c02178c8..0066468609dac95c7da02ab146a6680e4be1db32 100644 (file)
@@ -398,16 +398,22 @@ int nilfs_mdt_fetch_dirty(struct inode *inode)
 static int
 nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
 {
-       struct inode *inode = container_of(page->mapping,
-                                          struct inode, i_data);
-       struct super_block *sb = inode->i_sb;
-       struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
+       struct inode *inode;
+       struct super_block *sb;
+       struct the_nilfs *nilfs;
        struct nilfs_sb_info *writer = NULL;
        int err = 0;
 
        redirty_page_for_writepage(wbc, page);
        unlock_page(page);
 
+       inode = page->mapping->host;
+       if (!inode)
+               return 0;
+
+       sb = inode->i_sb;
+       nilfs = NILFS_MDT(inode)->mi_nilfs;
+
        if (page->mapping->assoc_mapping)
                return 0; /* Do not request flush for shadow page cache */
        if (!sb) {
@@ -439,6 +445,27 @@ static const struct address_space_operations def_mdt_aops = {
 static const struct inode_operations def_mdt_iops;
 static const struct file_operations def_mdt_fops;
 
+
+int nilfs_mdt_init(struct inode *inode, struct the_nilfs *nilfs,
+                  gfp_t gfp_mask, size_t objsz)
+{
+       struct nilfs_mdt_info *mi;
+
+       mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
+       if (!mi)
+               return -ENOMEM;
+
+       mi->mi_nilfs = nilfs;
+       init_rwsem(&mi->mi_sem);
+       inode->i_private = mi;
+
+       inode->i_mode = S_IFREG;
+       mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
+       inode->i_mapping->backing_dev_info = nilfs->ns_bdi;
+
+       return 0;
+}
+
 /*
  * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
  * ifile, or gcinodes.  This allows the B-tree code and segment constructor
@@ -454,12 +481,10 @@ static const struct file_operations def_mdt_fops;
  * @nilfs: nilfs object
  * @sb: super block instance the metadata file belongs to
  * @ino: inode number
- * @gfp_mask: gfp mask for data pages
- * @objsz: size of the private object attached to inode->i_private
  */
 struct inode *
 nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
-                    ino_t ino, gfp_t gfp_mask, size_t objsz)
+                    ino_t ino)
 {
        struct inode *inode = nilfs_alloc_inode_common(nilfs);
 
@@ -467,15 +492,6 @@ nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
                return NULL;
        else {
                struct address_space * const mapping = &inode->i_data;
-               struct nilfs_mdt_info *mi;
-
-               mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
-               if (!mi) {
-                       nilfs_destroy_inode(inode);
-                       return NULL;
-               }
-               mi->mi_nilfs = nilfs;
-               init_rwsem(&mi->mi_sem);
 
                inode->i_sb = sb; /* sb may be NULL for some meta data files */
                inode->i_blkbits = nilfs->ns_blocksize_bits;
@@ -483,8 +499,6 @@ nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
                atomic_set(&inode->i_count, 1);
                inode->i_nlink = 1;
                inode->i_ino = ino;
-               inode->i_mode = S_IFREG;
-               inode->i_private = mi;
 
 #ifdef INIT_UNUSED_INODE_FIELDS
                atomic_set(&inode->i_writecount, 0);
@@ -515,9 +529,7 @@ nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
 
                mapping->host = NULL;  /* instead of inode */
                mapping->flags = 0;
-               mapping_set_gfp_mask(mapping, gfp_mask);
                mapping->assoc_mapping = NULL;
-               mapping->backing_dev_info = nilfs->ns_bdi;
 
                inode->i_mapping = mapping;
        }
@@ -530,10 +542,14 @@ struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
 {
        struct inode *inode;
 
-       inode = nilfs_mdt_new_common(nilfs, sb, ino, NILFS_MDT_GFP, objsz);
+       inode = nilfs_mdt_new_common(nilfs, sb, ino);
        if (!inode)
                return NULL;
 
+       if (nilfs_mdt_init(inode, nilfs, NILFS_MDT_GFP, objsz) < 0) {
+               nilfs_destroy_inode(inode);
+               return NULL;
+       }
        inode->i_op = &def_mdt_iops;
        inode->i_fop = &def_mdt_fops;
        inode->i_mapping->a_ops = &def_mdt_aops;
@@ -557,6 +573,96 @@ void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow)
                &NILFS_I(orig)->i_btnode_cache;
 }
 
+static const struct address_space_operations shadow_map_aops = {
+       .sync_page              = block_sync_page,
+};
+
+/**
+ * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
+ * @inode: inode of the metadata file
+ * @shadow: shadow mapping
+ */
+int nilfs_mdt_setup_shadow_map(struct inode *inode,
+                              struct nilfs_shadow_map *shadow)
+{
+       struct nilfs_mdt_info *mi = NILFS_MDT(inode);
+       struct backing_dev_info *bdi = NILFS_I_NILFS(inode)->ns_bdi;
+
+       INIT_LIST_HEAD(&shadow->frozen_buffers);
+       nilfs_mapping_init_once(&shadow->frozen_data);
+       nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
+       nilfs_mapping_init_once(&shadow->frozen_btnodes);
+       nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
+       mi->mi_shadow = shadow;
+       return 0;
+}
+
+/**
+ * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
+ * @inode: inode of the metadata file
+ */
+int nilfs_mdt_save_to_shadow_map(struct inode *inode)
+{
+       struct nilfs_mdt_info *mi = NILFS_MDT(inode);
+       struct nilfs_inode_info *ii = NILFS_I(inode);
+       struct nilfs_shadow_map *shadow = mi->mi_shadow;
+       int ret;
+
+       ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
+       if (ret)
+               goto out;
+
+       ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
+                                    &ii->i_btnode_cache);
+       if (ret)
+               goto out;
+
+       nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
+ out:
+       return ret;
+}
+
+/**
+ * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
+ * @inode: inode of the metadata file
+ */
+void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
+{
+       struct nilfs_mdt_info *mi = NILFS_MDT(inode);
+       struct nilfs_inode_info *ii = NILFS_I(inode);
+       struct nilfs_shadow_map *shadow = mi->mi_shadow;
+
+       down_write(&mi->mi_sem);
+
+       if (mi->mi_palloc_cache)
+               nilfs_palloc_clear_cache(inode);
+
+       nilfs_clear_dirty_pages(inode->i_mapping);
+       nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
+
+       nilfs_clear_dirty_pages(&ii->i_btnode_cache);
+       nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
+
+       nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
+
+       up_write(&mi->mi_sem);
+}
+
+/**
+ * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
+ * @inode: inode of the metadata file
+ */
+void nilfs_mdt_clear_shadow_map(struct inode *inode)
+{
+       struct nilfs_mdt_info *mi = NILFS_MDT(inode);
+       struct nilfs_shadow_map *shadow = mi->mi_shadow;
+
+       down_write(&mi->mi_sem);
+       truncate_inode_pages(&shadow->frozen_data, 0);
+       truncate_inode_pages(&shadow->frozen_btnodes, 0);
+       up_write(&mi->mi_sem);
+}
+
 static void nilfs_mdt_clear(struct inode *inode)
 {
        struct nilfs_inode_info *ii = NILFS_I(inode);
@@ -577,7 +683,5 @@ void nilfs_mdt_destroy(struct inode *inode)
                nilfs_palloc_destroy_cache(inode);
        nilfs_mdt_clear(inode);
 
-       kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
-       kfree(mdi);
        nilfs_destroy_inode(inode);
 }
This page took 0.027366 seconds and 5 git commands to generate.