Split check_request into helpers to check the request epoch, and to check
if the request access a busy object.  Peer I/O requests call both helpers,
while gateway requests never need to call check_request_epoch because
we just assigned the epoch to be the current one.

Also lift the check for cached objets into queue_gateway_request.

Signed-off-by: Christoph Hellwig <hch@lst.de>

diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index da346aa..d9882b1 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -199,20 +199,8 @@ static void do_local_request(struct work *work)
 	req->rp.result = ret;
 }
 
-static int check_request(struct request *req)
+static int check_request_epoch(struct request *req)
 {
-	/*
-	 * if we go for a cached object, we don't care if it is busy
-	 * or being recovered.
-	 */
-	if (sys->enable_write_cache &&
-	    (req->rq.flags & SD_FLAG_CMD_CACHE) &&
-	    object_is_cached(req->rq.obj.oid))
-		return 0;
-
-	if (!req->local_oid && !req->local_cow_oid)
-		return 0;
-
 	if (before(req->rq.epoch, sys->epoch)) {
 		eprintf("old node version %u, %u, %x\n",
 			sys->epoch, req->rq.epoch, req->rq.opcode);
@@ -232,15 +220,17 @@ static int check_request(struct request *req)
 		return -1;
 	}
 
-	if (!req->local_oid)
-		return 0;
+	return 0;
+}
 
-	/* IO request of recovery should not wait, or else it may cause
-	   dead lock of recovery, if fails, recovery will take its own
-	   retrying mechanism. */
+static int check_request_busy(struct request *req)
+{
+	/*
+	 * Request from recovery should not wait for objects under recovery to
+	 * avoid deadlocks.  The recovery code will perfom its own retries.
+	 */
 	if (is_recoverying_oid(req->local_oid) &&
 	    !(req->rq.flags & SD_FLAG_CMD_RECOVERY)) {
-		/* Peer requests and gateway requests all need to retry */
 		if (is_recovery_init()) {
 			req->rp.result = SD_RES_OBJ_RECOVERING;
 			list_add_tail(&req->request_list,
@@ -347,8 +337,10 @@ static void queue_io_request(struct request *req)
 {
 	req->local_oid = req->rq.obj.oid;
 
-	if (check_request(req) < 0)
+	if (check_request_epoch(req) < 0)
 		return;
+	if (check_request_busy(req) < 0)
+		return
 	list_add_tail(&req->request_list, &sys->outstanding_req_list);
 
 	req->work.fn = do_io_request;
@@ -368,8 +360,17 @@ static void queue_gateway_request(struct request *req)
 			req->local_cow_oid = hdr->obj.cow_oid;
 	}
 
-	if (check_request(req) < 0)
-		return;
+	/*
+	 * If we go for a cached object, we don't care if it is busy
+	 * or being recovered.
+	 */
+	if (req->local_oid &&
+	    (!sys->enable_write_cache ||
+	     !(req->rq.flags & SD_FLAG_CMD_CACHE) ||
+	     !object_is_cached(req->rq.obj.oid))) {
+		if (check_request_busy(req) < 0)
+			return;
+	}
 
 	list_add_tail(&req->request_list, &sys->outstanding_req_list);
 


