operation.c 12 KB

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