operation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* FS-Cache worker operation management routines
  2. *
  3. * Copyright (C) 2008 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. * See Documentation/filesystems/caching/operations.txt
  12. */
  13. #define FSCACHE_DEBUG_LEVEL OPERATION
  14. #include <linux/module.h>
  15. #include "internal.h"
  16. atomic_t fscache_op_debug_id;
  17. EXPORT_SYMBOL(fscache_op_debug_id);
  18. /**
  19. * fscache_enqueue_operation - Enqueue an operation for processing
  20. * @op: The operation to enqueue
  21. *
  22. * Enqueue an operation for processing by the FS-Cache thread pool.
  23. *
  24. * This will get its own ref on the object.
  25. */
  26. void fscache_enqueue_operation(struct fscache_operation *op)
  27. {
  28. _enter("{OBJ%x OP%x,%u}",
  29. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  30. ASSERT(op->processor != NULL);
  31. ASSERTCMP(op->object->state, >=, FSCACHE_OBJECT_AVAILABLE);
  32. ASSERTCMP(atomic_read(&op->usage), >, 0);
  33. if (list_empty(&op->pend_link)) {
  34. switch (op->flags & FSCACHE_OP_TYPE) {
  35. case FSCACHE_OP_FAST:
  36. _debug("queue fast");
  37. atomic_inc(&op->usage);
  38. if (!schedule_work(&op->fast_work))
  39. fscache_put_operation(op);
  40. break;
  41. case FSCACHE_OP_SLOW:
  42. _debug("queue slow");
  43. slow_work_enqueue(&op->slow_work);
  44. break;
  45. case FSCACHE_OP_MYTHREAD:
  46. _debug("queue for caller's attention");
  47. break;
  48. default:
  49. printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
  50. op->flags);
  51. BUG();
  52. break;
  53. }
  54. fscache_stat(&fscache_n_op_enqueue);
  55. }
  56. }
  57. EXPORT_SYMBOL(fscache_enqueue_operation);
  58. /*
  59. * start an op running
  60. */
  61. static void fscache_run_op(struct fscache_object *object,
  62. struct fscache_operation *op)
  63. {
  64. object->n_in_progress++;
  65. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  66. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  67. if (op->processor)
  68. fscache_enqueue_operation(op);
  69. fscache_stat(&fscache_n_op_run);
  70. }
  71. /*
  72. * submit an exclusive operation for an object
  73. * - other ops are excluded from running simultaneously with this one
  74. * - this gets any extra refs it needs on an op
  75. */
  76. int fscache_submit_exclusive_op(struct fscache_object *object,
  77. struct fscache_operation *op)
  78. {
  79. int ret;
  80. _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
  81. spin_lock(&object->lock);
  82. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  83. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  84. ret = -ENOBUFS;
  85. if (fscache_object_is_active(object)) {
  86. op->object = object;
  87. object->n_ops++;
  88. object->n_exclusive++; /* reads and writes must wait */
  89. if (object->n_ops > 0) {
  90. atomic_inc(&op->usage);
  91. list_add_tail(&op->pend_link, &object->pending_ops);
  92. fscache_stat(&fscache_n_op_pend);
  93. } else if (!list_empty(&object->pending_ops)) {
  94. atomic_inc(&op->usage);
  95. list_add_tail(&op->pend_link, &object->pending_ops);
  96. fscache_stat(&fscache_n_op_pend);
  97. fscache_start_operations(object);
  98. } else {
  99. ASSERTCMP(object->n_in_progress, ==, 0);
  100. fscache_run_op(object, op);
  101. }
  102. /* need to issue a new write op after this */
  103. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  104. ret = 0;
  105. } else if (object->state == FSCACHE_OBJECT_CREATING) {
  106. op->object = object;
  107. object->n_ops++;
  108. object->n_exclusive++; /* reads and writes must wait */
  109. atomic_inc(&op->usage);
  110. list_add_tail(&op->pend_link, &object->pending_ops);
  111. fscache_stat(&fscache_n_op_pend);
  112. ret = 0;
  113. } else {
  114. /* not allowed to submit ops in any other state */
  115. BUG();
  116. }
  117. spin_unlock(&object->lock);
  118. return ret;
  119. }
  120. /*
  121. * report an unexpected submission
  122. */
  123. static void fscache_report_unexpected_submission(struct fscache_object *object,
  124. struct fscache_operation *op,
  125. unsigned long ostate)
  126. {
  127. static bool once_only;
  128. struct fscache_operation *p;
  129. unsigned n;
  130. if (once_only)
  131. return;
  132. once_only = true;
  133. kdebug("unexpected submission OP%x [OBJ%x %s]",
  134. op->debug_id, object->debug_id,
  135. fscache_object_states[object->state]);
  136. kdebug("objstate=%s [%s]",
  137. fscache_object_states[object->state],
  138. fscache_object_states[ostate]);
  139. kdebug("objflags=%lx", object->flags);
  140. kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
  141. kdebug("ops=%u inp=%u exc=%u",
  142. object->n_ops, object->n_in_progress, object->n_exclusive);
  143. if (!list_empty(&object->pending_ops)) {
  144. n = 0;
  145. list_for_each_entry(p, &object->pending_ops, pend_link) {
  146. ASSERTCMP(p->object, ==, object);
  147. kdebug("%p %p", op->processor, op->release);
  148. n++;
  149. }
  150. kdebug("n=%u", n);
  151. }
  152. dump_stack();
  153. }
  154. /*
  155. * submit an operation for an object
  156. * - objects may be submitted only in the following states:
  157. * - during object creation (write ops may be submitted)
  158. * - whilst the object is active
  159. * - after an I/O error incurred in one of the two above states (op rejected)
  160. * - this gets any extra refs it needs on an op
  161. */
  162. int fscache_submit_op(struct fscache_object *object,
  163. struct fscache_operation *op)
  164. {
  165. unsigned long ostate;
  166. int ret;
  167. _enter("{OBJ%x OP%x},{%u}",
  168. object->debug_id, op->debug_id, atomic_read(&op->usage));
  169. ASSERTCMP(atomic_read(&op->usage), >, 0);
  170. spin_lock(&object->lock);
  171. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  172. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  173. ostate = object->state;
  174. smp_rmb();
  175. if (fscache_object_is_active(object)) {
  176. op->object = object;
  177. object->n_ops++;
  178. if (object->n_exclusive > 0) {
  179. atomic_inc(&op->usage);
  180. list_add_tail(&op->pend_link, &object->pending_ops);
  181. fscache_stat(&fscache_n_op_pend);
  182. } else if (!list_empty(&object->pending_ops)) {
  183. atomic_inc(&op->usage);
  184. list_add_tail(&op->pend_link, &object->pending_ops);
  185. fscache_stat(&fscache_n_op_pend);
  186. fscache_start_operations(object);
  187. } else {
  188. ASSERTCMP(object->n_exclusive, ==, 0);
  189. fscache_run_op(object, op);
  190. }
  191. ret = 0;
  192. } else if (object->state == FSCACHE_OBJECT_CREATING) {
  193. op->object = object;
  194. object->n_ops++;
  195. atomic_inc(&op->usage);
  196. list_add_tail(&op->pend_link, &object->pending_ops);
  197. fscache_stat(&fscache_n_op_pend);
  198. ret = 0;
  199. } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  200. fscache_report_unexpected_submission(object, op, ostate);
  201. ASSERT(!fscache_object_is_active(object));
  202. ret = -ENOBUFS;
  203. } else {
  204. ret = -ENOBUFS;
  205. }
  206. spin_unlock(&object->lock);
  207. return ret;
  208. }
  209. /*
  210. * queue an object for withdrawal on error, aborting all following asynchronous
  211. * operations
  212. */
  213. void fscache_abort_object(struct fscache_object *object)
  214. {
  215. _enter("{OBJ%x}", object->debug_id);
  216. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  217. }
  218. /*
  219. * jump start the operation processing on an object
  220. * - caller must hold object->lock
  221. */
  222. void fscache_start_operations(struct fscache_object *object)
  223. {
  224. struct fscache_operation *op;
  225. bool stop = false;
  226. while (!list_empty(&object->pending_ops) && !stop) {
  227. op = list_entry(object->pending_ops.next,
  228. struct fscache_operation, pend_link);
  229. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  230. if (object->n_in_progress > 0)
  231. break;
  232. stop = true;
  233. }
  234. list_del_init(&op->pend_link);
  235. object->n_in_progress++;
  236. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  237. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  238. if (op->processor)
  239. fscache_enqueue_operation(op);
  240. /* the pending queue was holding a ref on the object */
  241. fscache_put_operation(op);
  242. }
  243. ASSERTCMP(object->n_in_progress, <=, object->n_ops);
  244. _debug("woke %d ops on OBJ%x",
  245. object->n_in_progress, object->debug_id);
  246. }
  247. /*
  248. * release an operation
  249. * - queues pending ops if this is the last in-progress op
  250. */
  251. void fscache_put_operation(struct fscache_operation *op)
  252. {
  253. struct fscache_object *object;
  254. struct fscache_cache *cache;
  255. _enter("{OBJ%x OP%x,%d}",
  256. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  257. ASSERTCMP(atomic_read(&op->usage), >, 0);
  258. if (!atomic_dec_and_test(&op->usage))
  259. return;
  260. _debug("PUT OP");
  261. if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
  262. BUG();
  263. fscache_stat(&fscache_n_op_release);
  264. if (op->release) {
  265. op->release(op);
  266. op->release = NULL;
  267. }
  268. object = op->object;
  269. /* now... we may get called with the object spinlock held, so we
  270. * complete the cleanup here only if we can immediately acquire the
  271. * lock, and defer it otherwise */
  272. if (!spin_trylock(&object->lock)) {
  273. _debug("defer put");
  274. fscache_stat(&fscache_n_op_deferred_release);
  275. cache = object->cache;
  276. spin_lock(&cache->op_gc_list_lock);
  277. list_add_tail(&op->pend_link, &cache->op_gc_list);
  278. spin_unlock(&cache->op_gc_list_lock);
  279. schedule_work(&cache->op_gc);
  280. _leave(" [defer]");
  281. return;
  282. }
  283. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  284. ASSERTCMP(object->n_exclusive, >, 0);
  285. object->n_exclusive--;
  286. }
  287. ASSERTCMP(object->n_in_progress, >, 0);
  288. object->n_in_progress--;
  289. if (object->n_in_progress == 0)
  290. fscache_start_operations(object);
  291. ASSERTCMP(object->n_ops, >, 0);
  292. object->n_ops--;
  293. if (object->n_ops == 0)
  294. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  295. spin_unlock(&object->lock);
  296. kfree(op);
  297. _leave(" [done]");
  298. }
  299. EXPORT_SYMBOL(fscache_put_operation);
  300. /*
  301. * garbage collect operations that have had their release deferred
  302. */
  303. void fscache_operation_gc(struct work_struct *work)
  304. {
  305. struct fscache_operation *op;
  306. struct fscache_object *object;
  307. struct fscache_cache *cache =
  308. container_of(work, struct fscache_cache, op_gc);
  309. int count = 0;
  310. _enter("");
  311. do {
  312. spin_lock(&cache->op_gc_list_lock);
  313. if (list_empty(&cache->op_gc_list)) {
  314. spin_unlock(&cache->op_gc_list_lock);
  315. break;
  316. }
  317. op = list_entry(cache->op_gc_list.next,
  318. struct fscache_operation, pend_link);
  319. list_del(&op->pend_link);
  320. spin_unlock(&cache->op_gc_list_lock);
  321. object = op->object;
  322. _debug("GC DEFERRED REL OBJ%x OP%x",
  323. object->debug_id, op->debug_id);
  324. fscache_stat(&fscache_n_op_gc);
  325. ASSERTCMP(atomic_read(&op->usage), ==, 0);
  326. spin_lock(&object->lock);
  327. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  328. ASSERTCMP(object->n_exclusive, >, 0);
  329. object->n_exclusive--;
  330. }
  331. ASSERTCMP(object->n_in_progress, >, 0);
  332. object->n_in_progress--;
  333. if (object->n_in_progress == 0)
  334. fscache_start_operations(object);
  335. ASSERTCMP(object->n_ops, >, 0);
  336. object->n_ops--;
  337. if (object->n_ops == 0)
  338. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  339. spin_unlock(&object->lock);
  340. } while (count++ < 20);
  341. if (!list_empty(&cache->op_gc_list))
  342. schedule_work(&cache->op_gc);
  343. _leave("");
  344. }
  345. /*
  346. * allow the slow work item processor to get a ref on an operation
  347. */
  348. static int fscache_op_get_ref(struct slow_work *work)
  349. {
  350. struct fscache_operation *op =
  351. container_of(work, struct fscache_operation, slow_work);
  352. atomic_inc(&op->usage);
  353. return 0;
  354. }
  355. /*
  356. * allow the slow work item processor to discard a ref on an operation
  357. */
  358. static void fscache_op_put_ref(struct slow_work *work)
  359. {
  360. struct fscache_operation *op =
  361. container_of(work, struct fscache_operation, slow_work);
  362. fscache_put_operation(op);
  363. }
  364. /*
  365. * execute an operation using the slow thread pool to provide processing context
  366. * - the caller holds a ref to this object, so we don't need to hold one
  367. */
  368. static void fscache_op_execute(struct slow_work *work)
  369. {
  370. struct fscache_operation *op =
  371. container_of(work, struct fscache_operation, slow_work);
  372. unsigned long start;
  373. _enter("{OBJ%x OP%x,%d}",
  374. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  375. ASSERT(op->processor != NULL);
  376. start = jiffies;
  377. op->processor(op);
  378. fscache_hist(fscache_ops_histogram, start);
  379. _leave("");
  380. }
  381. const struct slow_work_ops fscache_op_slow_work_ops = {
  382. .get_ref = fscache_op_get_ref,
  383. .put_ref = fscache_op_put_ref,
  384. .execute = fscache_op_execute,
  385. };