set ro attribute of block device according to write-protection status
Subsystems (3)
cover set ro attribute of block device according to write-protection status
From: Light Hsieh <[email protected]>
Light Hsieh (3):
mmc: record wp_grp_size and boot_wp_status
mmc: check write-protection status during BLKROSET ioctl
block: set partition read/write policy according to write-protection
status
block/ioctl.c | 2 +- block/partition-generic.c | 10 +++ drivers/mmc/core/block.c | 217 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/mmc/core/mmc.c | 16 ++++ include/linux/blkdev.h | 1 + include/linux/mmc/card.h | 3 + include/linux/mmc/mmc.h | 2 + 7 files changed, 250 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.1.1.dirty
PATCH 1/3 mmc: record wp_grp_size and boot_wp_status
From: Light Hsieh <[email protected]>
1. record wp_grp_size (write-protection group size) in struct mmc_csd and struct mmc_card.
2. record boot_wp_status (boot partition write-protection status) in struct mmc_ext_csd.
--- drivers/mmc/core/mmc.c | 16 ++++++++++++++++ include/linux/mmc/card.h | 3 +++ include/linux/mmc/mmc.h | 1 + 3 files changed, 20 insertions(+)
drivers/mmc/core/mmc.c +16 -0
@@ -127,6 +127,16 @@ static void mmc_set_erase_size(struct mmc_card *card)mmc_init_erase (card); } +static void mmc_set_wp_grp_size (struct mmc_card *card) +{ +if (card->ext_csd.erase_group_def &1 ) + card->wp_grp_size = card->ext_csd.hc_erase_size * + card->ext_csd.raw_hc_erase_gap_size; +else + card->wp_grp_size = card->csd.erase_size * + (card->csd.wp_grp_size +1 ); +} +/* * Given a 128-bit response, decode to our card CSD structure. */
@@ -177,6 +187,7 @@ static int mmc_decode_csd(struct mmc_card *card) b =UNSTUFF_BITS (resp,37 ,5 ); csd->erase_size = (a +1 ) * (b +1 ); csd->erase_size <<= csd->write_blkbits -9 ; + csd->wp_grp_size =UNSTUFF_BITS (resp,32 ,5 ); }return 0 ;
@@ -496,6 +507,8 @@ static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd) card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP ]; card->ext_csd.boot_ro_lockable = true; + card->ext_csd.boot_wp_status = ext_csd[EXT_CSD_BOOT_WP_STATUS ]; +/* Save power class values */ card->ext_csd.raw_pwr_cl_52_195 = ext_csd[EXT_CSD_PWR_CL_52_195 ];
@@ -769,6 +782,7 @@ static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width) card->raw_csd[2], card->raw_csd[3]);MMC_DEV_ATTR (date,"%02d/%04d\n" , card->cid.month, card->cid.year);MMC_DEV_ATTR (erase_size,"%u\n" , card->erase_size <<9 ); +MMC_DEV_ATTR (wp_grp_size,"%u\n" , card->wp_grp_size <<9 );MMC_DEV_ATTR (preferred_erase_size,"%u\n" , card->pref_erase <<9 );MMC_DEV_ATTR (ffu_capable,"%d\n" , card->ext_csd.ffu_capable);MMC_DEV_ATTR (hwrev,"0x%x\n" , card->cid.hwrev);
@@ -828,6 +842,7 @@ static ssize_t mmc_dsr_show(struct device *dev, &dev_attr_csd.attr, &dev_attr_date.attr, &dev_attr_erase_size.attr, + &dev_attr_wp_grp_size.attr, &dev_attr_preferred_erase_size.attr, &dev_attr_fwrev.attr, &dev_attr_ffu_capable.attr,
@@ -1709,6 +1724,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,mmc_set_erase_size (card); } } +mmc_set_wp_grp_size (card);/* * Ensure eMMC user default partition is enabled
include/linux/mmc/card.h +3 -0
@@ -32,6 +32,7 @@ struct mmc_csd {unsigned int r2w_factor;unsigned int max_dtr;unsigned int erase_size;/* In sectors */ +unsigned int wp_grp_size;unsigned int read_blkbits;unsigned int write_blkbits;unsigned int capacity;
@@ -94,6 +95,7 @@ struct mmc_ext_csd {u8 raw_exception_status;/* 54 */ u8 raw_partition_support;/* 160 */ u8 raw_rpmb_size_mult;/* 168 */ +u8 boot_wp_status;/* 174 */ u8 raw_erased_mem_count;/* 181 */ u8 strobe_support;/* 184 */ u8 raw_ext_csd_structure;/* 194 */
@@ -278,6 +280,7 @@ struct mmc_card {unsigned int eg_boundary;/* don't cross erase-group boundaries */ unsigned int erase_arg;/* erase / trim / discard */ u8 erased_byte;/* value of erased bytes */ +unsigned int wp_grp_size;/* write group size(sectors) */ u32 raw_cid[4 ];/* raw card CID */ u32 raw_csd[4 ];/* raw card CSD */
include/linux/mmc/mmc.h +1 -0
@@ -259,6 +259,7 @@ static inline bool mmc_op_multi(u32 opcode)#define EXT_CSD_RPMB_MULT 168/* RO */ #define EXT_CSD_FW_CONFIG 169/* R/W */ #define EXT_CSD_BOOT_WP 173/* R/W */ +#define EXT_CSD_BOOT_WP_STATUS 174/* RO */ #define EXT_CSD_ERASE_GROUP_DEF 175/* R/W */ #define EXT_CSD_PART_CONFIG 179/* R/W */ #define EXT_CSD_ERASED_MEM_CONT 181/* RO */
Signature · 1 line
1.8.1.1.dirty
PATCH 2/3 mmc: check write-protection status during BLKROSET ioctl
In reply to [email protected]From: Light Hsieh <[email protected]>
Since MMC layer does not implement BLKROSET ioctl, BLKROSET ioctl that tries to clear ro attribute of an mmcblk device will get -EINVAL from __blkdev_driver_ioctl(). blkdev_roset() regard -EINVAL as "unrecogized ioctl" and then clear the ro attribute un-conditionally. However, when eMMC write-protection (power-on, temporarily write, or permanent) is enabled in some area, this un-conditional clear of ro will lead to issue. From user's view, eMMC device is writable since ro is not set. But write operation sent to eMMC will get write-protection error. Since most write are asynchronus buffered write, such write-protection error won't be delivered to user who send the write operation.
This patch implement BLKROSET in MMC layer.
1. For SD device, 0 is retured.
2. For setting ro to eMMC area, 0 is returned without any other check.
2. For clearing ro to eMMC area, write-proetction status is checked:
2a. For boot0 or boot1 partition, boot_wp_status get from EXTCSD is
checked.
-EACCES is returned when the target boot partition is write-protected; 0 is returned otherwise.
2b. For user area partition, one or more MMC_SEND_WRITE_PROT_TYPE commands
are sent to get/check write-protection status of target address range. -EACCES is returned when target address range is fully/partially
write-protected;
0 is returned otherwise.
With the above implementation and correct ioctl parameters, return value of __blkdev_driver_ioctl() will be 0 or -EACCES. blkdev_roset() can continue to clear ro attribute when return value is 0, which means whole target eMMC address range is not write-protected.
--- block/ioctl.c | 2 +- drivers/mmc/core/block.c | 216 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/mmc/mmc.h | 1 + 3 files changed, 218 insertions(+), 1 deletion(-)
block/ioctl.c +1 -1
@@ -485,7 +485,7 @@ static int blkdev_roset(struct block_device *bdev, fmode_t mode,return -EACCES ; ret =__blkdev_driver_ioctl (bdev, mode, cmd, arg); -if (!is_unrecognized_ioctl (ret)) +if (ret && !is_unrecognized_ioctl (ret))return ret;if (get_user (n, (int __user *)arg))return -EFAULT ;
drivers/mmc/core/block.c +216 -0
228 lines not on this page · raw message
include/linux/mmc/mmc.h +1 -0
@@ -69,6 +69,7 @@#define MMC_SET_WRITE_PROT 28/* ac [31:0] data addr R1b */ #define MMC_CLR_WRITE_PROT 29/* ac [31:0] data addr R1b */ #define MMC_SEND_WRITE_PROT 30/* adtc [31:0] wpdata addr R1 */ +#define MMC_SEND_WRITE_PROT_TYPE 31/* adtc [31:0] wpdata addr R1 */ /* class 5 */ #define MMC_ERASE_GROUP_START 35/* ac [31:0] data addr R1 */
Signature · 1 line
1.8.1.1.dirty
PATCH 3/3 block: set partition read/write policy according to write-protection status
In reply to [email protected]From: Light Hsieh <[email protected]>
For storage device with write-protection support, e.g. eMMC, register check_disk_range_wp() in struct block_device_operations for checking write-protection status. When creating block device for a partition, set read/write policy according to result of check_disk_range_wp() operation (if registered).
Without this patch, ro attribute is not set for created block device of write-protected partition. User perform asynchronous buffered write to such partition won't get immediate error and therefore he won't be awared that write is not actually performed.
With this patch, ro attribute is set for created block device of write-protected partition. User perform asynchronous buffered write to such partition will get immediate error and therefore he will be awared.
--- block/partition-generic.c | 10 ++++++++++ drivers/mmc/core/block.c | 1 + include/linux/blkdev.h | 1 + 3 files changed, 12 insertions(+)
block/partition-generic.c +10 -0
@@ -394,6 +394,16 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno, goto out_free_info; pdev->devt = devt; +if (!p->policy) { +if (disk->fops->check_disk_range_wp) { + err = disk->fops->check_disk_range_wp (disk, start, len); +if (err >0 ) + p->policy =1 ; +else if (err !=0 ) + goto out_free_info; + } + } +/* delay uevent until 'holders' subdir is created */ dev_set_uevent_suppress (pdev,1 ); err =device_add (pdev);
drivers/mmc/core/block.c +1 -0
@@ -1047,6 +1047,7 @@ static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,#ifdef CONFIG_COMPAT .compat_ioctl = mmc_blk_compat_ioctl, #endif + .check_disk_range_wp = mmc_blk_check_disk_range_wp, }; static int mmc_blk_part_switch_pre(struct mmc_card *card,
include/linux/blkdev.h +1 -0
@@ -1707,6 +1707,7 @@ struct block_device_operations {void (*swap_slot_free_notify) (struct block_device *,unsigned long );int (*report_zones)(struct gendisk *,sector_t sector,unsigned int nr_zones,report_zones_cb cb,void *data); +int (*check_disk_range_wp)(struct gendisk *d,sector_t s,sector_t l);struct module *owner;const struct pr_ops *pr_ops; };
Signature · 1 line
1.8.1.1.dirty
Re: [PATCH v1 3/3] block: set partition read/write policy according to write-protection status
Quoted 8 lines · On Wed, Mar 04, 2020 at 10:51:14AM +0800, [email protected] wrote:
From: Light Hsieh <[email protected]>
For storage device with write-protection support, e.g. eMMC, register
check_disk_range_wp()in structblock_device_operationsfor checking write-protection status. When creating block device for a partition, set read/write policy according to result ofcheck_disk_range_wp()operation (if registered).
Quoted 7 lines
Without this patch, ro attribute is not set for created block device of write-protected partition. User perform asynchronous buffered write to such partition won't get immediate error and therefore he won't be awared that write is not actually performed.
With this patch, ro attribute is set for created block device of write-protected partition. User perform asynchronous buffered write to such partition will get immediate error and therefore he will be awared.
NAK. This is complete BS. Partitions are a complete software concepts and idiotic features like a range read only should not interact with it at all (and I urge all Linux users to never make use of such broken features, so the less support we have for them, the better).