waitqueue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. * (C) 2011 Omnibond Systems
  5. *
  6. * Changes by Acxiom Corporation to implement generic service_operation()
  7. * function, Copyright Acxiom Corporation, 2005.
  8. *
  9. * See COPYING in top-level directory.
  10. */
  11. /*
  12. * In-kernel waitqueue operations.
  13. */
  14. #include "protocol.h"
  15. #include "orangefs-kernel.h"
  16. #include "orangefs-bufmap.h"
  17. static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, long, bool);
  18. static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
  19. /*
  20. * What we do in this function is to walk the list of operations that are
  21. * present in the request queue and mark them as purged.
  22. * NOTE: This is called from the device close after client-core has
  23. * guaranteed that no new operations could appear on the list since the
  24. * client-core is anyway going to exit.
  25. */
  26. void purge_waiting_ops(void)
  27. {
  28. struct orangefs_kernel_op_s *op, *tmp;
  29. spin_lock(&orangefs_request_list_lock);
  30. list_for_each_entry_safe(op, tmp, &orangefs_request_list, list) {
  31. gossip_debug(GOSSIP_WAIT_DEBUG,
  32. "pvfs2-client-core: purging op tag %llu %s\n",
  33. llu(op->tag),
  34. get_opname_string(op));
  35. set_op_state_purged(op);
  36. gossip_debug(GOSSIP_DEV_DEBUG,
  37. "%s: op:%s: op_state:%d: process:%s:\n",
  38. __func__,
  39. get_opname_string(op),
  40. op->op_state,
  41. current->comm);
  42. }
  43. spin_unlock(&orangefs_request_list_lock);
  44. }
  45. /*
  46. * submits a ORANGEFS operation and waits for it to complete
  47. *
  48. * Note op->downcall.status will contain the status of the operation (in
  49. * errno format), whether provided by pvfs2-client or a result of failure to
  50. * service the operation. If the caller wishes to distinguish, then
  51. * op->state can be checked to see if it was serviced or not.
  52. *
  53. * Returns contents of op->downcall.status for convenience
  54. */
  55. int service_operation(struct orangefs_kernel_op_s *op,
  56. const char *op_name,
  57. int flags)
  58. {
  59. long timeout = MAX_SCHEDULE_TIMEOUT;
  60. int ret = 0;
  61. DEFINE_WAIT(wait_entry);
  62. op->upcall.tgid = current->tgid;
  63. op->upcall.pid = current->pid;
  64. retry_servicing:
  65. op->downcall.status = 0;
  66. gossip_debug(GOSSIP_WAIT_DEBUG,
  67. "%s: %s op:%p: process:%s: pid:%d:\n",
  68. __func__,
  69. op_name,
  70. op,
  71. current->comm,
  72. current->pid);
  73. /*
  74. * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
  75. * acquiring the request_mutex because we're servicing a
  76. * high priority remount operation and the request_mutex is
  77. * already taken.
  78. */
  79. if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
  80. if (flags & ORANGEFS_OP_INTERRUPTIBLE)
  81. ret = mutex_lock_interruptible(&orangefs_request_mutex);
  82. else
  83. ret = mutex_lock_killable(&orangefs_request_mutex);
  84. /*
  85. * check to see if we were interrupted while waiting for
  86. * mutex
  87. */
  88. if (ret < 0) {
  89. op->downcall.status = ret;
  90. gossip_debug(GOSSIP_WAIT_DEBUG,
  91. "%s: service_operation interrupted.\n",
  92. __func__);
  93. return ret;
  94. }
  95. }
  96. /* queue up the operation */
  97. spin_lock(&orangefs_request_list_lock);
  98. spin_lock(&op->lock);
  99. set_op_state_waiting(op);
  100. gossip_debug(GOSSIP_DEV_DEBUG,
  101. "%s: op:%s: op_state:%d: process:%s:\n",
  102. __func__,
  103. get_opname_string(op),
  104. op->op_state,
  105. current->comm);
  106. /* add high priority remount op to the front of the line. */
  107. if (flags & ORANGEFS_OP_PRIORITY)
  108. list_add(&op->list, &orangefs_request_list);
  109. else
  110. list_add_tail(&op->list, &orangefs_request_list);
  111. spin_unlock(&op->lock);
  112. wake_up_interruptible(&orangefs_request_list_waitq);
  113. if (!__is_daemon_in_service()) {
  114. gossip_debug(GOSSIP_WAIT_DEBUG,
  115. "%s:client core is NOT in service.\n",
  116. __func__);
  117. /*
  118. * Don't wait for the userspace component to return if
  119. * the filesystem is being umounted anyway.
  120. */
  121. if (op->upcall.type == ORANGEFS_VFS_OP_FS_UMOUNT)
  122. timeout = 0;
  123. else
  124. timeout = op_timeout_secs * HZ;
  125. }
  126. spin_unlock(&orangefs_request_list_lock);
  127. if (!(flags & ORANGEFS_OP_NO_MUTEX))
  128. mutex_unlock(&orangefs_request_mutex);
  129. ret = wait_for_matching_downcall(op, timeout,
  130. flags & ORANGEFS_OP_INTERRUPTIBLE);
  131. gossip_debug(GOSSIP_WAIT_DEBUG,
  132. "%s: wait_for_matching_downcall returned %d for %p\n",
  133. __func__,
  134. ret,
  135. op);
  136. /* got matching downcall; make sure status is in errno format */
  137. if (!ret) {
  138. spin_unlock(&op->lock);
  139. op->downcall.status =
  140. orangefs_normalize_to_errno(op->downcall.status);
  141. ret = op->downcall.status;
  142. goto out;
  143. }
  144. /* failed to get matching downcall */
  145. if (ret == -ETIMEDOUT) {
  146. gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
  147. __func__,
  148. op_name);
  149. }
  150. /*
  151. * remove a waiting op from the request list or
  152. * remove an in-progress op from the in-progress list.
  153. */
  154. orangefs_clean_up_interrupted_operation(op);
  155. op->downcall.status = ret;
  156. /* retry if operation has not been serviced and if requested */
  157. if (ret == -EAGAIN) {
  158. op->attempts++;
  159. timeout = op_timeout_secs * HZ;
  160. gossip_debug(GOSSIP_WAIT_DEBUG,
  161. "orangefs: tag %llu (%s)"
  162. " -- operation to be retried (%d attempt)\n",
  163. llu(op->tag),
  164. op_name,
  165. op->attempts);
  166. /*
  167. * io ops (ops that use the shared memory buffer) have
  168. * to be returned to their caller for a retry. Other ops
  169. * can just be recycled here.
  170. */
  171. if (!op->uses_shared_memory)
  172. goto retry_servicing;
  173. }
  174. out:
  175. gossip_debug(GOSSIP_WAIT_DEBUG,
  176. "%s: %s returning: %d for %p.\n",
  177. __func__,
  178. op_name,
  179. ret,
  180. op);
  181. return ret;
  182. }
  183. /* This can get called on an I/O op if it had a bad service_operation. */
  184. bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
  185. {
  186. u64 tag = op->tag;
  187. if (!op_state_in_progress(op))
  188. return false;
  189. op->slot_to_free = op->upcall.req.io.buf_index;
  190. memset(&op->upcall, 0, sizeof(op->upcall));
  191. memset(&op->downcall, 0, sizeof(op->downcall));
  192. op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
  193. op->upcall.req.cancel.op_tag = tag;
  194. op->downcall.type = ORANGEFS_VFS_OP_INVALID;
  195. op->downcall.status = -1;
  196. orangefs_new_tag(op);
  197. spin_lock(&orangefs_request_list_lock);
  198. /* orangefs_request_list_lock is enough of a barrier here */
  199. if (!__is_daemon_in_service()) {
  200. spin_unlock(&orangefs_request_list_lock);
  201. return false;
  202. }
  203. spin_lock(&op->lock);
  204. set_op_state_waiting(op);
  205. gossip_debug(GOSSIP_DEV_DEBUG,
  206. "%s: op:%s: op_state:%d: process:%s:\n",
  207. __func__,
  208. get_opname_string(op),
  209. op->op_state,
  210. current->comm);
  211. list_add(&op->list, &orangefs_request_list);
  212. spin_unlock(&op->lock);
  213. spin_unlock(&orangefs_request_list_lock);
  214. gossip_debug(GOSSIP_WAIT_DEBUG,
  215. "Attempting ORANGEFS operation cancellation of tag %llu\n",
  216. llu(tag));
  217. return true;
  218. }
  219. /*
  220. * Change an op to the "given up" state and remove it from its list.
  221. */
  222. static void
  223. orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
  224. {
  225. /*
  226. * handle interrupted cases depending on what state we were in when
  227. * the interruption is detected.
  228. *
  229. * Called with op->lock held.
  230. */
  231. /*
  232. * List manipulation code elsewhere will ignore ops that
  233. * have been given up upon.
  234. */
  235. op->op_state |= OP_VFS_STATE_GIVEN_UP;
  236. if (list_empty(&op->list)) {
  237. /* caught copying to/from daemon */
  238. BUG_ON(op_state_serviced(op));
  239. spin_unlock(&op->lock);
  240. wait_for_completion(&op->waitq);
  241. } else if (op_state_waiting(op)) {
  242. /*
  243. * upcall hasn't been read; remove op from upcall request
  244. * list.
  245. */
  246. spin_unlock(&op->lock);
  247. spin_lock(&orangefs_request_list_lock);
  248. list_del_init(&op->list);
  249. spin_unlock(&orangefs_request_list_lock);
  250. gossip_debug(GOSSIP_WAIT_DEBUG,
  251. "Interrupted: Removed op %p from request_list\n",
  252. op);
  253. } else if (op_state_in_progress(op)) {
  254. /* op must be removed from the in progress htable */
  255. spin_unlock(&op->lock);
  256. spin_lock(&orangefs_htable_ops_in_progress_lock);
  257. list_del_init(&op->list);
  258. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  259. gossip_debug(GOSSIP_WAIT_DEBUG,
  260. "Interrupted: Removed op %p"
  261. " from htable_ops_in_progress\n",
  262. op);
  263. } else {
  264. spin_unlock(&op->lock);
  265. gossip_err("interrupted operation is in a weird state 0x%x\n",
  266. op->op_state);
  267. }
  268. reinit_completion(&op->waitq);
  269. }
  270. /*
  271. * Sleeps on waitqueue waiting for matching downcall.
  272. * If client-core finishes servicing, then we are good to go.
  273. * else if client-core exits, we get woken up here, and retry with a timeout
  274. *
  275. * When this call returns to the caller, the specified op will no
  276. * longer be in either the in_progress hash table or on the request list.
  277. *
  278. * Returns 0 on success and -errno on failure
  279. * Errors are:
  280. * EAGAIN in case we want the caller to requeue and try again..
  281. * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
  282. * operation since client-core seems to be exiting too often
  283. * or if we were interrupted.
  284. *
  285. * Returns with op->lock taken.
  286. */
  287. static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
  288. long timeout,
  289. bool interruptible)
  290. {
  291. long n;
  292. /*
  293. * There's a "schedule_timeout" inside of these wait
  294. * primitives, during which the op is out of the hands of the
  295. * user process that needs something done and is being
  296. * manipulated by the client-core process.
  297. */
  298. if (interruptible)
  299. n = wait_for_completion_interruptible_timeout(&op->waitq,
  300. timeout);
  301. else
  302. n = wait_for_completion_killable_timeout(&op->waitq, timeout);
  303. spin_lock(&op->lock);
  304. if (op_state_serviced(op))
  305. return 0;
  306. if (unlikely(n < 0)) {
  307. gossip_debug(GOSSIP_WAIT_DEBUG,
  308. "%s: operation interrupted, tag %llu, %p\n",
  309. __func__,
  310. llu(op->tag),
  311. op);
  312. return -EINTR;
  313. }
  314. if (op_state_purged(op)) {
  315. gossip_debug(GOSSIP_WAIT_DEBUG,
  316. "%s: operation purged, tag %llu, %p, %d\n",
  317. __func__,
  318. llu(op->tag),
  319. op,
  320. op->attempts);
  321. return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
  322. -EAGAIN :
  323. -EIO;
  324. }
  325. /* must have timed out, then... */
  326. gossip_debug(GOSSIP_WAIT_DEBUG,
  327. "%s: operation timed out, tag %llu, %p, %d)\n",
  328. __func__,
  329. llu(op->tag),
  330. op,
  331. op->attempts);
  332. return -ETIMEDOUT;
  333. }