main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. DEFINE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
  40. /* these values serve as lower bounds, will be adjusted in fscache_init() */
  41. static unsigned fscache_object_max_active = 4;
  42. #ifdef CONFIG_SYSCTL
  43. static struct ctl_table_header *fscache_sysctl_header;
  44. static int fscache_max_active_sysctl(struct ctl_table *table, int write,
  45. void __user *buffer,
  46. size_t *lenp, loff_t *ppos)
  47. {
  48. struct workqueue_struct **wqp = table->extra1;
  49. unsigned int *datap = table->data;
  50. int ret;
  51. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  52. if (ret == 0)
  53. workqueue_set_max_active(*wqp, *datap);
  54. return ret;
  55. }
  56. ctl_table fscache_sysctls[] = {
  57. {
  58. .procname = "object_max_active",
  59. .data = &fscache_object_max_active,
  60. .maxlen = sizeof(unsigned),
  61. .mode = 0644,
  62. .proc_handler = fscache_max_active_sysctl,
  63. .extra1 = &fscache_object_wq,
  64. },
  65. {}
  66. };
  67. ctl_table fscache_sysctls_root[] = {
  68. {
  69. .procname = "fscache",
  70. .mode = 0555,
  71. .child = fscache_sysctls,
  72. },
  73. {}
  74. };
  75. #endif
  76. /*
  77. * initialise the fs caching module
  78. */
  79. static int __init fscache_init(void)
  80. {
  81. unsigned int nr_cpus = num_possible_cpus();
  82. unsigned int cpu;
  83. int ret;
  84. ret = slow_work_register_user(THIS_MODULE);
  85. if (ret < 0)
  86. goto error_slow_work;
  87. fscache_object_max_active =
  88. clamp_val(nr_cpus,
  89. fscache_object_max_active, WQ_UNBOUND_MAX_ACTIVE);
  90. ret = -ENOMEM;
  91. fscache_object_wq = alloc_workqueue("fscache_object", WQ_UNBOUND,
  92. fscache_object_max_active);
  93. if (!fscache_object_wq)
  94. goto error_object_wq;
  95. for_each_possible_cpu(cpu)
  96. init_waitqueue_head(&per_cpu(fscache_object_cong_wait, cpu));
  97. ret = fscache_proc_init();
  98. if (ret < 0)
  99. goto error_proc;
  100. #ifdef CONFIG_SYSCTL
  101. ret = -ENOMEM;
  102. fscache_sysctl_header = register_sysctl_table(fscache_sysctls_root);
  103. if (!fscache_sysctl_header)
  104. goto error_sysctl;
  105. #endif
  106. fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
  107. sizeof(struct fscache_cookie),
  108. 0,
  109. 0,
  110. fscache_cookie_init_once);
  111. if (!fscache_cookie_jar) {
  112. printk(KERN_NOTICE
  113. "FS-Cache: Failed to allocate a cookie jar\n");
  114. ret = -ENOMEM;
  115. goto error_cookie_jar;
  116. }
  117. fscache_root = kobject_create_and_add("fscache", kernel_kobj);
  118. if (!fscache_root)
  119. goto error_kobj;
  120. printk(KERN_NOTICE "FS-Cache: Loaded\n");
  121. return 0;
  122. error_kobj:
  123. kmem_cache_destroy(fscache_cookie_jar);
  124. error_cookie_jar:
  125. #ifdef CONFIG_SYSCTL
  126. unregister_sysctl_table(fscache_sysctl_header);
  127. error_sysctl:
  128. #endif
  129. fscache_proc_cleanup();
  130. error_proc:
  131. destroy_workqueue(fscache_object_wq);
  132. error_object_wq:
  133. slow_work_unregister_user(THIS_MODULE);
  134. error_slow_work:
  135. return ret;
  136. }
  137. fs_initcall(fscache_init);
  138. /*
  139. * clean up on module removal
  140. */
  141. static void __exit fscache_exit(void)
  142. {
  143. _enter("");
  144. kobject_put(fscache_root);
  145. kmem_cache_destroy(fscache_cookie_jar);
  146. unregister_sysctl_table(fscache_sysctl_header);
  147. fscache_proc_cleanup();
  148. destroy_workqueue(fscache_object_wq);
  149. slow_work_unregister_user(THIS_MODULE);
  150. printk(KERN_NOTICE "FS-Cache: Unloaded\n");
  151. }
  152. module_exit(fscache_exit);
  153. /*
  154. * wait_on_bit() sleep function for uninterruptible waiting
  155. */
  156. int fscache_wait_bit(void *flags)
  157. {
  158. schedule();
  159. return 0;
  160. }
  161. EXPORT_SYMBOL(fscache_wait_bit);
  162. /*
  163. * wait_on_bit() sleep function for interruptible waiting
  164. */
  165. int fscache_wait_bit_interruptible(void *flags)
  166. {
  167. schedule();
  168. return signal_pending(current);
  169. }
  170. EXPORT_SYMBOL(fscache_wait_bit_interruptible);