core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991-1998 Linus Torvalds
  4. * Re-organised Feb 1998 Russell King
  5. * Copyright (C) 2020 Christoph Hellwig
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/major.h>
  9. #include <linux/slab.h>
  10. #include <linux/ctype.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/raid/detect.h>
  13. #include "check.h"
  14. static int (*check_part[])(struct parsed_partitions *) = {
  15. /*
  16. * Probe partition formats with tables at disk address 0
  17. * that also have an ADFS boot block at 0xdc0.
  18. */
  19. #ifdef CONFIG_ACORN_PARTITION_ICS
  20. adfspart_check_ICS,
  21. #endif
  22. #ifdef CONFIG_ACORN_PARTITION_POWERTEC
  23. adfspart_check_POWERTEC,
  24. #endif
  25. #ifdef CONFIG_ACORN_PARTITION_EESOX
  26. adfspart_check_EESOX,
  27. #endif
  28. /*
  29. * Now move on to formats that only have partition info at
  30. * disk address 0xdc0. Since these may also have stale
  31. * PC/BIOS partition tables, they need to come before
  32. * the msdos entry.
  33. */
  34. #ifdef CONFIG_ACORN_PARTITION_CUMANA
  35. adfspart_check_CUMANA,
  36. #endif
  37. #ifdef CONFIG_ACORN_PARTITION_ADFS
  38. adfspart_check_ADFS,
  39. #endif
  40. #ifdef CONFIG_CMDLINE_PARTITION
  41. cmdline_partition,
  42. #endif
  43. #ifdef CONFIG_EFI_PARTITION
  44. efi_partition, /* this must come before msdos */
  45. #endif
  46. #ifdef CONFIG_SGI_PARTITION
  47. sgi_partition,
  48. #endif
  49. #ifdef CONFIG_LDM_PARTITION
  50. ldm_partition, /* this must come before msdos */
  51. #endif
  52. #ifdef CONFIG_MSDOS_PARTITION
  53. msdos_partition,
  54. #endif
  55. #ifdef CONFIG_OSF_PARTITION
  56. osf_partition,
  57. #endif
  58. #ifdef CONFIG_SUN_PARTITION
  59. sun_partition,
  60. #endif
  61. #ifdef CONFIG_AMIGA_PARTITION
  62. amiga_partition,
  63. #endif
  64. #ifdef CONFIG_ATARI_PARTITION
  65. atari_partition,
  66. #endif
  67. #ifdef CONFIG_MAC_PARTITION
  68. mac_partition,
  69. #endif
  70. #ifdef CONFIG_ULTRIX_PARTITION
  71. ultrix_partition,
  72. #endif
  73. #ifdef CONFIG_IBM_PARTITION
  74. ibm_partition,
  75. #endif
  76. #ifdef CONFIG_KARMA_PARTITION
  77. karma_partition,
  78. #endif
  79. #ifdef CONFIG_SYSV68_PARTITION
  80. sysv68_partition,
  81. #endif
  82. NULL
  83. };
  84. static void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
  85. {
  86. spin_lock(&bdev->bd_size_lock);
  87. i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
  88. bdev->bd_nr_sectors = sectors;
  89. spin_unlock(&bdev->bd_size_lock);
  90. }
  91. static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
  92. {
  93. struct parsed_partitions *state;
  94. int nr = DISK_MAX_PARTS;
  95. state = kzalloc(sizeof(*state), GFP_KERNEL);
  96. if (!state)
  97. return NULL;
  98. state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
  99. if (!state->parts) {
  100. kfree(state);
  101. return NULL;
  102. }
  103. state->limit = nr;
  104. return state;
  105. }
  106. static void free_partitions(struct parsed_partitions *state)
  107. {
  108. vfree(state->parts);
  109. kfree(state);
  110. }
  111. static struct parsed_partitions *check_partition(struct gendisk *hd)
  112. {
  113. struct parsed_partitions *state;
  114. int i, res, err;
  115. state = allocate_partitions(hd);
  116. if (!state)
  117. return NULL;
  118. state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
  119. if (!state->pp_buf) {
  120. free_partitions(state);
  121. return NULL;
  122. }
  123. state->pp_buf[0] = '\0';
  124. state->disk = hd;
  125. snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
  126. snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
  127. if (isdigit(state->name[strlen(state->name)-1]))
  128. sprintf(state->name, "p");
  129. i = res = err = 0;
  130. while (!res && check_part[i]) {
  131. memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
  132. res = check_part[i++](state);
  133. if (res < 0) {
  134. /*
  135. * We have hit an I/O error which we don't report now.
  136. * But record it, and let the others do their job.
  137. */
  138. err = res;
  139. res = 0;
  140. }
  141. }
  142. if (res > 0) {
  143. printk(KERN_INFO "%s", state->pp_buf);
  144. free_page((unsigned long)state->pp_buf);
  145. return state;
  146. }
  147. if (state->access_beyond_eod)
  148. err = -ENOSPC;
  149. /*
  150. * The partition is unrecognized. So report I/O errors if there were any
  151. */
  152. if (err)
  153. res = err;
  154. if (res) {
  155. strlcat(state->pp_buf,
  156. " unable to read partition table\n", PAGE_SIZE);
  157. printk(KERN_INFO "%s", state->pp_buf);
  158. }
  159. free_page((unsigned long)state->pp_buf);
  160. free_partitions(state);
  161. return ERR_PTR(res);
  162. }
  163. static ssize_t part_partition_show(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_partno);
  167. }
  168. static ssize_t part_start_show(struct device *dev,
  169. struct device_attribute *attr, char *buf)
  170. {
  171. return sprintf(buf, "%llu\n", dev_to_bdev(dev)->bd_start_sect);
  172. }
  173. static ssize_t part_ro_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. return sprintf(buf, "%d\n", bdev_read_only(dev_to_bdev(dev)));
  177. }
  178. static ssize_t part_alignment_offset_show(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. return sprintf(buf, "%u\n", bdev_alignment_offset(dev_to_bdev(dev)));
  182. }
  183. static ssize_t part_discard_alignment_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. return sprintf(buf, "%u\n", bdev_discard_alignment(dev_to_bdev(dev)));
  187. }
  188. static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
  189. static DEVICE_ATTR(start, 0444, part_start_show, NULL);
  190. static DEVICE_ATTR(size, 0444, part_size_show, NULL);
  191. static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
  192. static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
  193. static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
  194. static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
  195. static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
  196. #ifdef CONFIG_FAIL_MAKE_REQUEST
  197. static struct device_attribute dev_attr_fail =
  198. __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
  199. #endif
  200. static struct attribute *part_attrs[] = {
  201. &dev_attr_partition.attr,
  202. &dev_attr_start.attr,
  203. &dev_attr_size.attr,
  204. &dev_attr_ro.attr,
  205. &dev_attr_alignment_offset.attr,
  206. &dev_attr_discard_alignment.attr,
  207. &dev_attr_stat.attr,
  208. &dev_attr_inflight.attr,
  209. #ifdef CONFIG_FAIL_MAKE_REQUEST
  210. &dev_attr_fail.attr,
  211. #endif
  212. NULL
  213. };
  214. static struct attribute_group part_attr_group = {
  215. .attrs = part_attrs,
  216. };
  217. static const struct attribute_group *part_attr_groups[] = {
  218. &part_attr_group,
  219. #ifdef CONFIG_BLK_DEV_IO_TRACE
  220. &blk_trace_attr_group,
  221. #endif
  222. NULL
  223. };
  224. static void part_release(struct device *dev)
  225. {
  226. put_disk(dev_to_bdev(dev)->bd_disk);
  227. iput(dev_to_bdev(dev)->bd_inode);
  228. }
  229. static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
  230. {
  231. struct block_device *part = dev_to_bdev(dev);
  232. add_uevent_var(env, "PARTN=%u", part->bd_partno);
  233. if (part->bd_meta_info && part->bd_meta_info->volname[0])
  234. add_uevent_var(env, "PARTNAME=%s", part->bd_meta_info->volname);
  235. return 0;
  236. }
  237. struct device_type part_type = {
  238. .name = "partition",
  239. .groups = part_attr_groups,
  240. .release = part_release,
  241. .uevent = part_uevent,
  242. };
  243. static void delete_partition(struct block_device *part)
  244. {
  245. lockdep_assert_held(&part->bd_disk->open_mutex);
  246. fsync_bdev(part);
  247. __invalidate_device(part, true);
  248. xa_erase(&part->bd_disk->part_tbl, part->bd_partno);
  249. kobject_put(part->bd_holder_dir);
  250. device_del(&part->bd_device);
  251. /*
  252. * Remove the block device from the inode hash, so that it cannot be
  253. * looked up any more even when openers still hold references.
  254. */
  255. remove_inode_hash(part->bd_inode);
  256. put_device(&part->bd_device);
  257. }
  258. static ssize_t whole_disk_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. return 0;
  262. }
  263. static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
  264. /*
  265. * Must be called either with open_mutex held, before a disk can be opened or
  266. * after all disk users are gone.
  267. */
  268. static struct block_device *add_partition(struct gendisk *disk, int partno,
  269. sector_t start, sector_t len, int flags,
  270. struct partition_meta_info *info)
  271. {
  272. dev_t devt = MKDEV(0, 0);
  273. struct device *ddev = disk_to_dev(disk);
  274. struct device *pdev;
  275. struct block_device *bdev;
  276. const char *dname;
  277. int err;
  278. lockdep_assert_held(&disk->open_mutex);
  279. if (partno >= DISK_MAX_PARTS)
  280. return ERR_PTR(-EINVAL);
  281. /*
  282. * Partitions are not supported on zoned block devices that are used as
  283. * such.
  284. */
  285. switch (disk->queue->limits.zoned) {
  286. case BLK_ZONED_HM:
  287. pr_warn("%s: partitions not supported on host managed zoned block device\n",
  288. disk->disk_name);
  289. return ERR_PTR(-ENXIO);
  290. case BLK_ZONED_HA:
  291. pr_info("%s: disabling host aware zoned block device support due to partitions\n",
  292. disk->disk_name);
  293. disk_set_zoned(disk, BLK_ZONED_NONE);
  294. break;
  295. case BLK_ZONED_NONE:
  296. break;
  297. }
  298. if (xa_load(&disk->part_tbl, partno))
  299. return ERR_PTR(-EBUSY);
  300. /* ensure we always have a reference to the whole disk */
  301. get_device(disk_to_dev(disk));
  302. err = -ENOMEM;
  303. bdev = bdev_alloc(disk, partno);
  304. if (!bdev)
  305. goto out_put_disk;
  306. bdev->bd_start_sect = start;
  307. bdev_set_nr_sectors(bdev, len);
  308. pdev = &bdev->bd_device;
  309. dname = dev_name(ddev);
  310. if (isdigit(dname[strlen(dname) - 1]))
  311. dev_set_name(pdev, "%sp%d", dname, partno);
  312. else
  313. dev_set_name(pdev, "%s%d", dname, partno);
  314. device_initialize(pdev);
  315. pdev->class = &block_class;
  316. pdev->type = &part_type;
  317. pdev->parent = ddev;
  318. /* in consecutive minor range? */
  319. if (bdev->bd_partno < disk->minors) {
  320. devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
  321. } else {
  322. err = blk_alloc_ext_minor();
  323. if (err < 0)
  324. goto out_put;
  325. devt = MKDEV(BLOCK_EXT_MAJOR, err);
  326. }
  327. pdev->devt = devt;
  328. if (info) {
  329. err = -ENOMEM;
  330. bdev->bd_meta_info = kmemdup(info, sizeof(*info), GFP_KERNEL);
  331. if (!bdev->bd_meta_info)
  332. goto out_put;
  333. }
  334. /* delay uevent until 'holders' subdir is created */
  335. dev_set_uevent_suppress(pdev, 1);
  336. err = device_add(pdev);
  337. if (err)
  338. goto out_put;
  339. err = -ENOMEM;
  340. bdev->bd_holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  341. if (!bdev->bd_holder_dir)
  342. goto out_del;
  343. dev_set_uevent_suppress(pdev, 0);
  344. if (flags & ADDPART_FLAG_WHOLEDISK) {
  345. err = device_create_file(pdev, &dev_attr_whole_disk);
  346. if (err)
  347. goto out_del;
  348. }
  349. /* everything is up and running, commence */
  350. err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL);
  351. if (err)
  352. goto out_del;
  353. bdev_add(bdev, devt);
  354. /* suppress uevent if the disk suppresses it */
  355. if (!dev_get_uevent_suppress(ddev))
  356. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  357. return bdev;
  358. out_del:
  359. kobject_put(bdev->bd_holder_dir);
  360. device_del(pdev);
  361. out_put:
  362. put_device(pdev);
  363. return ERR_PTR(err);
  364. out_put_disk:
  365. put_disk(disk);
  366. return ERR_PTR(err);
  367. }
  368. static bool partition_overlaps(struct gendisk *disk, sector_t start,
  369. sector_t length, int skip_partno)
  370. {
  371. struct block_device *part;
  372. bool overlap = false;
  373. unsigned long idx;
  374. rcu_read_lock();
  375. xa_for_each_start(&disk->part_tbl, idx, part, 1) {
  376. if (part->bd_partno != skip_partno &&
  377. start < part->bd_start_sect + bdev_nr_sectors(part) &&
  378. start + length > part->bd_start_sect) {
  379. overlap = true;
  380. break;
  381. }
  382. }
  383. rcu_read_unlock();
  384. return overlap;
  385. }
  386. int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
  387. sector_t length)
  388. {
  389. struct block_device *part;
  390. int ret;
  391. mutex_lock(&disk->open_mutex);
  392. if (!disk_live(disk)) {
  393. ret = -ENXIO;
  394. goto out;
  395. }
  396. if (partition_overlaps(disk, start, length, -1)) {
  397. ret = -EBUSY;
  398. goto out;
  399. }
  400. part = add_partition(disk, partno, start, length,
  401. ADDPART_FLAG_NONE, NULL);
  402. ret = PTR_ERR_OR_ZERO(part);
  403. out:
  404. mutex_unlock(&disk->open_mutex);
  405. return ret;
  406. }
  407. int bdev_del_partition(struct gendisk *disk, int partno)
  408. {
  409. struct block_device *part = NULL;
  410. int ret = -ENXIO;
  411. mutex_lock(&disk->open_mutex);
  412. part = xa_load(&disk->part_tbl, partno);
  413. if (!part)
  414. goto out_unlock;
  415. ret = -EBUSY;
  416. if (atomic_read(&part->bd_openers))
  417. goto out_unlock;
  418. delete_partition(part);
  419. ret = 0;
  420. out_unlock:
  421. mutex_unlock(&disk->open_mutex);
  422. return ret;
  423. }
  424. int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
  425. sector_t length)
  426. {
  427. struct block_device *part = NULL;
  428. int ret = -ENXIO;
  429. mutex_lock(&disk->open_mutex);
  430. part = xa_load(&disk->part_tbl, partno);
  431. if (!part)
  432. goto out_unlock;
  433. ret = -EINVAL;
  434. if (start != part->bd_start_sect)
  435. goto out_unlock;
  436. ret = -EBUSY;
  437. if (partition_overlaps(disk, start, length, partno))
  438. goto out_unlock;
  439. bdev_set_nr_sectors(part, length);
  440. ret = 0;
  441. out_unlock:
  442. mutex_unlock(&disk->open_mutex);
  443. return ret;
  444. }
  445. static bool disk_unlock_native_capacity(struct gendisk *disk)
  446. {
  447. if (!disk->fops->unlock_native_capacity ||
  448. test_and_set_bit(GD_NATIVE_CAPACITY, &disk->state)) {
  449. printk(KERN_CONT "truncated\n");
  450. return false;
  451. }
  452. printk(KERN_CONT "enabling native capacity\n");
  453. disk->fops->unlock_native_capacity(disk);
  454. return true;
  455. }
  456. void blk_drop_partitions(struct gendisk *disk)
  457. {
  458. struct block_device *part;
  459. unsigned long idx;
  460. lockdep_assert_held(&disk->open_mutex);
  461. xa_for_each_start(&disk->part_tbl, idx, part, 1)
  462. delete_partition(part);
  463. }
  464. static bool blk_add_partition(struct gendisk *disk,
  465. struct parsed_partitions *state, int p)
  466. {
  467. sector_t size = state->parts[p].size;
  468. sector_t from = state->parts[p].from;
  469. struct block_device *part;
  470. if (!size)
  471. return true;
  472. if (from >= get_capacity(disk)) {
  473. printk(KERN_WARNING
  474. "%s: p%d start %llu is beyond EOD, ",
  475. disk->disk_name, p, (unsigned long long) from);
  476. if (disk_unlock_native_capacity(disk))
  477. return false;
  478. return true;
  479. }
  480. if (from + size > get_capacity(disk)) {
  481. printk(KERN_WARNING
  482. "%s: p%d size %llu extends beyond EOD, ",
  483. disk->disk_name, p, (unsigned long long) size);
  484. if (disk_unlock_native_capacity(disk))
  485. return false;
  486. /*
  487. * We can not ignore partitions of broken tables created by for
  488. * example camera firmware, but we limit them to the end of the
  489. * disk to avoid creating invalid block devices.
  490. */
  491. size = get_capacity(disk) - from;
  492. }
  493. part = add_partition(disk, p, from, size, state->parts[p].flags,
  494. &state->parts[p].info);
  495. if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
  496. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  497. disk->disk_name, p, -PTR_ERR(part));
  498. return true;
  499. }
  500. if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
  501. (state->parts[p].flags & ADDPART_FLAG_RAID))
  502. md_autodetect_dev(part->bd_dev);
  503. return true;
  504. }
  505. static int blk_add_partitions(struct gendisk *disk)
  506. {
  507. struct parsed_partitions *state;
  508. int ret = -EAGAIN, p;
  509. if (disk->flags & GENHD_FL_NO_PART)
  510. return 0;
  511. if (test_bit(GD_SUPPRESS_PART_SCAN, &disk->state))
  512. return 0;
  513. state = check_partition(disk);
  514. if (!state)
  515. return 0;
  516. if (IS_ERR(state)) {
  517. /*
  518. * I/O error reading the partition table. If we tried to read
  519. * beyond EOD, retry after unlocking the native capacity.
  520. */
  521. if (PTR_ERR(state) == -ENOSPC) {
  522. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  523. disk->disk_name);
  524. if (disk_unlock_native_capacity(disk))
  525. return -EAGAIN;
  526. }
  527. return -EIO;
  528. }
  529. /*
  530. * Partitions are not supported on host managed zoned block devices.
  531. */
  532. if (disk->queue->limits.zoned == BLK_ZONED_HM) {
  533. pr_warn("%s: ignoring partition table on host managed zoned block device\n",
  534. disk->disk_name);
  535. ret = 0;
  536. goto out_free_state;
  537. }
  538. /*
  539. * If we read beyond EOD, try unlocking native capacity even if the
  540. * partition table was successfully read as we could be missing some
  541. * partitions.
  542. */
  543. if (state->access_beyond_eod) {
  544. printk(KERN_WARNING
  545. "%s: partition table partially beyond EOD, ",
  546. disk->disk_name);
  547. if (disk_unlock_native_capacity(disk))
  548. goto out_free_state;
  549. }
  550. /* tell userspace that the media / partition table may have changed */
  551. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  552. for (p = 1; p < state->limit; p++)
  553. if (!blk_add_partition(disk, state, p))
  554. goto out_free_state;
  555. ret = 0;
  556. out_free_state:
  557. free_partitions(state);
  558. return ret;
  559. }
  560. int bdev_disk_changed(struct gendisk *disk, bool invalidate)
  561. {
  562. int ret = 0;
  563. lockdep_assert_held(&disk->open_mutex);
  564. if (!disk_live(disk))
  565. return -ENXIO;
  566. rescan:
  567. if (disk->open_partitions)
  568. return -EBUSY;
  569. sync_blockdev(disk->part0);
  570. invalidate_bdev(disk->part0);
  571. blk_drop_partitions(disk);
  572. clear_bit(GD_NEED_PART_SCAN, &disk->state);
  573. /*
  574. * Historically we only set the capacity to zero for devices that
  575. * support partitions (independ of actually having partitions created).
  576. * Doing that is rather inconsistent, but changing it broke legacy
  577. * udisks polling for legacy ide-cdrom devices. Use the crude check
  578. * below to get the sane behavior for most device while not breaking
  579. * userspace for this particular setup.
  580. */
  581. if (invalidate) {
  582. if (!(disk->flags & GENHD_FL_NO_PART) ||
  583. !(disk->flags & GENHD_FL_REMOVABLE))
  584. set_capacity(disk, 0);
  585. }
  586. if (get_capacity(disk)) {
  587. ret = blk_add_partitions(disk);
  588. if (ret == -EAGAIN)
  589. goto rescan;
  590. } else if (invalidate) {
  591. /*
  592. * Tell userspace that the media / partition table may have
  593. * changed.
  594. */
  595. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  596. }
  597. return ret;
  598. }
  599. /*
  600. * Only exported for loop and dasd for historic reasons. Don't use in new
  601. * code!
  602. */
  603. EXPORT_SYMBOL_GPL(bdev_disk_changed);
  604. void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
  605. {
  606. struct address_space *mapping = state->disk->part0->bd_inode->i_mapping;
  607. struct folio *folio;
  608. if (n >= get_capacity(state->disk)) {
  609. state->access_beyond_eod = true;
  610. goto out;
  611. }
  612. folio = read_mapping_folio(mapping, n >> PAGE_SECTORS_SHIFT, NULL);
  613. if (IS_ERR(folio))
  614. goto out;
  615. p->v = folio;
  616. return folio_address(folio) + offset_in_folio(folio, n * SECTOR_SIZE);
  617. out:
  618. p->v = NULL;
  619. return NULL;
  620. }