operation.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. ASSERT(fscache_object_is_available(op->object));
  35. ASSERTCMP(atomic_read(&op->usage), >, 0);
  36. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
  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. pr_err("Unexpected op type %lx", 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. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
  62. op->state = FSCACHE_OP_ST_IN_PROGRESS;
  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. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
  81. ASSERTCMP(atomic_read(&op->usage), >, 0);
  82. spin_lock(&object->lock);
  83. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  84. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  85. ASSERT(list_empty(&op->pend_link));
  86. op->state = FSCACHE_OP_ST_PENDING;
  87. if (fscache_object_is_active(object)) {
  88. op->object = object;
  89. object->n_ops++;
  90. object->n_exclusive++; /* reads and writes must wait */
  91. if (object->n_in_progress > 0) {
  92. atomic_inc(&op->usage);
  93. list_add_tail(&op->pend_link, &object->pending_ops);
  94. fscache_stat(&fscache_n_op_pend);
  95. } else if (!list_empty(&object->pending_ops)) {
  96. atomic_inc(&op->usage);
  97. list_add_tail(&op->pend_link, &object->pending_ops);
  98. fscache_stat(&fscache_n_op_pend);
  99. fscache_start_operations(object);
  100. } else {
  101. ASSERTCMP(object->n_in_progress, ==, 0);
  102. fscache_run_op(object, op);
  103. }
  104. /* need to issue a new write op after this */
  105. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  106. ret = 0;
  107. } else if (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
  108. op->object = object;
  109. object->n_ops++;
  110. object->n_exclusive++; /* reads and writes must wait */
  111. atomic_inc(&op->usage);
  112. list_add_tail(&op->pend_link, &object->pending_ops);
  113. fscache_stat(&fscache_n_op_pend);
  114. ret = 0;
  115. } else {
  116. /* If we're in any other state, there must have been an I/O
  117. * error of some nature.
  118. */
  119. ASSERT(test_bit(FSCACHE_IOERROR, &object->cache->flags));
  120. ret = -EIO;
  121. }
  122. spin_unlock(&object->lock);
  123. return ret;
  124. }
  125. /*
  126. * report an unexpected submission
  127. */
  128. static void fscache_report_unexpected_submission(struct fscache_object *object,
  129. struct fscache_operation *op,
  130. const struct fscache_state *ostate)
  131. {
  132. static bool once_only;
  133. struct fscache_operation *p;
  134. unsigned n;
  135. if (once_only)
  136. return;
  137. once_only = true;
  138. kdebug("unexpected submission OP%x [OBJ%x %s]",
  139. op->debug_id, object->debug_id, object->state->name);
  140. kdebug("objstate=%s [%s]", object->state->name, ostate->name);
  141. kdebug("objflags=%lx", object->flags);
  142. kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
  143. kdebug("ops=%u inp=%u exc=%u",
  144. object->n_ops, object->n_in_progress, object->n_exclusive);
  145. if (!list_empty(&object->pending_ops)) {
  146. n = 0;
  147. list_for_each_entry(p, &object->pending_ops, pend_link) {
  148. ASSERTCMP(p->object, ==, object);
  149. kdebug("%p %p", op->processor, op->release);
  150. n++;
  151. }
  152. kdebug("n=%u", n);
  153. }
  154. dump_stack();
  155. }
  156. /*
  157. * submit an operation for an object
  158. * - objects may be submitted only in the following states:
  159. * - during object creation (write ops may be submitted)
  160. * - whilst the object is active
  161. * - after an I/O error incurred in one of the two above states (op rejected)
  162. * - this gets any extra refs it needs on an op
  163. */
  164. int fscache_submit_op(struct fscache_object *object,
  165. struct fscache_operation *op)
  166. {
  167. const struct fscache_state *ostate;
  168. int ret;
  169. _enter("{OBJ%x OP%x},{%u}",
  170. object->debug_id, op->debug_id, atomic_read(&op->usage));
  171. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
  172. ASSERTCMP(atomic_read(&op->usage), >, 0);
  173. spin_lock(&object->lock);
  174. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  175. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  176. ASSERT(list_empty(&op->pend_link));
  177. ostate = object->state;
  178. smp_rmb();
  179. op->state = FSCACHE_OP_ST_PENDING;
  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 (test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
  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 (fscache_object_is_dying(object)) {
  205. fscache_stat(&fscache_n_op_rejected);
  206. op->state = FSCACHE_OP_ST_CANCELLED;
  207. ret = -ENOBUFS;
  208. } else if (!test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  209. fscache_report_unexpected_submission(object, op, ostate);
  210. ASSERT(!fscache_object_is_active(object));
  211. op->state = FSCACHE_OP_ST_CANCELLED;
  212. ret = -ENOBUFS;
  213. } else {
  214. op->state = FSCACHE_OP_ST_CANCELLED;
  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. The caller must hold
  231. * 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. void (*do_cancel)(struct fscache_operation *))
  259. {
  260. struct fscache_object *object = op->object;
  261. int ret;
  262. _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
  263. ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
  264. ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
  265. ASSERTCMP(atomic_read(&op->usage), >, 0);
  266. spin_lock(&object->lock);
  267. ret = -EBUSY;
  268. if (op->state == FSCACHE_OP_ST_PENDING) {
  269. ASSERT(!list_empty(&op->pend_link));
  270. fscache_stat(&fscache_n_op_cancelled);
  271. list_del_init(&op->pend_link);
  272. if (do_cancel)
  273. do_cancel(op);
  274. op->state = FSCACHE_OP_ST_CANCELLED;
  275. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  276. object->n_exclusive--;
  277. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  278. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  279. fscache_put_operation(op);
  280. ret = 0;
  281. }
  282. spin_unlock(&object->lock);
  283. _leave(" = %d", ret);
  284. return ret;
  285. }
  286. /*
  287. * Cancel all pending operations on an object
  288. */
  289. void fscache_cancel_all_ops(struct fscache_object *object)
  290. {
  291. struct fscache_operation *op;
  292. _enter("OBJ%x", object->debug_id);
  293. spin_lock(&object->lock);
  294. while (!list_empty(&object->pending_ops)) {
  295. op = list_entry(object->pending_ops.next,
  296. struct fscache_operation, pend_link);
  297. fscache_stat(&fscache_n_op_cancelled);
  298. list_del_init(&op->pend_link);
  299. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
  300. op->state = FSCACHE_OP_ST_CANCELLED;
  301. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  302. object->n_exclusive--;
  303. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  304. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  305. fscache_put_operation(op);
  306. cond_resched_lock(&object->lock);
  307. }
  308. spin_unlock(&object->lock);
  309. _leave("");
  310. }
  311. /*
  312. * Record the completion or cancellation of an in-progress operation.
  313. */
  314. void fscache_op_complete(struct fscache_operation *op, bool cancelled)
  315. {
  316. struct fscache_object *object = op->object;
  317. _enter("OBJ%x", object->debug_id);
  318. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
  319. ASSERTCMP(object->n_in_progress, >, 0);
  320. ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
  321. object->n_exclusive, >, 0);
  322. ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
  323. object->n_in_progress, ==, 1);
  324. spin_lock(&object->lock);
  325. op->state = cancelled ?
  326. FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE;
  327. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  328. object->n_exclusive--;
  329. object->n_in_progress--;
  330. if (object->n_in_progress == 0)
  331. fscache_start_operations(object);
  332. spin_unlock(&object->lock);
  333. _leave("");
  334. }
  335. EXPORT_SYMBOL(fscache_op_complete);
  336. /*
  337. * release an operation
  338. * - queues pending ops if this is the last in-progress op
  339. */
  340. void fscache_put_operation(struct fscache_operation *op)
  341. {
  342. struct fscache_object *object;
  343. struct fscache_cache *cache;
  344. _enter("{OBJ%x OP%x,%d}",
  345. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  346. ASSERTCMP(atomic_read(&op->usage), >, 0);
  347. if (!atomic_dec_and_test(&op->usage))
  348. return;
  349. _debug("PUT OP");
  350. ASSERTIFCMP(op->state != FSCACHE_OP_ST_COMPLETE,
  351. op->state, ==, FSCACHE_OP_ST_CANCELLED);
  352. op->state = FSCACHE_OP_ST_DEAD;
  353. fscache_stat(&fscache_n_op_release);
  354. if (op->release) {
  355. op->release(op);
  356. op->release = NULL;
  357. }
  358. object = op->object;
  359. if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
  360. atomic_dec(&object->n_reads);
  361. if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags))
  362. fscache_unuse_cookie(object);
  363. /* now... we may get called with the object spinlock held, so we
  364. * complete the cleanup here only if we can immediately acquire the
  365. * lock, and defer it otherwise */
  366. if (!spin_trylock(&object->lock)) {
  367. _debug("defer put");
  368. fscache_stat(&fscache_n_op_deferred_release);
  369. cache = object->cache;
  370. spin_lock(&cache->op_gc_list_lock);
  371. list_add_tail(&op->pend_link, &cache->op_gc_list);
  372. spin_unlock(&cache->op_gc_list_lock);
  373. schedule_work(&cache->op_gc);
  374. _leave(" [defer]");
  375. return;
  376. }
  377. ASSERTCMP(object->n_ops, >, 0);
  378. object->n_ops--;
  379. if (object->n_ops == 0)
  380. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  381. spin_unlock(&object->lock);
  382. kfree(op);
  383. _leave(" [done]");
  384. }
  385. EXPORT_SYMBOL(fscache_put_operation);
  386. /*
  387. * garbage collect operations that have had their release deferred
  388. */
  389. void fscache_operation_gc(struct work_struct *work)
  390. {
  391. struct fscache_operation *op;
  392. struct fscache_object *object;
  393. struct fscache_cache *cache =
  394. container_of(work, struct fscache_cache, op_gc);
  395. int count = 0;
  396. _enter("");
  397. do {
  398. spin_lock(&cache->op_gc_list_lock);
  399. if (list_empty(&cache->op_gc_list)) {
  400. spin_unlock(&cache->op_gc_list_lock);
  401. break;
  402. }
  403. op = list_entry(cache->op_gc_list.next,
  404. struct fscache_operation, pend_link);
  405. list_del(&op->pend_link);
  406. spin_unlock(&cache->op_gc_list_lock);
  407. object = op->object;
  408. spin_lock(&object->lock);
  409. _debug("GC DEFERRED REL OBJ%x OP%x",
  410. object->debug_id, op->debug_id);
  411. fscache_stat(&fscache_n_op_gc);
  412. ASSERTCMP(atomic_read(&op->usage), ==, 0);
  413. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
  414. ASSERTCMP(object->n_ops, >, 0);
  415. object->n_ops--;
  416. if (object->n_ops == 0)
  417. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  418. spin_unlock(&object->lock);
  419. kfree(op);
  420. } while (count++ < 20);
  421. if (!list_empty(&cache->op_gc_list))
  422. schedule_work(&cache->op_gc);
  423. _leave("");
  424. }
  425. /*
  426. * execute an operation using fs_op_wq to provide processing context -
  427. * the caller holds a ref to this object, so we don't need to hold one
  428. */
  429. void fscache_op_work_func(struct work_struct *work)
  430. {
  431. struct fscache_operation *op =
  432. container_of(work, struct fscache_operation, work);
  433. unsigned long start;
  434. _enter("{OBJ%x OP%x,%d}",
  435. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  436. ASSERT(op->processor != NULL);
  437. start = jiffies;
  438. op->processor(op);
  439. fscache_hist(fscache_ops_histogram, start);
  440. fscache_put_operation(op);
  441. _leave("");
  442. }