/* SPDX-License-Identifier: GPL-2.0-only */1/*2drbd_req.h34This file is part of DRBD by Philipp Reisner and Lars Ellenberg.56Copyright (C) 2006-2008, LINBIT Information Technologies GmbH.7Copyright (C) 2006-2008, Lars Ellenberg <[email protected]>.8Copyright (C) 2006-2008, Philipp Reisner <[email protected]>.910*/1112#ifndef _DRBD_REQ_H13#define _DRBD_REQ_H1415#include <linux/module.h>1617#include <linux/slab.h>18#include <linux/drbd.h>19#include "drbd_int.h"2021/* The request callbacks will be called in irq context by the IDE drivers,22and in Softirqs/Tasklets/BH context by the SCSI drivers,23and by the receiver and worker in kernel-thread context.24Try to get the locking right :) */2526/*27* Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are28* associated with IO requests originating from the block layer above us.29*30* There are quite a few things that may happen to a drbd request31* during its lifetime.32*33* It will be created.34* It will be marked with the intention to be35* submitted to local disk and/or36* send via the network.37*38* It has to be placed on the transfer log and other housekeeping lists,39* In case we have a network connection.40*41* It may be identified as a concurrent (write) request42* and be handled accordingly.43*44* It may me handed over to the local disk subsystem.45* It may be completed by the local disk subsystem,46* either successfully or with io-error.47* In case it is a READ request, and it failed locally,48* it may be retried remotely.49*50* It may be queued for sending.51* It may be handed over to the network stack,52* which may fail.53* It may be acknowledged by the "peer" according to the wire_protocol in use.54* this may be a negative ack.55* It may receive a faked ack when the network connection is lost and the56* transfer log is cleaned up.57* Sending may be canceled due to network connection loss.58* When it finally has outlived its time,59* corresponding dirty bits in the resync-bitmap may be cleared or set,60* it will be destroyed,61* and completion will be signalled to the originator,62* with or without "success".63*/6465enum drbd_req_event {66CREATED,67TO_BE_SENT,68TO_BE_SUBMITTED,6970/* XXX yes, now I am inconsistent...71* these are not "events" but "actions"72* oh, well... */73QUEUE_FOR_NET_WRITE,74QUEUE_FOR_NET_READ,75QUEUE_FOR_SEND_OOS,7677/* An empty flush is queued as P_BARRIER,78* which will cause it to complete "successfully",79* even if the local disk flush failed.80*81* Just like "real" requests, empty flushes (blkdev_issue_flush()) will82* only see an error if neither local nor remote data is reachable. */83QUEUE_AS_DRBD_BARRIER,8485SEND_CANCELED,86SEND_FAILED,87HANDED_OVER_TO_NETWORK,88OOS_HANDED_TO_NETWORK,89CONNECTION_LOST_WHILE_PENDING,90READ_RETRY_REMOTE_CANCELED,91RECV_ACKED_BY_PEER,92WRITE_ACKED_BY_PEER,93WRITE_ACKED_BY_PEER_AND_SIS, /* and set_in_sync */94CONFLICT_RESOLVED,95POSTPONE_WRITE,96NEG_ACKED,97BARRIER_ACKED, /* in protocol A and B */98DATA_RECEIVED, /* (remote read) */99100COMPLETED_OK,101READ_COMPLETED_WITH_ERROR,102READ_AHEAD_COMPLETED_WITH_ERROR,103WRITE_COMPLETED_WITH_ERROR,104DISCARD_COMPLETED_NOTSUPP,105DISCARD_COMPLETED_WITH_ERROR,106107ABORT_DISK_IO,108RESEND,109FAIL_FROZEN_DISK_IO,110RESTART_FROZEN_DISK_IO,111NOTHING,112};113114/* encoding of request states for now. we don't actually need that many bits.115* we don't need to do atomic bit operations either, since most of the time we116* need to look at the connection state and/or manipulate some lists at the117* same time, so we should hold the request lock anyways.118*/119enum drbd_req_state_bits {120/* 3210121* 0000: no local possible122* 0001: to be submitted123* UNUSED, we could map: 011: submitted, completion still pending124* 0110: completed ok125* 0010: completed with error126* 1001: Aborted (before completion)127* 1x10: Aborted and completed -> free128*/129__RQ_LOCAL_PENDING,130__RQ_LOCAL_COMPLETED,131__RQ_LOCAL_OK,132__RQ_LOCAL_ABORTED,133134/* 87654135* 00000: no network possible136* 00001: to be send137* 00011: to be send, on worker queue138* 00101: sent, expecting recv_ack (B) or write_ack (C)139* 11101: sent,140* recv_ack (B) or implicit "ack" (A),141* still waiting for the barrier ack.142* master_bio may already be completed and invalidated.143* 11100: write acked (C),144* data received (for remote read, any protocol)145* or finally the barrier ack has arrived (B,A)...146* request can be freed147* 01100: neg-acked (write, protocol C)148* or neg-d-acked (read, any protocol)149* or killed from the transfer log150* during cleanup after connection loss151* request can be freed152* 01000: canceled or send failed...153* request can be freed154*/155156/* if "SENT" is not set, yet, this can still fail or be canceled.157* if "SENT" is set already, we still wait for an Ack packet.158* when cleared, the master_bio may be completed.159* in (B,A) the request object may still linger on the transaction log160* until the corresponding barrier ack comes in */161__RQ_NET_PENDING,162163/* If it is QUEUED, and it is a WRITE, it is also registered in the164* transfer log. Currently we need this flag to avoid conflicts between165* worker canceling the request and tl_clear_barrier killing it from166* transfer log. We should restructure the code so this conflict does167* no longer occur. */168__RQ_NET_QUEUED,169170/* well, actually only "handed over to the network stack".171*172* TODO can potentially be dropped because of the similar meaning173* of RQ_NET_SENT and ~RQ_NET_QUEUED.174* however it is not exactly the same. before we drop it175* we must ensure that we can tell a request with network part176* from a request without, regardless of what happens to it. */177__RQ_NET_SENT,178179/* when set, the request may be freed (if RQ_NET_QUEUED is clear).180* basically this means the corresponding P_BARRIER_ACK was received */181__RQ_NET_DONE,182183/* whether or not we know (C) or pretend (B,A) that the write184* was successfully written on the peer.185*/186__RQ_NET_OK,187188/* peer called drbd_set_in_sync() for this write */189__RQ_NET_SIS,190191/* keep this last, its for the RQ_NET_MASK */192__RQ_NET_MAX,193194/* Set when this is a write, clear for a read */195__RQ_WRITE,196__RQ_WSAME,197__RQ_UNMAP,198__RQ_ZEROES,199200/* Should call drbd_al_complete_io() for this request... */201__RQ_IN_ACT_LOG,202203/* This was the most recent request during some blk_finish_plug()204* or its implicit from-schedule equivalent.205* We may use it as hint to send a P_UNPLUG_REMOTE */206__RQ_UNPLUG,207208/* The peer has sent a retry ACK */209__RQ_POSTPONED,210211/* would have been completed,212* but was not, because of drbd_suspended() */213__RQ_COMPLETION_SUSP,214215/* We expect a receive ACK (wire proto B) */216__RQ_EXP_RECEIVE_ACK,217218/* We expect a write ACK (wite proto C) */219__RQ_EXP_WRITE_ACK,220221/* waiting for a barrier ack, did an extra kref_get */222__RQ_EXP_BARR_ACK,223};224225#define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)226#define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)227#define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)228#define RQ_LOCAL_ABORTED (1UL << __RQ_LOCAL_ABORTED)229230#define RQ_LOCAL_MASK ((RQ_LOCAL_ABORTED << 1)-1)231232#define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)233#define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)234#define RQ_NET_SENT (1UL << __RQ_NET_SENT)235#define RQ_NET_DONE (1UL << __RQ_NET_DONE)236#define RQ_NET_OK (1UL << __RQ_NET_OK)237#define RQ_NET_SIS (1UL << __RQ_NET_SIS)238239#define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)240241#define RQ_WRITE (1UL << __RQ_WRITE)242#define RQ_WSAME (1UL << __RQ_WSAME)243#define RQ_UNMAP (1UL << __RQ_UNMAP)244#define RQ_ZEROES (1UL << __RQ_ZEROES)245#define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)246#define RQ_UNPLUG (1UL << __RQ_UNPLUG)247#define RQ_POSTPONED (1UL << __RQ_POSTPONED)248#define RQ_COMPLETION_SUSP (1UL << __RQ_COMPLETION_SUSP)249#define RQ_EXP_RECEIVE_ACK (1UL << __RQ_EXP_RECEIVE_ACK)250#define RQ_EXP_WRITE_ACK (1UL << __RQ_EXP_WRITE_ACK)251#define RQ_EXP_BARR_ACK (1UL << __RQ_EXP_BARR_ACK)252253/* For waking up the frozen transfer log mod_req() has to return if the request254should be counted in the epoch object*/255#define MR_WRITE 1256#define MR_READ 2257258/* Short lived temporary struct on the stack.259* We could squirrel the error to be returned into260* bio->bi_iter.bi_size, or similar. But that would be too ugly. */261struct bio_and_error {262struct bio *bio;263int error;264};265266extern void start_new_tl_epoch(struct drbd_connection *connection);267extern void drbd_req_destroy(struct kref *kref);268extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,269struct drbd_peer_device *peer_device,270struct bio_and_error *m);271extern void complete_master_bio(struct drbd_device *device,272struct bio_and_error *m);273extern void request_timer_fn(struct timer_list *t);274extern void tl_restart(struct drbd_connection *connection, enum drbd_req_event what);275extern void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what);276extern void tl_abort_disk_io(struct drbd_device *device);277278/* this is in drbd_main.c */279extern void drbd_restart_request(struct drbd_request *req);280281/* use this if you don't want to deal with calling complete_master_bio()282* outside the spinlock, e.g. when walking some list on cleanup. */283static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what,284struct drbd_peer_device *peer_device)285{286struct drbd_device *device = req->device;287struct bio_and_error m;288int rv;289290/* __req_mod possibly frees req, do not touch req after that! */291rv = __req_mod(req, what, peer_device, &m);292if (m.bio)293complete_master_bio(device, &m);294295return rv;296}297298/* completion of master bio is outside of our spinlock.299* We still may or may not be inside some irqs disabled section300* of the lower level driver completion callback, so we need to301* spin_lock_irqsave here. */302static inline int req_mod(struct drbd_request *req,303enum drbd_req_event what,304struct drbd_peer_device *peer_device)305{306unsigned long flags;307struct drbd_device *device = req->device;308struct bio_and_error m;309int rv;310311spin_lock_irqsave(&device->resource->req_lock, flags);312rv = __req_mod(req, what, peer_device, &m);313spin_unlock_irqrestore(&device->resource->req_lock, flags);314315if (m.bio)316complete_master_bio(device, &m);317318return rv;319}320321extern bool drbd_should_do_remote(union drbd_dev_state);322323#endif324325326