operation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  206. fscache_report_unexpected_submission(object, op, ostate);
  207. ASSERT(!fscache_object_is_active(object));
  208. ret = -ENOBUFS;
  209. } else {
  210. ret = -ENOBUFS;
  211. }
  212. spin_unlock(&object->lock);
  213. return ret;
  214. }
  215. /*
  216. * queue an object for withdrawal on error, aborting all following asynchronous
  217. * operations
  218. */
  219. void fscache_abort_object(struct fscache_object *object)
  220. {
  221. _enter("{OBJ%x}", object->debug_id);
  222. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  223. }
  224. /*
  225. * jump start the operation processing on an object
  226. * - caller must hold object->lock
  227. */
  228. void fscache_start_operations(struct fscache_object *object)
  229. {
  230. struct fscache_operation *op;
  231. bool stop = false;
  232. while (!list_empty(&object->pending_ops) && !stop) {
  233. op = list_entry(object->pending_ops.next,
  234. struct fscache_operation, pend_link);
  235. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  236. if (object->n_in_progress > 0)
  237. break;
  238. stop = true;
  239. }
  240. list_del_init(&op->pend_link);
  241. fscache_run_op(object, op);
  242. /* the pending queue was holding a ref on the object */
  243. fscache_put_operation(op);
  244. }
  245. ASSERTCMP(object->n_in_progress, <=, object->n_ops);
  246. _debug("woke %d ops on OBJ%x",
  247. object->n_in_progress, object->debug_id);
  248. }
  249. /*
  250. * cancel an operation that's pending on an object
  251. */
  252. int fscache_cancel_op(struct fscache_operation *op)
  253. {
  254. struct fscache_object *object = op->object;
  255. int ret;
  256. _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
  257. spin_lock(&object->lock);
  258. ret = -EBUSY;
  259. if (!list_empty(&op->pend_link)) {
  260. fscache_stat(&fscache_n_op_cancelled);
  261. list_del_init(&op->pend_link);
  262. object->n_ops--;
  263. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  264. object->n_exclusive--;
  265. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  266. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  267. fscache_put_operation(op);
  268. ret = 0;
  269. }
  270. spin_unlock(&object->lock);
  271. _leave(" = %d", ret);
  272. return ret;
  273. }
  274. /*
  275. * release an operation
  276. * - queues pending ops if this is the last in-progress op
  277. */
  278. void fscache_put_operation(struct fscache_operation *op)
  279. {
  280. struct fscache_object *object;
  281. struct fscache_cache *cache;
  282. _enter("{OBJ%x OP%x,%d}",
  283. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  284. ASSERTCMP(atomic_read(&op->usage), >, 0);
  285. if (!atomic_dec_and_test(&op->usage))
  286. return;
  287. fscache_set_op_state(op, "Put");
  288. _debug("PUT OP");
  289. if (test_and_set_bit(FSCACHE_OP_DEAD, &op->flags))
  290. BUG();
  291. fscache_stat(&fscache_n_op_release);
  292. if (op->release) {
  293. op->release(op);
  294. op->release = NULL;
  295. }
  296. object = op->object;
  297. if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
  298. atomic_dec(&object->n_reads);
  299. /* now... we may get called with the object spinlock held, so we
  300. * complete the cleanup here only if we can immediately acquire the
  301. * lock, and defer it otherwise */
  302. if (!spin_trylock(&object->lock)) {
  303. _debug("defer put");
  304. fscache_stat(&fscache_n_op_deferred_release);
  305. cache = object->cache;
  306. spin_lock(&cache->op_gc_list_lock);
  307. list_add_tail(&op->pend_link, &cache->op_gc_list);
  308. spin_unlock(&cache->op_gc_list_lock);
  309. schedule_work(&cache->op_gc);
  310. _leave(" [defer]");
  311. return;
  312. }
  313. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  314. ASSERTCMP(object->n_exclusive, >, 0);
  315. object->n_exclusive--;
  316. }
  317. ASSERTCMP(object->n_in_progress, >, 0);
  318. object->n_in_progress--;
  319. if (object->n_in_progress == 0)
  320. fscache_start_operations(object);
  321. ASSERTCMP(object->n_ops, >, 0);
  322. object->n_ops--;
  323. if (object->n_ops == 0)
  324. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  325. spin_unlock(&object->lock);
  326. kfree(op);
  327. _leave(" [done]");
  328. }
  329. EXPORT_SYMBOL(fscache_put_operation);
  330. /*
  331. * garbage collect operations that have had their release deferred
  332. */
  333. void fscache_operation_gc(struct work_struct *work)
  334. {
  335. struct fscache_operation *op;
  336. struct fscache_object *object;
  337. struct fscache_cache *cache =
  338. container_of(work, struct fscache_cache, op_gc);
  339. int count = 0;
  340. _enter("");
  341. do {
  342. spin_lock(&cache->op_gc_list_lock);
  343. if (list_empty(&cache->op_gc_list)) {
  344. spin_unlock(&cache->op_gc_list_lock);
  345. break;
  346. }
  347. op = list_entry(cache->op_gc_list.next,
  348. struct fscache_operation, pend_link);
  349. list_del(&op->pend_link);
  350. spin_unlock(&cache->op_gc_list_lock);
  351. object = op->object;
  352. _debug("GC DEFERRED REL OBJ%x OP%x",
  353. object->debug_id, op->debug_id);
  354. fscache_stat(&fscache_n_op_gc);
  355. ASSERTCMP(atomic_read(&op->usage), ==, 0);
  356. spin_lock(&object->lock);
  357. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  358. ASSERTCMP(object->n_exclusive, >, 0);
  359. object->n_exclusive--;
  360. }
  361. ASSERTCMP(object->n_in_progress, >, 0);
  362. object->n_in_progress--;
  363. if (object->n_in_progress == 0)
  364. fscache_start_operations(object);
  365. ASSERTCMP(object->n_ops, >, 0);
  366. object->n_ops--;
  367. if (object->n_ops == 0)
  368. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  369. spin_unlock(&object->lock);
  370. } while (count++ < 20);
  371. if (!list_empty(&cache->op_gc_list))
  372. schedule_work(&cache->op_gc);
  373. _leave("");
  374. }
  375. /*
  376. * allow the slow work item processor to get a ref on an operation
  377. */
  378. static int fscache_op_get_ref(struct slow_work *work)
  379. {
  380. struct fscache_operation *op =
  381. container_of(work, struct fscache_operation, slow_work);
  382. atomic_inc(&op->usage);
  383. return 0;
  384. }
  385. /*
  386. * allow the slow work item processor to discard a ref on an operation
  387. */
  388. static void fscache_op_put_ref(struct slow_work *work)
  389. {
  390. struct fscache_operation *op =
  391. container_of(work, struct fscache_operation, slow_work);
  392. fscache_put_operation(op);
  393. }
  394. /*
  395. * execute an operation using the slow thread pool to provide processing context
  396. * - the caller holds a ref to this object, so we don't need to hold one
  397. */
  398. static void fscache_op_execute(struct slow_work *work)
  399. {
  400. struct fscache_operation *op =
  401. container_of(work, struct fscache_operation, slow_work);
  402. unsigned long start;
  403. _enter("{OBJ%x OP%x,%d}",
  404. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  405. ASSERT(op->processor != NULL);
  406. start = jiffies;
  407. op->processor(op);
  408. fscache_hist(fscache_ops_histogram, start);
  409. _leave("");
  410. }
  411. /*
  412. * describe an operation for slow-work debugging
  413. */
  414. #ifdef CONFIG_SLOW_WORK_PROC
  415. static void fscache_op_desc(struct slow_work *work, struct seq_file *m)
  416. {
  417. struct fscache_operation *op =
  418. container_of(work, struct fscache_operation, slow_work);
  419. seq_printf(m, "FSC: OBJ%x OP%x: %s/%s fl=%lx",
  420. op->object->debug_id, op->debug_id,
  421. op->name, op->state, op->flags);
  422. }
  423. #endif
  424. const struct slow_work_ops fscache_op_slow_work_ops = {
  425. .owner = THIS_MODULE,
  426. .get_ref = fscache_op_get_ref,
  427. .put_ref = fscache_op_put_ref,
  428. .execute = fscache_op_execute,
  429. #ifdef CONFIG_SLOW_WORK_PROC
  430. .desc = fscache_op_desc,
  431. #endif
  432. };