1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CLEANCACHE_H
3 #define _LINUX_CLEANCACHE_H
4
5 #include <linux/fs.h>
6 #include <linux/exportfs.h>
7 #include <linux/mm.h>
8 #include <linux/android_vendor.h>
9
10 #define CLEANCACHE_NO_POOL -1
11 #define CLEANCACHE_NO_BACKEND -2
12 #define CLEANCACHE_NO_BACKEND_SHARED -3
13
14 #define CLEANCACHE_KEY_MAX 6
15
16 /*
17 * cleancache requires every file with a page in cleancache to have a
18 * unique key unless/until the file is removed/truncated. For some
19 * filesystems, the inode number is unique, but for "modern" filesystems
20 * an exportable filehandle is required (see exportfs.h)
21 */
22 struct cleancache_filekey {
23 union {
24 ino_t ino;
25 __u32 fh[CLEANCACHE_KEY_MAX];
26 u32 key[CLEANCACHE_KEY_MAX];
27 } u;
28 };
29
30 struct cleancache_ops {
31 int (*init_fs)(size_t);
32 int (*init_shared_fs)(uuid_t *uuid, size_t);
33 int (*get_page)(int, struct cleancache_filekey,
34 pgoff_t, struct page *);
35 void (*put_page)(int, struct cleancache_filekey,
36 pgoff_t, struct page *);
37 void (*invalidate_page)(int, struct cleancache_filekey, pgoff_t);
38 void (*invalidate_inode)(int, struct cleancache_filekey);
39 void (*invalidate_fs)(int);
40 ANDROID_OEM_DATA(1);
41 };
42
43 extern int cleancache_register_ops(const struct cleancache_ops *ops);
44 extern void __cleancache_init_fs(struct super_block *);
45 extern void __cleancache_init_shared_fs(struct super_block *);
46 extern int __cleancache_get_page(struct page *);
47 extern void __cleancache_put_page(struct page *);
48 extern void __cleancache_invalidate_page(struct address_space *, struct page *);
49 extern void __cleancache_invalidate_inode(struct address_space *);
50 extern void __cleancache_invalidate_fs(struct super_block *);
51
52 #ifdef CONFIG_CLEANCACHE
53 #define cleancache_enabled (1)
cleancache_fs_enabled_mapping(struct address_space * mapping)54 static inline bool cleancache_fs_enabled_mapping(struct address_space *mapping)
55 {
56 return mapping->host->i_sb->cleancache_poolid >= 0;
57 }
cleancache_fs_enabled(struct page * page)58 static inline bool cleancache_fs_enabled(struct page *page)
59 {
60 return cleancache_fs_enabled_mapping(page->mapping);
61 }
62 #else
63 #define cleancache_enabled (0)
64 #define cleancache_fs_enabled(_page) (0)
65 #define cleancache_fs_enabled_mapping(_page) (0)
66 #endif
67
68 /*
69 * The shim layer provided by these inline functions allows the compiler
70 * to reduce all cleancache hooks to nothingness if CONFIG_CLEANCACHE
71 * is disabled, to a single global variable check if CONFIG_CLEANCACHE
72 * is enabled but no cleancache "backend" has dynamically enabled it,
73 * and, for the most frequent cleancache ops, to a single global variable
74 * check plus a superblock element comparison if CONFIG_CLEANCACHE is enabled
75 * and a cleancache backend has dynamically enabled cleancache, but the
76 * filesystem referenced by that cleancache op has not enabled cleancache.
77 * As a result, CONFIG_CLEANCACHE can be enabled by default with essentially
78 * no measurable performance impact.
79 */
80
cleancache_init_fs(struct super_block * sb)81 static inline void cleancache_init_fs(struct super_block *sb)
82 {
83 if (cleancache_enabled)
84 __cleancache_init_fs(sb);
85 }
86
cleancache_init_shared_fs(struct super_block * sb)87 static inline void cleancache_init_shared_fs(struct super_block *sb)
88 {
89 if (cleancache_enabled)
90 __cleancache_init_shared_fs(sb);
91 }
92
cleancache_get_page(struct page * page)93 static inline int cleancache_get_page(struct page *page)
94 {
95 if (cleancache_enabled && cleancache_fs_enabled(page))
96 return __cleancache_get_page(page);
97 return -1;
98 }
99
cleancache_put_page(struct page * page)100 static inline void cleancache_put_page(struct page *page)
101 {
102 if (cleancache_enabled && cleancache_fs_enabled(page))
103 __cleancache_put_page(page);
104 }
105
cleancache_invalidate_page(struct address_space * mapping,struct page * page)106 static inline void cleancache_invalidate_page(struct address_space *mapping,
107 struct page *page)
108 {
109 /* careful... page->mapping is NULL sometimes when this is called */
110 if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
111 __cleancache_invalidate_page(mapping, page);
112 }
113
cleancache_invalidate_inode(struct address_space * mapping)114 static inline void cleancache_invalidate_inode(struct address_space *mapping)
115 {
116 if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
117 __cleancache_invalidate_inode(mapping);
118 }
119
cleancache_invalidate_fs(struct super_block * sb)120 static inline void cleancache_invalidate_fs(struct super_block *sb)
121 {
122 if (cleancache_enabled)
123 __cleancache_invalidate_fs(sb);
124 }
125
126 #endif /* _LINUX_CLEANCACHE_H */
127