add patch factor-out-helper-ext4_sample_last_mounted
[ext4-patch-queue.git] / factor-out-helper-ext4_sample_last_mounted
blob882209356d12c02ad1fa264d0cf88120cb349ce7
1 ext4: factor out helper ext4_sample_last_mounted()
3 From: Amir Goldstein <amir73il@gmail.com>
5 Signed-off-by: Amir Goldstein <amir73il@gmail.com>
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
7 Reviewed-by: Jan Kara <jack@suse.cz>
8 ---
9  fs/ext4/file.c | 82 ++++++++++++++++++++++++++++++++--------------------------
10  1 file changed, 46 insertions(+), 36 deletions(-)
12 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
13 index fb6f023622fe..065e95bb7186 100644
14 --- a/fs/ext4/file.c
15 +++ b/fs/ext4/file.c
16 @@ -380,50 +380,60 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
17         return 0;
18  }
20 -static int ext4_file_open(struct inode * inode, struct file * filp)
21 +static int ext4_sample_last_mounted(struct super_block *sb,
22 +                                   struct vfsmount *mnt)
23  {
24 -       struct super_block *sb = inode->i_sb;
25 -       struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
26 -       struct vfsmount *mnt = filp->f_path.mnt;
27 +       struct ext4_sb_info *sbi = EXT4_SB(sb);
28         struct path path;
29         char buf[64], *cp;
30 +       handle_t *handle;
31 +       int err;
33 +       if (likely(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED))
34 +               return 0;
36 +       if (sb_rdonly(sb))
37 +               return 0;
39 +       sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
40 +       /*
41 +        * Sample where the filesystem has been mounted and
42 +        * store it in the superblock for sysadmin convenience
43 +        * when trying to sort through large numbers of block
44 +        * devices or filesystem images.
45 +        */
46 +       memset(buf, 0, sizeof(buf));
47 +       path.mnt = mnt;
48 +       path.dentry = mnt->mnt_root;
49 +       cp = d_path(&path, buf, sizeof(buf));
50 +       if (IS_ERR(cp))
51 +               return 0;
53 +       handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
54 +       if (IS_ERR(handle))
55 +               return PTR_ERR(handle);
56 +       BUFFER_TRACE(sbi->s_sbh, "get_write_access");
57 +       err = ext4_journal_get_write_access(handle, sbi->s_sbh);
58 +       if (err)
59 +               goto out;
60 +       strlcpy(sbi->s_es->s_last_mounted, cp,
61 +               sizeof(sbi->s_es->s_last_mounted));
62 +       ext4_handle_dirty_super(handle, sb);
63 +out:
64 +       ext4_journal_stop(handle);
65 +       return err;
68 +static int ext4_file_open(struct inode * inode, struct file * filp)
70         int ret;
72         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
73                 return -EIO;
75 -       if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
76 -                    !sb_rdonly(sb))) {
77 -               sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
78 -               /*
79 -                * Sample where the filesystem has been mounted and
80 -                * store it in the superblock for sysadmin convenience
81 -                * when trying to sort through large numbers of block
82 -                * devices or filesystem images.
83 -                */
84 -               memset(buf, 0, sizeof(buf));
85 -               path.mnt = mnt;
86 -               path.dentry = mnt->mnt_root;
87 -               cp = d_path(&path, buf, sizeof(buf));
88 -               if (!IS_ERR(cp)) {
89 -                       handle_t *handle;
90 -                       int err;
92 -                       handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
93 -                       if (IS_ERR(handle))
94 -                               return PTR_ERR(handle);
95 -                       BUFFER_TRACE(sbi->s_sbh, "get_write_access");
96 -                       err = ext4_journal_get_write_access(handle, sbi->s_sbh);
97 -                       if (err) {
98 -                               ext4_journal_stop(handle);
99 -                               return err;
100 -                       }
101 -                       strlcpy(sbi->s_es->s_last_mounted, cp,
102 -                               sizeof(sbi->s_es->s_last_mounted));
103 -                       ext4_handle_dirty_super(handle, sb);
104 -                       ext4_journal_stop(handle);
105 -               }
106 -       }
107 +       ret = ext4_sample_last_mounted(inode->i_sb, filp->f_path.mnt);
108 +       if (ret)
109 +               return ret;
111         ret = fscrypt_file_open(inode, filp);
112         if (ret)
113 -- 
114 2.7.4