waitqueue.c 10 KB

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