operation.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. static void fscache_operation_dummy_cancel(struct fscache_operation *op)
  21. {
  22. }
  23. /**
  24. * fscache_operation_init - Do basic initialisation of an operation
  25. * @op: The operation to initialise
  26. * @release: The release function to assign
  27. *
  28. * Do basic initialisation of an operation. The caller must still set flags,
  29. * object and processor if needed.
  30. */
  31. void fscache_operation_init(struct fscache_operation *op,
  32. fscache_operation_processor_t processor,
  33. fscache_operation_cancel_t cancel,
  34. fscache_operation_release_t release)
  35. {
  36. INIT_WORK(&op->work, fscache_op_work_func);
  37. atomic_set(&op->usage, 1);
  38. op->state = FSCACHE_OP_ST_INITIALISED;
  39. op->debug_id = atomic_inc_return(&fscache_op_debug_id);
  40. op->processor = processor;
  41. op->cancel = cancel ?: fscache_operation_dummy_cancel;
  42. op->release = release;
  43. INIT_LIST_HEAD(&op->pend_link);
  44. fscache_stat(&fscache_n_op_initialised);
  45. }
  46. EXPORT_SYMBOL(fscache_operation_init);
  47. /**
  48. * fscache_enqueue_operation - Enqueue an operation for processing
  49. * @op: The operation to enqueue
  50. *
  51. * Enqueue an operation for processing by the FS-Cache thread pool.
  52. *
  53. * This will get its own ref on the object.
  54. */
  55. void fscache_enqueue_operation(struct fscache_operation *op)
  56. {
  57. _enter("{OBJ%x OP%x,%u}",
  58. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  59. ASSERT(list_empty(&op->pend_link));
  60. ASSERT(op->processor != NULL);
  61. ASSERT(fscache_object_is_available(op->object));
  62. ASSERTCMP(atomic_read(&op->usage), >, 0);
  63. ASSERTIFCMP(op->state != FSCACHE_OP_ST_IN_PROGRESS,
  64. op->state, ==, FSCACHE_OP_ST_CANCELLED);
  65. fscache_stat(&fscache_n_op_enqueue);
  66. switch (op->flags & FSCACHE_OP_TYPE) {
  67. case FSCACHE_OP_ASYNC:
  68. _debug("queue async");
  69. atomic_inc(&op->usage);
  70. if (!queue_work(fscache_op_wq, &op->work))
  71. fscache_put_operation(op);
  72. break;
  73. case FSCACHE_OP_MYTHREAD:
  74. _debug("queue for caller's attention");
  75. break;
  76. default:
  77. pr_err("Unexpected op type %lx", op->flags);
  78. BUG();
  79. break;
  80. }
  81. }
  82. EXPORT_SYMBOL(fscache_enqueue_operation);
  83. /*
  84. * start an op running
  85. */
  86. static void fscache_run_op(struct fscache_object *object,
  87. struct fscache_operation *op)
  88. {
  89. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
  90. op->state = FSCACHE_OP_ST_IN_PROGRESS;
  91. object->n_in_progress++;
  92. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  93. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  94. if (op->processor)
  95. fscache_enqueue_operation(op);
  96. fscache_stat(&fscache_n_op_run);
  97. }
  98. /*
  99. * report an unexpected submission
  100. */
  101. static void fscache_report_unexpected_submission(struct fscache_object *object,
  102. struct fscache_operation *op,
  103. const struct fscache_state *ostate)
  104. {
  105. static bool once_only;
  106. struct fscache_operation *p;
  107. unsigned n;
  108. if (once_only)
  109. return;
  110. once_only = true;
  111. kdebug("unexpected submission OP%x [OBJ%x %s]",
  112. op->debug_id, object->debug_id, object->state->name);
  113. kdebug("objstate=%s [%s]", object->state->name, ostate->name);
  114. kdebug("objflags=%lx", object->flags);
  115. kdebug("objevent=%lx [%lx]", object->events, object->event_mask);
  116. kdebug("ops=%u inp=%u exc=%u",
  117. object->n_ops, object->n_in_progress, object->n_exclusive);
  118. if (!list_empty(&object->pending_ops)) {
  119. n = 0;
  120. list_for_each_entry(p, &object->pending_ops, pend_link) {
  121. ASSERTCMP(p->object, ==, object);
  122. kdebug("%p %p", op->processor, op->release);
  123. n++;
  124. }
  125. kdebug("n=%u", n);
  126. }
  127. dump_stack();
  128. }
  129. /*
  130. * submit an exclusive operation for an object
  131. * - other ops are excluded from running simultaneously with this one
  132. * - this gets any extra refs it needs on an op
  133. */
  134. int fscache_submit_exclusive_op(struct fscache_object *object,
  135. struct fscache_operation *op)
  136. {
  137. const struct fscache_state *ostate;
  138. unsigned long flags;
  139. int ret;
  140. _enter("{OBJ%x OP%x},", object->debug_id, op->debug_id);
  141. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
  142. ASSERTCMP(atomic_read(&op->usage), >, 0);
  143. spin_lock(&object->lock);
  144. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  145. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  146. ASSERT(list_empty(&op->pend_link));
  147. ostate = object->state;
  148. smp_rmb();
  149. op->state = FSCACHE_OP_ST_PENDING;
  150. flags = READ_ONCE(object->flags);
  151. if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
  152. fscache_stat(&fscache_n_op_rejected);
  153. op->cancel(op);
  154. op->state = FSCACHE_OP_ST_CANCELLED;
  155. ret = -ENOBUFS;
  156. } else if (unlikely(fscache_cache_is_broken(object))) {
  157. op->cancel(op);
  158. op->state = FSCACHE_OP_ST_CANCELLED;
  159. ret = -EIO;
  160. } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
  161. op->object = object;
  162. object->n_ops++;
  163. object->n_exclusive++; /* reads and writes must wait */
  164. if (object->n_in_progress > 0) {
  165. atomic_inc(&op->usage);
  166. list_add_tail(&op->pend_link, &object->pending_ops);
  167. fscache_stat(&fscache_n_op_pend);
  168. } else if (!list_empty(&object->pending_ops)) {
  169. atomic_inc(&op->usage);
  170. list_add_tail(&op->pend_link, &object->pending_ops);
  171. fscache_stat(&fscache_n_op_pend);
  172. fscache_start_operations(object);
  173. } else {
  174. ASSERTCMP(object->n_in_progress, ==, 0);
  175. fscache_run_op(object, op);
  176. }
  177. /* need to issue a new write op after this */
  178. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  179. ret = 0;
  180. } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) {
  181. op->object = object;
  182. object->n_ops++;
  183. object->n_exclusive++; /* reads and writes must wait */
  184. atomic_inc(&op->usage);
  185. list_add_tail(&op->pend_link, &object->pending_ops);
  186. fscache_stat(&fscache_n_op_pend);
  187. ret = 0;
  188. } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
  189. op->cancel(op);
  190. op->state = FSCACHE_OP_ST_CANCELLED;
  191. ret = -ENOBUFS;
  192. } else {
  193. fscache_report_unexpected_submission(object, op, ostate);
  194. op->cancel(op);
  195. op->state = FSCACHE_OP_ST_CANCELLED;
  196. ret = -ENOBUFS;
  197. }
  198. spin_unlock(&object->lock);
  199. return ret;
  200. }
  201. /*
  202. * submit an operation for an object
  203. * - objects may be submitted only in the following states:
  204. * - during object creation (write ops may be submitted)
  205. * - whilst the object is active
  206. * - after an I/O error incurred in one of the two above states (op rejected)
  207. * - this gets any extra refs it needs on an op
  208. */
  209. int fscache_submit_op(struct fscache_object *object,
  210. struct fscache_operation *op)
  211. {
  212. const struct fscache_state *ostate;
  213. unsigned long flags;
  214. int ret;
  215. _enter("{OBJ%x OP%x},{%u}",
  216. object->debug_id, op->debug_id, atomic_read(&op->usage));
  217. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_INITIALISED);
  218. ASSERTCMP(atomic_read(&op->usage), >, 0);
  219. spin_lock(&object->lock);
  220. ASSERTCMP(object->n_ops, >=, object->n_in_progress);
  221. ASSERTCMP(object->n_ops, >=, object->n_exclusive);
  222. ASSERT(list_empty(&op->pend_link));
  223. ostate = object->state;
  224. smp_rmb();
  225. op->state = FSCACHE_OP_ST_PENDING;
  226. flags = READ_ONCE(object->flags);
  227. if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
  228. fscache_stat(&fscache_n_op_rejected);
  229. op->cancel(op);
  230. op->state = FSCACHE_OP_ST_CANCELLED;
  231. ret = -ENOBUFS;
  232. } else if (unlikely(fscache_cache_is_broken(object))) {
  233. op->cancel(op);
  234. op->state = FSCACHE_OP_ST_CANCELLED;
  235. ret = -EIO;
  236. } else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
  237. op->object = object;
  238. object->n_ops++;
  239. if (object->n_exclusive > 0) {
  240. atomic_inc(&op->usage);
  241. list_add_tail(&op->pend_link, &object->pending_ops);
  242. fscache_stat(&fscache_n_op_pend);
  243. } else if (!list_empty(&object->pending_ops)) {
  244. atomic_inc(&op->usage);
  245. list_add_tail(&op->pend_link, &object->pending_ops);
  246. fscache_stat(&fscache_n_op_pend);
  247. fscache_start_operations(object);
  248. } else {
  249. ASSERTCMP(object->n_exclusive, ==, 0);
  250. fscache_run_op(object, op);
  251. }
  252. ret = 0;
  253. } else if (flags & BIT(FSCACHE_OBJECT_IS_LOOKED_UP)) {
  254. op->object = object;
  255. object->n_ops++;
  256. atomic_inc(&op->usage);
  257. list_add_tail(&op->pend_link, &object->pending_ops);
  258. fscache_stat(&fscache_n_op_pend);
  259. ret = 0;
  260. } else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
  261. op->cancel(op);
  262. op->state = FSCACHE_OP_ST_CANCELLED;
  263. ret = -ENOBUFS;
  264. } else {
  265. fscache_report_unexpected_submission(object, op, ostate);
  266. ASSERT(!fscache_object_is_active(object));
  267. op->cancel(op);
  268. op->state = FSCACHE_OP_ST_CANCELLED;
  269. ret = -ENOBUFS;
  270. }
  271. spin_unlock(&object->lock);
  272. return ret;
  273. }
  274. /*
  275. * queue an object for withdrawal on error, aborting all following asynchronous
  276. * operations
  277. */
  278. void fscache_abort_object(struct fscache_object *object)
  279. {
  280. _enter("{OBJ%x}", object->debug_id);
  281. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  282. }
  283. /*
  284. * Jump start the operation processing on an object. The caller must hold
  285. * object->lock.
  286. */
  287. void fscache_start_operations(struct fscache_object *object)
  288. {
  289. struct fscache_operation *op;
  290. bool stop = false;
  291. while (!list_empty(&object->pending_ops) && !stop) {
  292. op = list_entry(object->pending_ops.next,
  293. struct fscache_operation, pend_link);
  294. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags)) {
  295. if (object->n_in_progress > 0)
  296. break;
  297. stop = true;
  298. }
  299. list_del_init(&op->pend_link);
  300. fscache_run_op(object, op);
  301. /* the pending queue was holding a ref on the object */
  302. fscache_put_operation(op);
  303. }
  304. ASSERTCMP(object->n_in_progress, <=, object->n_ops);
  305. _debug("woke %d ops on OBJ%x",
  306. object->n_in_progress, object->debug_id);
  307. }
  308. /*
  309. * cancel an operation that's pending on an object
  310. */
  311. int fscache_cancel_op(struct fscache_operation *op,
  312. bool cancel_in_progress_op)
  313. {
  314. struct fscache_object *object = op->object;
  315. bool put = false;
  316. int ret;
  317. _enter("OBJ%x OP%x}", op->object->debug_id, op->debug_id);
  318. ASSERTCMP(op->state, >=, FSCACHE_OP_ST_PENDING);
  319. ASSERTCMP(op->state, !=, FSCACHE_OP_ST_CANCELLED);
  320. ASSERTCMP(atomic_read(&op->usage), >, 0);
  321. spin_lock(&object->lock);
  322. ret = -EBUSY;
  323. if (op->state == FSCACHE_OP_ST_PENDING) {
  324. ASSERT(!list_empty(&op->pend_link));
  325. list_del_init(&op->pend_link);
  326. put = true;
  327. fscache_stat(&fscache_n_op_cancelled);
  328. op->cancel(op);
  329. op->state = FSCACHE_OP_ST_CANCELLED;
  330. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  331. object->n_exclusive--;
  332. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  333. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  334. ret = 0;
  335. } else if (op->state == FSCACHE_OP_ST_IN_PROGRESS && cancel_in_progress_op) {
  336. ASSERTCMP(object->n_in_progress, >, 0);
  337. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  338. object->n_exclusive--;
  339. object->n_in_progress--;
  340. if (object->n_in_progress == 0)
  341. fscache_start_operations(object);
  342. fscache_stat(&fscache_n_op_cancelled);
  343. op->cancel(op);
  344. op->state = FSCACHE_OP_ST_CANCELLED;
  345. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  346. object->n_exclusive--;
  347. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  348. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  349. ret = 0;
  350. }
  351. if (put)
  352. fscache_put_operation(op);
  353. spin_unlock(&object->lock);
  354. _leave(" = %d", ret);
  355. return ret;
  356. }
  357. /*
  358. * Cancel all pending operations on an object
  359. */
  360. void fscache_cancel_all_ops(struct fscache_object *object)
  361. {
  362. struct fscache_operation *op;
  363. _enter("OBJ%x", object->debug_id);
  364. spin_lock(&object->lock);
  365. while (!list_empty(&object->pending_ops)) {
  366. op = list_entry(object->pending_ops.next,
  367. struct fscache_operation, pend_link);
  368. fscache_stat(&fscache_n_op_cancelled);
  369. list_del_init(&op->pend_link);
  370. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
  371. op->cancel(op);
  372. op->state = FSCACHE_OP_ST_CANCELLED;
  373. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  374. object->n_exclusive--;
  375. if (test_and_clear_bit(FSCACHE_OP_WAITING, &op->flags))
  376. wake_up_bit(&op->flags, FSCACHE_OP_WAITING);
  377. fscache_put_operation(op);
  378. cond_resched_lock(&object->lock);
  379. }
  380. spin_unlock(&object->lock);
  381. _leave("");
  382. }
  383. /*
  384. * Record the completion or cancellation of an in-progress operation.
  385. */
  386. void fscache_op_complete(struct fscache_operation *op, bool cancelled)
  387. {
  388. struct fscache_object *object = op->object;
  389. _enter("OBJ%x", object->debug_id);
  390. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_IN_PROGRESS);
  391. ASSERTCMP(object->n_in_progress, >, 0);
  392. ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
  393. object->n_exclusive, >, 0);
  394. ASSERTIFCMP(test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags),
  395. object->n_in_progress, ==, 1);
  396. spin_lock(&object->lock);
  397. if (!cancelled) {
  398. op->state = FSCACHE_OP_ST_COMPLETE;
  399. } else {
  400. op->cancel(op);
  401. op->state = FSCACHE_OP_ST_CANCELLED;
  402. }
  403. if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
  404. object->n_exclusive--;
  405. object->n_in_progress--;
  406. if (object->n_in_progress == 0)
  407. fscache_start_operations(object);
  408. spin_unlock(&object->lock);
  409. _leave("");
  410. }
  411. EXPORT_SYMBOL(fscache_op_complete);
  412. /*
  413. * release an operation
  414. * - queues pending ops if this is the last in-progress op
  415. */
  416. void fscache_put_operation(struct fscache_operation *op)
  417. {
  418. struct fscache_object *object;
  419. struct fscache_cache *cache;
  420. _enter("{OBJ%x OP%x,%d}",
  421. op->object ? op->object->debug_id : 0,
  422. op->debug_id, atomic_read(&op->usage));
  423. ASSERTCMP(atomic_read(&op->usage), >, 0);
  424. if (!atomic_dec_and_test(&op->usage))
  425. return;
  426. _debug("PUT OP");
  427. ASSERTIFCMP(op->state != FSCACHE_OP_ST_INITIALISED &&
  428. op->state != FSCACHE_OP_ST_COMPLETE,
  429. op->state, ==, FSCACHE_OP_ST_CANCELLED);
  430. fscache_stat(&fscache_n_op_release);
  431. if (op->release) {
  432. op->release(op);
  433. op->release = NULL;
  434. }
  435. op->state = FSCACHE_OP_ST_DEAD;
  436. object = op->object;
  437. if (likely(object)) {
  438. if (test_bit(FSCACHE_OP_DEC_READ_CNT, &op->flags))
  439. atomic_dec(&object->n_reads);
  440. if (test_bit(FSCACHE_OP_UNUSE_COOKIE, &op->flags))
  441. fscache_unuse_cookie(object);
  442. /* now... we may get called with the object spinlock held, so we
  443. * complete the cleanup here only if we can immediately acquire the
  444. * lock, and defer it otherwise */
  445. if (!spin_trylock(&object->lock)) {
  446. _debug("defer put");
  447. fscache_stat(&fscache_n_op_deferred_release);
  448. cache = object->cache;
  449. spin_lock(&cache->op_gc_list_lock);
  450. list_add_tail(&op->pend_link, &cache->op_gc_list);
  451. spin_unlock(&cache->op_gc_list_lock);
  452. schedule_work(&cache->op_gc);
  453. _leave(" [defer]");
  454. return;
  455. }
  456. ASSERTCMP(object->n_ops, >, 0);
  457. object->n_ops--;
  458. if (object->n_ops == 0)
  459. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  460. spin_unlock(&object->lock);
  461. }
  462. kfree(op);
  463. _leave(" [done]");
  464. }
  465. EXPORT_SYMBOL(fscache_put_operation);
  466. /*
  467. * garbage collect operations that have had their release deferred
  468. */
  469. void fscache_operation_gc(struct work_struct *work)
  470. {
  471. struct fscache_operation *op;
  472. struct fscache_object *object;
  473. struct fscache_cache *cache =
  474. container_of(work, struct fscache_cache, op_gc);
  475. int count = 0;
  476. _enter("");
  477. do {
  478. spin_lock(&cache->op_gc_list_lock);
  479. if (list_empty(&cache->op_gc_list)) {
  480. spin_unlock(&cache->op_gc_list_lock);
  481. break;
  482. }
  483. op = list_entry(cache->op_gc_list.next,
  484. struct fscache_operation, pend_link);
  485. list_del(&op->pend_link);
  486. spin_unlock(&cache->op_gc_list_lock);
  487. object = op->object;
  488. spin_lock(&object->lock);
  489. _debug("GC DEFERRED REL OBJ%x OP%x",
  490. object->debug_id, op->debug_id);
  491. fscache_stat(&fscache_n_op_gc);
  492. ASSERTCMP(atomic_read(&op->usage), ==, 0);
  493. ASSERTCMP(op->state, ==, FSCACHE_OP_ST_DEAD);
  494. ASSERTCMP(object->n_ops, >, 0);
  495. object->n_ops--;
  496. if (object->n_ops == 0)
  497. fscache_raise_event(object, FSCACHE_OBJECT_EV_CLEARED);
  498. spin_unlock(&object->lock);
  499. kfree(op);
  500. } while (count++ < 20);
  501. if (!list_empty(&cache->op_gc_list))
  502. schedule_work(&cache->op_gc);
  503. _leave("");
  504. }
  505. /*
  506. * execute an operation using fs_op_wq to provide processing context -
  507. * the caller holds a ref to this object, so we don't need to hold one
  508. */
  509. void fscache_op_work_func(struct work_struct *work)
  510. {
  511. struct fscache_operation *op =
  512. container_of(work, struct fscache_operation, work);
  513. unsigned long start;
  514. _enter("{OBJ%x OP%x,%d}",
  515. op->object->debug_id, op->debug_id, atomic_read(&op->usage));
  516. ASSERT(op->processor != NULL);
  517. start = jiffies;
  518. op->processor(op);
  519. fscache_hist(fscache_ops_histogram, start);
  520. fscache_put_operation(op);
  521. _leave("");
  522. }