Files
kernel_google_wahoo/block/partition-generic.c
Greg Kroah-Hartman b120ed1853 Merge 4.4.128 into android-msm-wahoo-4.4-lts
Linux 4.4.128
  * Revert "xhci: plat: Register shutdown for xhci_plat"
      drivers/usb/host/xhci-plat.c
    vrf: Fix use after free and double free in vrf_finish_output
  * ipv6: the entire IPv6 header chain must fit the first fragment
      net/ipv6/ip6_output.c
  * net/ipv6: Increment OUTxxx counters after netfilter hook
      net/ipv6/ip6_output.c
  * net sched actions: fix dumping which requires several messages to user space
      net/sched/act_api.c
    r8169: fix setting driver_data after register_netdev
  * vti6: better validate user provided tunnel names
      net/ipv6/ip6_vti.c
  * ip6_tunnel: better validate user provided tunnel names
      net/ipv6/ip6_tunnel.c
  * ip6_gre: better validate user provided tunnel names
      net/ipv6/ip6_gre.c
  * ipv6: sit: better validate user provided tunnel names
      net/ipv6/sit.c
  * ip_tunnel: better validate user provided tunnel names
      net/ipv4/ip_tunnel.c
  * net: fool proof dev_valid_name()
      net/core/dev.c
  * bonding: process the err returned by dev_set_allmulti properly in bond_enslave
      drivers/net/bonding/bond_main.c
  * bonding: move dev_mc_sync after master_upper_dev_link in bond_enslave
      drivers/net/bonding/bond_main.c
  * bonding: fix the err path for dev hwaddr sync in bond_enslave
      drivers/net/bonding/bond_main.c
  * vlan: also check phy_driver ts_info for vlan's real device
      net/8021q/vlan_dev.c
    vhost: correctly remove wait queue during poll failure
    sky2: Increase D3 delay to sky2 stops working after suspend
  * sctp: sctp_sockaddr_af must check minimal addr length for AF_INET6
      net/sctp/socket.c
  * sctp: do not leak kernel memory to user space
      net/sctp/ipv6.c
    pptp: remove a buggy dst release in pptp_connect()
    net/sched: fix NULL dereference in the error path of tcf_bpf_init()
  * netlink: make sure nladdr has correct size in netlink_connect()
      net/netlink/af_netlink.c
  * net/ipv6: Fix route leaking between VRFs
      net/ipv6/route.c
  * net: fix possible out-of-bound read in skb_network_protocol()
      net/core/dev.c
  * arp: fix arp_filter on l3slave devices
      net/ipv4/arp.c
  * Kbuild: provide a __UNIQUE_ID for clang
      include/linux/compiler-clang.h
  * futex: Remove requirement for lock_page() in get_futex_key()
      kernel/futex.c
  * random: use lockless method of accessing and updating f->reg_idx
      drivers/char/random.c
    virtio_net: check return value of skb_to_sgvec in one more location
    virtio_net: check return value of skb_to_sgvec always
    rxrpc: check return value of skb_to_sgvec always
  * ipsec: check return value of skb_to_sgvec always
      net/ipv4/ah4.c
      net/ipv4/esp4.c
      net/ipv6/ah6.c
      net/ipv6/esp6.c
    perf tools: Fix copyfile_offset update of output offset
    cxgb4vf: Fix SGE FL buffer initialization logic for 64K pages
    EDAC, mv64x60: Fix an error handling path
    tty: n_gsm: Allow ADM response in addition to UA for control dlci
  * blk-mq: fix kernel oops in blk_mq_tag_idle()
      block/blk-mq.c
    scsi: libsas: initialize sas_phy status according to response of DISCOVER
    scsi: libsas: fix error when getting phy events
    scsi: libsas: fix memory leak in sas_smp_get_phy_events()
    bcache: segregate flash only volume write streams
    bcache: stop writeback thread after detaching
    vxlan: dont migrate permanent fdb entries during learn
    s390/dasd: fix hanging safe offline
  * ACPICA: Disassembler: Abort on an invalid/unknown AML opcode
      drivers/acpi/acpica/psobject.c
  * ACPICA: Events: Add runtime stub support for event APIs
      drivers/acpi/acpica/evxfevnt.c
    cpuidle: dt: Add missing 'of_node_put()'
  * Bluetooth: Send HCI Set Event Mask Page 2 command only when needed
      net/bluetooth/hci_core.c
    iio: magnetometer: st_magn_spi: fix spi_device_id table
    sparc64: ldc abort during vds iso boot
  * sctp: fix recursive locking warning in sctp_do_peeloff
      net/sctp/socket.c
    bnx2x: Allow vfs to disable txvlan offload
    xen: avoid type warning in xchg_xen_ulong
  * skbuff: only inherit relevant tx_flags
      net/core/skbuff.c
    perf tests: Decompress kernel module before objdump
    net: emac: fix reset timeout with AR8035 phy
  * Fix loop device flush before configure v3
      drivers/block/loop.c
    MIPS: kprobes: flush_insn_slot should flush only if probe initialised
    MIPS: mm: adjust PKMAP location
    MIPS: mm: fixed mappings: correct initialisation
  * perf/core: Correct event creation with PERF_FORMAT_GROUP
      kernel/events/core.c
    e1000e: Undo e1000e_pm_freeze if __e1000_shutdown fails
    ARM: imx: Add MXC_CPU_IMX6ULL and cpu_is_imx6ull
  * net: phy: avoid genphy_aneg_done() for PHYs without clause 22 support
      drivers/net/phy/phy.c
    mceusb: sporadic RX truncation corruption fix
    cx25840: fix unchecked return values
    e1000e: fix race condition around skb_tstamp_tx()
    tags: honor COMPILED_SOURCE with apart output directory
    perf report: Ensure the perf DSO mapping matches what libdw sees
    perf header: Set proper module name when build-id event found
    net/mlx4: Check if Granular QoS per VF has been enabled before updating QP qos_vport
    net/mlx4: Fix the check in attaching steering rules
  * sit: reload iphdr in ipip6_rcv
      net/ipv6/sit.c
  * skbuff: return -EMSGSIZE in skb_to_sgvec to prevent overflow
      include/linux/skbuff.h
      net/core/skbuff.c
    bio-integrity: Do not allocate integrity context for bio w/o data
    Fix serial console on SNI RM400 machines
    cxgb4: fix incorrect cim_la output for T6
    drm/omap: fix tiled buffer stride calculations
    mISDN: Fix a sleep-in-atomic bug
    qlcnic: Fix a sleep-in-atomic bug in qlcnic_82xx_hw_write_wx_2M and qlcnic_82xx_hw_read_wx_2M
  * perf trace: Add mmap alias for s390
      tools/perf/builtin-trace.c
    powerpc/spufs: Fix coredump of SPU contexts
    clk: Fix __set_clk_rates error print-string
    clk: scpi: fix return type of __scpi_dvfs_round_rate
    KVM: SVM: do not zero out segment attributes if segment is unusable or not present
    net: freescale: fix potential null pointer dereference
  * SUNRPC: ensure correct error is reported by xs_tcp_setup_socket()
      net/sunrpc/xprtsock.c
  * rtc: interface: Validate alarm-time before handling rollover
      drivers/rtc/interface.c
    rtc: opal: Handle disabled TPO in opal_get_tpo_time()
    cxgb4: FW upgrade fixes
    net/mlx5: avoid build warning for uniprocessor
  * arm64: futex: Fix undefined behaviour with FUTEX_OP_OPARG_SHIFT usage
      arch/arm64/include/asm/futex.h
    dmaengine: imx-sdma: Handle return value of clk_prepare_enable
    powerpc/[booke|4xx]: Don't clobber TCR[WP] when setting TCR[DIE]
    ovl: filter trusted xattr for non-admin
    hdlcdrv: Fix divide by zero in hdlcdrv_ioctl
    wl1251: check return from call to wl1251_acx_arp_ip_filter
    ASoC: Intel: sst: Fix the return value of 'sst_send_byte_stream_mrfld()'
  * gpio: label descriptors using the device name
      drivers/gpio/gpiolib.c
  * vfb: fix video mode and line_length being set when loaded
      drivers/video/fbdev/vfb.c
    scsi: mpt3sas: Proper handling of set/clear of "ATA command pending" flag.
    scsi: libiscsi: Allow sd_shutdown on bad transport
    ASoC: Intel: cht_bsw_rt5645: Analog Mic support
  * media: videobuf2-core: don't go out of the buffer range
      drivers/media/v4l2-core/videobuf2-core.c
    hwmon: (ina2xx) Make calibration register value fixed
    rds; Reset rs->rs_bound_addr in rds_add_bound() failure path
  * l2tp: fix missing print session offset info
      net/l2tp/l2tp_netlink.c
    perf probe: Add warning message if there is unexpected event name
    thermal: power_allocator: fix one race condition issue for thermal_instances list
    ARM: dts: ls1021a: add "fsl,ls1021a-esdhc" compatible string to esdhc node
    net: llc: add lock_sock in llc_ui_bind to avoid a race condition
    KVM: nVMX: Fix handling of lmsw instruction
  * bonding: Don't update slave->link until ready to commit
      drivers/net/bonding/bond_main.c
    Input: elan_i2c - clear INT before resetting controller
  * net: move somaxconn init from sysctl code
      net/core/net_namespace.c
      net/core/sysctl_net_core.c
  * tcp: better validation of received ack sequences
      net/ipv4/tcp_input.c
  * ext4: fix off-by-one on max nr_pages in ext4_find_unwritten_pgoff()
      fs/ext4/file.c
  * fix race in drivers/char/random.c:get_reg()
      drivers/char/random.c
    scsi: bnx2fc: fix race condition in bnx2fc_get_host_stats()
  * ASoC: rsnd: SSI PIO adjust to 24bit mode
      sound/soc/sh/rcar/ssi.c
    pNFS/flexfiles: missing error code in ff_layout_alloc_lseg()
  * netfilter: ctnetlink: fix incorrect nf_ct_put during hash resize
      net/netfilter/nf_conntrack_netlink.c
  * libceph: NULL deref on crush_decode() error path
      net/ceph/osdmap.c
    net: ieee802154: fix net_device reference release too early
    mlx5: fix bug reading rss_hash_type from CQE
  * block: fix an error code in add_partition()
      block/partition-generic.c
  * selinux: do not check open permission on sockets
      security/selinux/hooks.c
    net/mlx5: Tolerate irq_set_affinity_hint() failures
  * sched/numa: Use down_read_trylock() for the mmap_sem
      kernel/sched/fair.c
    leds: pca955x: Correct I2C Functionality
    ray_cs: Avoid reading past end of buffer
    ARM: davinci: da8xx: Create DSP device only when assigned memory
    md-cluster: fix potential lock issue in add_new_disk
  * ext4: handle the rest of ext4_mb_load_buddy() ENOMEM errors
      fs/ext4/mballoc.c
    iio: hi8435: cleanup reset gpio
    iio: hi8435: avoid garbage event at first enable
  * xfrm: fix state migration copy replay sequence numbers
      net/xfrm/xfrm_state.c
    selftests/powerpc: Fix TM resched DSCR test with some compilers
    ath5k: fix memory leak on buf on failed eeprom read
    powerpc/mm: Fix virt_addr_valid() etc. on 64-bit hash
    scsi: csiostor: fix use after free in csio_hw_use_fwconfig()
    sh_eth: Use platform device for printing before register_netdev()
  * serial: sh-sci: Fix race condition causing garbage during shutdown
      drivers/tty/serial/sh-sci.c
  * serial: 8250: omap: Disable DMA for console UART
      drivers/tty/serial/8250/8250_omap.c
  * USB: ene_usb6250: fix SCSI residue overwriting
      drivers/usb/storage/ene_ub6250.c
    net: x25: fix one potential use-after-free issue
  * USB: ene_usb6250: fix first command execution
      drivers/usb/storage/ene_ub6250.c
  * usb: chipidea: properly handle host or gadget initialization failure
      drivers/usb/chipidea/core.c
  * arp: honour gratuitous ARP _replies_
      net/ipv4/arp.c
  * neighbour: update neigh timestamps iff update is effective
      net/core/neighbour.c
    ata: libahci: properly propagate return value of platform_get_irq()
    btrfs: fix incorrect error return ret being passed to mapping_set_error
    usb: dwc3: keystone: check return value
    async_tx: Fix DMA_PREP_FENCE usage in do_async_gen_syndrome()
  * ipv6: avoid dad-failures for addresses with NODAD
      net/ipv6/addrconf.c
    ARM: dts: imx6qdl-wandboard: Fix audio channel swap
    x86/tsc: Provide 'tsc=unstable' boot parameter
    staging: wlan-ng: prism2mgmt.c: fixed a double endian conversion before calling hfa384x_drvr_setconfig16, also fixes relative sparse warning
    ARM: dts: imx53-qsrb: Pulldown PMIC IRQ pin
    PowerCap: Fix an error code in powercap_register_zone()
    bus: brcmstb_gisb: correct support for 64-bit address output
    bus: brcmstb_gisb: Use register offsets with writes too
  * SMB2: Fix share type handling
      fs/cifs/smb2pdu.c
    vmxnet3: ensure that adapter is in proper state during force_close
    KVM: PPC: Book3S PR: Check copy_to/from_user return values
    Input: elantech - force relative mode on a certain module
    Input: elan_i2c - check if device is there before really probing
    netxen_nic: set rcode to the return status from the call to netxen_issue_cmd
    net: qca_spi: Fix alignment issues in rx path
  * blk-mq: NVMe 512B/4K+T10 DIF/DIX format returns I/O error on dd with split op
      block/blk-mq.c
  * CIFS: silence lockdep splat in cifs_relock_file()
      fs/cifs/file.c
  * NFSv4.1: Work around a Linux server bug...
      fs/nfs/nfs4proc.c
    net/mlx4_en: Avoid adding steering rules with invalid ring
    s390: move _text symbol to address higher than zero
  * pidns: disable pid allocation if pid_ns_prepare_proc() is failed in alloc_pid()
      kernel/pid.c
    drivers/misc/vmw_vmci/vmci_queue_pair.c: fix a couple integer overflow tests
    lockd: fix lockd shutdown race
    net: ethernet: ti: cpsw: adjust cpsw fifos depth for fullduplex flow control
  * net: cdc_ncm: Fix TX zero padding
      drivers/net/usb/cdc_ncm.c
    ipmi_ssif: unlock on allocation failure
    qlge: Avoid reading past end of buffer
    bna: Avoid reading past end of buffer
  * mac80211: bail out from prep_connection() if a reconfig is ongoing
      net/mac80211/mlme.c
  * af_key: Fix slab-out-of-bounds in pfkey_compile_policy.
      net/key/af_key.c
    IB/srpt: Fix abort handling
  * NFSv4.1: RECLAIM_COMPLETE must handle NFS4ERR_CONN_NOT_BOUND_TO_SESSION
      fs/nfs/nfs4proc.c
      fs/nfs/nfs4state.c
    x86/asm: Don't use RBP as a temporary register in csum_partial_copy_generic()
    rtc: snvs: fix an incorrect check of return value
    md/raid5: make use of spin_lock_irq over local_irq_disable + spin_lock
  * cfg80211: make RATE_INFO_BW_20 the default
      include/net/cfg80211.h

Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2018-07-28 05:07:42 -07:00

