operation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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(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_FAST:
  39. _debug("queue fast");
  40. atomic_inc(&op->usage);
  41. if (!schedule_work(&op->fast_work))
  42. fscache_put_operation(op);
  43. break;
  44. case FSCACHE_OP_SLOW:
  45. _debug("queue slow");
  46. slow_work_enqueue(&op->slow_work);
  47. break;
  48. case FSCACHE_OP_MYTHREAD:
  49. _debug("queue for caller's attention");
  50. break;
  51. default:
  52. printk(KERN_ERR "FS-Cache: Unexpected op type %lx",
  53. op->flags);
  54. BUG();
  55. break;
  56. }
  57. }
  58. EXPORT_SYMBOL(fscache_enqueue_operation);
  59. /*
  60. * start an op running
  61. */
  62. static void fscache_run_op(struct fscache_object *object,
  63. struct fscache_operation *op)
  64. {
  65. fscache_set_op_state(op, "Run");
  66. object->n_in_progress++;
  67. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  68. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  69. if (op->processor)
  70. fscache_enqueue_operation(op);
  71. fscache_stat(&fscache_n_op_run);
  72. }
  73. /*
  74. * submit an exclusive operation for an object
  75. * - other ops are excluded from running simultaneously with this one
  76. * - this gets any extra refs it needs on an op
  77. */
  78. int fscache_submit_exclusive_op(struct fscache_object *object,
  79. struct fscache_operation *op)
  80. {
  81. int ret;
  82. _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
  83. fscache_set_op_state(op, "SubmitX");
  84. spin_lock(&object->lock);
  85. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  86. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  87. ASSERT(list_empty(&op->pend_link));
  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. ASSERT(list_empty(&op->pend_link));
  179. ostate = object->state;
  180. smp_rmb();
  181. if (fscache_object_is_active(object)) {
  182. op->object = object;
  183. object->n_ops++;
  184. if (object->n_exclusive > 0) {
  185. atomic_inc(&op->usage);
  186. list_add_tail(&op->pend_link, &object->pending_ops);
  187. fscache_stat(&fscache_n_op_pend);
  188. } else if (!list_empty(&object->pending_ops)) {
  189. atomic_inc(&op->usage);
  190. list_add_tail(&op->pend_link, &object->pending_ops);
  191. fscache_stat(&fscache_n_op_pend);
  192. fscache_start_operations(object);
  193. } else {
  194. ASSERTCMP(object->n_exclusive, ==, 0);
  195. fscache_run_op(object, op);
  196. }
  197. ret = 0;
  198. } else if (object->state == FSCACHE_OBJECT_CREATING) {
  199. op->object = object;
  200. object->n_ops++;
  201. atomic_inc(&op->usage);
  202. list_add_tail(&op->pend_link, &object->pending_ops);
  203. fscache_stat(&fscache_n_op_pend);
  204. ret = 0;
  205. } else if (object->state == FSCACHE_OBJECT_DYING ||
  206. object->state == FSCACHE_OBJECT_LC_DYING ||
  207. object->state == FSCACHE_OBJECT_WITHDRAWING) {
  208. fscache_stat(&fscache_n_op_rejected);
  209. ret = -ENOBUFS;
  210. } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  211. fscache_report_unexpected_submission(object, op, ostate);
  212. ASSERT(!fscache_object_is_active(object));
  213. ret = -ENOBUFS;
  214. } else {
  215. ret = -ENOBUFS;
  216. }
  217. spin_unlock(&object->lock);
  218. return ret;
  219. }
  220. /*
  221. * queue an object for withdrawal on error, aborting all following asynchronous
  222. * operations
  223. */
  224. void fscache_abort_object(struct fscache_object *object)
  225. {
  226. _enter("{OBJ%x}", object->debug_id);
  227. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  228. }
  229. /*
  230. * jump start the operation processing on an object
  231. * - caller must hold object->lock
  232. */
  233. void fscache_start_operations(struct fscache_object *object)
  234. {
  235. struct fscache_operation *op;
  236. bool stop = false;
  237. while (!list_empty(&object->pending_ops) && !stop) {
  238. op = list_entry(object->pending_ops.next,
  239. struct fscache_operation, pend_link);
  240. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  241. if (object->n_in_progress > 0)
  242. break;
  243. stop = true;
  244. }
  245. list_del_init(&op->pend_link);
  246. fscache_run_op(object, op);
  247. /* the pending queue was holding a ref on the object */
  248. fscache_put_operation(op);
  249. }
  250. ASSERTCMP(object->n_in_progress, <=, object->n_ops);
  251. _debug("woke %d ops on OBJ%x",
  252. object->n_in_progress, object->debug_id);
  253. }
  254. /*
  255. * cancel an operation that's pending on an object
  256. */
  257. int fscache_cancel_op(struct fscache_operation *op)
  258. {
  259. struct fscache_object *object = op->object;
  260. int ret;
  261. _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
  262. spin_lock(&object->lock);
  263. ret = -EBUSY;
  264. if (!list_empty(&op->pend_link)) {
  265. fscache_stat(&fscache_n_op_cancelled);
  266. list_del_init(&op->pend_link);
  267. object->n_ops--;
  268. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  269. object->n_exclusive--;
  270. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  271. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  272. fscache_put_operation(op);
  273. ret = 0;
  274. }
  275. spin_unlock(&object->lock);
  276. _leave(" = %d", ret);
  277. return ret;
  278. }
  279. /*
  280. * release an operation
  281. * - queues pending ops if this is the last in-progress op
  282. */
  283. void fscache_put_operation(struct fscache_operation *op)
  284. {
  285. struct fscache_object *object;
  286. struct fscache_cache *cache;
  287. _enter("{OBJ%x OP%x,%d}",
  288. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  289. ASSERTCMP(atomic_read(&op->usage), >, 0);
  290. if (!atomic_dec_and_test(&op->usage))
  291. return;
  292. fscache_set_op_state(op, "Put");
  293. _debug("PUT OP");
  294. if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
  295. BUG();
  296. fscache_stat(&fscache_n_op_release);
  297. if (op->release) {
  298. op->release(op);
  299. op->release = NULL;
  300. }
  301. object = op->object;
  302. if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
  303. atomic_dec(&object->n_reads);
  304. /* now... we may get called with the object spinlock held, so we
  305. * complete the cleanup here only if we can immediately acquire the
  306. * lock, and defer it otherwise */
  307. if (!spin_trylock(&object->lock)) {
  308. _debug("defer put");
  309. fscache_stat(&fscache_n_op_deferred_release);
  310. cache = object->cache;
  311. spin_lock(&cache->op_gc_list_lock);
  312. list_add_tail(&op->pend_link, &cache->op_gc_list);
  313. spin_unlock(&cache->op_gc_list_lock);
  314. schedule_work(&cache->op_gc);
  315. _leave(" [defer]");
  316. return;
  317. }
  318. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  319. ASSERTCMP(object->n_exclusive, >, 0);
  320. object->n_exclusive--;
  321. }
  322. ASSERTCMP(object->n_in_progress, >, 0);
  323. object->n_in_progress--;
  324. if (object->n_in_progress == 0)
  325. fscache_start_operations(object);
  326. ASSERTCMP(object->n_ops, >, 0);
  327. object->n_ops--;
  328. if (object->n_ops == 0)
  329. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  330. spin_unlock(&object->lock);
  331. kfree(op);
  332. _leave(" [done]");
  333. }
  334. EXPORT_SYMBOL(fscache_put_operation);
  335. /*
  336. * garbage collect operations that have had their release deferred
  337. */
  338. void fscache_operation_gc(struct work_struct *work)
  339. {
  340. struct fscache_operation *op;
  341. struct fscache_object *object;
  342. struct fscache_cache *cache =
  343. container_of(work, struct fscache_cache, op_gc);
  344. int count = 0;
  345. _enter("");
  346. do {
  347. spin_lock(&cache->op_gc_list_lock);
  348. if (list_empty(&cache->op_gc_list)) {
  349. spin_unlock(&cache->op_gc_list_lock);
  350. break;
  351. }
  352. op = list_entry(cache->op_gc_list.next,
  353. struct fscache_operation, pend_link);
  354. list_del(&op->pend_link);
  355. spin_unlock(&cache->op_gc_list_lock);
  356. object = op->object;
  357. _debug("GC DEFERRED REL OBJ%x OP%x",
  358. object->debug_id, op->debug_id);
  359. fscache_stat(&fscache_n_op_gc);
  360. ASSERTCMP(atomic_read(&op->usage), ==, 0);
  361. spin_lock(&object->lock);
  362. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  363. ASSERTCMP(object->n_exclusive, >, 0);
  364. object->n_exclusive--;
  365. }
  366. ASSERTCMP(object->n_in_progress, >, 0);
  367. object->n_in_progress--;
  368. if (object->n_in_progress == 0)
  369. fscache_start_operations(object);
  370. ASSERTCMP(object->n_ops, >, 0);
  371. object->n_ops--;
  372. if (object->n_ops == 0)
  373. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  374. spin_unlock(&object->lock);
  375. } while (count++ < 20);
  376. if (!list_empty(&cache->op_gc_list))
  377. schedule_work(&cache->op_gc);
  378. _leave("");
  379. }
  380. /*
  381. * allow the slow work item processor to get a ref on an operation
  382. */
  383. static int fscache_op_get_ref(struct slow_work *work)
  384. {
  385. struct fscache_operation *op =
  386. container_of(work, struct fscache_operation, slow_work);
  387. atomic_inc(&op->usage);
  388. return 0;
  389. }
  390. /*
  391. * allow the slow work item processor to discard a ref on an operation
  392. */
  393. static void fscache_op_put_ref(struct slow_work *work)
  394. {
  395. struct fscache_operation *op =
  396. container_of(work, struct fscache_operation, slow_work);
  397. fscache_put_operation(op);
  398. }
  399. /*
  400. * execute an operation using the slow thread pool to provide processing context
  401. * - the caller holds a ref to this object, so we don't need to hold one
  402. */
  403. static void fscache_op_execute(struct slow_work *work)
  404. {
  405. struct fscache_operation *op =
  406. container_of(work, struct fscache_operation, slow_work);
  407. unsigned long start;
  408. _enter("{OBJ%x OP%x,%d}",
  409. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  410. ASSERT(op->processor != NULL);
  411. start = jiffies;
  412. op->processor(op);
  413. fscache_hist(fscache_ops_histogram, start);
  414. _leave("");
  415. }
  416. /*
  417. * describe an operation for slow-work debugging
  418. */
  419. #ifdef CONFIG_SLOW_WORK_PROC
  420. static void fscache_op_desc(struct slow_work *work, struct seq_file *m)
  421. {
  422. struct fscache_operation *op =
  423. container_of(work, struct fscache_operation, slow_work);
  424. seq_printf(m, "FSC: OBJ%x OP%x: %s/%s fl=%lx",
  425. op->object->debug_id, op->debug_id,
  426. op->name, op->state, op->flags);
  427. }
  428. #endif
  429. const struct slow_work_ops fscache_op_slow_work_ops = {
  430. .owner = THIS_MODULE,
  431. .get_ref = fscache_op_get_ref,
  432. .put_ref = fscache_op_put_ref,
  433. .execute = fscache_op_execute,
  434. #ifdef CONFIG_SLOW_WORK_PROC
  435. .desc = fscache_op_desc,
  436. #endif
  437. };