operation.c 12 KB

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