waitqueue.c 10.0 KB

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