operation.c 14 KB

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