main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. static struct 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. static struct 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. pr_notice("Failed to allocate a cookie jar\n");
  128. ret = -ENOMEM;
  129. goto error_cookie_jar;
  130. }
  131. fscache_root = kobject_create_and_add("fscache", kernel_kobj);
  132. if (!fscache_root)
  133. goto error_kobj;
  134. pr_notice("Loaded\n");
  135. return 0;
  136. error_kobj:
  137. kmem_cache_destroy(fscache_cookie_jar);
  138. error_cookie_jar:
  139. #ifdef CONFIG_SYSCTL
  140. unregister_sysctl_table(fscache_sysctl_header);
  141. error_sysctl:
  142. #endif
  143. fscache_proc_cleanup();
  144. error_proc:
  145. destroy_workqueue(fscache_op_wq);
  146. error_op_wq:
  147. destroy_workqueue(fscache_object_wq);
  148. error_object_wq:
  149. return ret;
  150. }
  151. fs_initcall(fscache_init);
  152. /*
  153. * clean up on module removal
  154. */
  155. static void __exit fscache_exit(void)
  156. {
  157. _enter("");
  158. kobject_put(fscache_root);
  159. kmem_cache_destroy(fscache_cookie_jar);
  160. #ifdef CONFIG_SYSCTL
  161. unregister_sysctl_table(fscache_sysctl_header);
  162. #endif
  163. fscache_proc_cleanup();
  164. destroy_workqueue(fscache_op_wq);
  165. destroy_workqueue(fscache_object_wq);
  166. pr_notice("Unloaded\n");
  167. }
  168. module_exit(fscache_exit);
  169. /*
  170. * wait_on_atomic_t() sleep function for uninterruptible waiting
  171. */
  172. int fscache_wait_atomic_t(atomic_t *p)
  173. {
  174. schedule();
  175. return 0;
  176. }