u32 type;
        u32 tag = blk_mq_unique_tag(req);
 
+       if (req->cmd_type != REQ_TYPE_FS)
+               return -EIO;
+
        if (req_op(req) == REQ_OP_DISCARD)
                type = NBD_CMD_TRIM;
        else if (req_op(req) == REQ_OP_FLUSH)
        else
                type = NBD_CMD_READ;
 
+       if (rq_data_dir(req) == WRITE &&
+           (nbd->flags & NBD_FLAG_READ_ONLY)) {
+               dev_err_ratelimited(disk_to_dev(nbd->disk),
+                                   "Write on read-only\n");
+               return -EIO;
+       }
+
        memset(&request, 0, sizeof(request));
        request.magic = htonl(NBD_REQUEST_MAGIC);
        request.type = htonl(type);
                goto error_out;
        }
 
-       if (req->cmd_type != REQ_TYPE_FS)
-               goto error_out;
-
-       if (req->cmd_type == REQ_TYPE_FS &&
-           rq_data_dir(req) == WRITE &&
-           (nbd->flags & NBD_FLAG_READ_ONLY)) {
-               dev_err_ratelimited(disk_to_dev(nbd->disk),
-                                   "Write on read-only\n");
-               goto error_out;
-       }
-
        req->errors = 0;
 
        nsock = nbd->socks[index];