/*1drbd_req.h23This file is part of DRBD by Philipp Reisner and Lars Ellenberg.45Copyright (C) 2006-2008, LINBIT Information Technologies GmbH.6Copyright (C) 2006-2008, Lars Ellenberg <[email protected]>.7Copyright (C) 2006-2008, Philipp Reisner <[email protected]>.89DRBD is free software; you can redistribute it and/or modify10it under the terms of the GNU General Public License as published by11the Free Software Foundation; either version 2, or (at your option)12any later version.1314DRBD is distributed in the hope that it will be useful,15but WITHOUT ANY WARRANTY; without even the implied warranty of16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17GNU General Public License for more details.1819You should have received a copy of the GNU General Public License20along with drbd; see the file COPYING. If not, write to21the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.22*/2324#ifndef _DRBD_REQ_H25#define _DRBD_REQ_H2627#include <linux/module.h>2829#include <linux/slab.h>30#include <linux/drbd.h>31#include "drbd_int.h"32#include "drbd_wrappers.h"3334/* The request callbacks will be called in irq context by the IDE drivers,35and in Softirqs/Tasklets/BH context by the SCSI drivers,36and by the receiver and worker in kernel-thread context.37Try to get the locking right :) */3839/*40* Objects of type struct drbd_request do only exist on a R_PRIMARY node, and are41* associated with IO requests originating from the block layer above us.42*43* There are quite a few things that may happen to a drbd request44* during its lifetime.45*46* It will be created.47* It will be marked with the intention to be48* submitted to local disk and/or49* send via the network.50*51* It has to be placed on the transfer log and other housekeeping lists,52* In case we have a network connection.53*54* It may be identified as a concurrent (write) request55* and be handled accordingly.56*57* It may me handed over to the local disk subsystem.58* It may be completed by the local disk subsystem,59* either successfully or with io-error.60* In case it is a READ request, and it failed locally,61* it may be retried remotely.62*63* It may be queued for sending.64* It may be handed over to the network stack,65* which may fail.66* It may be acknowledged by the "peer" according to the wire_protocol in use.67* this may be a negative ack.68* It may receive a faked ack when the network connection is lost and the69* transfer log is cleaned up.70* Sending may be canceled due to network connection loss.71* When it finally has outlived its time,72* corresponding dirty bits in the resync-bitmap may be cleared or set,73* it will be destroyed,74* and completion will be signalled to the originator,75* with or without "success".76*/7778enum drbd_req_event {79created,80to_be_send,81to_be_submitted,8283/* XXX yes, now I am inconsistent...84* these are not "events" but "actions"85* oh, well... */86queue_for_net_write,87queue_for_net_read,88queue_for_send_oos,8990send_canceled,91send_failed,92handed_over_to_network,93oos_handed_to_network,94connection_lost_while_pending,95read_retry_remote_canceled,96recv_acked_by_peer,97write_acked_by_peer,98write_acked_by_peer_and_sis, /* and set_in_sync */99conflict_discarded_by_peer,100neg_acked,101barrier_acked, /* in protocol A and B */102data_received, /* (remote read) */103104read_completed_with_error,105read_ahead_completed_with_error,106write_completed_with_error,107completed_ok,108resend,109fail_frozen_disk_io,110restart_frozen_disk_io,111nothing, /* for tracing only */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/* 210121* 000: no local possible122* 001: to be submitted123* UNUSED, we could map: 011: submitted, completion still pending124* 110: completed ok125* 010: completed with error126*/127__RQ_LOCAL_PENDING,128__RQ_LOCAL_COMPLETED,129__RQ_LOCAL_OK,130131/* 76543132* 00000: no network possible133* 00001: to be send134* 00011: to be send, on worker queue135* 00101: sent, expecting recv_ack (B) or write_ack (C)136* 11101: sent,137* recv_ack (B) or implicit "ack" (A),138* still waiting for the barrier ack.139* master_bio may already be completed and invalidated.140* 11100: write_acked (C),141* data_received (for remote read, any protocol)142* or finally the barrier ack has arrived (B,A)...143* request can be freed144* 01100: neg-acked (write, protocol C)145* or neg-d-acked (read, any protocol)146* or killed from the transfer log147* during cleanup after connection loss148* request can be freed149* 01000: canceled or send failed...150* request can be freed151*/152153/* if "SENT" is not set, yet, this can still fail or be canceled.154* if "SENT" is set already, we still wait for an Ack packet.155* when cleared, the master_bio may be completed.156* in (B,A) the request object may still linger on the transaction log157* until the corresponding barrier ack comes in */158__RQ_NET_PENDING,159160/* If it is QUEUED, and it is a WRITE, it is also registered in the161* transfer log. Currently we need this flag to avoid conflicts between162* worker canceling the request and tl_clear_barrier killing it from163* transfer log. We should restructure the code so this conflict does164* no longer occur. */165__RQ_NET_QUEUED,166167/* well, actually only "handed over to the network stack".168*169* TODO can potentially be dropped because of the similar meaning170* of RQ_NET_SENT and ~RQ_NET_QUEUED.171* however it is not exactly the same. before we drop it172* we must ensure that we can tell a request with network part173* from a request without, regardless of what happens to it. */174__RQ_NET_SENT,175176/* when set, the request may be freed (if RQ_NET_QUEUED is clear).177* basically this means the corresponding P_BARRIER_ACK was received */178__RQ_NET_DONE,179180/* whether or not we know (C) or pretend (B,A) that the write181* was successfully written on the peer.182*/183__RQ_NET_OK,184185/* peer called drbd_set_in_sync() for this write */186__RQ_NET_SIS,187188/* keep this last, its for the RQ_NET_MASK */189__RQ_NET_MAX,190191/* Set when this is a write, clear for a read */192__RQ_WRITE,193194/* Should call drbd_al_complete_io() for this request... */195__RQ_IN_ACT_LOG,196};197198#define RQ_LOCAL_PENDING (1UL << __RQ_LOCAL_PENDING)199#define RQ_LOCAL_COMPLETED (1UL << __RQ_LOCAL_COMPLETED)200#define RQ_LOCAL_OK (1UL << __RQ_LOCAL_OK)201202#define RQ_LOCAL_MASK ((RQ_LOCAL_OK << 1)-1) /* 0x07 */203204#define RQ_NET_PENDING (1UL << __RQ_NET_PENDING)205#define RQ_NET_QUEUED (1UL << __RQ_NET_QUEUED)206#define RQ_NET_SENT (1UL << __RQ_NET_SENT)207#define RQ_NET_DONE (1UL << __RQ_NET_DONE)208#define RQ_NET_OK (1UL << __RQ_NET_OK)209#define RQ_NET_SIS (1UL << __RQ_NET_SIS)210211/* 0x1f8 */212#define RQ_NET_MASK (((1UL << __RQ_NET_MAX)-1) & ~RQ_LOCAL_MASK)213214#define RQ_WRITE (1UL << __RQ_WRITE)215#define RQ_IN_ACT_LOG (1UL << __RQ_IN_ACT_LOG)216217/* For waking up the frozen transfer log mod_req() has to return if the request218should be counted in the epoch object*/219#define MR_WRITE_SHIFT 0220#define MR_WRITE (1 << MR_WRITE_SHIFT)221#define MR_READ_SHIFT 1222#define MR_READ (1 << MR_READ_SHIFT)223224/* epoch entries */225static inline226struct hlist_head *ee_hash_slot(struct drbd_conf *mdev, sector_t sector)227{228BUG_ON(mdev->ee_hash_s == 0);229return mdev->ee_hash +230((unsigned int)(sector>>HT_SHIFT) % mdev->ee_hash_s);231}232233/* transfer log (drbd_request objects) */234static inline235struct hlist_head *tl_hash_slot(struct drbd_conf *mdev, sector_t sector)236{237BUG_ON(mdev->tl_hash_s == 0);238return mdev->tl_hash +239((unsigned int)(sector>>HT_SHIFT) % mdev->tl_hash_s);240}241242/* application reads (drbd_request objects) */243static struct hlist_head *ar_hash_slot(struct drbd_conf *mdev, sector_t sector)244{245return mdev->app_reads_hash246+ ((unsigned int)(sector) % APP_R_HSIZE);247}248249/* when we receive the answer for a read request,250* verify that we actually know about it */251static inline struct drbd_request *_ar_id_to_req(struct drbd_conf *mdev,252u64 id, sector_t sector)253{254struct hlist_head *slot = ar_hash_slot(mdev, sector);255struct hlist_node *n;256struct drbd_request *req;257258hlist_for_each_entry(req, n, slot, collision) {259if ((unsigned long)req == (unsigned long)id) {260D_ASSERT(req->sector == sector);261return req;262}263}264return NULL;265}266267static inline void drbd_req_make_private_bio(struct drbd_request *req, struct bio *bio_src)268{269struct bio *bio;270bio = bio_clone(bio_src, GFP_NOIO); /* XXX cannot fail?? */271272req->private_bio = bio;273274bio->bi_private = req;275bio->bi_end_io = drbd_endio_pri;276bio->bi_next = NULL;277}278279static inline struct drbd_request *drbd_req_new(struct drbd_conf *mdev,280struct bio *bio_src)281{282struct drbd_request *req =283mempool_alloc(drbd_request_mempool, GFP_NOIO);284if (likely(req)) {285drbd_req_make_private_bio(req, bio_src);286287req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;288req->mdev = mdev;289req->master_bio = bio_src;290req->epoch = 0;291req->sector = bio_src->bi_sector;292req->size = bio_src->bi_size;293INIT_HLIST_NODE(&req->collision);294INIT_LIST_HEAD(&req->tl_requests);295INIT_LIST_HEAD(&req->w.list);296}297return req;298}299300static inline void drbd_req_free(struct drbd_request *req)301{302mempool_free(req, drbd_request_mempool);303}304305static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)306{307return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));308}309310/* Short lived temporary struct on the stack.311* We could squirrel the error to be returned into312* bio->bi_size, or similar. But that would be too ugly. */313struct bio_and_error {314struct bio *bio;315int error;316};317318extern void _req_may_be_done(struct drbd_request *req,319struct bio_and_error *m);320extern int __req_mod(struct drbd_request *req, enum drbd_req_event what,321struct bio_and_error *m);322extern void complete_master_bio(struct drbd_conf *mdev,323struct bio_and_error *m);324extern void request_timer_fn(unsigned long data);325extern void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what);326327/* use this if you don't want to deal with calling complete_master_bio()328* outside the spinlock, e.g. when walking some list on cleanup. */329static inline int _req_mod(struct drbd_request *req, enum drbd_req_event what)330{331struct drbd_conf *mdev = req->mdev;332struct bio_and_error m;333int rv;334335/* __req_mod possibly frees req, do not touch req after that! */336rv = __req_mod(req, what, &m);337if (m.bio)338complete_master_bio(mdev, &m);339340return rv;341}342343/* completion of master bio is outside of our spinlock.344* We still may or may not be inside some irqs disabled section345* of the lower level driver completion callback, so we need to346* spin_lock_irqsave here. */347static inline int req_mod(struct drbd_request *req,348enum drbd_req_event what)349{350unsigned long flags;351struct drbd_conf *mdev = req->mdev;352struct bio_and_error m;353int rv;354355spin_lock_irqsave(&mdev->req_lock, flags);356rv = __req_mod(req, what, &m);357spin_unlock_irqrestore(&mdev->req_lock, flags);358359if (m.bio)360complete_master_bio(mdev, &m);361362return rv;363}364365static inline bool drbd_should_do_remote(union drbd_state s)366{367return s.pdsk == D_UP_TO_DATE ||368(s.pdsk >= D_INCONSISTENT &&369s.conn >= C_WF_BITMAP_T &&370s.conn < C_AHEAD);371/* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.372That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*373states. */374}375static inline bool drbd_should_send_oos(union drbd_state s)376{377return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;378/* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary379since we enter state C_AHEAD only if proto >= 96 */380}381382#endif383384385