main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #define FSCACHE_DEBUG_LEVEL CACHE
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/completion.h>
  16. #include <linux/slab.h>
  17. #include <linux/seq_file.h>
  18. #include "internal.h"
  19. MODULE_DESCRIPTION("FS Cache Manager");
  20. MODULE_AUTHOR("Red Hat, Inc.");
  21. MODULE_LICENSE("GPL");
  22. unsigned fscache_defer_lookup = 1;
  23. module_param_named(defer_lookup, fscache_defer_lookup, uint,
  24. S_IWUSR | S_IRUGO);
  25. MODULE_PARM_DESC(fscache_defer_lookup,
  26. "Defer cookie lookup to background thread");
  27. unsigned fscache_defer_create = 1;
  28. module_param_named(defer_create, fscache_defer_create, uint,
  29. S_IWUSR | S_IRUGO);
  30. MODULE_PARM_DESC(fscache_defer_create,
  31. "Defer cookie creation to background thread");
  32. unsigned fscache_debug;
  33. module_param_named(debug, fscache_debug, uint,
  34. S_IWUSR | S_IRUGO);
  35. MODULE_PARM_DESC(fscache_debug,
  36. "FS-Cache debugging mask");
  37. struct kobject *fscache_root;
  38. struct workqueue_struct *fscache_object_wq;
  39. struct workqueue_struct *fscache_op_wq;
  40. DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
  41. /* these values serve as lower bounds, will be adjusted in fscache_init() */
  42. static unsigned fscache_object_max_active = 4;
  43. static unsigned fscache_op_max_active = 2;
  44. #ifdef CONFIG_SYSCTL
  45. static struct ctl_table_header *fscache_sysctl_header;
  46. static int fscache_max_active_sysctl(struct ctl_table *table, int write,
  47. void __user *buffer,
  48. size_t *lenp, loff_t *ppos)
  49. {
  50. struct workqueue_struct **wqp = table->extra1;
  51. unsigned int *datap = table->data;
  52. int ret;
  53. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  54. if (ret == 0)
  55. workqueue_set_max_active(*wqp, *datap);
  56. return ret;
  57. }
  58. ctl_table fscache_sysctls[] = {
  59. {
  60. .procname = "object_max_active",
  61. .data = &fscache_object_max_active,
  62. .maxlen = sizeof(unsigned),
  63. .mode = 0644,
  64. .proc_handler = fscache_max_active_sysctl,
  65. .extra1 = &fscache_object_wq,
  66. },
  67. {
  68. .procname = "operation_max_active",
  69. .data = &fscache_op_max_active,
  70. .maxlen = sizeof(unsigned),
  71. .mode = 0644,
  72. .proc_handler = fscache_max_active_sysctl,
  73. .extra1 = &fscache_op_wq,
  74. },
  75. {}
  76. };
  77. ctl_table fscache_sysctls_root[] = {
  78. {
  79. .procname = "fscache",
  80. .mode = 0555,
  81. .child = fscache_sysctls,
  82. },
  83. {}
  84. };
  85. #endif
  86. /*
  87. * initialise the fs caching module
  88. */
  89. static int __init fscache_init(void)
  90. {
  91. unsigned int nr_cpus = num_possible_cpus();
  92. unsigned int cpu;
  93. int ret;
  94. ret = slow_work_register_user(THIS_MODULE);
  95. if (ret < 0)
  96. goto error_slow_work;
  97. fscache_object_max_active =
  98. clamp_val(nr_cpus,
  99. fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
  100. ret = -ENOMEM;
  101. fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
  102. fscache_object_max_active);
  103. if (!fscache_object_wq)
  104. goto error_object_wq;
  105. fscache_op_max_active =
  106. clamp_val(fscache_object_max_active / 2,
  107. fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
  108. ret = -ENOMEM;
  109. fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
  110. fscache_op_max_active);
  111. if (!fscache_op_wq)
  112. goto error_op_wq;
  113. for_each_possible_cpu(cpu)
  114. init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
  115. ret = fscache_proc_init();
  116. if (ret < 0)
  117. goto error_proc;
  118. #ifdef CONFIG_SYSCTL
  119. ret = -ENOMEM;
  120. fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
  121. if (!fscache_sysctl_header)
  122. goto error_sysctl;
  123. #endif
  124. fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
  125. sizeof(struct fscache_cookie),
  126. 0,
  127. 0,
  128. fscache_cookie_init_once);
  129. if (!fscache_cookie_jar) {
  130. printk(KERN_NOTICE
  131. "FS-Cache: Failed to allocate a cookie jar\n");
  132. ret = -ENOMEM;
  133. goto error_cookie_jar;
  134. }
  135. fscache_root = kobject_create_and_add("fscache", kernel_kobj);
  136. if (!fscache_root)
  137. goto error_kobj;
  138. printk(KERN_NOTICE "FS-Cache: Loaded\n");
  139. return 0;
  140. error_kobj:
  141. kmem_cache_destroy(fscache_cookie_jar);
  142. error_cookie_jar:
  143. #ifdef CONFIG_SYSCTL
  144. unregister_sysctl_table(fscache_sysctl_header);
  145. error_sysctl:
  146. #endif
  147. fscache_proc_cleanup();
  148. error_proc:
  149. destroy_workqueue(fscache_op_wq);
  150. error_op_wq:
  151. destroy_workqueue(fscache_object_wq);
  152. error_object_wq:
  153. slow_work_unregister_user(THIS_MODULE);
  154. error_slow_work:
  155. return ret;
  156. }
  157. fs_initcall(fscache_init);
  158. /*
  159. * clean up on module removal
  160. */
  161. static void __exit fscache_exit(void)
  162. {
  163. _enter("");
  164. kobject_put(fscache_root);
  165. kmem_cache_destroy(fscache_cookie_jar);
  166. unregister_sysctl_table(fscache_sysctl_header);
  167. fscache_proc_cleanup();
  168. destroy_workqueue(fscache_op_wq);
  169. destroy_workqueue(fscache_object_wq);
  170. slow_work_unregister_user(THIS_MODULE);
  171. printk(KERN_NOTICE "FS-Cache: Unloaded\n");
  172. }
  173. module_exit(fscache_exit);
  174. /*
  175. * wait_on_bit() sleep function for uninterruptible waiting
  176. */
  177. int fscache_wait_bit(void *flags)
  178. {
  179. schedule();
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(fscache_wait_bit);
  183. /*
  184. * wait_on_bit() sleep function for interruptible waiting
  185. */
  186. int fscache_wait_bit_interruptible(void *flags)
  187. {
  188. schedule();
  189. return signal_pending(current);
  190. }
  191. EXPORT_SYMBOL(fscache_wait_bit_interruptible);