waitqueue.c 9.8 KB

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