Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)    drbd.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)    Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)    Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)    Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)    Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)    from Logicworks, Inc. for making SDP replication support possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/drbd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <asm/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/mm_inline.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define __KERNEL_SYSCALLS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/drbd_limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "drbd_int.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "drbd_protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include "drbd_vli.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include "drbd_debugfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static DEFINE_MUTEX(drbd_main_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static int drbd_open(struct block_device *bdev, fmode_t mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) static void drbd_release(struct gendisk *gd, fmode_t mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static void md_sync_timer_fn(struct timer_list *t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static int w_bitmap_io(struct drbd_work *w, int unused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	      "Lars Ellenberg <lars@linbit.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) MODULE_VERSION(REL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /* thanks to these macros, if compiled into the kernel (not-module),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * these become boot parameters (e.g., drbd.minor_count) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #ifdef CONFIG_DRBD_FAULT_INJECTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) int drbd_enable_faults;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) int drbd_fault_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) static int drbd_fault_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static int drbd_fault_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /* bitmap of enabled faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) module_param_named(enable_faults, drbd_enable_faults, int, 0664);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) /* fault rate % value - applies to all enabled faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) module_param_named(fault_rate, drbd_fault_rate, int, 0664);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* count of faults inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) module_param_named(fault_count, drbd_fault_count, int, 0664);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) /* bitmap of devices to insert faults on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) module_param_named(fault_devs, drbd_fault_devs, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) /* module parameters we can keep static */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) static bool drbd_allow_oos; /* allow_open_on_secondary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static bool drbd_disable_sendpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) MODULE_PARM_DESC(allow_oos, "DONT USE!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) module_param_named(allow_oos, drbd_allow_oos, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) module_param_named(disable_sendpage, drbd_disable_sendpage, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) /* module parameters we share */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) int drbd_proc_details; /* Detail level in proc drbd*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) module_param_named(proc_details, drbd_proc_details, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) /* module parameters shared with defaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) unsigned int drbd_minor_count = DRBD_MINOR_COUNT_DEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) /* Module parameter for setting the user mode helper program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * to run. Default is /sbin/drbdadm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) char drbd_usermode_helper[80] = "/sbin/drbdadm";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) module_param_named(minor_count, drbd_minor_count, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) module_param_string(usermode_helper, drbd_usermode_helper, sizeof(drbd_usermode_helper), 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) /* in 2.6.x, our device mapping and config info contains our virtual gendisks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * as member "struct gendisk *vdisk;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) struct idr drbd_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) struct list_head drbd_resources;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) struct mutex resources_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) struct kmem_cache *drbd_request_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) struct kmem_cache *drbd_ee_cache;	/* peer requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) struct kmem_cache *drbd_bm_ext_cache;	/* bitmap extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) struct kmem_cache *drbd_al_ext_cache;	/* activity log extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) mempool_t drbd_request_mempool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) mempool_t drbd_ee_mempool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) mempool_t drbd_md_io_page_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) struct bio_set drbd_md_io_bio_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) struct bio_set drbd_io_bio_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) /* I do not use a standard mempool, because:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)    1) I want to hand out the pre-allocated objects first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)    2) I want to be able to interrupt sleeping allocation with a signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)    Note: This is a single linked list, the next pointer is the private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	 member of struct page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) struct page *drbd_pp_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) spinlock_t   drbd_pp_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) int          drbd_pp_vacant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) wait_queue_head_t drbd_pp_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) static const struct block_device_operations drbd_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	.submit_bio	= drbd_submit_bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	.open		= drbd_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	.release	= drbd_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) struct bio *bio_alloc_drbd(gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (!bioset_initialized(&drbd_md_io_bio_set))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		return bio_alloc(gfp_mask, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	bio = bio_alloc_bioset(gfp_mask, 1, &drbd_md_io_bio_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	if (!bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	return bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #ifdef __CHECKER__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) /* When checking with sparse, and this is an inline function, sparse will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)    give tons of false positives. When this is a real functions sparse works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	int io_allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	atomic_inc(&device->local_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	io_allowed = (device->state.disk >= mins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (!io_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		if (atomic_dec_and_test(&device->local_cnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			wake_up(&device->misc_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	return io_allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * @connection:	DRBD connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * @barrier_nr:	Expected identifier of the DRBD write barrier packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * @set_size:	Expected number of requests before that barrier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * In case the passed barrier_nr or set_size does not match the oldest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * epoch of not yet barrier-acked requests, this function will cause a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * termination of the connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) void tl_release(struct drbd_connection *connection, unsigned int barrier_nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		unsigned int set_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct drbd_request *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	struct drbd_request *req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	int expect_epoch = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	int expect_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	spin_lock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	/* find oldest not yet barrier-acked write request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	 * count writes in its epoch. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	list_for_each_entry(r, &connection->transfer_log, tl_requests) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		const unsigned s = r->rq_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			if (!(s & RQ_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 			if (!(s & RQ_NET_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			if (s & RQ_NET_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			req = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			expect_epoch = req->epoch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			expect_size ++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			if (r->epoch != expect_epoch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			if (!(s & RQ_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			/* if (s & RQ_DONE): not expected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			/* if (!(s & RQ_NET_MASK)): not expected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			expect_size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	/* first some paranoia code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	if (req == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		drbd_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			 barrier_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (expect_epoch != barrier_nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		drbd_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			 barrier_nr, expect_epoch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (expect_size != set_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		drbd_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			 barrier_nr, set_size, expect_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/* Clean up list of requests processed during current epoch. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	/* this extra list walk restart is paranoia,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 * to catch requests being barrier-acked "unexpectedly".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * It usually should find the same req again, or some READ preceding it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	list_for_each_entry(req, &connection->transfer_log, tl_requests)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		if (req->epoch == expect_epoch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		if (req->epoch != expect_epoch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		_req_mod(req, BARRIER_ACKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	spin_unlock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	spin_unlock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  * _tl_restart() - Walks the transfer log, and applies an action to all requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * @connection:	DRBD connection to operate on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * @what:       The action/event to perform with all request objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * RESTART_FROZEN_DISK_IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) /* must hold resource->req_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct drbd_request *req, *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		_req_mod(req, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) void tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	spin_lock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	_tl_restart(connection, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	spin_unlock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  * This is called after the connection to the peer was lost. The storage covered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  * by the requests on the transfer gets marked as our of sync. Called from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  * receiver thread and the worker thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) void tl_clear(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) void tl_abort_disk_io(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	struct drbd_connection *connection = first_peer_device(device)->connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	struct drbd_request *req, *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	spin_lock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		if (!(req->rq_state & RQ_LOCAL_PENDING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if (req->device != device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		_req_mod(req, ABORT_DISK_IO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	spin_unlock_irq(&connection->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static int drbd_thread_setup(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct drbd_thread *thi = (struct drbd_thread *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	struct drbd_resource *resource = thi->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		 thi->name[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		 resource->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	allow_kernel_signal(DRBD_SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	allow_kernel_signal(SIGXCPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	retval = thi->function(thi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	spin_lock_irqsave(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	/* if the receiver has been "EXITING", the last thing it did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * was set the conn state to "StandAlone",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 * and receiver thread will be "started".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	 * drbd_thread_start needs to set "RESTARTING" in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	 * t_state check and assignment needs to be within the same spinlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	 * so either thread_start sees EXITING, and can remap to RESTARTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	 * or thread_start see NONE, and can proceed as normal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (thi->t_state == RESTARTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		drbd_info(resource, "Restarting %s thread\n", thi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		thi->t_state = RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	thi->task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	thi->t_state = NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	complete_all(&thi->stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	drbd_info(resource, "Terminating %s\n", current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	/* Release mod reference taken when thread was started */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (thi->connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		kref_put(&thi->connection->kref, drbd_destroy_connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static void drbd_thread_init(struct drbd_resource *resource, struct drbd_thread *thi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			     int (*func) (struct drbd_thread *), const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	spin_lock_init(&thi->t_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	thi->task    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	thi->t_state = NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	thi->function = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	thi->resource = resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	thi->connection = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	thi->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) int drbd_thread_start(struct drbd_thread *thi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	struct drbd_resource *resource = thi->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	struct task_struct *nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	/* is used from state engine doing drbd_thread_stop_nowait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	 * while holding the req lock irqsave */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	spin_lock_irqsave(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	switch (thi->t_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	case NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		drbd_info(resource, "Starting %s thread (from %s [%d])\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 			 thi->name, current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		/* Get ref on module for thread - this is released when thread exits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		if (!try_module_get(THIS_MODULE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			drbd_err(resource, "Failed to get module reference in drbd_thread_start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		kref_get(&resource->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		if (thi->connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 			kref_get(&thi->connection->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		init_completion(&thi->stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		thi->reset_cpu_mask = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		thi->t_state = RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		nt = kthread_create(drbd_thread_setup, (void *) thi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 				    "drbd_%c_%s", thi->name[0], thi->resource->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		if (IS_ERR(nt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			drbd_err(resource, "Couldn't start thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			if (thi->connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 				kref_put(&thi->connection->kref, drbd_destroy_connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		spin_lock_irqsave(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		thi->task = nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		thi->t_state = RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		wake_up_process(nt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	case EXITING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		thi->t_state = RESTARTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		drbd_info(resource, "Restarting %s thread (from %s [%d])\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 				thi->name, current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	case RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	case RESTARTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	/* may be called from state engine, holding the req lock irqsave */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	spin_lock_irqsave(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (thi->t_state == NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		if (restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			drbd_thread_start(thi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	if (thi->t_state != ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		if (thi->task == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		thi->t_state = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		init_completion(&thi->stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		if (thi->task != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			send_sig(DRBD_SIGKILL, thi->task, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	spin_unlock_irqrestore(&thi->t_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		wait_for_completion(&thi->stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) int conn_lowest_minor(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct drbd_peer_device *peer_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	int vnr = 0, minor = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	peer_device = idr_get_next(&connection->peer_devices, &vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		minor = device_to_minor(peer_device->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  * Forces all threads of a resource onto the same CPU. This is beneficial for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  * DRBD's performance. May be overwritten by user's configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) static void drbd_calc_cpu_mask(cpumask_var_t *cpu_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	unsigned int *resources_per_cpu, min_index = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	resources_per_cpu = kcalloc(nr_cpu_ids, sizeof(*resources_per_cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (resources_per_cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		unsigned int cpu, min = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		for_each_resource_rcu(resource, &drbd_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			for_each_cpu(cpu, resource->cpu_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 				resources_per_cpu[cpu]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			if (resources_per_cpu[cpu] < min) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 				min = resources_per_cpu[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 				min_index = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		kfree(resources_per_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (min_index == ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		cpumask_setall(*cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	cpumask_set_cpu(min_index, *cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  * @thi:	drbd_thread object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538)  * prematurely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) void drbd_thread_current_set_cpu(struct drbd_thread *thi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	struct drbd_resource *resource = thi->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	struct task_struct *p = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (!thi->reset_cpu_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	thi->reset_cpu_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	set_cpus_allowed_ptr(p, resource->cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) #define drbd_calc_cpu_mask(A) ({})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  * drbd_header_size  -  size of a packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557)  * The header size is a multiple of 8, so any payload following the header is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558)  * word aligned on 64-bit architectures.  (The bitmap send and receive code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  * relies on this.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) unsigned int drbd_header_size(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	if (connection->agreed_pro_version >= 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		return sizeof(struct p_header100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		BUILD_BUG_ON(sizeof(struct p_header80) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			     sizeof(struct p_header95));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		return sizeof(struct p_header80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	h->magic   = cpu_to_be32(DRBD_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	h->command = cpu_to_be16(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	h->length  = cpu_to_be16(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return sizeof(struct p_header80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	h->magic   = cpu_to_be16(DRBD_MAGIC_BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	h->command = cpu_to_be16(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	h->length = cpu_to_be32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return sizeof(struct p_header95);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				      int size, int vnr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	h->magic = cpu_to_be32(DRBD_MAGIC_100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	h->volume = cpu_to_be16(vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	h->command = cpu_to_be16(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	h->length = cpu_to_be32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	h->pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	return sizeof(struct p_header100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) static unsigned int prepare_header(struct drbd_connection *connection, int vnr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				   void *buffer, enum drbd_packet cmd, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (connection->agreed_pro_version >= 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		return prepare_header100(buffer, cmd, size, vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	else if (connection->agreed_pro_version >= 95 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		 size > DRBD_MAX_SIZE_H80_PACKET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		return prepare_header95(buffer, cmd, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return prepare_header80(buffer, cmd, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) static void *__conn_prepare_command(struct drbd_connection *connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 				    struct drbd_socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (!sock->socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	return sock->sbuf + drbd_header_size(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	mutex_lock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	p = __conn_prepare_command(connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		mutex_unlock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) void *drbd_prepare_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	return conn_prepare_command(peer_device->connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) static int __send_command(struct drbd_connection *connection, int vnr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			  struct drbd_socket *sock, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			  unsigned int header_size, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			  unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	int msg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	 * Called with @data == NULL and the size of the data blocks in @size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	 * for commands that send data blocks.  For those commands, omit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 * MSG_MORE flag: this will increase the likelihood that data blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * which are page aligned on the sender will end up page aligned on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 * receiver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	msg_flags = data ? MSG_MORE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	header_size += prepare_header(connection, vnr, sock->sbuf, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				      header_size + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			    msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (data && !err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		err = drbd_send_all(connection, sock->socket, data, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	/* DRBD protocol "pings" are latency critical.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	 * This is supposed to trigger tcp_push_pending_frames() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	if (!err && (cmd == P_PING || cmd == P_PING_ACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		tcp_sock_set_nodelay(sock->socket->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			       enum drbd_packet cmd, unsigned int header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			       void *data, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	return __send_command(connection, 0, sock, cmd, header_size, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		      enum drbd_packet cmd, unsigned int header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		      void *data, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	err = __conn_send_command(connection, sock, cmd, header_size, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	mutex_unlock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) int drbd_send_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		      enum drbd_packet cmd, unsigned int header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		      void *data, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	err = __send_command(peer_device->connection, peer_device->device->vnr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			     sock, cmd, header_size, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	mutex_unlock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) int drbd_send_ping(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	sock = &connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (!conn_prepare_command(connection, sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	return conn_send_command(connection, sock, P_PING, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) int drbd_send_ping_ack(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	sock = &connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (!conn_prepare_command(connection, sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) int drbd_send_sync_param(struct drbd_peer_device *peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct p_rs_param_95 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	const int apv = peer_device->connection->agreed_pro_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	enum drbd_packet cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct net_conf *nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	struct disk_conf *dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	nc = rcu_dereference(peer_device->connection->net_conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	size = apv <= 87 ? sizeof(struct p_rs_param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		: apv == 88 ? sizeof(struct p_rs_param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			+ strlen(nc->verify_alg) + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		: apv <= 94 ? sizeof(struct p_rs_param_89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		: /* apv >= 95 */ sizeof(struct p_rs_param_95);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	/* initialize verify_alg and csums_alg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (get_ldev(peer_device->device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		dc = rcu_dereference(peer_device->device->ldev->disk_conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		p->resync_rate = cpu_to_be32(dc->resync_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		p->c_delay_target = cpu_to_be32(dc->c_delay_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		p->c_fill_target = cpu_to_be32(dc->c_fill_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		p->c_max_rate = cpu_to_be32(dc->c_max_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		put_ldev(peer_device->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	if (apv >= 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		strcpy(p->verify_alg, nc->verify_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	if (apv >= 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		strcpy(p->csums_alg, nc->csums_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	return drbd_send_command(peer_device, sock, cmd, size, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	struct p_protocol *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	struct net_conf *nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	int size, cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	sock = &connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	p = __conn_prepare_command(connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	nc = rcu_dereference(connection->net_conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (nc->tentative && connection->agreed_pro_version < 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		drbd_err(connection, "--dry-run is not supported by peer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	size = sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if (connection->agreed_pro_version >= 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		size += strlen(nc->integrity_alg) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	p->protocol      = cpu_to_be32(nc->wire_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	p->after_sb_0p   = cpu_to_be32(nc->after_sb_0p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	p->after_sb_1p   = cpu_to_be32(nc->after_sb_1p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	p->after_sb_2p   = cpu_to_be32(nc->after_sb_2p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	p->two_primaries = cpu_to_be32(nc->two_primaries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	cf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (nc->discard_my_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		cf |= CF_DISCARD_MY_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (nc->tentative)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		cf |= CF_DRY_RUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	p->conn_flags    = cpu_to_be32(cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (connection->agreed_pro_version >= 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		strcpy(p->integrity_alg, nc->integrity_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	return __conn_send_command(connection, sock, cmd, size, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) int drbd_send_protocol(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	mutex_lock(&connection->data.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	err = __drbd_send_protocol(connection, P_PROTOCOL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	mutex_unlock(&connection->data.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) static int _drbd_send_uuids(struct drbd_peer_device *peer_device, u64 uuid_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	struct p_uuids *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (!get_ldev_if_state(device, D_NEGOTIATING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	spin_lock_irq(&device->ldev->md.uuid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	for (i = UI_CURRENT; i < UI_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	spin_unlock_irq(&device->ldev->md.uuid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	device->comm_bm_set = drbd_bm_total_weight(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	uuid_flags |= rcu_dereference(peer_device->connection->net_conf)->discard_my_data ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	return drbd_send_command(peer_device, sock, P_UUIDS, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) int drbd_send_uuids(struct drbd_peer_device *peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	return _drbd_send_uuids(peer_device, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	return _drbd_send_uuids(peer_device, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) void drbd_print_uuids(struct drbd_device *device, const char *text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	if (get_ldev_if_state(device, D_NEGOTIATING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		u64 *uuid = device->ldev->md.uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		     text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		     (unsigned long long)uuid[UI_CURRENT],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		     (unsigned long long)uuid[UI_BITMAP],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		     (unsigned long long)uuid[UI_HISTORY_START],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		     (unsigned long long)uuid[UI_HISTORY_END]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		drbd_info(device, "%s effective data uuid: %016llX\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 				text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				(unsigned long long)device->ed_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	struct p_rs_uuid *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	u64 uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	D_ASSERT(device, device->state.disk == D_UP_TO_DATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	uuid = device->ldev->md.uuid[UI_BITMAP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (uuid && uuid != UUID_JUST_CREATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		uuid = uuid + UUID_NEW_BM_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		get_random_bytes(&uuid, sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	drbd_uuid_set(device, UI_BITMAP, uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	drbd_print_uuids(device, "updated sync UUID");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		p->uuid = cpu_to_be64(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		drbd_send_command(peer_device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) /* communicated if (agreed_features & DRBD_FF_WSAME) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) assign_p_sizes_qlim(struct drbd_device *device, struct p_sizes *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 					struct request_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		p->qlim->physical_block_size = cpu_to_be32(queue_physical_block_size(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		p->qlim->logical_block_size = cpu_to_be32(queue_logical_block_size(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		p->qlim->alignment_offset = cpu_to_be32(queue_alignment_offset(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		p->qlim->io_min = cpu_to_be32(queue_io_min(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		p->qlim->io_opt = cpu_to_be32(queue_io_opt(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		p->qlim->discard_enabled = blk_queue_discard(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		p->qlim->write_same_capable = !!q->limits.max_write_same_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		q = device->rq_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		p->qlim->physical_block_size = cpu_to_be32(queue_physical_block_size(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		p->qlim->logical_block_size = cpu_to_be32(queue_logical_block_size(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		p->qlim->alignment_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		p->qlim->io_min = cpu_to_be32(queue_io_min(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		p->qlim->io_opt = cpu_to_be32(queue_io_opt(q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		p->qlim->discard_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		p->qlim->write_same_capable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) int drbd_send_sizes(struct drbd_peer_device *peer_device, int trigger_reply, enum dds_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	struct p_sizes *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	sector_t d_size, u_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	int q_order_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	unsigned int max_bio_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	unsigned int packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	packet_size = sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if (peer_device->connection->agreed_features & DRBD_FF_WSAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		packet_size += sizeof(p->qlim[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	memset(p, 0, packet_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if (get_ldev_if_state(device, D_NEGOTIATING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		struct request_queue *q = bdev_get_queue(device->ldev->backing_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		d_size = drbd_get_max_capacity(device->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		q_order_type = drbd_queue_order_type(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		max_bio_size = queue_max_hw_sectors(q) << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		assign_p_sizes_qlim(device, p, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		d_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		u_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		q_order_type = QUEUE_ORDERED_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		assign_p_sizes_qlim(device, p, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (peer_device->connection->agreed_pro_version <= 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	else if (peer_device->connection->agreed_pro_version < 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	p->d_size = cpu_to_be64(d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	p->u_size = cpu_to_be64(u_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (trigger_reply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		p->c_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		p->c_size = cpu_to_be64(get_capacity(device->vdisk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	p->max_bio_size = cpu_to_be32(max_bio_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	p->queue_order_type = cpu_to_be16(q_order_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	p->dds_flags = cpu_to_be16(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	return drbd_send_command(peer_device, sock, P_SIZES, packet_size, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * drbd_send_current_state() - Sends the drbd state to the peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * @peer_device:	DRBD peer device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) int drbd_send_current_state(struct drbd_peer_device *peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	struct p_state *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	p->state = cpu_to_be32(peer_device->device->state.i); /* Within the send mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  * drbd_send_state() - After a state change, sends the new state to the peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  * @peer_device:      DRBD peer device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  * @state:     the state to send, not necessarily the current state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)  * Each state change queues an "after_state_ch" work, which will eventually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)  * send the resulting new state to the peer. If more state changes happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)  * between queuing and processing of the after_state_ch work, we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  * want to send each intermediary state in the order it occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) int drbd_send_state(struct drbd_peer_device *peer_device, union drbd_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	struct p_state *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	p->state = cpu_to_be32(state.i); /* Within the send mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) int drbd_send_state_req(struct drbd_peer_device *peer_device, union drbd_state mask, union drbd_state val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	struct p_req_state *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	p->mask = cpu_to_be32(mask.i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	p->val = cpu_to_be32(val.i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	return drbd_send_command(peer_device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	enum drbd_packet cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	struct p_req_state *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	sock = &connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	p = conn_prepare_command(connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	p->mask = cpu_to_be32(mask.i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	p->val = cpu_to_be32(val.i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) void drbd_send_sr_reply(struct drbd_peer_device *peer_device, enum drbd_state_rv retcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	struct p_req_state_reply *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	sock = &peer_device->connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		p->retcode = cpu_to_be32(retcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		drbd_send_command(peer_device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct p_req_state_reply *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	sock = &connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	p = conn_prepare_command(connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		p->retcode = cpu_to_be32(retcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	BUG_ON(code & ~0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	p->encoding = (p->encoding & ~0xf) | code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) static void dcbp_set_start(struct p_compressed_bm *p, int set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	BUG_ON(n & ~0x7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) static int fill_bitmap_rle_bits(struct drbd_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			 struct p_compressed_bm *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			 unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			 struct bm_xfer_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	struct bitstream bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	unsigned long plain_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	unsigned long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	unsigned long rl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	unsigned len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	unsigned toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	int bits, use_rle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	/* may we use this feature? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (c->bit_offset >= c->bm_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		return 0; /* nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	/* use at most thus many bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	bitstream_init(&bs, p->code, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	memset(p->code, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	/* plain bits covered in this code string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	plain_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	/* p->encoding & 0x80 stores whether the first run length is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	 * bit offset is implicit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * start with toggle == 2 to be able to tell the first iteration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	toggle = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	/* see how much plain bits we can stuff into one packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	 * using RLE and VLI. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 				    : _drbd_bm_find_next(device, c->bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		if (tmp == -1UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			tmp = c->bm_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		rl = tmp - c->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		if (toggle == 2) { /* first iteration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			if (rl == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 				/* the first checked bit was set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 				 * store start value, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 				dcbp_set_start(p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 				/* but skip encoding of zero run length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 				toggle = !toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			dcbp_set_start(p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		/* paranoia: catch zero runlength.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		 * can only happen if bitmap is modified while we scan it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		if (rl == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			drbd_err(device, "unexpected zero runlength while encoding bitmap "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			    "t:%u bo:%lu\n", toggle, c->bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		bits = vli_encode_bits(&bs, rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		if (bits == -ENOBUFS) /* buffer full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		if (bits <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			drbd_err(device, "error while encoding bitmap: %d\n", bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		toggle = !toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		plain_bits += rl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		c->bit_offset = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	} while (c->bit_offset < c->bm_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	len = bs.cur.b - p->code + !!bs.cur.bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	if (plain_bits < (len << 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		/* incompressible with this method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		 * we need to rewind both word and bit position. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		c->bit_offset -= plain_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		bm_xfer_ctx_bit_to_word_offset(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		c->bit_offset = c->word_offset * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	/* RLE + VLI was able to compress it just fine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	 * update c->word_offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	bm_xfer_ctx_bit_to_word_offset(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	/* store pad_bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  * send_bitmap_rle_or_plain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  * Return 0 when done, 1 when another iteration is needed, and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  * code upon failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) send_bitmap_rle_or_plain(struct drbd_device *device, struct bm_xfer_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	struct drbd_socket *sock = &first_peer_device(device)->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	unsigned int header_size = drbd_header_size(first_peer_device(device)->connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	struct p_compressed_bm *p = sock->sbuf + header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	int len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	len = fill_bitmap_rle_bits(device, p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 			DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		dcbp_set_code(p, RLE_VLI_Bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		err = __send_command(first_peer_device(device)->connection, device->vnr, sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				     P_COMPRESSED_BITMAP, sizeof(*p) + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				     NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		c->packets[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		c->bytes[0] += header_size + sizeof(*p) + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		if (c->bit_offset >= c->bm_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			len = 0; /* DONE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		/* was not compressible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		 * send a buffer full of plain text bits instead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		unsigned int data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		unsigned long num_words;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		unsigned long *p = sock->sbuf + header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		num_words = min_t(size_t, data_size / sizeof(*p),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 				  c->bm_words - c->word_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		len = num_words * sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			drbd_bm_get_lel(device, c->word_offset, num_words, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_BITMAP, len, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		c->word_offset += num_words;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		c->bit_offset = c->word_offset * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		c->packets[1]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		c->bytes[1] += header_size + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		if (c->bit_offset > c->bm_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			c->bit_offset = c->bm_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			INFO_bm_xfer_stats(device, "send", c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /* See the comment at receive_bitmap() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) static int _drbd_send_bitmap(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	struct bm_xfer_ctx c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (!expect(device->bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (get_ldev(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			drbd_info(device, "Writing the whole bitmap, MDF_FullSync was set.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			drbd_bm_set_all(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			if (drbd_bm_write(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				/* write_bm did fail! Leave full sync flag set in Meta P_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 				 * but otherwise process as per normal - need to tell other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 				 * side that a full resync is required! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 				drbd_err(device, "Failed to write bitmap to disk!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				drbd_md_clear_flag(device, MDF_FULL_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 				drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	c = (struct bm_xfer_ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		.bm_bits = drbd_bm_bits(device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		.bm_words = drbd_bm_words(device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		err = send_bitmap_rle_or_plain(device, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	} while (err > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	return err == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) int drbd_send_bitmap(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct drbd_socket *sock = &first_peer_device(device)->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	mutex_lock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	if (sock->socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		err = !_drbd_send_bitmap(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	mutex_unlock(&sock->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	struct p_barrier_ack *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (connection->cstate < C_WF_REPORT_PARAMS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	sock = &connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	p = conn_prepare_command(connection, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	p->barrier = barrier_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	p->set_size = cpu_to_be32(set_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)  * _drbd_send_ack() - Sends an ack packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)  * @cmd:	Packet command code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)  * @sector:	sector, needs to be in big endian byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)  * @blksize:	size in byte, needs to be in big endian byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)  * @block_id:	Id, big endian byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) static int _drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			  u64 sector, u32 blksize, u64 block_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	struct p_block_ack *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	if (peer_device->device->state.conn < C_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	sock = &peer_device->connection->meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	p->sector = sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	p->block_id = block_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	p->blksize = blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	p->seq_num = cpu_to_be32(atomic_inc_return(&peer_device->device->packet_seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) /* dp->sector and dp->block_id already/still in network byte order,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * data_size is payload size according to dp->head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * and may need to be corrected for digest size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		      struct p_data *dp, int data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	if (peer_device->connection->peer_integrity_tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		data_size -= crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	_drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		       dp->block_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) void drbd_send_ack_rp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		      struct p_block_req *rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	_drbd_send_ack(peer_device, cmd, rp->sector, rp->blksize, rp->block_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385)  * drbd_send_ack() - Sends an ack packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)  * @device:	DRBD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)  * @cmd:	packet command code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)  * @peer_req:	peer request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) int drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		  struct drbd_peer_request *peer_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	return _drbd_send_ack(peer_device, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			      cpu_to_be64(peer_req->i.sector),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			      cpu_to_be32(peer_req->i.size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			      peer_req->block_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) /* This function misuses the block_id field to signal if the blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  * are is sync or not. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) int drbd_send_ack_ex(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		     sector_t sector, int blksize, u64 block_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	return _drbd_send_ack(peer_device, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			      cpu_to_be64(sector),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			      cpu_to_be32(blksize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 			      cpu_to_be64(block_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) int drbd_send_rs_deallocated(struct drbd_peer_device *peer_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			     struct drbd_peer_request *peer_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct p_block_desc *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	p->sector = cpu_to_be64(peer_req->i.sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	p->blksize = cpu_to_be32(peer_req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	p->pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	return drbd_send_command(peer_device, sock, P_RS_DEALLOCATED, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) int drbd_send_drequest(struct drbd_peer_device *peer_device, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		       sector_t sector, int size, u64 block_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	struct p_block_req *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	p->sector = cpu_to_be64(sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	p->block_id = block_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	p->blksize = cpu_to_be32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) int drbd_send_drequest_csum(struct drbd_peer_device *peer_device, sector_t sector, int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 			    void *digest, int digest_size, enum drbd_packet cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	struct p_block_req *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	/* FIXME: Put the digest into the preallocated socket buffer.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	p->sector = cpu_to_be64(sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	p->block_id = ID_SYNCER /* unused */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	p->blksize = cpu_to_be32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	return drbd_send_command(peer_device, sock, cmd, sizeof(*p), digest, digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) int drbd_send_ov_request(struct drbd_peer_device *peer_device, sector_t sector, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	struct p_block_req *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	p->sector = cpu_to_be64(sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	p->block_id = ID_SYNCER /* unused */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	p->blksize = cpu_to_be32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	return drbd_send_command(peer_device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) /* called on sndtimeo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  * returns false if we should retry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)  * true if we think connection is dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	int drop_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	/* long elapsed = (long)(jiffies - device->last_received); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	drop_it =   connection->meta.socket == sock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		|| !connection->ack_receiver.task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		|| get_t_state(&connection->ack_receiver) != RUNNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		|| connection->cstate < C_WF_REPORT_PARAMS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	if (drop_it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	drop_it = !--connection->ko_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (!drop_it) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		drbd_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 			 current->comm, current->pid, connection->ko_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		request_ping(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	return drop_it; /* && (device->state == R_PRIMARY) */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) static void drbd_update_congested(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	struct sock *sk = connection->data.socket->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		set_bit(NET_CONGESTED, &connection->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) /* The idea of sendpage seems to be to put some kind of reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  * to the page into the skb, and to hand it over to the NIC. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)  * this process get_page() gets called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513)  * As soon as the page was really sent over the network put_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)  * gets called by some part of the network layer. [ NIC driver? ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)  * [ get_page() / put_page() increment/decrement the count. If count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)  *   reaches 0 the page will be freed. ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)  * This works nicely with pages from FSs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)  * But this means that in protocol A we might signal IO completion too early!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)  * In order not to corrupt data during a resync we must make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)  * that we do not reuse our own buffer pages (EEs) to early, therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  * we have the net_ee list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)  * XFS seems to have problems, still, it submits pages with page_count == 0!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  * As a workaround, we disable sendpage on pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  * with page_count == 0 or PageSlab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) static int _drbd_no_send_page(struct drbd_peer_device *peer_device, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			      int offset, size_t size, unsigned msg_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	struct socket *socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	socket = peer_device->connection->data.socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	addr = kmap(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	err = drbd_send_all(peer_device->connection, socket, addr, size, msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		peer_device->device->send_cnt += size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) static int _drbd_send_page(struct drbd_peer_device *peer_device, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		    int offset, size_t size, unsigned msg_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	struct socket *socket = peer_device->connection->data.socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	int len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	int err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	/* e.g. XFS meta- & log-data is in slab pages, which have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	 * page_count of 0 and/or have PageSlab() set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 * we cannot use send_page for those, as that does get_page();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	 * put_page(); and would cause either a VM_BUG directly, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	 * __page_cache_release a page that would actually still be referenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	 * by someone, leading to some obscure delayed Oops somewhere else. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (drbd_disable_sendpage || !sendpage_ok(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		return _drbd_no_send_page(peer_device, page, offset, size, msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	msg_flags |= MSG_NOSIGNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	drbd_update_congested(peer_device->connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		int sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		sent = socket->ops->sendpage(socket, page, offset, len, msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		if (sent <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			if (sent == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				if (we_should_drop_the_connection(peer_device->connection, socket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			drbd_warn(peer_device->device, "%s: size=%d len=%d sent=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			     __func__, (int)size, len, sent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 			if (sent < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 				err = sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		len    -= sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		offset += sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	} while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	clear_bit(NET_CONGESTED, &peer_device->connection->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		peer_device->device->send_cnt += size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) static int _drbd_send_bio(struct drbd_peer_device *peer_device, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	struct bio_vec bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	struct bvec_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	/* hint all but last page with MSG_MORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	bio_for_each_segment(bvec, bio, iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		err = _drbd_no_send_page(peer_device, bvec.bv_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 					 bvec.bv_offset, bvec.bv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 					 bio_iter_last(bvec, iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 					 ? 0 : MSG_MORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		/* REQ_OP_WRITE_SAME has only one segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		if (bio_op(bio) == REQ_OP_WRITE_SAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) static int _drbd_send_zc_bio(struct drbd_peer_device *peer_device, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	struct bio_vec bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	struct bvec_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	/* hint all but last page with MSG_MORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	bio_for_each_segment(bvec, bio, iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		err = _drbd_send_page(peer_device, bvec.bv_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 				      bvec.bv_offset, bvec.bv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 				      bio_iter_last(bvec, iter) ? 0 : MSG_MORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		/* REQ_OP_WRITE_SAME has only one segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		if (bio_op(bio) == REQ_OP_WRITE_SAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) static int _drbd_send_zc_ee(struct drbd_peer_device *peer_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 			    struct drbd_peer_request *peer_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	struct page *page = peer_req->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	unsigned len = peer_req->i.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	/* hint all but last page with MSG_MORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	page_chain_for_each(page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		unsigned l = min_t(unsigned, len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		err = _drbd_send_page(peer_device, page, 0, l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 				      page_chain_next(page) ? MSG_MORE : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		len -= l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) static u32 bio_flags_to_wire(struct drbd_connection *connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			     struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (connection->agreed_pro_version >= 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		return  (bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			(bio->bi_opf & REQ_FUA ? DP_FUA : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			(bio->bi_opf & REQ_PREFLUSH ? DP_FLUSH : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			(bio_op(bio) == REQ_OP_WRITE_SAME ? DP_WSAME : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 			(bio_op(bio) == REQ_OP_DISCARD ? DP_DISCARD : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			(bio_op(bio) == REQ_OP_WRITE_ZEROES ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			  ((connection->agreed_features & DRBD_FF_WZEROES) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			   (DP_ZEROES |(!(bio->bi_opf & REQ_NOUNMAP) ? DP_DISCARD : 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 			   : DP_DISCARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			: 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		return bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) /* Used to send write or TRIM aka REQ_OP_DISCARD requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  * R_PRIMARY -> Peer	(P_DATA, P_TRIM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	struct p_data *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	struct p_wsame *wsame = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	void *digest_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	unsigned int dp_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	int digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	digest_size = peer_device->connection->integrity_tfm ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		      crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	p->sector = cpu_to_be64(req->i.sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	p->block_id = (unsigned long)req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	dp_flags = bio_flags_to_wire(peer_device->connection, req->master_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	if (device->state.conn >= C_SYNC_SOURCE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	    device->state.conn <= C_PAUSED_SYNC_T)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		dp_flags |= DP_MAY_SET_IN_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	if (peer_device->connection->agreed_pro_version >= 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		if (req->rq_state & RQ_EXP_RECEIVE_ACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 			dp_flags |= DP_SEND_RECEIVE_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		/* During resync, request an explicit write ack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		 * even in protocol != C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		if (req->rq_state & RQ_EXP_WRITE_ACK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		|| (dp_flags & DP_MAY_SET_IN_SYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			dp_flags |= DP_SEND_WRITE_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	p->dp_flags = cpu_to_be32(dp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	if (dp_flags & (DP_DISCARD|DP_ZEROES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		enum drbd_packet cmd = (dp_flags & DP_ZEROES) ? P_ZEROES : P_TRIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		struct p_trim *t = (struct p_trim*)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		t->size = cpu_to_be32(req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*t), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	if (dp_flags & DP_WSAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		/* this will only work if DRBD_FF_WSAME is set AND the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		 * handshake agreed that all nodes and backend devices are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		 * WRITE_SAME capable and agree on logical_block_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		wsame = (struct p_wsame*)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		digest_out = wsame + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		wsame->size = cpu_to_be32(req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		digest_out = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	/* our digest is still only over the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	 * TRIM does not carry any payload. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	if (wsame) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		err =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		    __send_command(peer_device->connection, device->vnr, sock, P_WSAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 				   sizeof(*wsame) + digest_size, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 				   bio_iovec(req->master_bio).bv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		err =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		    __send_command(peer_device->connection, device->vnr, sock, P_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 				   sizeof(*p) + digest_size, NULL, req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		/* For protocol A, we have to memcpy the payload into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		 * socket buffers, as we may complete right away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		 * as soon as we handed it over to tcp, at which point the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		 * pages may become invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		 * For data-integrity enabled, we copy it as well, so we can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		 * sure that even if the bio pages may still be modified, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		 * won't change the data on the wire, thus if the digest checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		 * out ok after sending on this side, but does not fit on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		 * receiving side, we sure have detected corruption elsewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			err = _drbd_send_bio(peer_device, req->master_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			err = _drbd_send_zc_bio(peer_device, req->master_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		/* double check digest, sometimes buffers have been modified in flight. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		if (digest_size > 0 && digest_size <= 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			/* 64 byte, 512 bit, is the largest digest size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			 * currently supported in kernel crypto. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			unsigned char digest[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			if (memcmp(p + 1, digest, digest_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 				drbd_warn(device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 					"Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 					(unsigned long long)req->i.sector, req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		} /* else if (digest_size > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		     ... Be noisy about digest too large ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		} */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	mutex_unlock(&sock->mutex);  /* locked by drbd_prepare_command() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) /* answer packet, used to send data back for read requests:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)  *  Peer       -> (diskless) R_PRIMARY   (P_DATA_REPLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)  *  C_SYNC_SOURCE -> C_SYNC_TARGET         (P_RS_DATA_REPLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		    struct drbd_peer_request *peer_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	struct p_data *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	int digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	digest_size = peer_device->connection->integrity_tfm ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		      crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	p->sector = cpu_to_be64(peer_req->i.sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	p->block_id = peer_req->block_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	p->seq_num = 0;  /* unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	p->dp_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	if (digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		drbd_csum_ee(peer_device->connection->integrity_tfm, peer_req, p + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*p) + digest_size, NULL, peer_req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		err = _drbd_send_zc_ee(peer_device, peer_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	mutex_unlock(&sock->mutex);  /* locked by drbd_prepare_command() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) int drbd_send_out_of_sync(struct drbd_peer_device *peer_device, struct drbd_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	struct drbd_socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	struct p_block_desc *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	sock = &peer_device->connection->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	p = drbd_prepare_command(peer_device, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	p->sector = cpu_to_be64(req->i.sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	p->blksize = cpu_to_be32(req->i.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	return drbd_send_command(peer_device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)   drbd_send distinguishes two cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)   Packets sent via the data socket "sock"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)   and packets sent via the meta data socket "msock"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		    sock                      msock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)   -----------------+-------------------------+------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)   timeout           conf.timeout / 2          conf.timeout / 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838)   timeout action    send a ping via msock     Abort communication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 					      and close all sockets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)  * you must have down()ed the appropriate [m]sock_mutex elsewhere!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) int drbd_send(struct drbd_connection *connection, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	      void *buf, size_t size, unsigned msg_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	struct kvec iov = {.iov_base = buf, .iov_len = size};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	struct msghdr msg = {.msg_flags = msg_flags | MSG_NOSIGNAL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	int rv, sent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	if (!sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		return -EBADR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	/* THINK  if (signal_pending) return ... ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	iov_iter_kvec(&msg.msg_iter, WRITE, &iov, 1, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	if (sock == connection->data.socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		connection->ko_count = rcu_dereference(connection->net_conf)->ko_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		drbd_update_congested(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		rv = sock_sendmsg(sock, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		if (rv == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			if (we_should_drop_the_connection(connection, sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		if (rv == -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			flush_signals(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 			rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		sent += rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	} while (sent < size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	if (sock == connection->data.socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		clear_bit(NET_CONGESTED, &connection->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	if (rv <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		if (rv != -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 			drbd_err(connection, "%s_sendmsg returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 				 sock == connection->meta.socket ? "msock" : "sock",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 				 rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	return sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)  * drbd_send_all  -  Send an entire buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)  * Returns 0 upon success and a negative error value otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		  size_t size, unsigned msg_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	err = drbd_send(connection, sock, buffer, size, msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	if (err != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) static int drbd_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	struct drbd_device *device = bdev->bd_disk->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	mutex_lock(&drbd_main_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	spin_lock_irqsave(&device->resource->req_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	/* to have a stable device->state.role
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	 * and no race with updating open_cnt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	if (device->state.role != R_PRIMARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		if (mode & FMODE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 			rv = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		else if (!drbd_allow_oos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 			rv = -EMEDIUMTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	if (!rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		device->open_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	spin_unlock_irqrestore(&device->resource->req_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	mutex_unlock(&drbd_main_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) static void drbd_release(struct gendisk *gd, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	struct drbd_device *device = gd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	mutex_lock(&drbd_main_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	device->open_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	mutex_unlock(&drbd_main_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) /* need to hold resource->req_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) void drbd_queue_unplug(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	if (device->state.pdsk >= D_INCONSISTENT && device->state.conn >= C_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		D_ASSERT(device, device->state.role == R_PRIMARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		if (test_and_clear_bit(UNPLUG_REMOTE, &device->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 			drbd_queue_work_if_unqueued(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				&first_peer_device(device)->connection->sender_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 				&device->unplug_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) static void drbd_set_defaults(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	/* Beware! The actual layout differs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	 * between big endian and little endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	device->state = (union drbd_dev_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		{ .role = R_SECONDARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		  .peer = R_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		  .conn = C_STANDALONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		  .disk = D_DISKLESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		  .pdsk = D_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		} };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) void drbd_init_set_defaults(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	/* the memset(,0,) did most of this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	 * note: only assignments, no allocation in here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	drbd_set_defaults(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	atomic_set(&device->ap_bio_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	atomic_set(&device->ap_actlog_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	atomic_set(&device->ap_pending_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	atomic_set(&device->rs_pending_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	atomic_set(&device->unacked_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	atomic_set(&device->local_cnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	atomic_set(&device->pp_in_use_by_net, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	atomic_set(&device->rs_sect_in, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	atomic_set(&device->rs_sect_ev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	atomic_set(&device->ap_in_flight, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	atomic_set(&device->md_io.in_use, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	mutex_init(&device->own_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	device->state_mutex = &device->own_state_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	spin_lock_init(&device->al_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	spin_lock_init(&device->peer_seq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	INIT_LIST_HEAD(&device->active_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	INIT_LIST_HEAD(&device->sync_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	INIT_LIST_HEAD(&device->done_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	INIT_LIST_HEAD(&device->read_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	INIT_LIST_HEAD(&device->net_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	INIT_LIST_HEAD(&device->resync_reads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	INIT_LIST_HEAD(&device->resync_work.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	INIT_LIST_HEAD(&device->unplug_work.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	INIT_LIST_HEAD(&device->bm_io_work.w.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	INIT_LIST_HEAD(&device->pending_master_completion[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	INIT_LIST_HEAD(&device->pending_master_completion[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	INIT_LIST_HEAD(&device->pending_completion[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	INIT_LIST_HEAD(&device->pending_completion[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	device->resync_work.cb  = w_resync_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	device->unplug_work.cb  = w_send_write_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	device->bm_io_work.w.cb = w_bitmap_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	timer_setup(&device->resync_timer, resync_timer_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	timer_setup(&device->md_sync_timer, md_sync_timer_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	timer_setup(&device->start_resync_timer, start_resync_timer_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	timer_setup(&device->request_timer, request_timer_fn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	init_waitqueue_head(&device->misc_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	init_waitqueue_head(&device->state_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	init_waitqueue_head(&device->ee_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	init_waitqueue_head(&device->al_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	init_waitqueue_head(&device->seq_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	device->resync_wenr = LC_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) void drbd_set_my_capacity(struct drbd_device *device, sector_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	char ppb[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	set_capacity(device->vdisk, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	revalidate_disk_size(device->vdisk, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	drbd_info(device, "size = %s (%llu KB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		ppsize(ppb, size>>1), (unsigned long long)size>>1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) void drbd_device_cleanup(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	if (first_peer_device(device)->connection->receiver.t_state != NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		drbd_err(device, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 				first_peer_device(device)->connection->receiver.t_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	device->al_writ_cnt  =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	device->bm_writ_cnt  =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	device->read_cnt     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	device->recv_cnt     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	device->send_cnt     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	device->writ_cnt     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	device->p_size       =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	device->rs_start     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	device->rs_total     =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	device->rs_failed    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	device->rs_last_events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	device->rs_last_sect_ev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	for (i = 0; i < DRBD_SYNC_MARKS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		device->rs_mark_left[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		device->rs_mark_time[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	D_ASSERT(device, first_peer_device(device)->connection->net_conf == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	set_capacity(device->vdisk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	revalidate_disk_size(device->vdisk, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	if (device->bitmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		/* maybe never allocated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		drbd_bm_resize(device, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		drbd_bm_cleanup(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	drbd_backing_dev_free(device, device->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	device->ldev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	clear_bit(AL_SUSPENDED, &device->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	D_ASSERT(device, list_empty(&device->active_ee));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	D_ASSERT(device, list_empty(&device->sync_ee));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	D_ASSERT(device, list_empty(&device->done_ee));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	D_ASSERT(device, list_empty(&device->read_ee));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	D_ASSERT(device, list_empty(&device->net_ee));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	D_ASSERT(device, list_empty(&device->resync_reads));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	D_ASSERT(device, list_empty(&first_peer_device(device)->connection->sender_work.q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	D_ASSERT(device, list_empty(&device->resync_work.list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	D_ASSERT(device, list_empty(&device->unplug_work.list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	drbd_set_defaults(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) static void drbd_destroy_mempools(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	while (drbd_pp_pool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		page = drbd_pp_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		drbd_pp_pool = (struct page *)page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		__free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		drbd_pp_vacant--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	/* D_ASSERT(device, atomic_read(&drbd_pp_vacant)==0); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	bioset_exit(&drbd_io_bio_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	bioset_exit(&drbd_md_io_bio_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	mempool_exit(&drbd_md_io_page_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	mempool_exit(&drbd_ee_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	mempool_exit(&drbd_request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	kmem_cache_destroy(drbd_ee_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	kmem_cache_destroy(drbd_request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	kmem_cache_destroy(drbd_bm_ext_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	kmem_cache_destroy(drbd_al_ext_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	drbd_ee_cache        = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	drbd_request_cache   = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	drbd_bm_ext_cache    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	drbd_al_ext_cache    = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) static int drbd_create_mempools(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * drbd_minor_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	/* caches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	drbd_request_cache = kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		"drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	if (drbd_request_cache == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	drbd_ee_cache = kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		"drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (drbd_ee_cache == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	drbd_bm_ext_cache = kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		"drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	if (drbd_bm_ext_cache == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	drbd_al_ext_cache = kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		"drbd_al", sizeof(struct lc_element), 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	if (drbd_al_ext_cache == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	/* mempools */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	ret = bioset_init(&drbd_io_bio_set, BIO_POOL_SIZE, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	ret = bioset_init(&drbd_md_io_bio_set, DRBD_MIN_POOL_PAGES, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 			  BIOSET_NEED_BVECS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	ret = mempool_init_page_pool(&drbd_md_io_page_pool, DRBD_MIN_POOL_PAGES, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	ret = mempool_init_slab_pool(&drbd_request_mempool, number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 				     drbd_request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	ret = mempool_init_slab_pool(&drbd_ee_mempool, number, drbd_ee_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	/* drbd's page pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	spin_lock_init(&drbd_pp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	for (i = 0; i < number; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		page = alloc_page(GFP_HIGHUSER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 			goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		set_page_private(page, (unsigned long)drbd_pp_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 		drbd_pp_pool = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	drbd_pp_vacant = number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) Enomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	drbd_destroy_mempools(); /* in case we allocated some */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) static void drbd_release_all_peer_reqs(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	int rr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	rr = drbd_free_peer_reqs(device, &device->active_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	if (rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		drbd_err(device, "%d EEs in active list found!\n", rr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	rr = drbd_free_peer_reqs(device, &device->sync_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	if (rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		drbd_err(device, "%d EEs in sync list found!\n", rr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	rr = drbd_free_peer_reqs(device, &device->read_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	if (rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		drbd_err(device, "%d EEs in read list found!\n", rr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	rr = drbd_free_peer_reqs(device, &device->done_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	if (rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		drbd_err(device, "%d EEs in done list found!\n", rr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	rr = drbd_free_peer_reqs(device, &device->net_ee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	if (rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		drbd_err(device, "%d EEs in net list found!\n", rr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) /* caution. no locking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) void drbd_destroy_device(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	struct drbd_device *device = container_of(kref, struct drbd_device, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	struct drbd_resource *resource = device->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	struct drbd_peer_device *peer_device, *tmp_peer_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	del_timer_sync(&device->request_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	/* paranoia asserts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	D_ASSERT(device, device->open_cnt == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	/* end paranoia asserts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	/* cleanup stuff that may have been allocated during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	 * device (re-)configuration or state changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	drbd_backing_dev_free(device, device->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	device->ldev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	drbd_release_all_peer_reqs(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	lc_destroy(device->act_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	lc_destroy(device->resync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	kfree(device->p_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	/* device->p_uuid = NULL; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	if (device->bitmap) /* should no longer be there. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		drbd_bm_cleanup(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	__free_page(device->md_io.page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	put_disk(device->vdisk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	blk_cleanup_queue(device->rq_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	kfree(device->rs_plan_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	/* not for_each_connection(connection, resource):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	 * those may have been cleaned up and disassociated already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		kref_put(&peer_device->connection->kref, drbd_destroy_connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		kfree(peer_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	memset(device, 0xfd, sizeof(*device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) /* One global retry thread, if we need to push back some bio and have it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)  * reinserted through our make request function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) static struct retry_worker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	struct workqueue_struct *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	struct work_struct worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	struct list_head writes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) } retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) static void do_retry(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	LIST_HEAD(writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	struct drbd_request *req, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	spin_lock_irq(&retry->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	list_splice_init(&retry->writes, &writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	spin_unlock_irq(&retry->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		struct drbd_device *device = req->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		struct bio *bio = req->master_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 		unsigned long start_jif = req->start_jif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		bool expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		expected =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 			expect(atomic_read(&req->completion_ref) == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 			expect(req->rq_state & RQ_POSTPONED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 				(req->rq_state & RQ_LOCAL_ABORTED) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 		if (!expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 			drbd_err(device, "req=%p completion_ref=%d rq_state=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 				req, atomic_read(&req->completion_ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 				req->rq_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		/* We still need to put one kref associated with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		 * "completion_ref" going zero in the code path that queued it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 		 * here.  The request object may still be referenced by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 		 * frozen local req->private_bio, in case we force-detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		kref_put(&req->kref, drbd_req_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 		/* A single suspended or otherwise blocking device may stall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 		 * all others as well.  Fortunately, this code path is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		 * recover from a situation that "should not happen":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 		 * concurrent writes in multi-primary setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 		 * In a "normal" lifecycle, this workqueue is supposed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 		 * destroyed without ever doing anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 		 * If it turns out to be an issue anyways, we can do per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 		 * resource (replication group) or per device (minor) retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 		 * workqueues instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		/* We are not just doing submit_bio_noacct(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		 * as we want to keep the start_time information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		inc_ap_bio(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		__drbd_make_request(device, bio, start_jif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) /* called via drbd_req_put_completion_ref(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)  * holds resource->req_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) void drbd_restart_request(struct drbd_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	spin_lock_irqsave(&retry.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	list_move_tail(&req->tl_requests, &retry.writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	spin_unlock_irqrestore(&retry.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	/* Drop the extra reference that would otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	 * have been dropped by complete_master_bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	 * do_retry() needs to grab a new one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	dec_ap_bio(req->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	queue_work(retry.wq, &retry.worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) void drbd_destroy_resource(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	struct drbd_resource *resource =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		container_of(kref, struct drbd_resource, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	idr_destroy(&resource->devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	free_cpumask_var(resource->cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	kfree(resource->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	memset(resource, 0xf2, sizeof(*resource));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	kfree(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) void drbd_free_resource(struct drbd_resource *resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	struct drbd_connection *connection, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	for_each_connection_safe(connection, tmp, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		list_del(&connection->connections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		drbd_debugfs_connection_cleanup(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 		kref_put(&connection->kref, drbd_destroy_connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	drbd_debugfs_resource_cleanup(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) static void drbd_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	struct drbd_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	struct drbd_resource *resource, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	/* first remove proc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	 * drbdsetup uses it's presence to detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	 * whether DRBD is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	 * If we would get stuck in proc removal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	 * but have netlink already deregistered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	 * some drbdsetup commands may wait forever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	 * for an answer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	if (drbd_proc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		remove_proc_entry("drbd", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	if (retry.wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		destroy_workqueue(retry.wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	drbd_genl_unregister();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	idr_for_each_entry(&drbd_devices, device, i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		drbd_delete_device(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	/* not _rcu since, no other updater anymore. Genl already unregistered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	for_each_resource_safe(resource, tmp, &drbd_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		list_del(&resource->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		drbd_free_resource(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	drbd_debugfs_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	drbd_destroy_mempools();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	unregister_blkdev(DRBD_MAJOR, "drbd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	idr_destroy(&drbd_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	pr_info("module cleanup done.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) static void drbd_init_workqueue(struct drbd_work_queue* wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	spin_lock_init(&wq->q_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	INIT_LIST_HEAD(&wq->q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	init_waitqueue_head(&wq->q_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) struct completion_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	struct drbd_work w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) static int w_complete(struct drbd_work *w, int cancel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	struct completion_work *completion_work =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 		container_of(w, struct completion_work, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	complete(&completion_work->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) void drbd_flush_workqueue(struct drbd_work_queue *work_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	struct completion_work completion_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	completion_work.w.cb = w_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	init_completion(&completion_work.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	drbd_queue_work(work_queue, &completion_work.w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	wait_for_completion(&completion_work.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) struct drbd_resource *drbd_find_resource(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	for_each_resource_rcu(resource, &drbd_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		if (!strcmp(resource->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 			kref_get(&resource->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	resource = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	return resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 				     void *peer_addr, int peer_addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	struct drbd_connection *connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	for_each_resource_rcu(resource, &drbd_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		for_each_connection_rcu(connection, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 			if (connection->my_addr_len == my_addr_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 			    connection->peer_addr_len == peer_addr_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 			    !memcmp(&connection->my_addr, my_addr, my_addr_len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 			    !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 				kref_get(&connection->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 				goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	connection = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	return connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) static int drbd_alloc_socket(struct drbd_socket *socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	if (!socket->rbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	if (!socket->sbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) static void drbd_free_socket(struct drbd_socket *socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	free_page((unsigned long) socket->sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	free_page((unsigned long) socket->rbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) void conn_free_crypto(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	drbd_free_sock(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	crypto_free_shash(connection->csums_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	crypto_free_shash(connection->verify_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	crypto_free_shash(connection->cram_hmac_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	crypto_free_shash(connection->integrity_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	crypto_free_shash(connection->peer_integrity_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	kfree(connection->int_dig_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	kfree(connection->int_dig_vv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	connection->csums_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	connection->verify_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	connection->cram_hmac_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	connection->integrity_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	connection->peer_integrity_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	connection->int_dig_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	connection->int_dig_vv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	struct drbd_connection *connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	cpumask_var_t new_cpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	/* silently ignore cpu mask on UP kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 		err = bitmap_parse(res_opts->cpu_mask, DRBD_CPU_MASK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 				   cpumask_bits(new_cpu_mask), nr_cpu_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		if (err == -EOVERFLOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 			/* So what. mask it out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 			cpumask_var_t tmp_cpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 			if (zalloc_cpumask_var(&tmp_cpu_mask, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 				cpumask_setall(tmp_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 				cpumask_and(new_cpu_mask, new_cpu_mask, tmp_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 				drbd_warn(resource, "Overflow in bitmap_parse(%.12s%s), truncating to %u bits\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 					res_opts->cpu_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 					strlen(res_opts->cpu_mask) > 12 ? "..." : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 					nr_cpu_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 				free_cpumask_var(tmp_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 			drbd_warn(resource, "bitmap_parse() failed with %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 			/* retcode = ERR_CPU_MASK_PARSE; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	resource->res_opts = *res_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	if (cpumask_empty(new_cpu_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		drbd_calc_cpu_mask(&new_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	if (!cpumask_equal(resource->cpu_mask, new_cpu_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 		cpumask_copy(resource->cpu_mask, new_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 		for_each_connection_rcu(connection, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 			connection->receiver.reset_cpu_mask = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 			connection->ack_receiver.reset_cpu_mask = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 			connection->worker.reset_cpu_mask = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	free_cpumask_var(new_cpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) struct drbd_resource *drbd_create_resource(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	resource = kzalloc(sizeof(struct drbd_resource), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	if (!resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	resource->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	if (!resource->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		goto fail_free_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	if (!zalloc_cpumask_var(&resource->cpu_mask, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		goto fail_free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	kref_init(&resource->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	idr_init(&resource->devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	INIT_LIST_HEAD(&resource->connections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	resource->write_ordering = WO_BDEV_FLUSH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	list_add_tail_rcu(&resource->resources, &drbd_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	mutex_init(&resource->conf_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	mutex_init(&resource->adm_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	spin_lock_init(&resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	drbd_debugfs_resource_add(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	return resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) fail_free_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	kfree(resource->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) fail_free_resource:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	kfree(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) /* caller must be under adm_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	struct drbd_connection *connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	connection = kzalloc(sizeof(struct drbd_connection), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	if (!connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	if (drbd_alloc_socket(&connection->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	if (drbd_alloc_socket(&connection->meta))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	connection->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	if (!connection->current_epoch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	INIT_LIST_HEAD(&connection->transfer_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	INIT_LIST_HEAD(&connection->current_epoch->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	connection->epochs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	spin_lock_init(&connection->epoch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	connection->send.seen_any_write_yet = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	connection->send.current_epoch_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	connection->send.current_epoch_writes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	resource = drbd_create_resource(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	if (!resource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	connection->cstate = C_STANDALONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	mutex_init(&connection->cstate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	init_waitqueue_head(&connection->ping_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	idr_init(&connection->peer_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	drbd_init_workqueue(&connection->sender_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	mutex_init(&connection->data.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	mutex_init(&connection->meta.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	drbd_thread_init(resource, &connection->receiver, drbd_receiver, "receiver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	connection->receiver.connection = connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	drbd_thread_init(resource, &connection->worker, drbd_worker, "worker");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	connection->worker.connection = connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	drbd_thread_init(resource, &connection->ack_receiver, drbd_ack_receiver, "ack_recv");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	connection->ack_receiver.connection = connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	kref_init(&connection->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	connection->resource = resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	if (set_resource_options(resource, res_opts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		goto fail_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	kref_get(&resource->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	list_add_tail_rcu(&connection->connections, &resource->connections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	drbd_debugfs_connection_add(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	return connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) fail_resource:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	list_del(&resource->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	drbd_free_resource(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	kfree(connection->current_epoch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	drbd_free_socket(&connection->meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	drbd_free_socket(&connection->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	kfree(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) void drbd_destroy_connection(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	struct drbd_resource *resource = connection->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	if (atomic_read(&connection->current_epoch->epoch_size) !=  0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 		drbd_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	kfree(connection->current_epoch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	idr_destroy(&connection->peer_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	drbd_free_socket(&connection->meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	drbd_free_socket(&connection->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	kfree(connection->int_dig_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	kfree(connection->int_dig_vv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	memset(connection, 0xfc, sizeof(*connection));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	kfree(connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) static int init_submitter(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	/* opencoded create_singlethread_workqueue(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	 * to be able to say "drbd%d", ..., minor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	device->submit.wq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 		alloc_ordered_workqueue("drbd%u_submit", WQ_MEM_RECLAIM, device->minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	if (!device->submit.wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	INIT_WORK(&device->submit.worker, do_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	INIT_LIST_HEAD(&device->submit.writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	struct drbd_resource *resource = adm_ctx->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	struct drbd_connection *connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	struct drbd_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	struct drbd_peer_device *peer_device, *tmp_peer_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	struct request_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	int vnr = adm_ctx->volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	enum drbd_ret_code err = ERR_NOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	device = minor_to_device(minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	if (device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 		return ERR_MINOR_OR_VOLUME_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	/* GFP_KERNEL, we are outside of all write-out paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	if (!device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		return ERR_NOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	kref_init(&device->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	kref_get(&resource->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	device->resource = resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	device->minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	device->vnr = vnr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	drbd_init_set_defaults(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	q = blk_alloc_queue(NUMA_NO_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		goto out_no_q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	device->rq_queue = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	disk = alloc_disk(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	if (!disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 		goto out_no_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	device->vdisk = disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	set_disk_ro(disk, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	disk->queue = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	disk->major = DRBD_MAJOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	disk->first_minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	disk->fops = &drbd_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	sprintf(disk->disk_name, "drbd%d", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	disk->private_data = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	blk_queue_write_cache(q, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	/* Setting the max_hw_sectors to an odd value of 8kibyte here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	   This triggers a max_bio_size message upon first attach or connect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	device->md_io.page = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	if (!device->md_io.page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		goto out_no_io_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (drbd_bm_init(device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		goto out_no_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	device->read_requests = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	device->write_requests = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	id = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	if (id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 		if (id == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 			err = ERR_MINOR_OR_VOLUME_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		goto out_no_minor_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	kref_get(&device->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	id = idr_alloc(&resource->devices, device, vnr, vnr + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	if (id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		if (id == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 			err = ERR_MINOR_OR_VOLUME_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 		goto out_idr_remove_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	kref_get(&device->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	INIT_LIST_HEAD(&device->peer_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	INIT_LIST_HEAD(&device->pending_bitmap_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	for_each_connection(connection, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 		peer_device = kzalloc(sizeof(struct drbd_peer_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		if (!peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 			goto out_idr_remove_from_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		peer_device->connection = connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		peer_device->device = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		list_add(&peer_device->peer_devices, &device->peer_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		kref_get(&device->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 		id = idr_alloc(&connection->peer_devices, peer_device, vnr, vnr + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		if (id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 			if (id == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 				err = ERR_INVALID_REQUEST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 			goto out_idr_remove_from_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		kref_get(&connection->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 		INIT_WORK(&peer_device->send_acks_work, drbd_send_acks_wf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	if (init_submitter(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 		err = ERR_NOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 		goto out_idr_remove_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	add_disk(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	/* inherit the connection state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	device->state.conn = first_connection(resource)->cstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	if (device->state.conn == C_WF_REPORT_PARAMS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 		for_each_peer_device(peer_device, device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 			drbd_connected(peer_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	/* move to create_peer_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	for_each_peer_device(peer_device, device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		drbd_debugfs_peer_device_add(peer_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	drbd_debugfs_device_add(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	return NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) out_idr_remove_vol:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	idr_remove(&connection->peer_devices, vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) out_idr_remove_from_resource:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	for_each_connection(connection, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		peer_device = idr_remove(&connection->peer_devices, vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		if (peer_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 			kref_put(&connection->kref, drbd_destroy_connection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		list_del(&peer_device->peer_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		kfree(peer_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	idr_remove(&resource->devices, vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) out_idr_remove_minor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	idr_remove(&drbd_devices, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) out_no_minor_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	drbd_bm_cleanup(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) out_no_bitmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	__free_page(device->md_io.page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) out_no_io_page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	put_disk(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) out_no_disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	blk_cleanup_queue(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) out_no_q:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	kref_put(&resource->kref, drbd_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	kfree(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) void drbd_delete_device(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	struct drbd_resource *resource = device->resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	struct drbd_connection *connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	struct drbd_peer_device *peer_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	/* move to free_peer_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	for_each_peer_device(peer_device, device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 		drbd_debugfs_peer_device_cleanup(peer_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	drbd_debugfs_device_cleanup(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	for_each_connection(connection, resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 		idr_remove(&connection->peer_devices, device->vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 		kref_put(&device->kref, drbd_destroy_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	idr_remove(&resource->devices, device->vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	kref_put(&device->kref, drbd_destroy_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	idr_remove(&drbd_devices, device_to_minor(device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	kref_put(&device->kref, drbd_destroy_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	del_gendisk(device->vdisk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 	kref_put(&device->kref, drbd_destroy_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) static int __init drbd_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	if (drbd_minor_count < DRBD_MINOR_COUNT_MIN || drbd_minor_count > DRBD_MINOR_COUNT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 		pr_err("invalid minor_count (%d)\n", drbd_minor_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) #ifdef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 		drbd_minor_count = DRBD_MINOR_COUNT_DEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 	err = register_blkdev(DRBD_MAJOR, "drbd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		pr_err("unable to register block device major %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		       DRBD_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 	 * allocate all necessary structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	init_waitqueue_head(&drbd_pp_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 	drbd_proc = NULL; /* play safe for drbd_cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 	idr_init(&drbd_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	mutex_init(&resources_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 	INIT_LIST_HEAD(&drbd_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 	err = drbd_genl_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		pr_err("unable to register generic netlink family\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	err = drbd_create_mempools();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 	drbd_proc = proc_create_single("drbd", S_IFREG | 0444 , NULL, drbd_seq_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 	if (!drbd_proc)	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 		pr_err("unable to register proc file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	retry.wq = create_singlethread_workqueue("drbd-reissue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	if (!retry.wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 		pr_err("unable to create retry workqueue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	INIT_WORK(&retry.worker, do_retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	spin_lock_init(&retry.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	INIT_LIST_HEAD(&retry.writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	drbd_debugfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 	pr_info("initialized. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 	       "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	       API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	pr_info("%s\n", drbd_buildtag());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	pr_info("registered as block device major %d\n", DRBD_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	return 0; /* Success! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	drbd_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	if (err == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 		pr_err("ran out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 		pr_err("initialization failure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) static void drbd_free_one_sock(struct drbd_socket *ds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 	struct socket *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	mutex_lock(&ds->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	s = ds->socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	ds->socket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	mutex_unlock(&ds->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	if (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 		/* so debugfs does not need to mutex_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 		kernel_sock_shutdown(s, SHUT_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		sock_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) void drbd_free_sock(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	if (connection->data.socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 		drbd_free_one_sock(&connection->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	if (connection->meta.socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 		drbd_free_one_sock(&connection->meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) /* meta data management */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) void conn_md_sync(struct drbd_connection *connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	struct drbd_peer_device *peer_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	int vnr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 		struct drbd_device *device = peer_device->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 		kref_get(&device->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 		drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 		kref_put(&device->kref, drbd_destroy_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) /* aligned 4kByte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) struct meta_data_on_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	u64 la_size_sect;      /* last agreed size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	u64 uuid[UI_SIZE];   /* UUIDs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 	u64 device_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	u64 reserved_u64_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 	u32 flags;             /* MDF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 	u32 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 	u32 md_size_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	u32 al_offset;         /* offset to this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	u32 al_nr_extents;     /* important for restoring the AL (userspace) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	      /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	u32 bm_offset;         /* offset to the bitmap, from here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	u32 bm_bytes_per_bit;  /* BM_BLOCK_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 	u32 la_peer_max_bio_size;   /* last peer max_bio_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	/* see al_tr_number_to_on_disk_sector() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	u32 al_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 	u32 al_stripe_size_4k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	u8 reserved_u8[4096 - (7*8 + 10*4)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) void drbd_md_write(struct drbd_device *device, void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	struct meta_data_on_disk *buffer = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	memset(buffer, 0, sizeof(*buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	buffer->la_size_sect = cpu_to_be64(get_capacity(device->vdisk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	for (i = UI_CURRENT; i < UI_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 		buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	buffer->flags = cpu_to_be32(device->ldev->md.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	buffer->md_size_sect  = cpu_to_be32(device->ldev->md.md_size_sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	buffer->al_offset     = cpu_to_be32(device->ldev->md.al_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	D_ASSERT(device, drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	sector = device->ldev->md.md_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	if (drbd_md_sync_page_io(device, device->ldev, sector, REQ_OP_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 		/* this was a try anyways ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 		drbd_err(device, "meta data update failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069)  * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) void drbd_md_sync(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	struct meta_data_on_disk *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	/* Don't accidentally change the DRBD meta data layout. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	BUILD_BUG_ON(UI_SIZE != 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 	del_timer(&device->md_sync_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	/* timer may be rearmed by drbd_md_mark_dirty() now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	if (!test_and_clear_bit(MD_DIRTY, &device->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	/* We use here D_FAILED and not D_ATTACHING because we try to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	 * metadata even if we detach due to a disk failure! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	if (!get_ldev_if_state(device, D_FAILED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	buffer = drbd_md_get_buffer(device, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	drbd_md_write(device, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	/* Update device->ldev->md.la_size_sect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	 * since we updated it on metadata. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	device->ldev->md.la_size_sect = get_capacity(device->vdisk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	drbd_md_put_buffer(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) static int check_activity_log_stripe_size(struct drbd_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 		struct meta_data_on_disk *on_disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 		struct drbd_md *in_core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	u64 al_size_4k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	/* both not set: default to old fixed size activity log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	if (al_stripes == 0 && al_stripe_size_4k == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 		al_stripes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 		al_stripe_size_4k = MD_32kB_SECT/8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	/* some paranoia plausibility checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	/* we need both values to be set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	if (al_stripes == 0 || al_stripe_size_4k == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 	al_size_4k = (u64)al_stripes * al_stripe_size_4k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	/* Upper limit of activity log area, to avoid potential overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	 * problems in al_tr_number_to_on_disk_sector(). As right now, more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	 * than 72 * 4k blocks total only increases the amount of history,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	 * limiting this arbitrarily to 16 GB is not a real limitation ;-)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	if (al_size_4k > (16 * 1024 * 1024/4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	/* Lower limit: we need at least 8 transaction slots (32kB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	 * to not break existing setups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	if (al_size_4k < MD_32kB_SECT/8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 	in_core->al_stripe_size_4k = al_stripe_size_4k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	in_core->al_stripes = al_stripes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 	in_core->al_size_4k = al_size_4k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	drbd_err(device, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 			al_stripes, al_stripe_size_4k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	sector_t capacity = drbd_get_capacity(bdev->md_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	struct drbd_md *in_core = &bdev->md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	s32 on_disk_al_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	s32 on_disk_bm_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	/* The on-disk size of the activity log, calculated from offsets, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	 * the size of the activity log calculated from the stripe settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	 * should match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 	 * Though we could relax this a bit: it is ok, if the striped activity log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 	 * fits in the available on-disk activity log size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	 * Right now, that would break how resize is implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 	 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 	 * of possible unused padding space in the on disk layout. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 	if (in_core->al_offset < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		if (in_core->bm_offset > in_core->al_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 		on_disk_al_sect = -in_core->al_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 		on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 		if (in_core->al_offset != MD_4kB_SECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 		on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 		on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	/* old fixed size meta data is exactly that: fixed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	if (in_core->meta_dev_idx >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 		if (in_core->md_size_sect != MD_128MB_SECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 		||  in_core->al_offset != MD_4kB_SECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 		||  in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 		||  in_core->al_stripes != 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 		||  in_core->al_stripe_size_4k != MD_32kB_SECT/8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 	if (capacity < in_core->md_size_sect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	/* should be aligned, and at least 32k */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	/* should fit (for now: exactly) into the available on-disk space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	 * overflow prevention is in check_activity_log_stripe_size() above. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	/* again, should be aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	if (in_core->bm_offset & 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	/* FIXME check for device grow with flex external meta data? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	/* can the available bitmap space cover the last agreed device size? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	drbd_err(device, "meta data offsets don't make sense: idx=%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 			"al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 			"md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 			in_core->meta_dev_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 			in_core->al_stripes, in_core->al_stripe_size_4k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 			in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 			(unsigned long long)in_core->la_size_sect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 			(unsigned long long)capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231)  * drbd_md_read() - Reads in the meta data super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233)  * @bdev:	Device from which the meta data should be read in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235)  * Return NO_ERROR on success, and an enum drbd_ret_code in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236)  * something goes wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238)  * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239)  * even before @bdev is assigned to @device->ldev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	struct meta_data_on_disk *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	u32 magic, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	int i, rv = NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	if (device->state.disk != D_DISKLESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 		return ERR_DISK_CONFIGURED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	buffer = drbd_md_get_buffer(device, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 		return ERR_NOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 	/* First, figure out where our meta data superblock is located,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	 * and read it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	bdev->md.md_offset = drbd_md_ss(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	/* Even for (flexible or indexed) external meta data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	 * initially restrict us to the 4k superblock for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	 * Affects the paranoia out-of-range access check in drbd_md_sync_page_io(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	bdev->md.md_size_sect = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 				 REQ_OP_READ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 		/* NOTE: can't do normal error processing here as this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		   called BEFORE disk is attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 		drbd_err(device, "Error while reading metadata.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 		rv = ERR_IO_MD_DISK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	magic = be32_to_cpu(buffer->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	flags = be32_to_cpu(buffer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	    (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 			/* btw: that's Activity Log clean, not "all" clean. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 		drbd_err(device, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		rv = ERR_MD_UNCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 	rv = ERR_MD_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	if (magic != DRBD_MD_MAGIC_08) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 		if (magic == DRBD_MD_MAGIC_07)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 			drbd_err(device, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 			drbd_err(device, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 	if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 		drbd_err(device, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 		    be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 	/* convert to in_core endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 	for (i = UI_CURRENT; i < UI_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	bdev->md.flags = be32_to_cpu(buffer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 	bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 	bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 	bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 	if (check_activity_log_stripe_size(device, buffer, &bdev->md))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 	if (check_offsets_and_sizes(device, bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 	if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 		drbd_err(device, "unexpected bm_offset: %d (expected %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 		    be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 	if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 		drbd_err(device, "unexpected md_size: %u (expected %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 		    be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 	rv = NO_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	spin_lock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	if (device->state.conn < C_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 		unsigned int peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 		peer = be32_to_cpu(buffer->la_peer_max_bio_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 		peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 		device->peer_max_bio_size = peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 	spin_unlock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	drbd_md_put_buffer(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343)  * drbd_md_mark_dirty() - Mark meta data super block as dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346)  * Call this function if you change anything that should be written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347)  * the meta-data super block. This function sets MD_DIRTY, and starts a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348)  * timer that ensures that within five seconds you have to call drbd_md_sync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) void drbd_md_mark_dirty(struct drbd_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 	if (!test_and_set_bit(MD_DIRTY, &device->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 		mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 	for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 		device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	if (idx == UI_CURRENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		if (device->state.role == R_PRIMARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 			val |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 			val &= ~((u64)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 		drbd_set_ed_uuid(device, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	device->ldev->md.uuid[idx] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	drbd_md_mark_dirty(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 	__drbd_uuid_set(device, idx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	if (device->ldev->md.uuid[idx]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 		drbd_uuid_move_history(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	__drbd_uuid_set(device, idx, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 	spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400)  * drbd_uuid_new_current() - Creates a new current UUID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403)  * Creates a new current UUID, and rotates the old current UUID into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404)  * the bitmap slot. Causes an incremental resync upon next connect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 	unsigned long long bm_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	get_random_bytes(&val, sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 	spin_lock_irq(&device->ldev->md.uuid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	bm_uuid = device->ldev->md.uuid[UI_BITMAP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 	if (bm_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 		drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	__drbd_uuid_set(device, UI_CURRENT, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	spin_unlock_irq(&device->ldev->md.uuid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 	drbd_print_uuids(device, "new current UUID");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	/* get it to stable storage _now_ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 	if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 	spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 	if (val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 		drbd_uuid_move_history(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 		device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 		device->ldev->md.uuid[UI_BITMAP] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 		unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 		if (bm_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 			drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 		device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	drbd_md_mark_dirty(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452)  * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455)  * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) int drbd_bmio_set_n_write(struct drbd_device *device) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	int rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 	drbd_md_set_flag(device, MDF_FULL_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	drbd_bm_set_all(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	rv = drbd_bm_write(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 	if (!rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		drbd_md_clear_flag(device, MDF_FULL_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 		drbd_md_sync(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476)  * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479)  * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) int drbd_bmio_clear_n_write(struct drbd_device *device) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	drbd_resume_al(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 	drbd_bm_clear_all(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 	return drbd_bm_write(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) static int w_bitmap_io(struct drbd_work *w, int unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	struct drbd_device *device =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 		container_of(w, struct drbd_device, bm_io_work.w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	struct bm_io_work *work = &device->bm_io_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	int rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 	if (work->flags != BM_LOCKED_CHANGE_ALLOWED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 		int cnt = atomic_read(&device->ap_bio_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 		if (cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 			drbd_err(device, "FIXME: ap_bio_cnt %d, expected 0; queued for '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 					cnt, work->why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	if (get_ldev(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 		drbd_bm_lock(device, work->why, work->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		rv = work->io_fn(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 		drbd_bm_unlock(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 		put_ldev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	clear_bit_unlock(BITMAP_IO, &device->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	wake_up(&device->misc_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	if (work->done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		work->done(device, rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	clear_bit(BITMAP_IO_QUEUED, &device->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	work->why = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	work->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523)  * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525)  * @io_fn:	IO callback to be called when bitmap IO is possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526)  * @done:	callback to be called after the bitmap IO was performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527)  * @why:	Descriptive text of the reason for doing the IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529)  * While IO on the bitmap happens we freeze application IO thus we ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530)  * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531)  * called from worker context. It MUST NOT be used while a previous such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532)  * work is still pending!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)  * Its worker function encloses the call of io_fn() by get_ldev() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535)  * put_ldev().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) void drbd_queue_bitmap_io(struct drbd_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 			  int (*io_fn)(struct drbd_device *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 			  void (*done)(struct drbd_device *, int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 			  char *why, enum bm_flag flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	D_ASSERT(device, !test_bit(BITMAP_IO_QUEUED, &device->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	D_ASSERT(device, !test_bit(BITMAP_IO, &device->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	D_ASSERT(device, list_empty(&device->bm_io_work.w.list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	if (device->bm_io_work.why)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 		drbd_err(device, "FIXME going to queue '%s' but '%s' still pending?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 			why, device->bm_io_work.why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	device->bm_io_work.io_fn = io_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	device->bm_io_work.done = done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	device->bm_io_work.why = why;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 	device->bm_io_work.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	spin_lock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	set_bit(BITMAP_IO, &device->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	/* don't wait for pending application IO if the caller indicates that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	 * application IO does not conflict anyways. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	if (flags == BM_LOCKED_CHANGE_ALLOWED || atomic_read(&device->ap_bio_cnt) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 		if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 			drbd_queue_work(&first_peer_device(device)->connection->sender_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 					&device->bm_io_work.w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	spin_unlock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569)  * drbd_bitmap_io() -  Does an IO operation on the whole bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570)  * @device:	DRBD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571)  * @io_fn:	IO callback to be called when bitmap IO is possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572)  * @why:	Descriptive text of the reason for doing the IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574)  * freezes application IO while that the actual IO operations runs. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575)  * functions MAY NOT be called from worker context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 		char *why, enum bm_flag flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 	/* Only suspend io, if some operation is supposed to be locked out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	const bool do_suspend_io = flags & (BM_DONT_CLEAR|BM_DONT_SET|BM_DONT_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 	D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 	if (do_suspend_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 		drbd_suspend_io(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	drbd_bm_lock(device, why, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 	rv = io_fn(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 	drbd_bm_unlock(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 	if (do_suspend_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		drbd_resume_io(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 	if ((device->ldev->md.flags & flag) != flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 		drbd_md_mark_dirty(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 		device->ldev->md.flags |= flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	if ((device->ldev->md.flags & flag) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 		drbd_md_mark_dirty(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 		device->ldev->md.flags &= ~flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 	return (bdev->md.flags & flag) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) static void md_sync_timer_fn(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	struct drbd_device *device = from_timer(device, t, md_sync_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 	drbd_device_post_work(device, MD_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) const char *cmdname(enum drbd_packet cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	/* THINK may need to become several global tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	 * when we want to support more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 	 * one PRO_VERSION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 	static const char *cmdnames[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 		[P_DATA]	        = "Data",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 		[P_WSAME]	        = "WriteSame",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 		[P_TRIM]	        = "Trim",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 		[P_DATA_REPLY]	        = "DataReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 		[P_RS_DATA_REPLY]	= "RSDataReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		[P_BARRIER]	        = "Barrier",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 		[P_BITMAP]	        = "ReportBitMap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 		[P_BECOME_SYNC_TARGET]  = "BecomeSyncTarget",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 		[P_BECOME_SYNC_SOURCE]  = "BecomeSyncSource",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 		[P_UNPLUG_REMOTE]	= "UnplugRemote",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 		[P_DATA_REQUEST]	= "DataRequest",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 		[P_RS_DATA_REQUEST]     = "RSDataRequest",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 		[P_SYNC_PARAM]	        = "SyncParam",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 		[P_SYNC_PARAM89]	= "SyncParam89",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 		[P_PROTOCOL]            = "ReportProtocol",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 		[P_UUIDS]	        = "ReportUUIDs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 		[P_SIZES]	        = "ReportSizes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 		[P_STATE]	        = "ReportState",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		[P_SYNC_UUID]           = "ReportSyncUUID",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 		[P_AUTH_CHALLENGE]      = "AuthChallenge",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 		[P_AUTH_RESPONSE]	= "AuthResponse",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 		[P_PING]		= "Ping",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 		[P_PING_ACK]	        = "PingAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 		[P_RECV_ACK]	        = "RecvAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		[P_WRITE_ACK]	        = "WriteAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 		[P_RS_WRITE_ACK]	= "RSWriteAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 		[P_SUPERSEDED]          = "Superseded",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		[P_NEG_ACK]	        = "NegAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 		[P_NEG_DREPLY]	        = "NegDReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 		[P_NEG_RS_DREPLY]	= "NegRSDReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		[P_BARRIER_ACK]	        = "BarrierAck",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 		[P_STATE_CHG_REQ]       = "StateChgRequest",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		[P_STATE_CHG_REPLY]     = "StateChgReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		[P_OV_REQUEST]          = "OVRequest",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 		[P_OV_REPLY]            = "OVReply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 		[P_OV_RESULT]           = "OVResult",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		[P_CSUM_RS_REQUEST]     = "CsumRSRequest",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		[P_RS_IS_IN_SYNC]	= "CsumRSIsInSync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 		[P_COMPRESSED_BITMAP]   = "CBitmap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		[P_DELAY_PROBE]         = "DelayProbe",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 		[P_OUT_OF_SYNC]		= "OutOfSync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 		[P_RETRY_WRITE]		= "RetryWrite",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		[P_RS_CANCEL]		= "RSCancel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 		[P_CONN_ST_CHG_REQ]	= "conn_st_chg_req",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 		[P_CONN_ST_CHG_REPLY]	= "conn_st_chg_reply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 		[P_RETRY_WRITE]		= "retry_write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 		[P_PROTOCOL_UPDATE]	= "protocol_update",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 		[P_RS_THIN_REQ]         = "rs_thin_req",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 		[P_RS_DEALLOCATED]      = "rs_deallocated",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 		/* enum drbd_packet, but not commands - obsoleted flags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 		 *	P_MAY_IGNORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 		 *	P_MAX_OPT_CMD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	/* too big for the array: 0xfffX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	if (cmd == P_INITIAL_META)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 		return "InitialMeta";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	if (cmd == P_INITIAL_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 		return "InitialData";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	if (cmd == P_CONNECTION_FEATURES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 		return "ConnectionFeatures";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	if (cmd >= ARRAY_SIZE(cmdnames))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 		return "Unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	return cmdnames[cmd];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700)  * drbd_wait_misc  -  wait for a request to make progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701)  * @device:	device associated with the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702)  * @i:		the struct drbd_interval embedded in struct drbd_request or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703)  *		struct drbd_peer_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	struct net_conf *nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 	long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 	if (!nc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 	timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	/* Indicate to wake up device->misc_wait on progress.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	i->waiting = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	spin_unlock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	timeout = schedule_timeout(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	finish_wait(&device->misc_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	spin_lock_irq(&device->resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	if (!timeout || device->state.conn < C_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 		return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) void lock_all_resources(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 	int __maybe_unused i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	mutex_lock(&resources_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 	local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 	for_each_resource(resource, &drbd_resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 		spin_lock_nested(&resource->req_lock, i++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) void unlock_all_resources(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	struct drbd_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	for_each_resource(resource, &drbd_resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 		spin_unlock(&resource->req_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 	local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	mutex_unlock(&resources_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) #ifdef CONFIG_DRBD_FAULT_INJECTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) /* Fault insertion support including random number generator shamelessly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757)  * stolen from kernel/rcutorture.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) struct fault_random_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	unsigned long count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) #define FAULT_RANDOM_MULT 39916801  /* prime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) #define FAULT_RANDOM_ADD	479001701 /* prime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) #define FAULT_RANDOM_REFRESH 10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768)  * Crude but fast random-number generator.  Uses a linear congruential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769)  * generator, with occasional help from get_random_bytes().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) _drbd_fault_random(struct fault_random_state *rsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	long refresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	if (!rsp->count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 		get_random_bytes(&refresh, sizeof(refresh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 		rsp->state += refresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 		rsp->count = FAULT_RANDOM_REFRESH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 	rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 	return swahw32(rsp->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) static char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) _drbd_fault_str(unsigned int type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 	static char *_faults[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 		[DRBD_FAULT_MD_WR] = "Meta-data write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 		[DRBD_FAULT_MD_RD] = "Meta-data read",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 		[DRBD_FAULT_RS_WR] = "Resync write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 		[DRBD_FAULT_RS_RD] = "Resync read",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 		[DRBD_FAULT_DT_WR] = "Data write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 		[DRBD_FAULT_DT_RD] = "Data read",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		[DRBD_FAULT_DT_RA] = "Data read ahead",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 		[DRBD_FAULT_BM_ALLOC] = "BM allocation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		[DRBD_FAULT_AL_EE] = "EE allocation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		[DRBD_FAULT_RECEIVE] = "receive data corruption",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 	return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) _drbd_insert_fault(struct drbd_device *device, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 	static struct fault_random_state rrs = {0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 	unsigned int ret = (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 		(drbd_fault_devs == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 			((1 << device_to_minor(device)) & drbd_fault_devs) != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 		(((_drbd_fault_random(&rrs) % 100) + 1) <= drbd_fault_rate));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 		drbd_fault_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 		if (__ratelimit(&drbd_ratelimit_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 			drbd_warn(device, "***Simulating %s failure\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 				_drbd_fault_str(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) const char *drbd_buildtag(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 	/* DRBD built from external sources has here a reference to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 	   git hash of the source code. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 	static char buildtag[38] = "\0uilt-in";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 	if (buildtag[0] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) #ifdef MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 		sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 		buildtag[0] = 'b';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 	return buildtag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) module_init(drbd_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) module_exit(drbd_cleanup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) EXPORT_SYMBOL(drbd_conn_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) EXPORT_SYMBOL(drbd_role_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) EXPORT_SYMBOL(drbd_disk_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) EXPORT_SYMBOL(drbd_set_st_err_str);