add patch fix-potential-use-after-free-after-remounting-with-noblock_validity
[ext4-patch-queue.git] / add-clear_es_cache-ioctl
blob490b065cb3d94d9fabde86f60a43cd4d6cccf9f2
1 ext4: add a new ioctl EXT4_IOC_CLEAR_ES_CACHE
3 The new ioctl EXT4_IOC_CLEAR_ES_CACHE will force an inode's extent
4 status cache to be cleared out.  This is intended for use for
5 debugging.
7 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 ---
9  fs/ext4/ext4.h           |  2 ++
10  fs/ext4/extents_status.c | 28 ++++++++++++++++++++++++++++
11  fs/ext4/extents_status.h |  1 +
12  fs/ext4/ioctl.c          |  9 +++++++++
13  4 files changed, 40 insertions(+)
15 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
16 index bf660aa7a9e0..b22f24f1d365 100644
17 --- a/fs/ext4/ext4.h
18 +++ b/fs/ext4/ext4.h
19 @@ -649,6 +649,8 @@ enum {
20  #define EXT4_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
21  #define EXT4_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
22  #define EXT4_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
23 +/* ioctl codes 19--39 are reserved for fscrypt */
24 +#define EXT4_IOC_CLEAR_ES_CACHE                _IO('f', 40)
26  #define EXT4_IOC_FSGETXATTR            FS_IOC_FSGETXATTR
27  #define EXT4_IOC_FSSETXATTR            FS_IOC_FSSETXATTR
28 diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
29 index 7521de2dcf3a..02cc8eb3eb0e 100644
30 --- a/fs/ext4/extents_status.c
31 +++ b/fs/ext4/extents_status.c
32 @@ -1374,6 +1374,34 @@ static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
33         return nr_shrunk;
34  }
36 +/*
37 + * Called to support EXT4_IOC_CLEAR_ES_CACHE.  We can only remove
38 + * discretionary entries from the extent status cache.  (Some entries
39 + * must be present for proper operations.)
40 + */
41 +void ext4_clear_inode_es(struct inode *inode)
43 +       struct ext4_inode_info *ei = EXT4_I(inode);
44 +       struct extent_status *es;
45 +       struct ext4_es_tree *tree;
46 +       struct rb_node *node;
48 +       write_lock(&ei->i_es_lock);
49 +       tree = &EXT4_I(inode)->i_es_tree;
50 +       tree->cache_es = NULL;
51 +       node = rb_first(&tree->root);
52 +       while (node) {
53 +               es = rb_entry(node, struct extent_status, rb_node);
54 +               node = rb_next(node);
55 +               if (!ext4_es_is_delayed(es)) {
56 +                       rb_erase(&es->rb_node, &tree->root);
57 +                       ext4_es_free_extent(inode, es);
58 +               }
59 +       }
60 +       ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
61 +       write_unlock(&ei->i_es_lock);
64  #ifdef ES_DEBUG__
65  static void ext4_print_pending_tree(struct inode *inode)
66  {
67 diff --git a/fs/ext4/extents_status.h b/fs/ext4/extents_status.h
68 index 131a8b7df265..e16785f431e7 100644
69 --- a/fs/ext4/extents_status.h
70 +++ b/fs/ext4/extents_status.h
71 @@ -248,5 +248,6 @@ extern unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk,
72                                         ext4_lblk_t len);
73  extern void ext4_es_remove_blks(struct inode *inode, ext4_lblk_t lblk,
74                                 ext4_lblk_t len);
75 +extern void ext4_clear_inode_es(struct inode *inode);
77  #endif /* _EXT4_EXTENTS_STATUS_H */
78 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
79 index 442f7ef873fc..15b1047878ab 100644
80 --- a/fs/ext4/ioctl.c
81 +++ b/fs/ext4/ioctl.c
82 @@ -1115,6 +1115,14 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
83         case EXT4_IOC_GET_ENCRYPTION_POLICY:
84                 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
86 +       case EXT4_IOC_CLEAR_ES_CACHE:
87 +       {
88 +               if (!inode_owner_or_capable(inode))
89 +                       return -EACCES;
90 +               ext4_clear_inode_es(inode);
91 +               return 0;
92 +       }
94         case EXT4_IOC_FSGETXATTR:
95         {
96                 struct fsxattr fa;
97 @@ -1233,6 +1241,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
98         case EXT4_IOC_GET_ENCRYPTION_POLICY:
99         case EXT4_IOC_SHUTDOWN:
100         case FS_IOC_GETFSMAP:
101 +       case EXT4_IOC_CLEAR_ES_CACHE:
102                 break;
103         default:
104                 return -ENOIOCTLCMD;