Commit ccf21b69 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'for-4.4/reservations' of git://git.kernel.dk/linux-block

Pull block reservation support from Jens Axboe:
 "This adds support for persistent reservations, both at the core level,
  as well as for sd and NVMe"

[ Background from the docs: "Persistent Reservations allow restricting
  access to block devices to specific initiators in a shared storage
  setup.  All implementations are expected to ensure the reservations
  survive a power loss and cover all connections in a multi path
  environment" ]

* 'for-4.4/reservations' of git://git.kernel.dk/linux-block:
  NVMe: Precedence error in nvme_pr_clear()
  nvme: add missing endianess annotations in nvme_pr_command
  NVMe: Add persistent reservation ops
  sd: implement the Persistent Reservation API
  block: add an API for Persistent Reservations
  block: cleanup blkdev_ioctl
parents 527d1529 73fcf4e2
Block layer support for Persistent Reservations
===============================================
The Linux kernel supports a user space interface for simplified
Persistent Reservations which map to block devices that support
these (like SCSI). Persistent Reservations allow restricting
access to block devices to specific initiators in a shared storage
setup.
This document gives a general overview of the support ioctl commands.
For a more detailed reference please refer the the SCSI Primary
Commands standard, specifically the section on Reservations and the
"PERSISTENT RESERVE IN" and "PERSISTENT RESERVE OUT" commands.
All implementations are expected to ensure the reservations survive
a power loss and cover all connections in a multi path environment.
These behaviors are optional in SPC but will be automatically applied
by Linux.
The following types of reservations are supported:
--------------------------------------------------
- PR_WRITE_EXCLUSIVE
Only the initiator that owns the reservation can write to the
device. Any initiator can read from the device.
- PR_EXCLUSIVE_ACCESS
Only the initiator that owns the reservation can access the
device.
- PR_WRITE_EXCLUSIVE_REG_ONLY
Only initiators with a registered key can write to the device,
Any initiator can read from the device.
- PR_EXCLUSIVE_ACCESS_REG_ONLY
Only initiators with a registered key can access the device.
- PR_WRITE_EXCLUSIVE_ALL_REGS
Only initiators with a registered key can write to the device,
Any initiator can read from the device.
All initiators with a registered key are considered reservation
holders.
Please reference the SPC spec on the meaning of a reservation
holder if you want to use this type.
- PR_EXCLUSIVE_ACCESS_ALL_REGS
Only initiators with a registered key can access the device.
All initiators with a registered key are considered reservation
holders.
Please reference the SPC spec on the meaning of a reservation
holder if you want to use this type.
The following ioctl are supported:
----------------------------------
1. IOC_PR_REGISTER
This ioctl command registers a new reservation if the new_key argument
is non-null. If no existing reservation exists old_key must be zero,
if an existing reservation should be replaced old_key must contain
the old reservation key.
If the new_key argument is 0 it unregisters the existing reservation passed
in old_key.
2. IOC_PR_RESERVE
This ioctl command reserves the device and thus restricts access for other
devices based on the type argument. The key argument must be the existing
reservation key for the device as acquired by the IOC_PR_REGISTER,
IOC_PR_REGISTER_IGNORE, IOC_PR_PREEMPT or IOC_PR_PREEMPT_ABORT commands.
3. IOC_PR_RELEASE
This ioctl command releases the reservation specified by key and flags
and thus removes any access restriction implied by it.
4. IOC_PR_PREEMPT
This ioctl command releases the existing reservation referred to by
old_key and replaces it with a a new reservation of type for the
reservation key new_key.
5. IOC_PR_PREEMPT_ABORT
This ioctl command works like IOC_PR_PREEMPT except that it also aborts
any outstanding command sent over a connection identified by old_key.
6. IOC_PR_CLEAR
This ioctl command unregisters both key and any other reservation key
registered with the device and drops any existing reservation.
Flags
-----
All the ioctls have a flag field. Currently only one flag is supported:
- PR_FL_IGNORE_KEY
Ignore the existing reservation key. This is commonly supported for
IOC_PR_REGISTER, and some implementation may support the flag for
IOC_PR_RESERVE.
For all unknown flags the kernel will return -EOPNOTSUPP.
This diff is collapsed.
...@@ -39,8 +39,10 @@ ...@@ -39,8 +39,10 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/t10-pi.h> #include <linux/t10-pi.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/pr.h>
#include <scsi/sg.h> #include <scsi/sg.h>
#include <asm-generic/io-64-nonatomic-lo-hi.h> #include <asm-generic/io-64-nonatomic-lo-hi.h>
#include <asm/unaligned.h>
#include <uapi/linux/nvme_ioctl.h> #include <uapi/linux/nvme_ioctl.h>
#include "nvme.h" #include "nvme.h"
...@@ -2064,6 +2066,98 @@ static int nvme_revalidate_disk(struct gendisk *disk) ...@@ -2064,6 +2066,98 @@ static int nvme_revalidate_disk(struct gendisk *disk)
return 0; return 0;
} }
static char nvme_pr_type(enum pr_type type)
{
switch (type) {
case PR_WRITE_EXCLUSIVE:
return 1;
case PR_EXCLUSIVE_ACCESS:
return 2;
case PR_WRITE_EXCLUSIVE_REG_ONLY:
return 3;
case PR_EXCLUSIVE_ACCESS_REG_ONLY:
return 4;
case PR_WRITE_EXCLUSIVE_ALL_REGS:
return 5;
case PR_EXCLUSIVE_ACCESS_ALL_REGS:
return 6;
default:
return 0;
}
};
static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
u64 key, u64 sa_key, u8 op)
{
struct nvme_ns *ns = bdev->bd_disk->private_data;
struct nvme_command c;
u8 data[16] = { 0, };
put_unaligned_le64(key, &data[0]);
put_unaligned_le64(sa_key, &data[8]);
memset(&c, 0, sizeof(c));
c.common.opcode = op;
c.common.nsid = cpu_to_le32(ns->ns_id);
c.common.cdw10[0] = cpu_to_le32(cdw10);
return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
}
static int nvme_pr_register(struct block_device *bdev, u64 old,
u64 new, unsigned flags)
{
u32 cdw10;
if (flags & ~PR_FL_IGNORE_KEY)
return -EOPNOTSUPP;
cdw10 = old ? 2 : 0;
cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
}
static int nvme_pr_reserve(struct block_device *bdev, u64 key,
enum pr_type type, unsigned flags)
{
u32 cdw10;
if (flags & ~PR_FL_IGNORE_KEY)
return -EOPNOTSUPP;
cdw10 = nvme_pr_type(type) << 8;
cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
}
static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
enum pr_type type, bool abort)
{
u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
}
static int nvme_pr_clear(struct block_device *bdev, u64 key)
{
u32 cdw10 = 1 | (key ? 1 << 3 : 0);
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
}
static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
{
u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
}
static const struct pr_ops nvme_pr_ops = {
.pr_register = nvme_pr_register,
.pr_reserve = nvme_pr_reserve,
.pr_release = nvme_pr_release,
.pr_preempt = nvme_pr_preempt,
.pr_clear = nvme_pr_clear,
};
static const struct block_device_operations nvme_fops = { static const struct block_device_operations nvme_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.ioctl = nvme_ioctl, .ioctl = nvme_ioctl,
...@@ -2072,6 +2166,7 @@ static const struct block_device_operations nvme_fops = { ...@@ -2072,6 +2166,7 @@ static const struct block_device_operations nvme_fops = {
.release = nvme_release, .release = nvme_release,
.getgeo = nvme_getgeo, .getgeo = nvme_getgeo,
.revalidate_disk= nvme_revalidate_disk, .revalidate_disk= nvme_revalidate_disk,
.pr_ops = &nvme_pr_ops,
}; };
static int nvme_kthread(void *data) static int nvme_kthread(void *data)
......
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include <linux/async.h> #include <linux/async.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/pm_runtime.h> #include <linux/pm_runtime.h>
#include <linux/pr.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
...@@ -1535,6 +1536,100 @@ static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1535,6 +1536,100 @@ static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
} }
#endif #endif
static char sd_pr_type(enum pr_type type)
{
switch (type) {
case PR_WRITE_EXCLUSIVE:
return 0x01;
case PR_EXCLUSIVE_ACCESS:
return 0x03;
case PR_WRITE_EXCLUSIVE_REG_ONLY:
return 0x05;
case PR_EXCLUSIVE_ACCESS_REG_ONLY:
return 0x06;
case PR_WRITE_EXCLUSIVE_ALL_REGS:
return 0x07;
case PR_EXCLUSIVE_ACCESS_ALL_REGS:
return 0x08;
default:
return 0;
}
};
static int sd_pr_command(struct block_device *bdev, u8 sa,
u64 key, u64 sa_key, u8 type, u8 flags)
{
struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
struct scsi_sense_hdr sshdr;
int result;
u8 cmd[16] = { 0, };
u8 data[24] = { 0, };
cmd[0] = PERSISTENT_RESERVE_OUT;
cmd[1] = sa;
cmd[2] = type;
put_unaligned_be32(sizeof(data), &cmd[5]);
put_unaligned_be64(key, &data[0]);
put_unaligned_be64(sa_key, &data[8]);
data[20] = flags;
result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
&sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
if ((driver_byte(result) & DRIVER_SENSE) &&
(scsi_sense_valid(&sshdr))) {
sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
scsi_print_sense_hdr(sdev, NULL, &sshdr);
}
return result;
}
static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
u32 flags)
{
if (flags & ~PR_FL_IGNORE_KEY)
return -EOPNOTSUPP;
return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
old_key, new_key, 0,
(1 << 0) /* APTPL */ |
(1 << 2) /* ALL_TG_PT */);
}
static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
u32 flags)
{
if (flags)
return -EOPNOTSUPP;
return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
}
static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
{
return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
}
static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
enum pr_type type, bool abort)
{
return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
sd_pr_type(type), 0);
}
static int sd_pr_clear(struct block_device *bdev, u64 key)
{
return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
}
static const struct pr_ops sd_pr_ops = {
.pr_register = sd_pr_register,
.pr_reserve = sd_pr_reserve,
.pr_release = sd_pr_release,
.pr_preempt = sd_pr_preempt,
.pr_clear = sd_pr_clear,
};
static const struct block_device_operations sd_fops = { static const struct block_device_operations sd_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.open = sd_open, .open = sd_open,
...@@ -1547,6 +1642,7 @@ static const struct block_device_operations sd_fops = { ...@@ -1547,6 +1642,7 @@ static const struct block_device_operations sd_fops = {
.check_events = sd_check_events, .check_events = sd_check_events,
.revalidate_disk = sd_revalidate_disk, .revalidate_disk = sd_revalidate_disk,
.unlock_native_capacity = sd_unlock_native_capacity, .unlock_native_capacity = sd_unlock_native_capacity,
.pr_ops = &sd_pr_ops,
}; };
/** /**
......
...@@ -35,6 +35,7 @@ struct sg_io_hdr; ...@@ -35,6 +35,7 @@ struct sg_io_hdr;
struct bsg_job; struct bsg_job;
struct blkcg_gq; struct blkcg_gq;
struct blk_flush_queue; struct blk_flush_queue;
struct pr_ops;
#define BLKDEV_MIN_RQ 4 #define BLKDEV_MIN_RQ 4
#define BLKDEV_MAX_RQ 128 /* Default maximum */ #define BLKDEV_MAX_RQ 128 /* Default maximum */
...@@ -1629,6 +1630,7 @@ struct block_device_operations { ...@@ -1629,6 +1630,7 @@ struct block_device_operations {
/* this callback is with swap_lock and sometimes page table lock held */ /* this callback is with swap_lock and sometimes page table lock held */
void (*swap_slot_free_notify) (struct block_device *, unsigned long); void (*swap_slot_free_notify) (struct block_device *, unsigned long);
struct module *owner; struct module *owner;
const struct pr_ops *pr_ops;
}; };
extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int, extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
......
#ifndef LINUX_PR_H
#define LINUX_PR_H
#include <uapi/linux/pr.h>
struct pr_ops {
int (*pr_register)(struct block_device *bdev, u64 old_key, u64 new_key,
u32 flags);
int (*pr_reserve)(struct block_device *bdev, u64 key,
enum pr_type type, u32 flags);
int (*pr_release)(struct block_device *bdev, u64 key,
enum pr_type type);
int (*pr_preempt)(struct block_device *bdev, u64 old_key, u64 new_key,
enum pr_type type, bool abort);
int (*pr_clear)(struct block_device *bdev, u64 key);
};
#endif /* LINUX_PR_H */
#ifndef _UAPI_PR_H
#define _UAPI_PR_H
enum pr_type {
PR_WRITE_EXCLUSIVE = 1,
PR_EXCLUSIVE_ACCESS = 2,
PR_WRITE_EXCLUSIVE_REG_ONLY = 3,
PR_EXCLUSIVE_ACCESS_REG_ONLY = 4,
PR_WRITE_EXCLUSIVE_ALL_REGS = 5,
PR_EXCLUSIVE_ACCESS_ALL_REGS = 6,
};
struct pr_reservation {
__u64 key;
__u32 type;
__u32 flags;
};
struct pr_registration {
__u64 old_key;
__u64 new_key;
__u32 flags;
__u32 __pad;
};
struct pr_preempt {
__u64 old_key;
__u64 new_key;
__u32 type;
__u32 flags;
};
struct pr_clear {
__u64 key;
__u32 flags;
__u32 __pad;
};
#define PR_FL_IGNORE_KEY (1 << 0) /* ignore existing key */
#define IOC_PR_REGISTER _IOW('p', 200, struct pr_registration)
#define IOC_PR_RESERVE _IOW('p', 201, struct pr_reservation)
#define IOC_PR_RELEASE _IOW('p', 202, struct pr_reservation)
#define IOC_PR_PREEMPT _IOW('p', 203, struct pr_preempt)
#define IOC_PR_PREEMPT_ABORT _IOW('p', 204, struct pr_preempt)
#define IOC_PR_CLEAR _IOW('p', 205, struct pr_clear)
#endif /* _UAPI_PR_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment