LCOV - code coverage report
Current view: top level - block - genhd.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 278 752 37.0 %
Date: 2021-04-22 12:43:58 Functions: 33 76 43.4 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  *  gendisk handling
       4             :  *
       5             :  * Portions Copyright (C) 2020 Christoph Hellwig
       6             :  */
       7             : 
       8             : #include <linux/module.h>
       9             : #include <linux/ctype.h>
      10             : #include <linux/fs.h>
      11             : #include <linux/genhd.h>
      12             : #include <linux/kdev_t.h>
      13             : #include <linux/kernel.h>
      14             : #include <linux/blkdev.h>
      15             : #include <linux/backing-dev.h>
      16             : #include <linux/init.h>
      17             : #include <linux/spinlock.h>
      18             : #include <linux/proc_fs.h>
      19             : #include <linux/seq_file.h>
      20             : #include <linux/slab.h>
      21             : #include <linux/kmod.h>
      22             : #include <linux/mutex.h>
      23             : #include <linux/idr.h>
      24             : #include <linux/log2.h>
      25             : #include <linux/pm_runtime.h>
      26             : #include <linux/badblocks.h>
      27             : 
      28             : #include "blk.h"
      29             : 
      30             : static struct kobject *block_depr;
      31             : 
      32             : DECLARE_RWSEM(bdev_lookup_sem);
      33             : 
      34             : /* for extended dynamic devt allocation, currently only one major is used */
      35             : #define NR_EXT_DEVT             (1 << MINORBITS)
      36             : static DEFINE_IDA(ext_devt_ida);
      37             : 
      38             : static void disk_check_events(struct disk_events *ev,
      39             :                               unsigned int *clearing_ptr);
      40             : static void disk_alloc_events(struct gendisk *disk);
      41             : static void disk_add_events(struct gendisk *disk);
      42             : static void disk_del_events(struct gendisk *disk);
      43             : static void disk_release_events(struct gendisk *disk);
      44             : 
      45           1 : void set_capacity(struct gendisk *disk, sector_t sectors)
      46             : {
      47           1 :         struct block_device *bdev = disk->part0;
      48             : 
      49           1 :         spin_lock(&bdev->bd_size_lock);
      50           1 :         i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
      51           1 :         spin_unlock(&bdev->bd_size_lock);
      52           1 : }
      53             : EXPORT_SYMBOL(set_capacity);
      54             : 
      55             : /*
      56             :  * Set disk capacity and notify if the size is not currently zero and will not
      57             :  * be set to zero.  Returns true if a uevent was sent, otherwise false.
      58             :  */
      59           1 : bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
      60             : {
      61           1 :         sector_t capacity = get_capacity(disk);
      62           1 :         char *envp[] = { "RESIZE=1", NULL };
      63             : 
      64           1 :         set_capacity(disk, size);
      65             : 
      66             :         /*
      67             :          * Only print a message and send a uevent if the gendisk is user visible
      68             :          * and alive.  This avoids spamming the log and udev when setting the
      69             :          * initial capacity during probing.
      70             :          */
      71           1 :         if (size == capacity ||
      72           1 :             (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
      73             :                 return false;
      74             : 
      75           0 :         pr_info("%s: detected capacity change from %lld to %lld\n",
      76             :                 disk->disk_name, capacity, size);
      77             : 
      78             :         /*
      79             :          * Historically we did not send a uevent for changes to/from an empty
      80             :          * device.
      81             :          */
      82           0 :         if (!capacity || !size)
      83             :                 return false;
      84           0 :         kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
      85           0 :         return true;
      86             : }
      87             : EXPORT_SYMBOL_GPL(set_capacity_and_notify);
      88             : 
      89             : /*
      90             :  * Format the device name of the indicated disk into the supplied buffer and
      91             :  * return a pointer to that same buffer for convenience.
      92             :  */
      93           4 : char *disk_name(struct gendisk *hd, int partno, char *buf)
      94             : {
      95           4 :         if (!partno)
      96           2 :                 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
      97           2 :         else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
      98           0 :                 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
      99             :         else
     100           2 :                 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
     101             : 
     102           4 :         return buf;
     103             : }
     104             : 
     105           1 : const char *bdevname(struct block_device *bdev, char *buf)
     106             : {
     107           1 :         return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
     108             : }
     109             : EXPORT_SYMBOL(bdevname);
     110             : 
     111           0 : static void part_stat_read_all(struct block_device *part,
     112             :                 struct disk_stats *stat)
     113             : {
     114           0 :         int cpu;
     115             : 
     116           0 :         memset(stat, 0, sizeof(struct disk_stats));
     117           0 :         for_each_possible_cpu(cpu) {
     118           0 :                 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
     119           0 :                 int group;
     120             : 
     121           0 :                 for (group = 0; group < NR_STAT_GROUPS; group++) {
     122           0 :                         stat->nsecs[group] += ptr->nsecs[group];
     123           0 :                         stat->sectors[group] += ptr->sectors[group];
     124           0 :                         stat->ios[group] += ptr->ios[group];
     125           0 :                         stat->merges[group] += ptr->merges[group];
     126             :                 }
     127             : 
     128           0 :                 stat->io_ticks += ptr->io_ticks;
     129             :         }
     130           0 : }
     131             : 
     132           0 : static unsigned int part_in_flight(struct block_device *part)
     133             : {
     134           0 :         unsigned int inflight = 0;
     135           0 :         int cpu;
     136             : 
     137           0 :         for_each_possible_cpu(cpu) {
     138           0 :                 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
     139           0 :                             part_stat_local_read_cpu(part, in_flight[1], cpu);
     140             :         }
     141           0 :         if ((int)inflight < 0)
     142           0 :                 inflight = 0;
     143             : 
     144           0 :         return inflight;
     145             : }
     146             : 
     147           0 : static void part_in_flight_rw(struct block_device *part,
     148             :                 unsigned int inflight[2])
     149             : {
     150           0 :         int cpu;
     151             : 
     152           0 :         inflight[0] = 0;
     153           0 :         inflight[1] = 0;
     154           0 :         for_each_possible_cpu(cpu) {
     155           0 :                 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
     156           0 :                 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
     157             :         }
     158           0 :         if ((int)inflight[0] < 0)
     159           0 :                 inflight[0] = 0;
     160           0 :         if ((int)inflight[1] < 0)
     161           0 :                 inflight[1] = 0;
     162           0 : }
     163             : 
     164             : /**
     165             :  * disk_part_iter_init - initialize partition iterator
     166             :  * @piter: iterator to initialize
     167             :  * @disk: disk to iterate over
     168             :  * @flags: DISK_PITER_* flags
     169             :  *
     170             :  * Initialize @piter so that it iterates over partitions of @disk.
     171             :  *
     172             :  * CONTEXT:
     173             :  * Don't care.
     174             :  */
     175          11 : void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
     176             :                           unsigned int flags)
     177             : {
     178          11 :         piter->disk = disk;
     179          11 :         piter->part = NULL;
     180           1 :         if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
     181           0 :                 piter->idx = 0;
     182             :         else
     183           1 :                 piter->idx = 1;
     184          11 :         piter->flags = flags;
     185          11 : }
     186             : 
     187             : /**
     188             :  * disk_part_iter_next - proceed iterator to the next partition and return it
     189             :  * @piter: iterator of interest
     190             :  *
     191             :  * Proceed @piter to the next partition and return it.
     192             :  *
     193             :  * CONTEXT:
     194             :  * Don't care.
     195             :  */
     196          23 : struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
     197             : {
     198          23 :         struct block_device *part;
     199          23 :         unsigned long idx;
     200             : 
     201             :         /* put the last partition */
     202          46 :         disk_part_iter_exit(piter);
     203             : 
     204          23 :         rcu_read_lock();
     205          23 :         xa_for_each_start(&piter->disk->part_tbl, idx, part, piter->idx) {
     206          12 :                 if (!bdev_nr_sectors(part) &&
     207           8 :                     !(piter->flags & DISK_PITER_INCL_EMPTY) &&
     208           8 :                     !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
     209           8 :                       piter->idx == 0))
     210           0 :                         continue;
     211             : 
     212          12 :                 piter->part = bdgrab(part);
     213          12 :                 if (!piter->part)
     214           0 :                         continue;
     215          12 :                 piter->idx = idx + 1;
     216          12 :                 break;
     217             :         }
     218          23 :         rcu_read_unlock();
     219             : 
     220          23 :         return piter->part;
     221             : }
     222             : 
     223             : /**
     224             :  * disk_part_iter_exit - finish up partition iteration
     225             :  * @piter: iter of interest
     226             :  *
     227             :  * Called when iteration is over.  Cleans up @piter.
     228             :  *
     229             :  * CONTEXT:
     230             :  * Don't care.
     231             :  */
     232          34 : void disk_part_iter_exit(struct disk_part_iter *piter)
     233             : {
     234          24 :         if (piter->part)
     235          12 :                 bdput(piter->part);
     236          23 :         piter->part = NULL;
     237           1 : }
     238             : 
     239             : /*
     240             :  * Can be deleted altogether. Later.
     241             :  *
     242             :  */
     243             : #define BLKDEV_MAJOR_HASH_SIZE 255
     244             : static struct blk_major_name {
     245             :         struct blk_major_name *next;
     246             :         int major;
     247             :         char name[16];
     248             :         void (*probe)(dev_t devt);
     249             : } *major_names[BLKDEV_MAJOR_HASH_SIZE];
     250             : static DEFINE_MUTEX(major_names_lock);
     251             : 
     252             : /* index in the above - for now: assume no multimajor ranges */
     253          21 : static inline int major_to_index(unsigned major)
     254             : {
     255          21 :         return major % BLKDEV_MAJOR_HASH_SIZE;
     256             : }
     257             : 
     258             : #ifdef CONFIG_PROC_FS
     259           0 : void blkdev_show(struct seq_file *seqf, off_t offset)
     260             : {
     261           0 :         struct blk_major_name *dp;
     262             : 
     263           0 :         mutex_lock(&major_names_lock);
     264           0 :         for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
     265           0 :                 if (dp->major == offset)
     266           0 :                         seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
     267           0 :         mutex_unlock(&major_names_lock);
     268           0 : }
     269             : #endif /* CONFIG_PROC_FS */
     270             : 
     271             : /**
     272             :  * __register_blkdev - register a new block device
     273             :  *
     274             :  * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
     275             :  *         @major = 0, try to allocate any unused major number.
     276             :  * @name: the name of the new block device as a zero terminated string
     277             :  * @probe: allback that is called on access to any minor number of @major
     278             :  *
     279             :  * The @name must be unique within the system.
     280             :  *
     281             :  * The return value depends on the @major input parameter:
     282             :  *
     283             :  *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
     284             :  *    then the function returns zero on success, or a negative error code
     285             :  *  - if any unused major number was requested with @major = 0 parameter
     286             :  *    then the return value is the allocated major number in range
     287             :  *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
     288             :  *
     289             :  * See Documentation/admin-guide/devices.txt for the list of allocated
     290             :  * major numbers.
     291             :  *
     292             :  * Use register_blkdev instead for any new code.
     293             :  */
     294          21 : int __register_blkdev(unsigned int major, const char *name,
     295             :                 void (*probe)(dev_t devt))
     296             : {
     297          21 :         struct blk_major_name **n, *p;
     298          21 :         int index, ret = 0;
     299             : 
     300          21 :         mutex_lock(&major_names_lock);
     301             : 
     302             :         /* temporary */
     303          21 :         if (major == 0) {
     304           3 :                 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
     305           3 :                         if (major_names[index] == NULL)
     306             :                                 break;
     307             :                 }
     308             : 
     309           2 :                 if (index == 0) {
     310           0 :                         printk("%s: failed to get major for %s\n",
     311             :                                __func__, name);
     312           0 :                         ret = -EBUSY;
     313           0 :                         goto out;
     314             :                 }
     315           2 :                 major = index;
     316           2 :                 ret = major;
     317             :         }
     318             : 
     319          21 :         if (major >= BLKDEV_MAJOR_MAX) {
     320           0 :                 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
     321             :                        __func__, major, BLKDEV_MAJOR_MAX-1, name);
     322             : 
     323           0 :                 ret = -EINVAL;
     324           0 :                 goto out;
     325             :         }
     326             : 
     327          21 :         p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
     328          21 :         if (p == NULL) {
     329           0 :                 ret = -ENOMEM;
     330           0 :                 goto out;
     331             :         }
     332             : 
     333          21 :         p->major = major;
     334          21 :         p->probe = probe;
     335          21 :         strlcpy(p->name, name, sizeof(p->name));
     336          21 :         p->next = NULL;
     337          21 :         index = major_to_index(major);
     338             : 
     339          21 :         for (n = &major_names[index]; *n; n = &(*n)->next) {
     340           0 :                 if ((*n)->major == major)
     341             :                         break;
     342             :         }
     343          21 :         if (!*n)
     344          21 :                 *n = p;
     345             :         else
     346             :                 ret = -EBUSY;
     347             : 
     348          21 :         if (ret < 0) {
     349           0 :                 printk("register_blkdev: cannot get major %u for %s\n",
     350             :                        major, name);
     351           0 :                 kfree(p);
     352             :         }
     353          21 : out:
     354          21 :         mutex_unlock(&major_names_lock);
     355          21 :         return ret;
     356             : }
     357             : EXPORT_SYMBOL(__register_blkdev);
     358             : 
     359           0 : void unregister_blkdev(unsigned int major, const char *name)
     360             : {
     361           0 :         struct blk_major_name **n;
     362           0 :         struct blk_major_name *p = NULL;
     363           0 :         int index = major_to_index(major);
     364             : 
     365           0 :         mutex_lock(&major_names_lock);
     366           0 :         for (n = &major_names[index]; *n; n = &(*n)->next)
     367           0 :                 if ((*n)->major == major)
     368             :                         break;
     369           0 :         if (!*n || strcmp((*n)->name, name)) {
     370           0 :                 WARN_ON(1);
     371             :         } else {
     372           0 :                 p = *n;
     373           0 :                 *n = p->next;
     374             :         }
     375           0 :         mutex_unlock(&major_names_lock);
     376           0 :         kfree(p);
     377           0 : }
     378             : 
     379             : EXPORT_SYMBOL(unregister_blkdev);
     380             : 
     381             : /**
     382             :  * blk_mangle_minor - scatter minor numbers apart
     383             :  * @minor: minor number to mangle
     384             :  *
     385             :  * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
     386             :  * is enabled.  Mangling twice gives the original value.
     387             :  *
     388             :  * RETURNS:
     389             :  * Mangled value.
     390             :  *
     391             :  * CONTEXT:
     392             :  * Don't care.
     393             :  */
     394           0 : static int blk_mangle_minor(int minor)
     395             : {
     396             : #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
     397             :         int i;
     398             : 
     399             :         for (i = 0; i < MINORBITS / 2; i++) {
     400             :                 int low = minor & (1 << i);
     401             :                 int high = minor & (1 << (MINORBITS - 1 - i));
     402             :                 int distance = MINORBITS - 1 - 2 * i;
     403             : 
     404             :                 minor ^= low | high;    /* clear both bits */
     405             :                 low <<= distance; /* swap the positions */
     406             :                 high >>= distance;
     407             :                 minor |= low | high;    /* and set */
     408             :         }
     409             : #endif
     410           0 :         return minor;
     411             : }
     412             : 
     413             : /**
     414             :  * blk_alloc_devt - allocate a dev_t for a block device
     415             :  * @bdev: block device to allocate dev_t for
     416             :  * @devt: out parameter for resulting dev_t
     417             :  *
     418             :  * Allocate a dev_t for block device.
     419             :  *
     420             :  * RETURNS:
     421             :  * 0 on success, allocated dev_t is returned in *@devt.  -errno on
     422             :  * failure.
     423             :  *
     424             :  * CONTEXT:
     425             :  * Might sleep.
     426             :  */
     427          10 : int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
     428             : {
     429          10 :         struct gendisk *disk = bdev->bd_disk;
     430          10 :         int idx;
     431             : 
     432             :         /* in consecutive minor range? */
     433          10 :         if (bdev->bd_partno < disk->minors) {
     434          10 :                 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
     435          10 :                 return 0;
     436             :         }
     437             : 
     438           0 :         idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
     439           0 :         if (idx < 0)
     440           0 :                 return idx == -ENOSPC ? -EBUSY : idx;
     441             : 
     442           0 :         *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
     443           0 :         return 0;
     444             : }
     445             : 
     446             : /**
     447             :  * blk_free_devt - free a dev_t
     448             :  * @devt: dev_t to free
     449             :  *
     450             :  * Free @devt which was allocated using blk_alloc_devt().
     451             :  *
     452             :  * CONTEXT:
     453             :  * Might sleep.
     454             :  */
     455           0 : void blk_free_devt(dev_t devt)
     456             : {
     457           0 :         if (MAJOR(devt) == BLOCK_EXT_MAJOR)
     458           0 :                 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
     459           0 : }
     460             : 
     461           0 : static char *bdevt_str(dev_t devt, char *buf)
     462             : {
     463           0 :         if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
     464           0 :                 char tbuf[BDEVT_SIZE];
     465           0 :                 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
     466           0 :                 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
     467             :         } else
     468           0 :                 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
     469             : 
     470           0 :         return buf;
     471             : }
     472             : 
     473           9 : void disk_uevent(struct gendisk *disk, enum kobject_action action)
     474             : {
     475           9 :         struct disk_part_iter piter;
     476           9 :         struct block_device *part;
     477             : 
     478           9 :         disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY_PART0);
     479          19 :         while ((part = disk_part_iter_next(&piter)))
     480          10 :                 kobject_uevent(bdev_kobj(part), action);
     481           9 :         disk_part_iter_exit(&piter);
     482           9 : }
     483             : EXPORT_SYMBOL_GPL(disk_uevent);
     484             : 
     485           9 : static void disk_scan_partitions(struct gendisk *disk)
     486             : {
     487           9 :         struct block_device *bdev;
     488             : 
     489          10 :         if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
     490             :                 return;
     491             : 
     492           1 :         set_bit(GD_NEED_PART_SCAN, &disk->state);
     493           1 :         bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
     494           1 :         if (!IS_ERR(bdev))
     495           1 :                 blkdev_put(bdev, FMODE_READ);
     496             : }
     497             : 
     498           9 : static void register_disk(struct device *parent, struct gendisk *disk,
     499             :                           const struct attribute_group **groups)
     500             : {
     501           9 :         struct device *ddev = disk_to_dev(disk);
     502           9 :         int err;
     503             : 
     504           9 :         ddev->parent = parent;
     505             : 
     506           9 :         dev_set_name(ddev, "%s", disk->disk_name);
     507             : 
     508             :         /* delay uevents, until we scanned partition table */
     509           9 :         dev_set_uevent_suppress(ddev, 1);
     510             : 
     511           9 :         if (groups) {
     512           1 :                 WARN_ON(ddev->groups);
     513           1 :                 ddev->groups = groups;
     514             :         }
     515           9 :         if (device_add(ddev))
     516             :                 return;
     517           9 :         if (!sysfs_deprecated) {
     518          18 :                 err = sysfs_create_link(block_depr, &ddev->kobj,
     519           9 :                                         kobject_name(&ddev->kobj));
     520           9 :                 if (err) {
     521           0 :                         device_del(ddev);
     522           0 :                         return;
     523             :                 }
     524             :         }
     525             : 
     526             :         /*
     527             :          * avoid probable deadlock caused by allocating memory with
     528             :          * GFP_KERNEL in runtime_resume callback of its all ancestor
     529             :          * devices
     530             :          */
     531           9 :         pm_runtime_set_memalloc_noio(ddev, true);
     532             : 
     533          18 :         disk->part0->bd_holder_dir =
     534           9 :                 kobject_create_and_add("holders", &ddev->kobj);
     535           9 :         disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
     536             : 
     537           9 :         if (disk->flags & GENHD_FL_HIDDEN)
     538             :                 return;
     539             : 
     540           9 :         disk_scan_partitions(disk);
     541             : 
     542             :         /* announce the disk and partitions after all partitions are created */
     543           9 :         dev_set_uevent_suppress(ddev, 0);
     544           9 :         disk_uevent(disk, KOBJ_ADD);
     545             : 
     546           9 :         if (disk->queue->backing_dev_info->dev) {
     547           9 :                 err = sysfs_create_link(&ddev->kobj,
     548             :                           &disk->queue->backing_dev_info->dev->kobj,
     549             :                           "bdi");
     550           9 :                 WARN_ON(err);
     551             :         }
     552             : }
     553             : 
     554             : /**
     555             :  * __device_add_disk - add disk information to kernel list
     556             :  * @parent: parent device for the disk
     557             :  * @disk: per-device partitioning information
     558             :  * @groups: Additional per-device sysfs groups
     559             :  * @register_queue: register the queue if set to true
     560             :  *
     561             :  * This function registers the partitioning information in @disk
     562             :  * with the kernel.
     563             :  *
     564             :  * FIXME: error handling
     565             :  */
     566           9 : static void __device_add_disk(struct device *parent, struct gendisk *disk,
     567             :                               const struct attribute_group **groups,
     568             :                               bool register_queue)
     569             : {
     570           9 :         dev_t devt;
     571           9 :         int retval;
     572             : 
     573             :         /*
     574             :          * The disk queue should now be all set with enough information about
     575             :          * the device for the elevator code to pick an adequate default
     576             :          * elevator if one is needed, that is, for devices requesting queue
     577             :          * registration.
     578             :          */
     579           9 :         if (register_queue)
     580           9 :                 elevator_init_mq(disk->queue);
     581             : 
     582             :         /* minors == 0 indicates to use ext devt from part0 and should
     583             :          * be accompanied with EXT_DEVT flag.  Make sure all
     584             :          * parameters make sense.
     585             :          */
     586          18 :         WARN_ON(disk->minors && !(disk->major || disk->first_minor));
     587          18 :         WARN_ON(!disk->minors &&
     588             :                 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
     589             : 
     590           9 :         disk->flags |= GENHD_FL_UP;
     591             : 
     592           9 :         retval = blk_alloc_devt(disk->part0, &devt);
     593           9 :         if (retval) {
     594           0 :                 WARN_ON(1);
     595           0 :                 return;
     596             :         }
     597           9 :         disk->major = MAJOR(devt);
     598           9 :         disk->first_minor = MINOR(devt);
     599             : 
     600           9 :         disk_alloc_events(disk);
     601             : 
     602           9 :         if (disk->flags & GENHD_FL_HIDDEN) {
     603             :                 /*
     604             :                  * Don't let hidden disks show up in /proc/partitions,
     605             :                  * and don't bother scanning for partitions either.
     606             :                  */
     607           0 :                 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
     608           0 :                 disk->flags |= GENHD_FL_NO_PART_SCAN;
     609             :         } else {
     610           9 :                 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
     611           9 :                 struct device *dev = disk_to_dev(disk);
     612           9 :                 int ret;
     613             : 
     614             :                 /* Register BDI before referencing it from bdev */
     615           9 :                 dev->devt = devt;
     616           9 :                 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
     617           9 :                 WARN_ON(ret);
     618           9 :                 bdi_set_owner(bdi, dev);
     619           9 :                 bdev_add(disk->part0, devt);
     620             :         }
     621           9 :         register_disk(parent, disk, groups);
     622           9 :         if (register_queue)
     623           9 :                 blk_register_queue(disk);
     624             : 
     625             :         /*
     626             :          * Take an extra ref on queue which will be put on disk_release()
     627             :          * so that it sticks around as long as @disk is there.
     628             :          */
     629           9 :         WARN_ON_ONCE(!blk_get_queue(disk->queue));
     630             : 
     631           9 :         disk_add_events(disk);
     632           9 :         blk_integrity_add(disk);
     633             : }
     634             : 
     635           9 : void device_add_disk(struct device *parent, struct gendisk *disk,
     636             :                      const struct attribute_group **groups)
     637             : 
     638             : {
     639           9 :         __device_add_disk(parent, disk, groups, true);
     640           9 : }
     641             : EXPORT_SYMBOL(device_add_disk);
     642             : 
     643           0 : void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
     644             : {
     645           0 :         __device_add_disk(parent, disk, NULL, false);
     646           0 : }
     647             : EXPORT_SYMBOL(device_add_disk_no_queue_reg);
     648             : 
     649           0 : static void invalidate_partition(struct block_device *bdev)
     650             : {
     651           0 :         fsync_bdev(bdev);
     652           0 :         __invalidate_device(bdev, true);
     653             : 
     654             :         /*
     655             :          * Unhash the bdev inode for this device so that it can't be looked
     656             :          * up any more even if openers still hold references to it.
     657             :          */
     658           0 :         remove_inode_hash(bdev->bd_inode);
     659           0 : }
     660             : 
     661             : /**
     662             :  * del_gendisk - remove the gendisk
     663             :  * @disk: the struct gendisk to remove
     664             :  *
     665             :  * Removes the gendisk and all its associated resources. This deletes the
     666             :  * partitions associated with the gendisk, and unregisters the associated
     667             :  * request_queue.
     668             :  *
     669             :  * This is the counter to the respective __device_add_disk() call.
     670             :  *
     671             :  * The final removal of the struct gendisk happens when its refcount reaches 0
     672             :  * with put_disk(), which should be called after del_gendisk(), if
     673             :  * __device_add_disk() was used.
     674             :  *
     675             :  * Drivers exist which depend on the release of the gendisk to be synchronous,
     676             :  * it should not be deferred.
     677             :  *
     678             :  * Context: can sleep
     679             :  */
     680           0 : void del_gendisk(struct gendisk *disk)
     681             : {
     682           0 :         struct disk_part_iter piter;
     683           0 :         struct block_device *part;
     684             : 
     685           0 :         might_sleep();
     686             : 
     687           0 :         if (WARN_ON_ONCE(!disk->queue))
     688           0 :                 return;
     689             : 
     690           0 :         blk_integrity_del(disk);
     691           0 :         disk_del_events(disk);
     692             : 
     693             :         /*
     694             :          * Block lookups of the disk until all bdevs are unhashed and the
     695             :          * disk is marked as dead (GENHD_FL_UP cleared).
     696             :          */
     697           0 :         down_write(&bdev_lookup_sem);
     698             : 
     699             :         /* invalidate stuff */
     700           0 :         disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
     701           0 :         while ((part = disk_part_iter_next(&piter))) {
     702           0 :                 invalidate_partition(part);
     703           0 :                 delete_partition(part);
     704             :         }
     705           0 :         disk_part_iter_exit(&piter);
     706             : 
     707           0 :         invalidate_partition(disk->part0);
     708           0 :         set_capacity(disk, 0);
     709           0 :         disk->flags &= ~GENHD_FL_UP;
     710           0 :         up_write(&bdev_lookup_sem);
     711             : 
     712           0 :         if (!(disk->flags & GENHD_FL_HIDDEN)) {
     713           0 :                 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
     714             : 
     715             :                 /*
     716             :                  * Unregister bdi before releasing device numbers (as they can
     717             :                  * get reused and we'd get clashes in sysfs).
     718             :                  */
     719           0 :                 bdi_unregister(disk->queue->backing_dev_info);
     720             :         }
     721             : 
     722           0 :         blk_unregister_queue(disk);
     723             : 
     724           0 :         kobject_put(disk->part0->bd_holder_dir);
     725           0 :         kobject_put(disk->slave_dir);
     726             : 
     727           0 :         part_stat_set_all(disk->part0, 0);
     728           0 :         disk->part0->bd_stamp = 0;
     729           0 :         if (!sysfs_deprecated)
     730           0 :                 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
     731           0 :         pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
     732           0 :         device_del(disk_to_dev(disk));
     733             : }
     734             : EXPORT_SYMBOL(del_gendisk);
     735             : 
     736             : /* sysfs access to bad-blocks list. */
     737           0 : static ssize_t disk_badblocks_show(struct device *dev,
     738             :                                         struct device_attribute *attr,
     739             :                                         char *page)
     740             : {
     741           0 :         struct gendisk *disk = dev_to_disk(dev);
     742             : 
     743           0 :         if (!disk->bb)
     744           0 :                 return sprintf(page, "\n");
     745             : 
     746           0 :         return badblocks_show(disk->bb, page, 0);
     747             : }
     748             : 
     749           0 : static ssize_t disk_badblocks_store(struct device *dev,
     750             :                                         struct device_attribute *attr,
     751             :                                         const char *page, size_t len)
     752             : {
     753           0 :         struct gendisk *disk = dev_to_disk(dev);
     754             : 
     755           0 :         if (!disk->bb)
     756             :                 return -ENXIO;
     757             : 
     758           0 :         return badblocks_store(disk->bb, page, len, 0);
     759             : }
     760             : 
     761           0 : void blk_request_module(dev_t devt)
     762             : {
     763           0 :         unsigned int major = MAJOR(devt);
     764           0 :         struct blk_major_name **n;
     765             : 
     766           0 :         mutex_lock(&major_names_lock);
     767           0 :         for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
     768           0 :                 if ((*n)->major == major && (*n)->probe) {
     769           0 :                         (*n)->probe(devt);
     770           0 :                         mutex_unlock(&major_names_lock);
     771           0 :                         return;
     772             :                 }
     773             :         }
     774           0 :         mutex_unlock(&major_names_lock);
     775             : 
     776           0 :         if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
     777             :                 /* Make old-style 2.4 aliases work */
     778           0 :                 request_module("block-major-%d", MAJOR(devt));
     779             : }
     780             : 
     781             : /**
     782             :  * bdget_disk - do bdget() by gendisk and partition number
     783             :  * @disk: gendisk of interest
     784             :  * @partno: partition number
     785             :  *
     786             :  * Find partition @partno from @disk, do bdget() on it.
     787             :  *
     788             :  * CONTEXT:
     789             :  * Don't care.
     790             :  *
     791             :  * RETURNS:
     792             :  * Resulting block_device on success, NULL on failure.
     793             :  */
     794           0 : struct block_device *bdget_disk(struct gendisk *disk, int partno)
     795             : {
     796           0 :         struct block_device *bdev = NULL;
     797             : 
     798           0 :         rcu_read_lock();
     799           0 :         bdev = xa_load(&disk->part_tbl, partno);
     800           0 :         if (bdev && !bdgrab(bdev))
     801           0 :                 bdev = NULL;
     802           0 :         rcu_read_unlock();
     803             : 
     804           0 :         return bdev;
     805             : }
     806             : 
     807             : /*
     808             :  * print a full list of all partitions - intended for places where the root
     809             :  * filesystem can't be mounted and thus to give the victim some idea of what
     810             :  * went wrong
     811             :  */
     812           0 : void __init printk_all_partitions(void)
     813             : {
     814           0 :         struct class_dev_iter iter;
     815           0 :         struct device *dev;
     816             : 
     817           0 :         class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
     818           0 :         while ((dev = class_dev_iter_next(&iter))) {
     819           0 :                 struct gendisk *disk = dev_to_disk(dev);
     820           0 :                 struct disk_part_iter piter;
     821           0 :                 struct block_device *part;
     822           0 :                 char name_buf[BDEVNAME_SIZE];
     823           0 :                 char devt_buf[BDEVT_SIZE];
     824             : 
     825             :                 /*
     826             :                  * Don't show empty devices or things that have been
     827             :                  * suppressed
     828             :                  */
     829           0 :                 if (get_capacity(disk) == 0 ||
     830           0 :                     (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
     831           0 :                         continue;
     832             : 
     833             :                 /*
     834             :                  * Note, unlike /proc/partitions, I am showing the
     835             :                  * numbers in hex - the same format as the root=
     836             :                  * option takes.
     837             :                  */
     838           0 :                 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
     839           0 :                 while ((part = disk_part_iter_next(&piter))) {
     840           0 :                         bool is_part0 = part == disk->part0;
     841             : 
     842           0 :                         printk("%s%s %10llu %s %s", is_part0 ? "" : "  ",
     843             :                                bdevt_str(part->bd_dev, devt_buf),
     844           0 :                                bdev_nr_sectors(part) >> 1,
     845           0 :                                disk_name(disk, part->bd_partno, name_buf),
     846           0 :                                part->bd_meta_info ?
     847             :                                         part->bd_meta_info->uuid : "");
     848           0 :                         if (is_part0) {
     849           0 :                                 if (dev->parent && dev->parent->driver)
     850           0 :                                         printk(" driver: %s\n",
     851             :                                               dev->parent->driver->name);
     852             :                                 else
     853           0 :                                         printk(" (driver?)\n");
     854             :                         } else
     855           0 :                                 printk("\n");
     856             :                 }
     857           0 :                 disk_part_iter_exit(&piter);
     858             :         }
     859           0 :         class_dev_iter_exit(&iter);
     860           0 : }
     861             : 
     862             : #ifdef CONFIG_PROC_FS
     863             : /* iterator */
     864           2 : static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
     865             : {
     866           2 :         loff_t skip = *pos;
     867           2 :         struct class_dev_iter *iter;
     868           2 :         struct device *dev;
     869             : 
     870           2 :         iter = kmalloc(sizeof(*iter), GFP_KERNEL);
     871           2 :         if (!iter)
     872           2 :                 return ERR_PTR(-ENOMEM);
     873             : 
     874           2 :         seqf->private = iter;
     875           2 :         class_dev_iter_init(iter, &block_class, NULL, &disk_type);
     876          11 :         do {
     877          11 :                 dev = class_dev_iter_next(iter);
     878          11 :                 if (!dev)
     879             :                         return NULL;
     880          10 :         } while (skip--);
     881             : 
     882           1 :         return dev_to_disk(dev);
     883             : }
     884             : 
     885           9 : static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
     886             : {
     887           9 :         struct device *dev;
     888             : 
     889           9 :         (*pos)++;
     890           9 :         dev = class_dev_iter_next(seqf->private);
     891           9 :         if (dev)
     892           8 :                 return dev_to_disk(dev);
     893             : 
     894             :         return NULL;
     895             : }
     896             : 
     897           2 : static void disk_seqf_stop(struct seq_file *seqf, void *v)
     898             : {
     899           2 :         struct class_dev_iter *iter = seqf->private;
     900             : 
     901             :         /* stop is called even after start failed :-( */
     902           2 :         if (iter) {
     903           2 :                 class_dev_iter_exit(iter);
     904           2 :                 kfree(iter);
     905           2 :                 seqf->private = NULL;
     906             :         }
     907           2 : }
     908             : 
     909           2 : static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
     910             : {
     911           2 :         void *p;
     912             : 
     913           2 :         p = disk_seqf_start(seqf, pos);
     914           3 :         if (!IS_ERR_OR_NULL(p) && !*pos)
     915           1 :                 seq_puts(seqf, "major minor  #blocks  name\n\n");
     916           2 :         return p;
     917             : }
     918             : 
     919           9 : static int show_partition(struct seq_file *seqf, void *v)
     920             : {
     921           9 :         struct gendisk *sgp = v;
     922           9 :         struct disk_part_iter piter;
     923           9 :         struct block_device *part;
     924           9 :         char buf[BDEVNAME_SIZE];
     925             : 
     926             :         /* Don't show non-partitionable removeable devices or empty devices */
     927           9 :         if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
     928           0 :                                    (sgp->flags & GENHD_FL_REMOVABLE)))
     929             :                 return 0;
     930           1 :         if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
     931             :                 return 0;
     932             : 
     933             :         /* show the full disk and all non-0 size partitions of it */
     934           1 :         disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
     935           3 :         while ((part = disk_part_iter_next(&piter)))
     936           2 :                 seq_printf(seqf, "%4d  %7d %10llu %s\n",
     937           2 :                            MAJOR(part->bd_dev), MINOR(part->bd_dev),
     938           2 :                            bdev_nr_sectors(part) >> 1,
     939           2 :                            disk_name(sgp, part->bd_partno, buf));
     940           1 :         disk_part_iter_exit(&piter);
     941             : 
     942           1 :         return 0;
     943             : }
     944             : 
     945             : static const struct seq_operations partitions_op = {
     946             :         .start  = show_partition_start,
     947             :         .next   = disk_seqf_next,
     948             :         .stop   = disk_seqf_stop,
     949             :         .show   = show_partition
     950             : };
     951             : #endif
     952             : 
     953           1 : static int __init genhd_device_init(void)
     954             : {
     955           1 :         int error;
     956             : 
     957           1 :         block_class.dev_kobj = sysfs_dev_block_kobj;
     958           1 :         error = class_register(&block_class);
     959           1 :         if (unlikely(error))
     960             :                 return error;
     961           1 :         blk_dev_init();
     962             : 
     963           1 :         register_blkdev(BLOCK_EXT_MAJOR, "blkext");
     964             : 
     965             :         /* create top-level block dir */
     966           1 :         if (!sysfs_deprecated)
     967           1 :                 block_depr = kobject_create_and_add("block", NULL);
     968           1 :         return 0;
     969             : }
     970             : 
     971             : subsys_initcall(genhd_device_init);
     972             : 
     973           0 : static ssize_t disk_range_show(struct device *dev,
     974             :                                struct device_attribute *attr, char *buf)
     975             : {
     976           0 :         struct gendisk *disk = dev_to_disk(dev);
     977             : 
     978           0 :         return sprintf(buf, "%d\n", disk->minors);
     979             : }
     980             : 
     981           0 : static ssize_t disk_ext_range_show(struct device *dev,
     982             :                                    struct device_attribute *attr, char *buf)
     983             : {
     984           0 :         struct gendisk *disk = dev_to_disk(dev);
     985             : 
     986           0 :         return sprintf(buf, "%d\n", disk_max_parts(disk));
     987             : }
     988             : 
     989           0 : static ssize_t disk_removable_show(struct device *dev,
     990             :                                    struct device_attribute *attr, char *buf)
     991             : {
     992           0 :         struct gendisk *disk = dev_to_disk(dev);
     993             : 
     994           0 :         return sprintf(buf, "%d\n",
     995           0 :                        (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
     996             : }
     997             : 
     998           0 : static ssize_t disk_hidden_show(struct device *dev,
     999             :                                    struct device_attribute *attr, char *buf)
    1000             : {
    1001           0 :         struct gendisk *disk = dev_to_disk(dev);
    1002             : 
    1003           0 :         return sprintf(buf, "%d\n",
    1004           0 :                        (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
    1005             : }
    1006             : 
    1007           0 : static ssize_t disk_ro_show(struct device *dev,
    1008             :                                    struct device_attribute *attr, char *buf)
    1009             : {
    1010           0 :         struct gendisk *disk = dev_to_disk(dev);
    1011             : 
    1012           0 :         return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
    1013             : }
    1014             : 
    1015           3 : ssize_t part_size_show(struct device *dev,
    1016             :                        struct device_attribute *attr, char *buf)
    1017             : {
    1018           3 :         return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
    1019             : }
    1020             : 
    1021           0 : ssize_t part_stat_show(struct device *dev,
    1022             :                        struct device_attribute *attr, char *buf)
    1023             : {
    1024           0 :         struct block_device *bdev = dev_to_bdev(dev);
    1025           0 :         struct request_queue *q = bdev->bd_disk->queue;
    1026           0 :         struct disk_stats stat;
    1027           0 :         unsigned int inflight;
    1028             : 
    1029           0 :         part_stat_read_all(bdev, &stat);
    1030           0 :         if (queue_is_mq(q))
    1031           0 :                 inflight = blk_mq_in_flight(q, bdev);
    1032             :         else
    1033           0 :                 inflight = part_in_flight(bdev);
    1034             : 
    1035           0 :         return sprintf(buf,
    1036             :                 "%8lu %8lu %8llu %8u "
    1037             :                 "%8lu %8lu %8llu %8u "
    1038             :                 "%8u %8u %8u "
    1039             :                 "%8lu %8lu %8llu %8u "
    1040             :                 "%8lu %8u"
    1041             :                 "\n",
    1042             :                 stat.ios[STAT_READ],
    1043             :                 stat.merges[STAT_READ],
    1044           0 :                 (unsigned long long)stat.sectors[STAT_READ],
    1045           0 :                 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
    1046             :                 stat.ios[STAT_WRITE],
    1047             :                 stat.merges[STAT_WRITE],
    1048           0 :                 (unsigned long long)stat.sectors[STAT_WRITE],
    1049           0 :                 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
    1050             :                 inflight,
    1051             :                 jiffies_to_msecs(stat.io_ticks),
    1052           0 :                 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
    1053           0 :                                       stat.nsecs[STAT_WRITE] +
    1054             :                                       stat.nsecs[STAT_DISCARD] +
    1055             :                                       stat.nsecs[STAT_FLUSH],
    1056             :                                                 NSEC_PER_MSEC),
    1057             :                 stat.ios[STAT_DISCARD],
    1058             :                 stat.merges[STAT_DISCARD],
    1059           0 :                 (unsigned long long)stat.sectors[STAT_DISCARD],
    1060           0 :                 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
    1061             :                 stat.ios[STAT_FLUSH],
    1062           0 :                 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
    1063             : }
    1064             : 
    1065           0 : ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
    1066             :                            char *buf)
    1067             : {
    1068           0 :         struct block_device *bdev = dev_to_bdev(dev);
    1069           0 :         struct request_queue *q = bdev->bd_disk->queue;
    1070           0 :         unsigned int inflight[2];
    1071             : 
    1072           0 :         if (queue_is_mq(q))
    1073           0 :                 blk_mq_in_flight_rw(q, bdev, inflight);
    1074             :         else
    1075           0 :                 part_in_flight_rw(bdev, inflight);
    1076             : 
    1077           0 :         return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
    1078             : }
    1079             : 
    1080           0 : static ssize_t disk_capability_show(struct device *dev,
    1081             :                                     struct device_attribute *attr, char *buf)
    1082             : {
    1083           0 :         struct gendisk *disk = dev_to_disk(dev);
    1084             : 
    1085           0 :         return sprintf(buf, "%x\n", disk->flags);
    1086             : }
    1087             : 
    1088           0 : static ssize_t disk_alignment_offset_show(struct device *dev,
    1089             :                                           struct device_attribute *attr,
    1090             :                                           char *buf)
    1091             : {
    1092           0 :         struct gendisk *disk = dev_to_disk(dev);
    1093             : 
    1094           0 :         return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
    1095             : }
    1096             : 
    1097           0 : static ssize_t disk_discard_alignment_show(struct device *dev,
    1098             :                                            struct device_attribute *attr,
    1099             :                                            char *buf)
    1100             : {
    1101           0 :         struct gendisk *disk = dev_to_disk(dev);
    1102             : 
    1103           0 :         return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
    1104             : }
    1105             : 
    1106             : static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
    1107             : static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
    1108             : static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
    1109             : static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
    1110             : static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
    1111             : static DEVICE_ATTR(size, 0444, part_size_show, NULL);
    1112             : static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
    1113             : static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
    1114             : static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
    1115             : static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
    1116             : static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
    1117             : static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
    1118             : 
    1119             : #ifdef CONFIG_FAIL_MAKE_REQUEST
    1120             : ssize_t part_fail_show(struct device *dev,
    1121             :                        struct device_attribute *attr, char *buf)
    1122             : {
    1123             :         return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
    1124             : }
    1125             : 
    1126             : ssize_t part_fail_store(struct device *dev,
    1127             :                         struct device_attribute *attr,
    1128             :                         const char *buf, size_t count)
    1129             : {
    1130             :         int i;
    1131             : 
    1132             :         if (count > 0 && sscanf(buf, "%d", &i) > 0)
    1133             :                 dev_to_bdev(dev)->bd_make_it_fail = i;
    1134             : 
    1135             :         return count;
    1136             : }
    1137             : 
    1138             : static struct device_attribute dev_attr_fail =
    1139             :         __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
    1140             : #endif /* CONFIG_FAIL_MAKE_REQUEST */
    1141             : 
    1142             : #ifdef CONFIG_FAIL_IO_TIMEOUT
    1143             : static struct device_attribute dev_attr_fail_timeout =
    1144             :         __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
    1145             : #endif
    1146             : 
    1147             : static struct attribute *disk_attrs[] = {
    1148             :         &dev_attr_range.attr,
    1149             :         &dev_attr_ext_range.attr,
    1150             :         &dev_attr_removable.attr,
    1151             :         &dev_attr_hidden.attr,
    1152             :         &dev_attr_ro.attr,
    1153             :         &dev_attr_size.attr,
    1154             :         &dev_attr_alignment_offset.attr,
    1155             :         &dev_attr_discard_alignment.attr,
    1156             :         &dev_attr_capability.attr,
    1157             :         &dev_attr_stat.attr,
    1158             :         &dev_attr_inflight.attr,
    1159             :         &dev_attr_badblocks.attr,
    1160             : #ifdef CONFIG_FAIL_MAKE_REQUEST
    1161             :         &dev_attr_fail.attr,
    1162             : #endif
    1163             : #ifdef CONFIG_FAIL_IO_TIMEOUT
    1164             :         &dev_attr_fail_timeout.attr,
    1165             : #endif
    1166             :         NULL
    1167             : };
    1168             : 
    1169         108 : static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
    1170             : {
    1171         108 :         struct device *dev = container_of(kobj, typeof(*dev), kobj);
    1172         108 :         struct gendisk *disk = dev_to_disk(dev);
    1173             : 
    1174         108 :         if (a == &dev_attr_badblocks.attr && !disk->bb)
    1175             :                 return 0;
    1176          99 :         return a->mode;
    1177             : }
    1178             : 
    1179             : static struct attribute_group disk_attr_group = {
    1180             :         .attrs = disk_attrs,
    1181             :         .is_visible = disk_visible,
    1182             : };
    1183             : 
    1184             : static const struct attribute_group *disk_attr_groups[] = {
    1185             :         &disk_attr_group,
    1186             :         NULL
    1187             : };
    1188             : 
    1189             : /**
    1190             :  * disk_release - releases all allocated resources of the gendisk
    1191             :  * @dev: the device representing this disk
    1192             :  *
    1193             :  * This function releases all allocated resources of the gendisk.
    1194             :  *
    1195             :  * Drivers which used __device_add_disk() have a gendisk with a request_queue
    1196             :  * assigned. Since the request_queue sits on top of the gendisk for these
    1197             :  * drivers we also call blk_put_queue() for them, and we expect the
    1198             :  * request_queue refcount to reach 0 at this point, and so the request_queue
    1199             :  * will also be freed prior to the disk.
    1200             :  *
    1201             :  * Context: can sleep
    1202             :  */
    1203           0 : static void disk_release(struct device *dev)
    1204             : {
    1205           0 :         struct gendisk *disk = dev_to_disk(dev);
    1206             : 
    1207           0 :         might_sleep();
    1208             : 
    1209           0 :         blk_free_devt(dev->devt);
    1210           0 :         disk_release_events(disk);
    1211           0 :         kfree(disk->random);
    1212           0 :         xa_destroy(&disk->part_tbl);
    1213           0 :         bdput(disk->part0);
    1214           0 :         if (disk->queue)
    1215           0 :                 blk_put_queue(disk->queue);
    1216           0 :         kfree(disk);
    1217           0 : }
    1218             : struct class block_class = {
    1219             :         .name           = "block",
    1220             : };
    1221             : 
    1222          56 : static char *block_devnode(struct device *dev, umode_t *mode,
    1223             :                            kuid_t *uid, kgid_t *gid)
    1224             : {
    1225          56 :         struct gendisk *disk = dev_to_disk(dev);
    1226             : 
    1227          56 :         if (disk->fops->devnode)
    1228           0 :                 return disk->fops->devnode(disk, mode);
    1229             :         return NULL;
    1230             : }
    1231             : 
    1232             : const struct device_type disk_type = {
    1233             :         .name           = "disk",
    1234             :         .groups         = disk_attr_groups,
    1235             :         .release        = disk_release,
    1236             :         .devnode        = block_devnode,
    1237             : };
    1238             : 
    1239             : #ifdef CONFIG_PROC_FS
    1240             : /*
    1241             :  * aggregate disk stat collector.  Uses the same stats that the sysfs
    1242             :  * entries do, above, but makes them available through one seq_file.
    1243             :  *
    1244             :  * The output looks suspiciously like /proc/partitions with a bunch of
    1245             :  * extra fields.
    1246             :  */
    1247           0 : static int diskstats_show(struct seq_file *seqf, void *v)
    1248             : {
    1249           0 :         struct gendisk *gp = v;
    1250           0 :         struct disk_part_iter piter;
    1251           0 :         struct block_device *hd;
    1252           0 :         char buf[BDEVNAME_SIZE];
    1253           0 :         unsigned int inflight;
    1254           0 :         struct disk_stats stat;
    1255             : 
    1256             :         /*
    1257             :         if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
    1258             :                 seq_puts(seqf,  "major minor name"
    1259             :                                 "     rio rmerge rsect ruse wio wmerge "
    1260             :                                 "wsect wuse running use aveq"
    1261             :                                 "\n\n");
    1262             :         */
    1263             : 
    1264           0 :         disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
    1265           0 :         while ((hd = disk_part_iter_next(&piter))) {
    1266           0 :                 part_stat_read_all(hd, &stat);
    1267           0 :                 if (queue_is_mq(gp->queue))
    1268           0 :                         inflight = blk_mq_in_flight(gp->queue, hd);
    1269             :                 else
    1270           0 :                         inflight = part_in_flight(hd);
    1271             : 
    1272           0 :                 seq_printf(seqf, "%4d %7d %s "
    1273             :                            "%lu %lu %lu %u "
    1274             :                            "%lu %lu %lu %u "
    1275             :                            "%u %u %u "
    1276             :                            "%lu %lu %lu %u "
    1277             :                            "%lu %u"
    1278             :                            "\n",
    1279           0 :                            MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
    1280           0 :                            disk_name(gp, hd->bd_partno, buf),
    1281             :                            stat.ios[STAT_READ],
    1282             :                            stat.merges[STAT_READ],
    1283             :                            stat.sectors[STAT_READ],
    1284           0 :                            (unsigned int)div_u64(stat.nsecs[STAT_READ],
    1285             :                                                         NSEC_PER_MSEC),
    1286             :                            stat.ios[STAT_WRITE],
    1287             :                            stat.merges[STAT_WRITE],
    1288             :                            stat.sectors[STAT_WRITE],
    1289           0 :                            (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
    1290             :                                                         NSEC_PER_MSEC),
    1291             :                            inflight,
    1292             :                            jiffies_to_msecs(stat.io_ticks),
    1293           0 :                            (unsigned int)div_u64(stat.nsecs[STAT_READ] +
    1294           0 :                                                  stat.nsecs[STAT_WRITE] +
    1295             :                                                  stat.nsecs[STAT_DISCARD] +
    1296             :                                                  stat.nsecs[STAT_FLUSH],
    1297             :                                                         NSEC_PER_MSEC),
    1298             :                            stat.ios[STAT_DISCARD],
    1299             :                            stat.merges[STAT_DISCARD],
    1300             :                            stat.sectors[STAT_DISCARD],
    1301           0 :                            (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
    1302             :                                                  NSEC_PER_MSEC),
    1303             :                            stat.ios[STAT_FLUSH],
    1304           0 :                            (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
    1305             :                                                  NSEC_PER_MSEC)
    1306             :                         );
    1307             :         }
    1308           0 :         disk_part_iter_exit(&piter);
    1309             : 
    1310           0 :         return 0;
    1311             : }
    1312             : 
    1313             : static const struct seq_operations diskstats_op = {
    1314             :         .start  = disk_seqf_start,
    1315             :         .next   = disk_seqf_next,
    1316             :         .stop   = disk_seqf_stop,
    1317             :         .show   = diskstats_show
    1318             : };
    1319             : 
    1320           1 : static int __init proc_genhd_init(void)
    1321             : {
    1322           1 :         proc_create_seq("diskstats", 0, NULL, &diskstats_op);
    1323           1 :         proc_create_seq("partitions", 0, NULL, &partitions_op);
    1324           1 :         return 0;
    1325             : }
    1326             : module_init(proc_genhd_init);
    1327             : #endif /* CONFIG_PROC_FS */
    1328             : 
    1329           2 : dev_t blk_lookup_devt(const char *name, int partno)
    1330             : {
    1331           2 :         dev_t devt = MKDEV(0, 0);
    1332           2 :         struct class_dev_iter iter;
    1333           2 :         struct device *dev;
    1334             : 
    1335           2 :         class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
    1336          19 :         while ((dev = class_dev_iter_next(&iter))) {
    1337          18 :                 struct gendisk *disk = dev_to_disk(dev);
    1338          18 :                 struct block_device *part;
    1339             : 
    1340          36 :                 if (strcmp(dev_name(dev), name))
    1341          17 :                         continue;
    1342             : 
    1343           1 :                 if (partno < disk->minors) {
    1344             :                         /* We need to return the right devno, even
    1345             :                          * if the partition doesn't exist yet.
    1346             :                          */
    1347           1 :                         devt = MKDEV(MAJOR(dev->devt),
    1348             :                                      MINOR(dev->devt) + partno);
    1349           1 :                         break;
    1350             :                 }
    1351           0 :                 part = bdget_disk(disk, partno);
    1352           0 :                 if (part) {
    1353           0 :                         devt = part->bd_dev;
    1354           0 :                         bdput(part);
    1355           0 :                         break;
    1356             :                 }
    1357             :         }
    1358           2 :         class_dev_iter_exit(&iter);
    1359           2 :         return devt;
    1360             : }
    1361             : 
    1362           9 : struct gendisk *__alloc_disk_node(int minors, int node_id)
    1363             : {
    1364           9 :         struct gendisk *disk;
    1365             : 
    1366           9 :         if (minors > DISK_MAX_PARTS) {
    1367           0 :                 printk(KERN_ERR
    1368             :                         "block: can't allocate more than %d partitions\n",
    1369             :                         DISK_MAX_PARTS);
    1370           0 :                 minors = DISK_MAX_PARTS;
    1371             :         }
    1372             : 
    1373           9 :         disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
    1374           9 :         if (!disk)
    1375             :                 return NULL;
    1376             : 
    1377           9 :         disk->part0 = bdev_alloc(disk, 0);
    1378           9 :         if (!disk->part0)
    1379           0 :                 goto out_free_disk;
    1380             : 
    1381           9 :         disk->node_id = node_id;
    1382           9 :         xa_init(&disk->part_tbl);
    1383           9 :         if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
    1384           0 :                 goto out_destroy_part_tbl;
    1385             : 
    1386           9 :         disk->minors = minors;
    1387           9 :         rand_initialize_disk(disk);
    1388           9 :         disk_to_dev(disk)->class = &block_class;
    1389           9 :         disk_to_dev(disk)->type = &disk_type;
    1390           9 :         device_initialize(disk_to_dev(disk));
    1391           9 :         return disk;
    1392             : 
    1393           0 : out_destroy_part_tbl:
    1394           0 :         xa_destroy(&disk->part_tbl);
    1395           0 :         bdput(disk->part0);
    1396           0 : out_free_disk:
    1397           0 :         kfree(disk);
    1398           0 :         return NULL;
    1399             : }
    1400             : EXPORT_SYMBOL(__alloc_disk_node);
    1401             : 
    1402             : /**
    1403             :  * put_disk - decrements the gendisk refcount
    1404             :  * @disk: the struct gendisk to decrement the refcount for
    1405             :  *
    1406             :  * This decrements the refcount for the struct gendisk. When this reaches 0
    1407             :  * we'll have disk_release() called.
    1408             :  *
    1409             :  * Context: Any context, but the last reference must not be dropped from
    1410             :  *          atomic context.
    1411             :  */
    1412          31 : void put_disk(struct gendisk *disk)
    1413             : {
    1414          31 :         if (disk)
    1415          31 :                 put_device(disk_to_dev(disk));
    1416          31 : }
    1417             : EXPORT_SYMBOL(put_disk);
    1418             : 
    1419           0 : static void set_disk_ro_uevent(struct gendisk *gd, int ro)
    1420             : {
    1421           0 :         char event[] = "DISK_RO=1";
    1422           0 :         char *envp[] = { event, NULL };
    1423             : 
    1424           0 :         if (!ro)
    1425           0 :                 event[8] = '0';
    1426           0 :         kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
    1427           0 : }
    1428             : 
    1429             : /**
    1430             :  * set_disk_ro - set a gendisk read-only
    1431             :  * @disk:       gendisk to operate on
    1432             :  * @read_only:  %true to set the disk read-only, %false set the disk read/write
    1433             :  *
    1434             :  * This function is used to indicate whether a given disk device should have its
    1435             :  * read-only flag set. set_disk_ro() is typically used by device drivers to
    1436             :  * indicate whether the underlying physical device is write-protected.
    1437             :  */
    1438           0 : void set_disk_ro(struct gendisk *disk, bool read_only)
    1439             : {
    1440           0 :         if (read_only) {
    1441           0 :                 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
    1442             :                         return;
    1443             :         } else {
    1444           0 :                 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
    1445             :                         return;
    1446             :         }
    1447           0 :         set_disk_ro_uevent(disk, read_only);
    1448             : }
    1449             : EXPORT_SYMBOL(set_disk_ro);
    1450             : 
    1451        3079 : int bdev_read_only(struct block_device *bdev)
    1452             : {
    1453        3079 :         return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
    1454             : }
    1455             : EXPORT_SYMBOL(bdev_read_only);
    1456             : 
    1457             : /*
    1458             :  * Disk events - monitor disk events like media change and eject request.
    1459             :  */
    1460             : struct disk_events {
    1461             :         struct list_head        node;           /* all disk_event's */
    1462             :         struct gendisk          *disk;          /* the associated disk */
    1463             :         spinlock_t              lock;
    1464             : 
    1465             :         struct mutex            block_mutex;    /* protects blocking */
    1466             :         int                     block;          /* event blocking depth */
    1467             :         unsigned int            pending;        /* events already sent out */
    1468             :         unsigned int            clearing;       /* events being cleared */
    1469             : 
    1470             :         long                    poll_msecs;     /* interval, -1 for default */
    1471             :         struct delayed_work     dwork;
    1472             : };
    1473             : 
    1474             : static const char *disk_events_strs[] = {
    1475             :         [ilog2(DISK_EVENT_MEDIA_CHANGE)]        = "media_change",
    1476             :         [ilog2(DISK_EVENT_EJECT_REQUEST)]       = "eject_request",
    1477             : };
    1478             : 
    1479             : static char *disk_uevents[] = {
    1480             :         [ilog2(DISK_EVENT_MEDIA_CHANGE)]        = "DISK_MEDIA_CHANGE=1",
    1481             :         [ilog2(DISK_EVENT_EJECT_REQUEST)]       = "DISK_EJECT_REQUEST=1",
    1482             : };
    1483             : 
    1484             : /* list of all disk_events */
    1485             : static DEFINE_MUTEX(disk_events_mutex);
    1486             : static LIST_HEAD(disk_events);
    1487             : 
    1488             : /* disable in-kernel polling by default */
    1489             : static unsigned long disk_events_dfl_poll_msecs;
    1490             : 
    1491           0 : static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
    1492             : {
    1493           0 :         struct disk_events *ev = disk->ev;
    1494           0 :         long intv_msecs = 0;
    1495             : 
    1496             :         /*
    1497             :          * If device-specific poll interval is set, always use it.  If
    1498             :          * the default is being used, poll if the POLL flag is set.
    1499             :          */
    1500           0 :         if (ev->poll_msecs >= 0)
    1501             :                 intv_msecs = ev->poll_msecs;
    1502           0 :         else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
    1503           0 :                 intv_msecs = disk_events_dfl_poll_msecs;
    1504             : 
    1505           0 :         return msecs_to_jiffies(intv_msecs);
    1506             : }
    1507             : 
    1508             : /**
    1509             :  * disk_block_events - block and flush disk event checking
    1510             :  * @disk: disk to block events for
    1511             :  *
    1512             :  * On return from this function, it is guaranteed that event checking
    1513             :  * isn't in progress and won't happen until unblocked by
    1514             :  * disk_unblock_events().  Events blocking is counted and the actual
    1515             :  * unblocking happens after the matching number of unblocks are done.
    1516             :  *
    1517             :  * Note that this intentionally does not block event checking from
    1518             :  * disk_clear_events().
    1519             :  *
    1520             :  * CONTEXT:
    1521             :  * Might sleep.
    1522             :  */
    1523          31 : void disk_block_events(struct gendisk *disk)
    1524             : {
    1525          31 :         struct disk_events *ev = disk->ev;
    1526          31 :         unsigned long flags;
    1527          31 :         bool cancel;
    1528             : 
    1529          31 :         if (!ev)
    1530             :                 return;
    1531             : 
    1532             :         /*
    1533             :          * Outer mutex ensures that the first blocker completes canceling
    1534             :          * the event work before further blockers are allowed to finish.
    1535             :          */
    1536           0 :         mutex_lock(&ev->block_mutex);
    1537             : 
    1538           0 :         spin_lock_irqsave(&ev->lock, flags);
    1539           0 :         cancel = !ev->block++;
    1540           0 :         spin_unlock_irqrestore(&ev->lock, flags);
    1541             : 
    1542           0 :         if (cancel)
    1543           0 :                 cancel_delayed_work_sync(&disk->ev->dwork);
    1544             : 
    1545           0 :         mutex_unlock(&ev->block_mutex);
    1546             : }
    1547             : 
    1548           0 : static void __disk_unblock_events(struct gendisk *disk, bool check_now)
    1549             : {
    1550           0 :         struct disk_events *ev = disk->ev;
    1551           0 :         unsigned long intv;
    1552           0 :         unsigned long flags;
    1553             : 
    1554           0 :         spin_lock_irqsave(&ev->lock, flags);
    1555             : 
    1556           0 :         if (WARN_ON_ONCE(ev->block <= 0))
    1557           0 :                 goto out_unlock;
    1558             : 
    1559           0 :         if (--ev->block)
    1560           0 :                 goto out_unlock;
    1561             : 
    1562           0 :         intv = disk_events_poll_jiffies(disk);
    1563           0 :         if (check_now)
    1564           0 :                 queue_delayed_work(system_freezable_power_efficient_wq,
    1565             :                                 &ev->dwork, 0);
    1566           0 :         else if (intv)
    1567           0 :                 queue_delayed_work(system_freezable_power_efficient_wq,
    1568             :                                 &ev->dwork, intv);
    1569           0 : out_unlock:
    1570           0 :         spin_unlock_irqrestore(&ev->lock, flags);
    1571           0 : }
    1572             : 
    1573             : /**
    1574             :  * disk_unblock_events - unblock disk event checking
    1575             :  * @disk: disk to unblock events for
    1576             :  *
    1577             :  * Undo disk_block_events().  When the block count reaches zero, it
    1578             :  * starts events polling if configured.
    1579             :  *
    1580             :  * CONTEXT:
    1581             :  * Don't care.  Safe to call from irq context.
    1582             :  */
    1583          31 : void disk_unblock_events(struct gendisk *disk)
    1584             : {
    1585          31 :         if (disk->ev)
    1586           0 :                 __disk_unblock_events(disk, false);
    1587          31 : }
    1588             : 
    1589             : /**
    1590             :  * disk_flush_events - schedule immediate event checking and flushing
    1591             :  * @disk: disk to check and flush events for
    1592             :  * @mask: events to flush
    1593             :  *
    1594             :  * Schedule immediate event checking on @disk if not blocked.  Events in
    1595             :  * @mask are scheduled to be cleared from the driver.  Note that this
    1596             :  * doesn't clear the events from @disk->ev.
    1597             :  *
    1598             :  * CONTEXT:
    1599             :  * If @mask is non-zero must be called with bdev->bd_mutex held.
    1600             :  */
    1601          30 : void disk_flush_events(struct gendisk *disk, unsigned int mask)
    1602             : {
    1603          30 :         struct disk_events *ev = disk->ev;
    1604             : 
    1605          30 :         if (!ev)
    1606             :                 return;
    1607             : 
    1608           0 :         spin_lock_irq(&ev->lock);
    1609           0 :         ev->clearing |= mask;
    1610           0 :         if (!ev->block)
    1611           0 :                 mod_delayed_work(system_freezable_power_efficient_wq,
    1612             :                                 &ev->dwork, 0);
    1613           0 :         spin_unlock_irq(&ev->lock);
    1614             : }
    1615             : 
    1616             : /**
    1617             :  * disk_clear_events - synchronously check, clear and return pending events
    1618             :  * @disk: disk to fetch and clear events from
    1619             :  * @mask: mask of events to be fetched and cleared
    1620             :  *
    1621             :  * Disk events are synchronously checked and pending events in @mask
    1622             :  * are cleared and returned.  This ignores the block count.
    1623             :  *
    1624             :  * CONTEXT:
    1625             :  * Might sleep.
    1626             :  */
    1627           0 : static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
    1628             : {
    1629           0 :         struct disk_events *ev = disk->ev;
    1630           0 :         unsigned int pending;
    1631           0 :         unsigned int clearing = mask;
    1632             : 
    1633           0 :         if (!ev)
    1634             :                 return 0;
    1635             : 
    1636           0 :         disk_block_events(disk);
    1637             : 
    1638             :         /*
    1639             :          * store the union of mask and ev->clearing on the stack so that the
    1640             :          * race with disk_flush_events does not cause ambiguity (ev->clearing
    1641             :          * can still be modified even if events are blocked).
    1642             :          */
    1643           0 :         spin_lock_irq(&ev->lock);
    1644           0 :         clearing |= ev->clearing;
    1645           0 :         ev->clearing = 0;
    1646           0 :         spin_unlock_irq(&ev->lock);
    1647             : 
    1648           0 :         disk_check_events(ev, &clearing);
    1649             :         /*
    1650             :          * if ev->clearing is not 0, the disk_flush_events got called in the
    1651             :          * middle of this function, so we want to run the workfn without delay.
    1652             :          */
    1653           0 :         __disk_unblock_events(disk, ev->clearing ? true : false);
    1654             : 
    1655             :         /* then, fetch and clear pending events */
    1656           0 :         spin_lock_irq(&ev->lock);
    1657           0 :         pending = ev->pending & mask;
    1658           0 :         ev->pending &= ~mask;
    1659           0 :         spin_unlock_irq(&ev->lock);
    1660           0 :         WARN_ON_ONCE(clearing & mask);
    1661             : 
    1662             :         return pending;
    1663             : }
    1664             : 
    1665             : /**
    1666             :  * bdev_check_media_change - check if a removable media has been changed
    1667             :  * @bdev: block device to check
    1668             :  *
    1669             :  * Check whether a removable media has been changed, and attempt to free all
    1670             :  * dentries and inodes and invalidates all block device page cache entries in
    1671             :  * that case.
    1672             :  *
    1673             :  * Returns %true if the block device changed, or %false if not.
    1674             :  */
    1675           0 : bool bdev_check_media_change(struct block_device *bdev)
    1676             : {
    1677           0 :         unsigned int events;
    1678             : 
    1679           0 :         events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
    1680             :                                    DISK_EVENT_EJECT_REQUEST);
    1681           0 :         if (!(events & DISK_EVENT_MEDIA_CHANGE))
    1682             :                 return false;
    1683             : 
    1684           0 :         if (__invalidate_device(bdev, true))
    1685           0 :                 pr_warn("VFS: busy inodes on changed media %s\n",
    1686             :                         bdev->bd_disk->disk_name);
    1687           0 :         set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
    1688           0 :         return true;
    1689             : }
    1690             : EXPORT_SYMBOL(bdev_check_media_change);
    1691             : 
    1692             : /*
    1693             :  * Separate this part out so that a different pointer for clearing_ptr can be
    1694             :  * passed in for disk_clear_events.
    1695             :  */
    1696           0 : static void disk_events_workfn(struct work_struct *work)
    1697             : {
    1698           0 :         struct delayed_work *dwork = to_delayed_work(work);
    1699           0 :         struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
    1700             : 
    1701           0 :         disk_check_events(ev, &ev->clearing);
    1702           0 : }
    1703             : 
    1704           0 : static void disk_check_events(struct disk_events *ev,
    1705             :                               unsigned int *clearing_ptr)
    1706             : {
    1707           0 :         struct gendisk *disk = ev->disk;
    1708           0 :         char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
    1709           0 :         unsigned int clearing = *clearing_ptr;
    1710           0 :         unsigned int events;
    1711           0 :         unsigned long intv;
    1712           0 :         int nr_events = 0, i;
    1713             : 
    1714             :         /* check events */
    1715           0 :         events = disk->fops->check_events(disk, clearing);
    1716             : 
    1717             :         /* accumulate pending events and schedule next poll if necessary */
    1718           0 :         spin_lock_irq(&ev->lock);
    1719             : 
    1720           0 :         events &= ~ev->pending;
    1721           0 :         ev->pending |= events;
    1722           0 :         *clearing_ptr &= ~clearing;
    1723             : 
    1724           0 :         intv = disk_events_poll_jiffies(disk);
    1725           0 :         if (!ev->block && intv)
    1726           0 :                 queue_delayed_work(system_freezable_power_efficient_wq,
    1727             :                                 &ev->dwork, intv);
    1728             : 
    1729           0 :         spin_unlock_irq(&ev->lock);
    1730             : 
    1731             :         /*
    1732             :          * Tell userland about new events.  Only the events listed in
    1733             :          * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
    1734             :          * is set. Otherwise, events are processed internally but never
    1735             :          * get reported to userland.
    1736             :          */
    1737           0 :         for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
    1738           0 :                 if ((events & disk->events & (1 << i)) &&
    1739           0 :                     (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
    1740           0 :                         envp[nr_events++] = disk_uevents[i];
    1741             : 
    1742           0 :         if (nr_events)
    1743           0 :                 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
    1744           0 : }
    1745             : 
    1746             : /*
    1747             :  * A disk events enabled device has the following sysfs nodes under
    1748             :  * its /sys/block/X/ directory.
    1749             :  *
    1750             :  * events               : list of all supported events
    1751             :  * events_async         : list of events which can be detected w/o polling
    1752             :  *                        (always empty, only for backwards compatibility)
    1753             :  * events_poll_msecs    : polling interval, 0: disable, -1: system default
    1754             :  */
    1755           0 : static ssize_t __disk_events_show(unsigned int events, char *buf)
    1756             : {
    1757           0 :         const char *delim = "";
    1758           0 :         ssize_t pos = 0;
    1759           0 :         int i;
    1760             : 
    1761           0 :         for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
    1762           0 :                 if (events & (1 << i)) {
    1763           0 :                         pos += sprintf(buf + pos, "%s%s",
    1764             :                                        delim, disk_events_strs[i]);
    1765           0 :                         delim = " ";
    1766             :                 }
    1767           0 :         if (pos)
    1768           0 :                 pos += sprintf(buf + pos, "\n");
    1769           0 :         return pos;
    1770             : }
    1771             : 
    1772           0 : static ssize_t disk_events_show(struct device *dev,
    1773             :                                 struct device_attribute *attr, char *buf)
    1774             : {
    1775           0 :         struct gendisk *disk = dev_to_disk(dev);
    1776             : 
    1777           0 :         if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
    1778             :                 return 0;
    1779             : 
    1780           0 :         return __disk_events_show(disk->events, buf);
    1781             : }
    1782             : 
    1783           0 : static ssize_t disk_events_async_show(struct device *dev,
    1784             :                                       struct device_attribute *attr, char *buf)
    1785             : {
    1786           0 :         return 0;
    1787             : }
    1788             : 
    1789           0 : static ssize_t disk_events_poll_msecs_show(struct device *dev,
    1790             :                                            struct device_attribute *attr,
    1791             :                                            char *buf)
    1792             : {
    1793           0 :         struct gendisk *disk = dev_to_disk(dev);
    1794             : 
    1795           0 :         if (!disk->ev)
    1796           0 :                 return sprintf(buf, "-1\n");
    1797             : 
    1798           0 :         return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
    1799             : }
    1800             : 
    1801           0 : static ssize_t disk_events_poll_msecs_store(struct device *dev,
    1802             :                                             struct device_attribute *attr,
    1803             :                                             const char *buf, size_t count)
    1804             : {
    1805           0 :         struct gendisk *disk = dev_to_disk(dev);
    1806           0 :         long intv;
    1807             : 
    1808           0 :         if (!count || !sscanf(buf, "%ld", &intv))
    1809           0 :                 return -EINVAL;
    1810             : 
    1811           0 :         if (intv < 0 && intv != -1)
    1812             :                 return -EINVAL;
    1813             : 
    1814           0 :         if (!disk->ev)
    1815             :                 return -ENODEV;
    1816             : 
    1817           0 :         disk_block_events(disk);
    1818           0 :         disk->ev->poll_msecs = intv;
    1819           0 :         __disk_unblock_events(disk, true);
    1820             : 
    1821           0 :         return count;
    1822             : }
    1823             : 
    1824             : static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
    1825             : static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
    1826             : static const DEVICE_ATTR(events_poll_msecs, 0644,
    1827             :                          disk_events_poll_msecs_show,
    1828             :                          disk_events_poll_msecs_store);
    1829             : 
    1830             : static const struct attribute *disk_events_attrs[] = {
    1831             :         &dev_attr_events.attr,
    1832             :         &dev_attr_events_async.attr,
    1833             :         &dev_attr_events_poll_msecs.attr,
    1834             :         NULL,
    1835             : };
    1836             : 
    1837             : /*
    1838             :  * The default polling interval can be specified by the kernel
    1839             :  * parameter block.events_dfl_poll_msecs which defaults to 0
    1840             :  * (disable).  This can also be modified runtime by writing to
    1841             :  * /sys/module/block/parameters/events_dfl_poll_msecs.
    1842             :  */
    1843           0 : static int disk_events_set_dfl_poll_msecs(const char *val,
    1844             :                                           const struct kernel_param *kp)
    1845             : {
    1846           0 :         struct disk_events *ev;
    1847           0 :         int ret;
    1848             : 
    1849           0 :         ret = param_set_ulong(val, kp);
    1850           0 :         if (ret < 0)
    1851             :                 return ret;
    1852             : 
    1853           0 :         mutex_lock(&disk_events_mutex);
    1854             : 
    1855           0 :         list_for_each_entry(ev, &disk_events, node)
    1856           0 :                 disk_flush_events(ev->disk, 0);
    1857             : 
    1858           0 :         mutex_unlock(&disk_events_mutex);
    1859             : 
    1860           0 :         return 0;
    1861             : }
    1862             : 
    1863             : static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
    1864             :         .set    = disk_events_set_dfl_poll_msecs,
    1865             :         .get    = param_get_ulong,
    1866             : };
    1867             : 
    1868             : #undef MODULE_PARAM_PREFIX
    1869             : #define MODULE_PARAM_PREFIX     "block."
    1870             : 
    1871             : module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
    1872             :                 &disk_events_dfl_poll_msecs, 0644);
    1873             : 
    1874             : /*
    1875             :  * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
    1876             :  */
    1877           9 : static void disk_alloc_events(struct gendisk *disk)
    1878             : {
    1879           9 :         struct disk_events *ev;
    1880             : 
    1881           9 :         if (!disk->fops->check_events || !disk->events)
    1882             :                 return;
    1883             : 
    1884           0 :         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
    1885           0 :         if (!ev) {
    1886           0 :                 pr_warn("%s: failed to initialize events\n", disk->disk_name);
    1887           0 :                 return;
    1888             :         }
    1889             : 
    1890           0 :         INIT_LIST_HEAD(&ev->node);
    1891           0 :         ev->disk = disk;
    1892           0 :         spin_lock_init(&ev->lock);
    1893           0 :         mutex_init(&ev->block_mutex);
    1894           0 :         ev->block = 1;
    1895           0 :         ev->poll_msecs = -1;
    1896           0 :         INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
    1897             : 
    1898           0 :         disk->ev = ev;
    1899             : }
    1900             : 
    1901           9 : static void disk_add_events(struct gendisk *disk)
    1902             : {
    1903             :         /* FIXME: error handling */
    1904           9 :         if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
    1905           0 :                 pr_warn("%s: failed to create sysfs files for events\n",
    1906             :                         disk->disk_name);
    1907             : 
    1908           9 :         if (!disk->ev)
    1909             :                 return;
    1910             : 
    1911           0 :         mutex_lock(&disk_events_mutex);
    1912           0 :         list_add_tail(&disk->ev->node, &disk_events);
    1913           0 :         mutex_unlock(&disk_events_mutex);
    1914             : 
    1915             :         /*
    1916             :          * Block count is initialized to 1 and the following initial
    1917             :          * unblock kicks it into action.
    1918             :          */
    1919           0 :         __disk_unblock_events(disk, true);
    1920             : }
    1921             : 
    1922           0 : static void disk_del_events(struct gendisk *disk)
    1923             : {
    1924           0 :         if (disk->ev) {
    1925           0 :                 disk_block_events(disk);
    1926             : 
    1927           0 :                 mutex_lock(&disk_events_mutex);
    1928           0 :                 list_del_init(&disk->ev->node);
    1929           0 :                 mutex_unlock(&disk_events_mutex);
    1930             :         }
    1931             : 
    1932           0 :         sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
    1933           0 : }
    1934             : 
    1935           0 : static void disk_release_events(struct gendisk *disk)
    1936             : {
    1937             :         /* the block count should be 1 from disk_del_events() */
    1938           0 :         WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
    1939           0 :         kfree(disk->ev);
    1940           0 : }

Generated by: LCOV version 1.14