core.c 17 KB

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