waitqueue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. int flags)
  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, flags);
  134. gossip_debug(GOSSIP_WAIT_DEBUG,
  135. "%s: wait_for_matching_downcall returned %d for %p\n",
  136. __func__,
  137. ret,
  138. op);
  139. /* got matching downcall; make sure status is in errno format */
  140. if (!ret) {
  141. spin_unlock(&op->lock);
  142. op->downcall.status =
  143. orangefs_normalize_to_errno(op->downcall.status);
  144. ret = op->downcall.status;
  145. goto out;
  146. }
  147. /* failed to get matching downcall */
  148. if (ret == -ETIMEDOUT) {
  149. gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
  150. __func__,
  151. op_name);
  152. }
  153. /*
  154. * remove a waiting op from the request list or
  155. * remove an in-progress op from the in-progress list.
  156. */
  157. orangefs_clean_up_interrupted_operation(op);
  158. op->downcall.status = ret;
  159. /* retry if operation has not been serviced and if requested */
  160. if (ret == -EAGAIN) {
  161. op->attempts++;
  162. timeout = op_timeout_secs * HZ;
  163. gossip_debug(GOSSIP_WAIT_DEBUG,
  164. "orangefs: tag %llu (%s)"
  165. " -- operation to be retried (%d attempt)\n",
  166. llu(op->tag),
  167. op_name,
  168. op->attempts);
  169. /*
  170. * io ops (ops that use the shared memory buffer) have
  171. * to be returned to their caller for a retry. Other ops
  172. * can just be recycled here.
  173. */
  174. if (!op->uses_shared_memory)
  175. goto retry_servicing;
  176. }
  177. out:
  178. gossip_debug(GOSSIP_WAIT_DEBUG,
  179. "%s: %s returning: %d for %p.\n",
  180. __func__,
  181. op_name,
  182. ret,
  183. op);
  184. return ret;
  185. }
  186. /* This can get called on an I/O op if it had a bad service_operation. */
  187. bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
  188. {
  189. u64 tag = op->tag;
  190. if (!op_state_in_progress(op))
  191. return false;
  192. op->slot_to_free = op->upcall.req.io.buf_index;
  193. memset(&op->upcall, 0, sizeof(op->upcall));
  194. memset(&op->downcall, 0, sizeof(op->downcall));
  195. op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
  196. op->upcall.req.cancel.op_tag = tag;
  197. op->downcall.type = ORANGEFS_VFS_OP_INVALID;
  198. op->downcall.status = -1;
  199. orangefs_new_tag(op);
  200. spin_lock(&orangefs_request_list_lock);
  201. /* orangefs_request_list_lock is enough of a barrier here */
  202. if (!__is_daemon_in_service()) {
  203. spin_unlock(&orangefs_request_list_lock);
  204. return false;
  205. }
  206. spin_lock(&op->lock);
  207. set_op_state_waiting(op);
  208. gossip_debug(GOSSIP_DEV_DEBUG,
  209. "%s: op:%s: op_state:%d: process:%s:\n",
  210. __func__,
  211. get_opname_string(op),
  212. op->op_state,
  213. current->comm);
  214. list_add(&op->list, &orangefs_request_list);
  215. spin_unlock(&op->lock);
  216. spin_unlock(&orangefs_request_list_lock);
  217. gossip_debug(GOSSIP_WAIT_DEBUG,
  218. "Attempting ORANGEFS operation cancellation of tag %llu\n",
  219. llu(tag));
  220. return true;
  221. }
  222. /*
  223. * Change an op to the "given up" state and remove it from its list.
  224. */
  225. static void
  226. orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
  227. __releases(op->lock)
  228. {
  229. /*
  230. * handle interrupted cases depending on what state we were in when
  231. * the interruption is detected.
  232. *
  233. * Called with op->lock held.
  234. */
  235. /*
  236. * List manipulation code elsewhere will ignore ops that
  237. * have been given up upon.
  238. */
  239. op->op_state |= OP_VFS_STATE_GIVEN_UP;
  240. if (list_empty(&op->list)) {
  241. /* caught copying to/from daemon */
  242. BUG_ON(op_state_serviced(op));
  243. spin_unlock(&op->lock);
  244. wait_for_completion(&op->waitq);
  245. } else if (op_state_waiting(op)) {
  246. /*
  247. * upcall hasn't been read; remove op from upcall request
  248. * list.
  249. */
  250. spin_unlock(&op->lock);
  251. spin_lock(&orangefs_request_list_lock);
  252. list_del_init(&op->list);
  253. spin_unlock(&orangefs_request_list_lock);
  254. gossip_debug(GOSSIP_WAIT_DEBUG,
  255. "Interrupted: Removed op %p from request_list\n",
  256. op);
  257. } else if (op_state_in_progress(op)) {
  258. /* op must be removed from the in progress htable */
  259. spin_unlock(&op->lock);
  260. spin_lock(&orangefs_htable_ops_in_progress_lock);
  261. list_del_init(&op->list);
  262. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  263. gossip_debug(GOSSIP_WAIT_DEBUG,
  264. "Interrupted: Removed op %p"
  265. " from htable_ops_in_progress\n",
  266. op);
  267. } else {
  268. spin_unlock(&op->lock);
  269. gossip_err("interrupted operation is in a weird state 0x%x\n",
  270. op->op_state);
  271. }
  272. reinit_completion(&op->waitq);
  273. }
  274. /*
  275. * Sleeps on waitqueue waiting for matching downcall.
  276. * If client-core finishes servicing, then we are good to go.
  277. * else if client-core exits, we get woken up here, and retry with a timeout
  278. *
  279. * When this call returns to the caller, the specified op will no
  280. * longer be in either the in_progress hash table or on the request list.
  281. *
  282. * Returns 0 on success and -errno on failure
  283. * Errors are:
  284. * EAGAIN in case we want the caller to requeue and try again..
  285. * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
  286. * operation since client-core seems to be exiting too often
  287. * or if we were interrupted.
  288. *
  289. * Returns with op->lock taken.
  290. */
  291. static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
  292. long timeout,
  293. int flags)
  294. __acquires(op->lock)
  295. {
  296. long n;
  297. int writeback = flags & ORANGEFS_OP_WRITEBACK,
  298. interruptible = flags & ORANGEFS_OP_INTERRUPTIBLE;
  299. /*
  300. * There's a "schedule_timeout" inside of these wait
  301. * primitives, during which the op is out of the hands of the
  302. * user process that needs something done and is being
  303. * manipulated by the client-core process.
  304. */
  305. if (writeback)
  306. n = wait_for_completion_io_timeout(&op->waitq, timeout);
  307. else if (!writeback && interruptible)
  308. n = wait_for_completion_interruptible_timeout(&op->waitq,
  309. timeout);
  310. else /* !writeback && !interruptible but compiler complains */
  311. n = wait_for_completion_killable_timeout(&op->waitq, timeout);
  312. spin_lock(&op->lock);
  313. if (op_state_serviced(op))
  314. return 0;
  315. if (unlikely(n < 0)) {
  316. gossip_debug(GOSSIP_WAIT_DEBUG,
  317. "%s: operation interrupted, tag %llu, %p\n",
  318. __func__,
  319. llu(op->tag),
  320. op);
  321. return -EINTR;
  322. }
  323. if (op_state_purged(op)) {
  324. gossip_debug(GOSSIP_WAIT_DEBUG,
  325. "%s: operation purged, tag %llu, %p, %d\n",
  326. __func__,
  327. llu(op->tag),
  328. op,
  329. op->attempts);
  330. return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
  331. -EAGAIN :
  332. -EIO;
  333. }
  334. /* must have timed out, then... */
  335. gossip_debug(GOSSIP_WAIT_DEBUG,
  336. "%s: operation timed out, tag %llu, %p, %d)\n",
  337. __func__,
  338. llu(op->tag),
  339. op,
  340. op->attempts);
  341. return -ETIMEDOUT;
  342. }