123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/exportfs.h>
- #include <linux/mm.h>
- #include <linux/debugfs.h>
- #include <linux/cleancache.h>
- static const struct cleancache_ops *cleancache_ops __read_mostly;
- static u64 cleancache_succ_gets;
- static u64 cleancache_failed_gets;
- static u64 cleancache_puts;
- static u64 cleancache_invalidates;
- static void cleancache_register_ops_sb(struct super_block *sb, void *unused)
- {
- switch (sb->cleancache_poolid) {
- case CLEANCACHE_NO_BACKEND:
- __cleancache_init_fs(sb);
- break;
- case CLEANCACHE_NO_BACKEND_SHARED:
- __cleancache_init_shared_fs(sb);
- break;
- }
- }
- int cleancache_register_ops(const struct cleancache_ops *ops)
- {
- if (cmpxchg(&cleancache_ops, NULL, ops))
- return -EBUSY;
-
- iterate_supers(cleancache_register_ops_sb, NULL);
- return 0;
- }
- EXPORT_SYMBOL(cleancache_register_ops);
- void __cleancache_init_fs(struct super_block *sb)
- {
- int pool_id = CLEANCACHE_NO_BACKEND;
- if (cleancache_ops) {
- pool_id = cleancache_ops->init_fs(PAGE_SIZE);
- if (pool_id < 0)
- pool_id = CLEANCACHE_NO_POOL;
- }
- sb->cleancache_poolid = pool_id;
- }
- EXPORT_SYMBOL(__cleancache_init_fs);
- void __cleancache_init_shared_fs(struct super_block *sb)
- {
- int pool_id = CLEANCACHE_NO_BACKEND_SHARED;
- if (cleancache_ops) {
- pool_id = cleancache_ops->init_shared_fs(sb->s_uuid, PAGE_SIZE);
- if (pool_id < 0)
- pool_id = CLEANCACHE_NO_POOL;
- }
- sb->cleancache_poolid = pool_id;
- }
- EXPORT_SYMBOL(__cleancache_init_shared_fs);
- static int cleancache_get_key(struct inode *inode,
- struct cleancache_filekey *key)
- {
- int (*fhfn)(struct inode *, __u32 *fh, int *, struct inode *);
- int len = 0, maxlen = CLEANCACHE_KEY_MAX;
- struct super_block *sb = inode->i_sb;
- key->u.ino = inode->i_ino;
- if (sb->s_export_op != NULL) {
- fhfn = sb->s_export_op->encode_fh;
- if (fhfn) {
- len = (*fhfn)(inode, &key->u.fh[0], &maxlen, NULL);
- if (len <= FILEID_ROOT || len == FILEID_INVALID)
- return -1;
- if (maxlen > CLEANCACHE_KEY_MAX)
- return -1;
- }
- }
- return 0;
- }
- int __cleancache_get_page(struct page *page)
- {
- int ret = -1;
- int pool_id;
- struct cleancache_filekey key = { .u.key = { 0 } };
- if (!cleancache_ops) {
- cleancache_failed_gets++;
- goto out;
- }
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- pool_id = page->mapping->host->i_sb->cleancache_poolid;
- if (pool_id < 0)
- goto out;
- if (cleancache_get_key(page->mapping->host, &key) < 0)
- goto out;
- ret = cleancache_ops->get_page(pool_id, key, page->index, page);
- if (ret == 0)
- cleancache_succ_gets++;
- else
- cleancache_failed_gets++;
- out:
- return ret;
- }
- EXPORT_SYMBOL(__cleancache_get_page);
- void __cleancache_put_page(struct page *page)
- {
- int pool_id;
- struct cleancache_filekey key = { .u.key = { 0 } };
- if (!cleancache_ops) {
- cleancache_puts++;
- return;
- }
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- pool_id = page->mapping->host->i_sb->cleancache_poolid;
- if (pool_id >= 0 &&
- cleancache_get_key(page->mapping->host, &key) >= 0) {
- cleancache_ops->put_page(pool_id, key, page->index, page);
- cleancache_puts++;
- }
- }
- EXPORT_SYMBOL(__cleancache_put_page);
- void __cleancache_invalidate_page(struct address_space *mapping,
- struct page *page)
- {
-
- int pool_id = mapping->host->i_sb->cleancache_poolid;
- struct cleancache_filekey key = { .u.key = { 0 } };
- if (!cleancache_ops)
- return;
- if (pool_id >= 0) {
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- if (cleancache_get_key(mapping->host, &key) >= 0) {
- cleancache_ops->invalidate_page(pool_id,
- key, page->index);
- cleancache_invalidates++;
- }
- }
- }
- EXPORT_SYMBOL(__cleancache_invalidate_page);
- void __cleancache_invalidate_inode(struct address_space *mapping)
- {
- int pool_id = mapping->host->i_sb->cleancache_poolid;
- struct cleancache_filekey key = { .u.key = { 0 } };
- if (!cleancache_ops)
- return;
- if (pool_id >= 0 && cleancache_get_key(mapping->host, &key) >= 0)
- cleancache_ops->invalidate_inode(pool_id, key);
- }
- EXPORT_SYMBOL(__cleancache_invalidate_inode);
- void __cleancache_invalidate_fs(struct super_block *sb)
- {
- int pool_id;
- pool_id = sb->cleancache_poolid;
- sb->cleancache_poolid = CLEANCACHE_NO_POOL;
- if (cleancache_ops && pool_id >= 0)
- cleancache_ops->invalidate_fs(pool_id);
- }
- EXPORT_SYMBOL(__cleancache_invalidate_fs);
- static int __init init_cleancache(void)
- {
- #ifdef CONFIG_DEBUG_FS
- struct dentry *root = debugfs_create_dir("cleancache", NULL);
- if (root == NULL)
- return -ENXIO;
- debugfs_create_u64("succ_gets", S_IRUGO, root, &cleancache_succ_gets);
- debugfs_create_u64("failed_gets", S_IRUGO,
- root, &cleancache_failed_gets);
- debugfs_create_u64("puts", S_IRUGO, root, &cleancache_puts);
- debugfs_create_u64("invalidates", S_IRUGO,
- root, &cleancache_invalidates);
- #endif
- return 0;
- }
- module_init(init_cleancache)
|