David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 1 | /* General filesystem local caching manager |
| 2 | * |
| 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #define FSCACHE_DEBUG_LEVEL CACHE |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/slab.h> |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 18 | #include <linux/seq_file.h> |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
| 21 | MODULE_DESCRIPTION("FS Cache Manager"); |
| 22 | MODULE_AUTHOR("Red Hat, Inc."); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
| 25 | unsigned fscache_defer_lookup = 1; |
| 26 | module_param_named(defer_lookup, fscache_defer_lookup, uint, |
| 27 | S_IWUSR | S_IRUGO); |
| 28 | MODULE_PARM_DESC(fscache_defer_lookup, |
| 29 | "Defer cookie lookup to background thread"); |
| 30 | |
| 31 | unsigned fscache_defer_create = 1; |
| 32 | module_param_named(defer_create, fscache_defer_create, uint, |
| 33 | S_IWUSR | S_IRUGO); |
| 34 | MODULE_PARM_DESC(fscache_defer_create, |
| 35 | "Defer cookie creation to background thread"); |
| 36 | |
| 37 | unsigned fscache_debug; |
| 38 | module_param_named(debug, fscache_debug, uint, |
| 39 | S_IWUSR | S_IRUGO); |
| 40 | MODULE_PARM_DESC(fscache_debug, |
| 41 | "FS-Cache debugging mask"); |
| 42 | |
| 43 | struct kobject *fscache_root; |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 44 | struct workqueue_struct *fscache_object_wq; |
| 45 | |
| 46 | DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait); |
| 47 | |
| 48 | /* these values serve as lower bounds, will be adjusted in fscache_init() */ |
| 49 | static unsigned fscache_object_max_active = 4; |
| 50 | |
| 51 | #ifdef CONFIG_SYSCTL |
| 52 | static struct ctl_table_header *fscache_sysctl_header; |
| 53 | |
| 54 | static int fscache_max_active_sysctl(struct ctl_table *table, int write, |
| 55 | void __user *buffer, |
| 56 | size_t *lenp, loff_t *ppos) |
| 57 | { |
| 58 | struct workqueue_struct **wqp = table->extra1; |
| 59 | unsigned int *datap = table->data; |
| 60 | int ret; |
| 61 | |
| 62 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 63 | if (ret == 0) |
| 64 | workqueue_set_max_active(*wqp, *datap); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | ctl_table fscache_sysctls[] = { |
| 69 | { |
| 70 | .procname = "object_max_active", |
| 71 | .data = &fscache_object_max_active, |
| 72 | .maxlen = sizeof(unsigned), |
| 73 | .mode = 0644, |
| 74 | .proc_handler = fscache_max_active_sysctl, |
| 75 | .extra1 = &fscache_object_wq, |
| 76 | }, |
| 77 | {} |
| 78 | }; |
| 79 | |
| 80 | ctl_table fscache_sysctls_root[] = { |
| 81 | { |
| 82 | .procname = "fscache", |
| 83 | .mode = 0555, |
| 84 | .child = fscache_sysctls, |
| 85 | }, |
| 86 | {} |
| 87 | }; |
| 88 | #endif |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * initialise the fs caching module |
| 92 | */ |
| 93 | static int __init fscache_init(void) |
| 94 | { |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 95 | unsigned int nr_cpus = num_possible_cpus(); |
| 96 | unsigned int cpu; |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 97 | int ret; |
| 98 | |
David Howells | 3d7a641 | 2009-11-19 18:10:23 +0000 | [diff] [blame] | 99 | ret = slow_work_register_user(THIS_MODULE); |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 100 | if (ret < 0) |
| 101 | goto error_slow_work; |
| 102 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 103 | fscache_object_max_active = |
| 104 | clamp_val(nr_cpus, |
| 105 | fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE); |
| 106 | |
| 107 | ret = -ENOMEM; |
| 108 | fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND, |
| 109 | fscache_object_max_active); |
| 110 | if (!fscache_object_wq) |
| 111 | goto error_object_wq; |
| 112 | |
| 113 | for_each_possible_cpu(cpu) |
| 114 | init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu)); |
| 115 | |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 116 | ret = fscache_proc_init(); |
| 117 | if (ret < 0) |
| 118 | goto error_proc; |
| 119 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 120 | #ifdef CONFIG_SYSCTL |
| 121 | ret = -ENOMEM; |
| 122 | fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root); |
| 123 | if (!fscache_sysctl_header) |
| 124 | goto error_sysctl; |
| 125 | #endif |
| 126 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 127 | fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar", |
| 128 | sizeof(struct fscache_cookie), |
| 129 | 0, |
| 130 | 0, |
| 131 | fscache_cookie_init_once); |
| 132 | if (!fscache_cookie_jar) { |
| 133 | printk(KERN_NOTICE |
| 134 | "FS-Cache: Failed to allocate a cookie jar\n"); |
| 135 | ret = -ENOMEM; |
| 136 | goto error_cookie_jar; |
| 137 | } |
| 138 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 139 | fscache_root = kobject_create_and_add("fscache", kernel_kobj); |
| 140 | if (!fscache_root) |
| 141 | goto error_kobj; |
| 142 | |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 143 | printk(KERN_NOTICE "FS-Cache: Loaded\n"); |
| 144 | return 0; |
| 145 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 146 | error_kobj: |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 147 | kmem_cache_destroy(fscache_cookie_jar); |
| 148 | error_cookie_jar: |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 149 | #ifdef CONFIG_SYSCTL |
| 150 | unregister_sysctl_table(fscache_sysctl_header); |
| 151 | error_sysctl: |
| 152 | #endif |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 153 | fscache_proc_cleanup(); |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 154 | error_proc: |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 155 | destroy_workqueue(fscache_object_wq); |
| 156 | error_object_wq: |
David Howells | 3d7a641 | 2009-11-19 18:10:23 +0000 | [diff] [blame] | 157 | slow_work_unregister_user(THIS_MODULE); |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 158 | error_slow_work: |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | fs_initcall(fscache_init); |
| 163 | |
| 164 | /* |
| 165 | * clean up on module removal |
| 166 | */ |
| 167 | static void __exit fscache_exit(void) |
| 168 | { |
| 169 | _enter(""); |
| 170 | |
David Howells | 4c515dd | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 171 | kobject_put(fscache_root); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 172 | kmem_cache_destroy(fscache_cookie_jar); |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 173 | unregister_sysctl_table(fscache_sysctl_header); |
David Howells | 7394daa | 2009-04-03 16:42:37 +0100 | [diff] [blame] | 174 | fscache_proc_cleanup(); |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame^] | 175 | destroy_workqueue(fscache_object_wq); |
David Howells | 3d7a641 | 2009-11-19 18:10:23 +0000 | [diff] [blame] | 176 | slow_work_unregister_user(THIS_MODULE); |
David Howells | 06b3db1 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 177 | printk(KERN_NOTICE "FS-Cache: Unloaded\n"); |
| 178 | } |
| 179 | |
| 180 | module_exit(fscache_exit); |
David Howells | 2868cbe | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * wait_on_bit() sleep function for uninterruptible waiting |
| 184 | */ |
| 185 | int fscache_wait_bit(void *flags) |
| 186 | { |
| 187 | schedule(); |
| 188 | return 0; |
| 189 | } |
| 190 | EXPORT_SYMBOL(fscache_wait_bit); |
| 191 | |
| 192 | /* |
| 193 | * wait_on_bit() sleep function for interruptible waiting |
| 194 | */ |
| 195 | int fscache_wait_bit_interruptible(void *flags) |
| 196 | { |
| 197 | schedule(); |
| 198 | return signal_pending(current); |
| 199 | } |
| 200 | EXPORT_SYMBOL(fscache_wait_bit_interruptible); |