main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. fscache_object_max_active =
  95. clamp_val(nr_cpus,
  96. fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
  97. ret = -ENOMEM;
  98. fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
  99. fscache_object_max_active);
  100. if (!fscache_object_wq)
  101. goto error_object_wq;
  102. fscache_op_max_active =
  103. clamp_val(fscache_object_max_active / 2,
  104. fscache_op_max_active, WQ_UNBOUND_MAX_ACTIVE);
  105. ret = -ENOMEM;
  106. fscache_op_wq = alloc_workqueue("fscache_operation", WQ_UNBOUND,
  107. fscache_op_max_active);
  108. if (!fscache_op_wq)
  109. goto error_op_wq;
  110. for_each_possible_cpu(cpu)
  111. init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
  112. ret = fscache_proc_init();
  113. if (ret < 0)
  114. goto error_proc;
  115. #ifdef CONFIG_SYSCTL
  116. ret = -ENOMEM;
  117. fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
  118. if (!fscache_sysctl_header)
  119. goto error_sysctl;
  120. #endif
  121. fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
  122. sizeof(struct fscache_cookie),
  123. 0,
  124. 0,
  125. fscache_cookie_init_once);
  126. if (!fscache_cookie_jar) {
  127. printk(KERN_NOTICE
  128. "FS-Cache: Failed to allocate a cookie jar\n");
  129. ret = -ENOMEM;
  130. goto error_cookie_jar;
  131. }
  132. fscache_root = kobject_create_and_add("fscache", kernel_kobj);
  133. if (!fscache_root)
  134. goto error_kobj;
  135. printk(KERN_NOTICE "FS-Cache: Loaded\n");
  136. return 0;
  137. error_kobj:
  138. kmem_cache_destroy(fscache_cookie_jar);
  139. error_cookie_jar:
  140. #ifdef CONFIG_SYSCTL
  141. unregister_sysctl_table(fscache_sysctl_header);
  142. error_sysctl:
  143. #endif
  144. fscache_proc_cleanup();
  145. error_proc:
  146. destroy_workqueue(fscache_op_wq);
  147. error_op_wq:
  148. destroy_workqueue(fscache_object_wq);
  149. error_object_wq:
  150. return ret;
  151. }
  152. fs_initcall(fscache_init);
  153. /*
  154. * clean up on module removal
  155. */
  156. static void __exit fscache_exit(void)
  157. {
  158. _enter("");
  159. kobject_put(fscache_root);
  160. kmem_cache_destroy(fscache_cookie_jar);
  161. #ifdef CONFIG_SYSCTL
  162. unregister_sysctl_table(fscache_sysctl_header);
  163. #endif
  164. fscache_proc_cleanup();
  165. destroy_workqueue(fscache_op_wq);
  166. destroy_workqueue(fscache_object_wq);
  167. printk(KERN_NOTICE "FS-Cache: Unloaded\n");
  168. }
  169. module_exit(fscache_exit);
  170. /*
  171. * wait_on_bit() sleep function for uninterruptible waiting
  172. */
  173. int fscache_wait_bit(void *flags)
  174. {
  175. schedule();
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(fscache_wait_bit);
  179. /*
  180. * wait_on_bit() sleep function for interruptible waiting
  181. */
  182. int fscache_wait_bit_interruptible(void *flags)
  183. {
  184. schedule();
  185. return signal_pending(current);
  186. }
  187. EXPORT_SYMBOL(fscache_wait_bit_interruptible);