main.c 4.8 KB

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