blob: 5f5730c1d324ffe39121869405baa29285d8631a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dan Magenheimer077b1f82011-05-26 10:01:36 -06002#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
Vladimir Davydov3cb29d12015-04-14 15:46:48 -07009#define CLEANCACHE_NO_POOL -1
10#define CLEANCACHE_NO_BACKEND -2
11#define CLEANCACHE_NO_BACKEND_SHARED -3
12
Dan Magenheimer077b1f82011-05-26 10:01:36 -060013#define CLEANCACHE_KEY_MAX 6
14
15/*
16 * cleancache requires every file with a page in cleancache to have a
17 * unique key unless/until the file is removed/truncated. For some
18 * filesystems, the inode number is unique, but for "modern" filesystems
19 * an exportable filehandle is required (see exportfs.h)
20 */
21struct cleancache_filekey {
22 union {
23 ino_t ino;
24 __u32 fh[CLEANCACHE_KEY_MAX];
25 u32 key[CLEANCACHE_KEY_MAX];
26 } u;
27};
28
29struct cleancache_ops {
30 int (*init_fs)(size_t);
Christoph Hellwig85787092017-05-10 15:06:33 +020031 int (*init_shared_fs)(uuid_t *uuid, size_t);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060032 int (*get_page)(int, struct cleancache_filekey,
33 pgoff_t, struct page *);
34 void (*put_page)(int, struct cleancache_filekey,
35 pgoff_t, struct page *);
Dan Magenheimer91c6cc92012-01-12 14:03:25 -050036 void (*invalidate_page)(int, struct cleancache_filekey, pgoff_t);
37 void (*invalidate_inode)(int, struct cleancache_filekey);
38 void (*invalidate_fs)(int);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060039};
40
Julia Lawallb3c6de42016-01-21 16:47:29 +010041extern int cleancache_register_ops(const struct cleancache_ops *ops);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060042extern void __cleancache_init_fs(struct super_block *);
Vladimir Davydov9de16262015-04-14 15:46:42 -070043extern void __cleancache_init_shared_fs(struct super_block *);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060044extern int __cleancache_get_page(struct page *);
45extern void __cleancache_put_page(struct page *);
Dan Magenheimer31677602011-09-21 11:56:28 -040046extern void __cleancache_invalidate_page(struct address_space *, struct page *);
47extern void __cleancache_invalidate_inode(struct address_space *);
48extern void __cleancache_invalidate_fs(struct super_block *);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060049
50#ifdef CONFIG_CLEANCACHE
Bob Liuff610a12013-04-30 15:26:58 -070051#define cleancache_enabled (1)
Dan Magenheimer077b1f82011-05-26 10:01:36 -060052static inline bool cleancache_fs_enabled_mapping(struct address_space *mapping)
53{
54 return mapping->host->i_sb->cleancache_poolid >= 0;
55}
Chen Ganga39bb9a2016-01-07 05:06:13 +080056static inline bool cleancache_fs_enabled(struct page *page)
57{
58 return cleancache_fs_enabled_mapping(page->mapping);
59}
Dan Magenheimer077b1f82011-05-26 10:01:36 -060060#else
61#define cleancache_enabled (0)
62#define cleancache_fs_enabled(_page) (0)
63#define cleancache_fs_enabled_mapping(_page) (0)
64#endif
65
66/*
67 * The shim layer provided by these inline functions allows the compiler
68 * to reduce all cleancache hooks to nothingness if CONFIG_CLEANCACHE
69 * is disabled, to a single global variable check if CONFIG_CLEANCACHE
70 * is enabled but no cleancache "backend" has dynamically enabled it,
71 * and, for the most frequent cleancache ops, to a single global variable
72 * check plus a superblock element comparison if CONFIG_CLEANCACHE is enabled
73 * and a cleancache backend has dynamically enabled cleancache, but the
74 * filesystem referenced by that cleancache op has not enabled cleancache.
75 * As a result, CONFIG_CLEANCACHE can be enabled by default with essentially
76 * no measurable performance impact.
77 */
78
79static inline void cleancache_init_fs(struct super_block *sb)
80{
81 if (cleancache_enabled)
82 __cleancache_init_fs(sb);
83}
84
Vladimir Davydov9de16262015-04-14 15:46:42 -070085static inline void cleancache_init_shared_fs(struct super_block *sb)
Dan Magenheimer077b1f82011-05-26 10:01:36 -060086{
87 if (cleancache_enabled)
Vladimir Davydov9de16262015-04-14 15:46:42 -070088 __cleancache_init_shared_fs(sb);
Dan Magenheimer077b1f82011-05-26 10:01:36 -060089}
90
91static inline int cleancache_get_page(struct page *page)
92{
Dan Magenheimer077b1f82011-05-26 10:01:36 -060093 if (cleancache_enabled && cleancache_fs_enabled(page))
Chen Ganga39bb9a2016-01-07 05:06:13 +080094 return __cleancache_get_page(page);
95 return -1;
Dan Magenheimer077b1f82011-05-26 10:01:36 -060096}
97
98static inline void cleancache_put_page(struct page *page)
99{
100 if (cleancache_enabled && cleancache_fs_enabled(page))
101 __cleancache_put_page(page);
102}
103
Dan Magenheimer31677602011-09-21 11:56:28 -0400104static inline void cleancache_invalidate_page(struct address_space *mapping,
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600105 struct page *page)
106{
107 /* careful... page->mapping is NULL sometimes when this is called */
108 if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
Dan Magenheimer31677602011-09-21 11:56:28 -0400109 __cleancache_invalidate_page(mapping, page);
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600110}
111
Dan Magenheimer31677602011-09-21 11:56:28 -0400112static inline void cleancache_invalidate_inode(struct address_space *mapping)
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600113{
114 if (cleancache_enabled && cleancache_fs_enabled_mapping(mapping))
Dan Magenheimer31677602011-09-21 11:56:28 -0400115 __cleancache_invalidate_inode(mapping);
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600116}
117
Dan Magenheimer31677602011-09-21 11:56:28 -0400118static inline void cleancache_invalidate_fs(struct super_block *sb)
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600119{
120 if (cleancache_enabled)
Dan Magenheimer31677602011-09-21 11:56:28 -0400121 __cleancache_invalidate_fs(sb);
Dan Magenheimer077b1f82011-05-26 10:01:36 -0600122}
123
124#endif /* _LINUX_CLEANCACHE_H */