592 lines
14 KiB
C

/*
* Code extracted from drivers/block/genhd.c
* Copyright (C) 1991-1998 Linus Torvalds
* Re-organised Feb 1998 Russell King
*
* We now have independent partition support from the
* block drivers, which allows all the partition code to
* be grouped in one location, and it to be mostly self
* contained.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/ctype.h>
#include <linux/genhd.h>
#include <linux/blktrace_api.h>
#include "partitions/check.h"
#ifdef CONFIG_BLK_DEV_MD
extern void md_autodetect_dev(dev_t dev);
#endif
/*
* disk_name() is used by partition check code and the genhd driver.
* It formats the devicename of the indicated disk into
* the supplied buffer (of size at least 32), and returns
* a pointer to that same buffer (for convenience).
*/
char *disk_name(struct gendisk *hd, int partno, char *buf)
{
if (!partno)
snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
else
snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
return buf;
}
const char *bdevname(struct block_device *bdev, char *buf)
{
return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
}
EXPORT_SYMBOL(bdevname);
/*
* There's very little reason to use this, you should really
* have a struct block_device just about everywhere and use
* bdevname() instead.
*/
const char *__bdevname(dev_t dev, char *buffer)
{
scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
MAJOR(dev), MINOR(dev));
return buffer;
}
EXPORT_SYMBOL(__bdevname);
static ssize_t part_partition_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%d\n", p->partno);
}
static ssize_t part_start_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
}
ssize_t part_size_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%llu\n",(unsigned long long)part_nr_sects_read(p));
}
static ssize_t part_ro_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%d\n", p->policy ? 1 : 0);
}
static ssize_t part_alignment_offset_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
}
static ssize_t part_discard_alignment_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%u\n", p->discard_alignment);
}
ssize_t part_stat_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
int cpu;
cpu = part_stat_lock();
part_round_stats(cpu, p);
part_stat_unlock();
return sprintf(buf,
"%8lu %8lu %8llu %8u "
"%8lu %8lu %8llu %8u "
"%8u %8u %8u"
"\n",
part_stat_read(p, ios[READ]),
part_stat_read(p, merges[READ]),
(unsigned long long)part_stat_read(p, sectors[READ]),
jiffies_to_msecs(part_stat_read(p, ticks[READ])),
part_stat_read(p, ios[WRITE]),
part_stat_read(p, merges[WRITE]),
(unsigned long long)part_stat_read(p, sectors[WRITE]),
jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
part_in_flight(p),
jiffies_to_msecs(part_stat_read(p, io_ticks)),
jiffies_to_msecs(part_stat_read(p, time_in_queue)));
}
ssize_t part_inflight_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%8u %8u\n", atomic_read(&p->in_flight[0]),
atomic_read(&p->in_flight[1]));
}
#ifdef CONFIG_FAIL_MAKE_REQUEST
ssize_t part_fail_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct hd_struct *p = dev_to_part(dev);
return sprintf(buf, "%d\n", p->make_it_fail);
}
ssize_t part_fail_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hd_struct *p = dev_to_part(dev);
int i;
if (count > 0 && sscanf(buf, "%d", &i) > 0)
p->make_it_fail = (i == 0) ? 0 : 1;
return count;
}
#endif
static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
static DEVICE_ATTR(ro, S_IRUGO, part_ro_show, NULL);
static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
NULL);
static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
#ifdef CONFIG_FAIL_MAKE_REQUEST
static struct device_attribute dev_attr_fail =
__ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
#endif
static struct attribute *part_attrs[] = {
&dev_attr_partition.attr,
&dev_attr_start.attr,
&dev_attr_size.attr,
&dev_attr_ro.attr,
&dev_attr_alignment_offset.attr,
&dev_attr_discard_alignment.attr,
&dev_attr_stat.attr,
&dev_attr_inflight.attr,
#ifdef CONFIG_FAIL_MAKE_REQUEST
&dev_attr_fail.attr,
#endif
NULL
};
static struct attribute_group part_attr_group = {
.attrs = part_attrs,
};
static const struct attribute_group *part_attr_groups[] = {
&part_attr_group,
#ifdef CONFIG_BLK_DEV_IO_TRACE
&blk_trace_attr_group,
#endif
NULL
};
static void part_release(struct device *dev)
{
struct hd_struct *p = dev_to_part(dev);
blk_free_devt(dev->devt);
hd_free_part(p);
kfree(p);
}
static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
{
struct hd_struct *part = dev_to_part(dev);
add_uevent_var(env, "PARTN=%u", part->partno);
if (part->info && part->info->volname[0])
add_uevent_var(env, "PARTNAME=%s", part->info->volname);
return 0;
}
struct device_type part_type = {
.name = "partition",
.groups = part_attr_groups,
.release = part_release,
.uevent = part_uevent,
};
static void delete_partition_rcu_cb(struct rcu_head *head)
{
struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
part->start_sect = 0;
part->nr_sects = 0;
part_stat_set_all(part, 0);
put_device(part_to_dev(part));
}
void __delete_partition(struct percpu_ref *ref)
{
struct hd_struct *part = container_of(ref, struct hd_struct, ref);
call_rcu(&part->rcu_head, delete_partition_rcu_cb);
}
void delete_partition(struct gendisk *disk, int partno)
{
struct disk_part_tbl *ptbl = disk->part_tbl;
struct hd_struct *part;
if (partno >= ptbl->len)
return;
part = ptbl->part[partno];
if (!part)
return;
rcu_assign_pointer(ptbl->part[partno], NULL);
rcu_assign_pointer(ptbl->last_lookup, NULL);
kobject_put(part->holder_dir);
device_del(part_to_dev(part));
hd_struct_kill(part);
}
static ssize_t whole_disk_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return 0;
}
static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
whole_disk_show, NULL);
struct hd_struct *add_partition(struct gendisk *disk, int partno,
sector_t start, sector_t len, int flags,
struct partition_meta_info *info)
{
struct hd_struct *p;
dev_t devt = MKDEV(0, 0);
struct device *ddev = disk_to_dev(disk);
struct device *pdev;
struct disk_part_tbl *ptbl;
const char *dname;
int err;
err = disk_expand_part_tbl(disk, partno);
if (err)
return ERR_PTR(err);
ptbl = disk->part_tbl;
if (ptbl->part[partno])
return ERR_PTR(-EBUSY);
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p)
return ERR_PTR(-EBUSY);
if (!init_part_stats(p)) {
err = -ENOMEM;
goto out_free;
}
seqcount_init(&p->nr_sects_seq);
pdev = part_to_dev(p);
p->start_sect = start;
p->alignment_offset =
queue_limit_alignment_offset(&disk->queue->limits, start);
p->discard_alignment =
queue_limit_discard_alignment(&disk->queue->limits, start);
p->nr_sects = len;
p->partno = partno;
p->policy = get_disk_ro(disk);
if (info) {
struct partition_meta_info *pinfo = alloc_part_info(disk);
if (!pinfo) {
err = -ENOMEM;
goto out_free_stats;
}
memcpy(pinfo, info, sizeof(*info));
p->info = pinfo;
}
dname = dev_name(ddev);
if (isdigit(dname[strlen(dname) - 1]))
dev_set_name(pdev, "%sp%d", dname, partno);
else
dev_set_name(pdev, "%s%d", dname, partno);
device_initialize(pdev);
pdev->class = &block_class;
pdev->type = &part_type;
pdev->parent = ddev;
err = blk_alloc_devt(p, &devt);
if (err)
goto out_free_info;
pdev->devt = devt;
/* delay uevent until 'holders' subdir is created */
dev_set_uevent_suppress(pdev, 1);
err = device_add(pdev);
if (err)
goto out_put;
err = -ENOMEM;
p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
if (!p->holder_dir)
goto out_del;
dev_set_uevent_suppress(pdev, 0);
if (flags & ADDPART_FLAG_WHOLEDISK) {
err = device_create_file(pdev, &dev_attr_whole_disk);
if (err)
goto out_del;
}
err = hd_ref_init(p);
if (err) {
if (flags & ADDPART_FLAG_WHOLEDISK)
goto out_remove_file;
goto out_del;
}
/* everything is up and running, commence */
rcu_assign_pointer(ptbl->part[partno], p);
/* suppress uevent if the disk suppresses it */
if (!dev_get_uevent_suppress(ddev))
kobject_uevent(&pdev->kobj, KOBJ_ADD);
return p;
out_free_info:
free_part_info(p);
out_free_stats:
free_part_stats(p);
out_free:
kfree(p);
return ERR_PTR(err);
out_remove_file:
device_remove_file(pdev, &dev_attr_whole_disk);
out_del:
kobject_put(p->holder_dir);
device_del(pdev);
out_put:
put_device(pdev);
blk_free_devt(devt);
return ERR_PTR(err);
}
static bool disk_unlock_native_capacity(struct gendisk *disk)
{
const struct block_device_operations *bdops = disk->fops;
if (bdops->unlock_native_capacity &&
!(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
printk(KERN_CONT "enabling native capacity\n");
bdops->unlock_native_capacity(disk);
disk->flags |= GENHD_FL_NATIVE_CAPACITY;
return true;
} else {
printk(KERN_CONT "truncated\n");
return false;
}
}
static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
{
struct disk_part_iter piter;
struct hd_struct *part;
int res;
if (bdev->bd_part_count || bdev->bd_super)
return -EBUSY;
res = invalidate_partition(disk, 0);
if (res)
return res;
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
while ((part = disk_part_iter_next(&piter)))
delete_partition(disk, part->partno);
disk_part_iter_exit(&piter);
return 0;
}
int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
{
struct parsed_partitions *state = NULL;
struct hd_struct *part;
int p, highest, res;
rescan:
if (state && !IS_ERR(state)) {
free_partitions(state);
state = NULL;
}
res = drop_partitions(disk, bdev);
if (res)
return res;
if (disk->fops->revalidate_disk)
disk->fops->revalidate_disk(disk);
check_disk_size_change(disk, bdev);
bdev->bd_invalidated = 0;
if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
return 0;
if (IS_ERR(state)) {
/*
* I/O error reading the partition table. If any
* partition code tried to read beyond EOD, retry
* after unlocking native capacity.
*/
if (PTR_ERR(state) == -ENOSPC) {
printk(KERN_WARNING "%s: partition table beyond EOD, ",
disk->disk_name);
if (disk_unlock_native_capacity(disk))
goto rescan;
}
return -EIO;
}
/*
* If any partition code tried to read beyond EOD, try
* unlocking native capacity even if partition table is
* successfully read as we could be missing some partitions.
*/
if (state->access_beyond_eod) {
printk(KERN_WARNING
"%s: partition table partially beyond EOD, ",
disk->disk_name);
if (disk_unlock_native_capacity(disk))
goto rescan;
}
/* tell userspace that the media / partition table may have changed */
kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
/* Detect the highest partition number and preallocate
* disk->part_tbl. This is an optimization and not strictly
* necessary.
*/
for (p = 1, highest = 0; p < state->limit; p++)
if (state->parts[p].size)
highest = p;
disk_expand_part_tbl(disk, highest);
/* add partitions */
for (p = 1; p < state->limit; p++) {
sector_t size, from;
struct partition_meta_info *info = NULL;
size = state->parts[p].size;
if (!size)
continue;
from = state->parts[p].from;
if (from >= get_capacity(disk)) {
printk(KERN_WARNING
"%s: p%d start %llu is beyond EOD, ",
disk->disk_name, p, (unsigned long long) from);
if (disk_unlock_native_capacity(disk))
goto rescan;
continue;
}
if (from + size > get_capacity(disk)) {
printk(KERN_WARNING
"%s: p%d size %llu extends beyond EOD, ",
disk->disk_name, p, (unsigned long long) size);
if (disk_unlock_native_capacity(disk)) {
/* free state and restart */
goto rescan;
} else {
/*
* we can not ignore partitions of broken tables
* created by for example camera firmware, but
* we limit them to the end of the disk to avoid
* creating invalid block devices
*/
size = get_capacity(disk) - from;
}
}
if (state->parts[p].has_info)
info = &state->parts[p].info;
part = add_partition(disk, p, from, size,
state->parts[p].flags,
&state->parts[p].info);
if (IS_ERR(part)) {
printk(KERN_ERR " %s: p%d could not be added: %ld\n",
disk->disk_name, p, -PTR_ERR(part));
continue;
}
#ifdef CONFIG_BLK_DEV_MD
if (state->parts[p].flags & ADDPART_FLAG_RAID)
md_autodetect_dev(part_to_dev(part)->devt);
#endif
}
free_partitions(state);
return 0;
}
int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
{
int res;
if (!bdev->bd_invalidated)
return 0;
res = drop_partitions(disk, bdev);
if (res)
return res;
set_capacity(disk, 0);
check_disk_size_change(disk, bdev);
bdev->bd_invalidated = 0;
/* tell userspace that the media / partition table may have changed */
kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
return 0;
}
unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
{
struct address_space *mapping = bdev->bd_inode->i_mapping;
struct page *page;
page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
NULL);
if (!IS_ERR(page)) {
if (PageError(page))
goto fail;
p->v = page;
return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
fail:
page_cache_release(page);
}
p->v = NULL;
return NULL;
}
EXPORT_SYMBOL(read_dev_sector);