waitqueue.c 8.9 KB

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