operation.c 12 KB

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