core.c 17 KB

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