blob: 3f369c6f816de1219dc8798dca93d7a36242472b [file] [log] [blame]
David Howells77443f62021-11-26 14:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
2/* Network filesystem caching backend to use cache files on a premounted
3 * filesystem
4 *
5 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 */
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/sched.h>
12#include <linux/completion.h>
13#include <linux/slab.h>
14#include <linux/fs.h>
15#include <linux/file.h>
16#include <linux/namei.h>
17#include <linux/mount.h>
18#include <linux/statfs.h>
19#include <linux/sysctl.h>
20#include <linux/miscdevice.h>
21#include <linux/netfs.h>
22#include <trace/events/netfs.h>
23#define CREATE_TRACE_POINTS
24#include "internal.h"
25
26unsigned cachefiles_debug;
27module_param_named(debug, cachefiles_debug, uint, S_IWUSR | S_IRUGO);
28MODULE_PARM_DESC(cachefiles_debug, "CacheFiles debugging mask");
29
30MODULE_DESCRIPTION("Mounted-filesystem based cache");
31MODULE_AUTHOR("Red Hat, Inc.");
32MODULE_LICENSE("GPL");
33
David Howellsdf98e872021-11-17 16:22:21 +000034struct kmem_cache *cachefiles_object_jar;
35
David Howells8667d432021-11-26 15:12:07 +000036static struct miscdevice cachefiles_dev = {
37 .minor = MISC_DYNAMIC_MINOR,
38 .name = "cachefiles",
39 .fops = &cachefiles_daemon_fops,
40};
41
David Howells77443f62021-11-26 14:32:29 +000042/*
43 * initialise the fs caching module
44 */
45static int __init cachefiles_init(void)
46{
David Howellsa70f6522021-10-21 08:15:26 +010047 int ret;
48
49 ret = cachefiles_register_error_injection();
50 if (ret < 0)
51 goto error_einj;
David Howells8667d432021-11-26 15:12:07 +000052 ret = misc_register(&cachefiles_dev);
53 if (ret < 0)
54 goto error_dev;
David Howellsa70f6522021-10-21 08:15:26 +010055
David Howellsdf98e872021-11-17 16:22:21 +000056 /* create an object jar */
57 ret = -ENOMEM;
58 cachefiles_object_jar =
59 kmem_cache_create("cachefiles_object_jar",
60 sizeof(struct cachefiles_object),
61 0, SLAB_HWCACHE_ALIGN, NULL);
62 if (!cachefiles_object_jar) {
63 pr_notice("Failed to allocate an object jar\n");
64 goto error_object_jar;
65 }
66
David Howells77443f62021-11-26 14:32:29 +000067 pr_info("Loaded\n");
68 return 0;
David Howellsa70f6522021-10-21 08:15:26 +010069
David Howellsdf98e872021-11-17 16:22:21 +000070error_object_jar:
71 misc_deregister(&cachefiles_dev);
David Howells8667d432021-11-26 15:12:07 +000072error_dev:
73 cachefiles_unregister_error_injection();
David Howellsa70f6522021-10-21 08:15:26 +010074error_einj:
75 pr_err("failed to register: %d\n", ret);
76 return ret;
David Howells77443f62021-11-26 14:32:29 +000077}
78
79fs_initcall(cachefiles_init);
80
81/*
82 * clean up on module removal
83 */
84static void __exit cachefiles_exit(void)
85{
86 pr_info("Unloading\n");
David Howellsa70f6522021-10-21 08:15:26 +010087
David Howellsdf98e872021-11-17 16:22:21 +000088 kmem_cache_destroy(cachefiles_object_jar);
David Howells8667d432021-11-26 15:12:07 +000089 misc_deregister(&cachefiles_dev);
David Howellsa70f6522021-10-21 08:15:26 +010090 cachefiles_unregister_error_injection();
David Howells77443f62021-11-26 14:32:29 +000091}
92
93module_exit(cachefiles_exit);