partition-generic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Code extracted from drivers/block/genhd.c
  4. * Copyright (C) 1991-1998 Linus Torvalds
  5. * Re-organised Feb 1998 Russell King
  6. *
  7. * We now have independent partition support from the
  8. * block drivers, which allows all the partition code to
  9. * be grouped in one location, and it to be mostly self
  10. * contained.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/slab.h>
  16. #include <linux/kmod.h>
  17. #include <linux/ctype.h>
  18. #include <linux/genhd.h>
  19. #include <linux/blktrace_api.h>
  20. #include "partitions/check.h"
  21. #ifdef CONFIG_BLK_DEV_MD
  22. extern void md_autodetect_dev(dev_t dev);
  23. #endif
  24. /*
  25. * disk_name() is used by partition check code and the genhd driver.
  26. * It formats the devicename of the indicated disk into
  27. * the supplied buffer (of size at least 32), and returns
  28. * a pointer to that same buffer (for convenience).
  29. */
  30. char *disk_name(struct gendisk *hd, int partno, char *buf)
  31. {
  32. if (!partno)
  33. snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
  34. else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
  35. snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
  36. else
  37. snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
  38. return buf;
  39. }
  40. const char *bdevname(struct block_device *bdev, char *buf)
  41. {
  42. return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
  43. }
  44. EXPORT_SYMBOL(bdevname);
  45. const char *bio_devname(struct bio *bio, char *buf)
  46. {
  47. return disk_name(bio->bi_disk, bio->bi_partno, buf);
  48. }
  49. EXPORT_SYMBOL(bio_devname);
  50. /*
  51. * There's very little reason to use this, you should really
  52. * have a struct block_device just about everywhere and use
  53. * bdevname() instead.
  54. */
  55. const char *__bdevname(dev_t dev, char *buffer)
  56. {
  57. scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
  58. MAJOR(dev), MINOR(dev));
  59. return buffer;
  60. }
  61. EXPORT_SYMBOL(__bdevname);
  62. static ssize_t part_partition_show(struct device *dev,
  63. struct device_attribute *attr, char *buf)
  64. {
  65. struct hd_struct *p = dev_to_part(dev);
  66. return sprintf(buf, "%d\n", p->partno);
  67. }
  68. static ssize_t part_start_show(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct hd_struct *p = dev_to_part(dev);
  72. return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
  73. }
  74. ssize_t part_size_show(struct device *dev,
  75. struct device_attribute *attr, char *buf)
  76. {
  77. struct hd_struct *p = dev_to_part(dev);
  78. return sprintf(buf, "%llu\n",(unsigned long long)part_nr_sects_read(p));
  79. }
  80. static ssize_t part_ro_show(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. struct hd_struct *p = dev_to_part(dev);
  84. return sprintf(buf, "%d\n", p->policy ? 1 : 0);
  85. }
  86. static ssize_t part_alignment_offset_show(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct hd_struct *p = dev_to_part(dev);
  90. return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
  91. }
  92. static ssize_t part_discard_alignment_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct hd_struct *p = dev_to_part(dev);
  96. return sprintf(buf, "%u\n", p->discard_alignment);
  97. }
  98. ssize_t part_stat_show(struct device *dev,
  99. struct device_attribute *attr, char *buf)
  100. {
  101. struct hd_struct *p = dev_to_part(dev);
  102. struct request_queue *q = part_to_disk(p)->queue;
  103. unsigned int inflight[2];
  104. int cpu;
  105. cpu = part_stat_lock();
  106. part_round_stats(q, cpu, p);
  107. part_stat_unlock();
  108. part_in_flight(q, p, inflight);
  109. return sprintf(buf,
  110. "%8lu %8lu %8llu %8u "
  111. "%8lu %8lu %8llu %8u "
  112. "%8u %8u %8u "
  113. "%8lu %8lu %8llu %8u"
  114. "\n",
  115. part_stat_read(p, ios[STAT_READ]),
  116. part_stat_read(p, merges[STAT_READ]),
  117. (unsigned long long)part_stat_read(p, sectors[STAT_READ]),
  118. (unsigned int)part_stat_read_msecs(p, STAT_READ),
  119. part_stat_read(p, ios[STAT_WRITE]),
  120. part_stat_read(p, merges[STAT_WRITE]),
  121. (unsigned long long)part_stat_read(p, sectors[STAT_WRITE]),
  122. (unsigned int)part_stat_read_msecs(p, STAT_WRITE),
  123. inflight[0],
  124. jiffies_to_msecs(part_stat_read(p, io_ticks)),
  125. jiffies_to_msecs(part_stat_read(p, time_in_queue)),
  126. part_stat_read(p, ios[STAT_DISCARD]),
  127. part_stat_read(p, merges[STAT_DISCARD]),
  128. (unsigned long long)part_stat_read(p, sectors[STAT_DISCARD]),
  129. (unsigned int)part_stat_read_msecs(p, STAT_DISCARD));
  130. }
  131. ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
  132. char *buf)
  133. {
  134. struct hd_struct *p = dev_to_part(dev);
  135. struct request_queue *q = part_to_disk(p)->queue;
  136. unsigned int inflight[2];
  137. part_in_flight_rw(q, p, inflight);
  138. return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
  139. }
  140. #ifdef CONFIG_FAIL_MAKE_REQUEST
  141. ssize_t part_fail_show(struct device *dev,
  142. struct device_attribute *attr, char *buf)
  143. {
  144. struct hd_struct *p = dev_to_part(dev);
  145. return sprintf(buf, "%d\n", p->make_it_fail);
  146. }
  147. ssize_t part_fail_store(struct device *dev,
  148. struct device_attribute *attr,
  149. const char *buf, size_t count)
  150. {
  151. struct hd_struct *p = dev_to_part(dev);
  152. int i;
  153. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  154. p->make_it_fail = (i == 0) ? 0 : 1;
  155. return count;
  156. }
  157. #endif
  158. static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
  159. static DEVICE_ATTR(start, 0444, part_start_show, NULL);
  160. static DEVICE_ATTR(size, 0444, part_size_show, NULL);
  161. static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
  162. static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
  163. static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
  164. static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
  165. static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
  166. #ifdef CONFIG_FAIL_MAKE_REQUEST
  167. static struct device_attribute dev_attr_fail =
  168. __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
  169. #endif
  170. static struct attribute *part_attrs[] = {
  171. &dev_attr_partition.attr,
  172. &dev_attr_start.attr,
  173. &dev_attr_size.attr,
  174. &dev_attr_ro.attr,
  175. &dev_attr_alignment_offset.attr,
  176. &dev_attr_discard_alignment.attr,
  177. &dev_attr_stat.attr,
  178. &dev_attr_inflight.attr,
  179. #ifdef CONFIG_FAIL_MAKE_REQUEST
  180. &dev_attr_fail.attr,
  181. #endif
  182. NULL
  183. };
  184. static struct attribute_group part_attr_group = {
  185. .attrs = part_attrs,
  186. };
  187. static const struct attribute_group *part_attr_groups[] = {
  188. &part_attr_group,
  189. #ifdef CONFIG_BLK_DEV_IO_TRACE
  190. &blk_trace_attr_group,
  191. #endif
  192. NULL
  193. };
  194. static void part_release(struct device *dev)
  195. {
  196. struct hd_struct *p = dev_to_part(dev);
  197. blk_free_devt(dev->devt);
  198. hd_free_part(p);
  199. kfree(p);
  200. }
  201. static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
  202. {
  203. struct hd_struct *part = dev_to_part(dev);
  204. add_uevent_var(env, "PARTN=%u", part->partno);
  205. if (part->info && part->info->volname[0])
  206. add_uevent_var(env, "PARTNAME=%s", part->info->volname);
  207. return 0;
  208. }
  209. struct device_type part_type = {
  210. .name = "partition",
  211. .groups = part_attr_groups,
  212. .release = part_release,
  213. .uevent = part_uevent,
  214. };
  215. static void delete_partition_work_fn(struct work_struct *work)
  216. {
  217. struct hd_struct *part = container_of(to_rcu_work(work), struct hd_struct,
  218. rcu_work);
  219. part->start_sect = 0;
  220. part->nr_sects = 0;
  221. part_stat_set_all(part, 0);
  222. put_device(part_to_dev(part));
  223. }
  224. void __delete_partition(struct percpu_ref *ref)
  225. {
  226. struct hd_struct *part = container_of(ref, struct hd_struct, ref);
  227. INIT_RCU_WORK(&part->rcu_work, delete_partition_work_fn);
  228. queue_rcu_work(system_wq, &part->rcu_work);
  229. }
  230. /*
  231. * Must be called either with bd_mutex held, before a disk can be opened or
  232. * after all disk users are gone.
  233. */
  234. void delete_partition(struct gendisk *disk, int partno)
  235. {
  236. struct disk_part_tbl *ptbl =
  237. rcu_dereference_protected(disk->part_tbl, 1);
  238. struct hd_struct *part;
  239. if (partno >= ptbl->len)
  240. return;
  241. part = rcu_dereference_protected(ptbl->part[partno], 1);
  242. if (!part)
  243. return;
  244. rcu_assign_pointer(ptbl->part[partno], NULL);
  245. rcu_assign_pointer(ptbl->last_lookup, NULL);
  246. kobject_put(part->holder_dir);
  247. device_del(part_to_dev(part));
  248. /*
  249. * Remove gendisk pointer from idr so that it cannot be looked up
  250. * while RCU period before freeing gendisk is running to prevent
  251. * use-after-free issues. Note that the device number stays
  252. * "in-use" until we really free the gendisk.
  253. */
  254. blk_invalidate_devt(part_devt(part));
  255. hd_struct_kill(part);
  256. }
  257. static ssize_t whole_disk_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. return 0;
  261. }
  262. static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
  263. /*
  264. * Must be called either with bd_mutex held, before a disk can be opened or
  265. * after all disk users are gone.
  266. */
  267. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  268. sector_t start, sector_t len, int flags,
  269. struct partition_meta_info *info)
  270. {
  271. struct hd_struct *p;
  272. dev_t devt = MKDEV(0, 0);
  273. struct device *ddev = disk_to_dev(disk);
  274. struct device *pdev;
  275. struct disk_part_tbl *ptbl;
  276. const char *dname;
  277. int err;
  278. err = disk_expand_part_tbl(disk, partno);
  279. if (err)
  280. return ERR_PTR(err);
  281. ptbl = rcu_dereference_protected(disk->part_tbl, 1);
  282. if (ptbl->part[partno])
  283. return ERR_PTR(-EBUSY);
  284. p = kzalloc(sizeof(*p), GFP_KERNEL);
  285. if (!p)
  286. return ERR_PTR(-EBUSY);
  287. if (!init_part_stats(p)) {
  288. err = -ENOMEM;
  289. goto out_free;
  290. }
  291. seqcount_init(&p->nr_sects_seq);
  292. pdev = part_to_dev(p);
  293. p->start_sect = start;
  294. p->alignment_offset =
  295. queue_limit_alignment_offset(&disk->queue->limits, start);
  296. p->discard_alignment =
  297. queue_limit_discard_alignment(&disk->queue->limits, start);
  298. p->nr_sects = len;
  299. p->partno = partno;
  300. p->policy = get_disk_ro(disk);
  301. if (info) {
  302. struct partition_meta_info *pinfo = alloc_part_info(disk);
  303. if (!pinfo) {
  304. err = -ENOMEM;
  305. goto out_free_stats;
  306. }
  307. memcpy(pinfo, info, sizeof(*info));
  308. p->info = pinfo;
  309. }
  310. dname = dev_name(ddev);
  311. if (isdigit(dname[strlen(dname) - 1]))
  312. dev_set_name(pdev, "%sp%d", dname, partno);
  313. else
  314. dev_set_name(pdev, "%s%d", dname, partno);
  315. device_initialize(pdev);
  316. pdev->class = &block_class;
  317. pdev->type = &part_type;
  318. pdev->parent = ddev;
  319. err = blk_alloc_devt(p, &devt);
  320. if (err)
  321. goto out_free_info;
  322. pdev->devt = devt;
  323. /* delay uevent until 'holders' subdir is created */
  324. dev_set_uevent_suppress(pdev, 1);
  325. err = device_add(pdev);
  326. if (err)
  327. goto out_put;
  328. err = -ENOMEM;
  329. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  330. if (!p->holder_dir)
  331. goto out_del;
  332. dev_set_uevent_suppress(pdev, 0);
  333. if (flags & ADDPART_FLAG_WHOLEDISK) {
  334. err = device_create_file(pdev, &dev_attr_whole_disk);
  335. if (err)
  336. goto out_del;
  337. }
  338. err = hd_ref_init(p);
  339. if (err) {
  340. if (flags & ADDPART_FLAG_WHOLEDISK)
  341. goto out_remove_file;
  342. goto out_del;
  343. }
  344. /* everything is up and running, commence */
  345. rcu_assign_pointer(ptbl->part[partno], p);
  346. /* suppress uevent if the disk suppresses it */
  347. if (!dev_get_uevent_suppress(ddev))
  348. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  349. return p;
  350. out_free_info:
  351. free_part_info(p);
  352. out_free_stats:
  353. free_part_stats(p);
  354. out_free:
  355. kfree(p);
  356. return ERR_PTR(err);
  357. out_remove_file:
  358. device_remove_file(pdev, &dev_attr_whole_disk);
  359. out_del:
  360. kobject_put(p->holder_dir);
  361. device_del(pdev);
  362. out_put:
  363. put_device(pdev);
  364. return ERR_PTR(err);
  365. }
  366. static bool disk_unlock_native_capacity(struct gendisk *disk)
  367. {
  368. const struct block_device_operations *bdops = disk->fops;
  369. if (bdops->unlock_native_capacity &&
  370. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  371. printk(KERN_CONT "enabling native capacity\n");
  372. bdops->unlock_native_capacity(disk);
  373. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  374. return true;
  375. } else {
  376. printk(KERN_CONT "truncated\n");
  377. return false;
  378. }
  379. }
  380. static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
  381. {
  382. struct disk_part_iter piter;
  383. struct hd_struct *part;
  384. int res;
  385. if (bdev->bd_part_count || bdev->bd_super)
  386. return -EBUSY;
  387. res = invalidate_partition(disk, 0);
  388. if (res)
  389. return res;
  390. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  391. while ((part = disk_part_iter_next(&piter)))
  392. delete_partition(disk, part->partno);
  393. disk_part_iter_exit(&piter);
  394. return 0;
  395. }
  396. static bool part_zone_aligned(struct gendisk *disk,
  397. struct block_device *bdev,
  398. sector_t from, sector_t size)
  399. {
  400. unsigned int zone_sectors = bdev_zone_sectors(bdev);
  401. /*
  402. * If this function is called, then the disk is a zoned block device
  403. * (host-aware or host-managed). This can be detected even if the
  404. * zoned block device support is disabled (CONFIG_BLK_DEV_ZONED not
  405. * set). In this case, however, only host-aware devices will be seen
  406. * as a block device is not created for host-managed devices. Without
  407. * zoned block device support, host-aware drives can still be used as
  408. * regular block devices (no zone operation) and their zone size will
  409. * be reported as 0. Allow this case.
  410. */
  411. if (!zone_sectors)
  412. return true;
  413. /*
  414. * Check partition start and size alignement. If the drive has a
  415. * smaller last runt zone, ignore it and allow the partition to
  416. * use it. Check the zone size too: it should be a power of 2 number
  417. * of sectors.
  418. */
  419. if (WARN_ON_ONCE(!is_power_of_2(zone_sectors))) {
  420. u32 rem;
  421. div_u64_rem(from, zone_sectors, &rem);
  422. if (rem)
  423. return false;
  424. if ((from + size) < get_capacity(disk)) {
  425. div_u64_rem(size, zone_sectors, &rem);
  426. if (rem)
  427. return false;
  428. }
  429. } else {
  430. if (from & (zone_sectors - 1))
  431. return false;
  432. if ((from + size) < get_capacity(disk) &&
  433. (size & (zone_sectors - 1)))
  434. return false;
  435. }
  436. return true;
  437. }
  438. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  439. {
  440. struct parsed_partitions *state = NULL;
  441. struct hd_struct *part;
  442. int p, highest, res;
  443. rescan:
  444. if (state && !IS_ERR(state)) {
  445. free_partitions(state);
  446. state = NULL;
  447. }
  448. res = drop_partitions(disk, bdev);
  449. if (res)
  450. return res;
  451. if (disk->fops->revalidate_disk)
  452. disk->fops->revalidate_disk(disk);
  453. check_disk_size_change(disk, bdev, true);
  454. bdev->bd_invalidated = 0;
  455. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  456. return 0;
  457. if (IS_ERR(state)) {
  458. /*
  459. * I/O error reading the partition table. If any
  460. * partition code tried to read beyond EOD, retry
  461. * after unlocking native capacity.
  462. */
  463. if (PTR_ERR(state) == -ENOSPC) {
  464. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  465. disk->disk_name);
  466. if (disk_unlock_native_capacity(disk))
  467. goto rescan;
  468. }
  469. return -EIO;
  470. }
  471. /*
  472. * If any partition code tried to read beyond EOD, try
  473. * unlocking native capacity even if partition table is
  474. * successfully read as we could be missing some partitions.
  475. */
  476. if (state->access_beyond_eod) {
  477. printk(KERN_WARNING
  478. "%s: partition table partially beyond EOD, ",
  479. disk->disk_name);
  480. if (disk_unlock_native_capacity(disk))
  481. goto rescan;
  482. }
  483. /* tell userspace that the media / partition table may have changed */
  484. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  485. /* Detect the highest partition number and preallocate
  486. * disk->part_tbl. This is an optimization and not strictly
  487. * necessary.
  488. */
  489. for (p = 1, highest = 0; p < state->limit; p++)
  490. if (state->parts[p].size)
  491. highest = p;
  492. disk_expand_part_tbl(disk, highest);
  493. /* add partitions */
  494. for (p = 1; p < state->limit; p++) {
  495. sector_t size, from;
  496. size = state->parts[p].size;
  497. if (!size)
  498. continue;
  499. from = state->parts[p].from;
  500. if (from >= get_capacity(disk)) {
  501. printk(KERN_WARNING
  502. "%s: p%d start %llu is beyond EOD, ",
  503. disk->disk_name, p, (unsigned long long) from);
  504. if (disk_unlock_native_capacity(disk))
  505. goto rescan;
  506. continue;
  507. }
  508. if (from + size > get_capacity(disk)) {
  509. printk(KERN_WARNING
  510. "%s: p%d size %llu extends beyond EOD, ",
  511. disk->disk_name, p, (unsigned long long) size);
  512. if (disk_unlock_native_capacity(disk)) {
  513. /* free state and restart */
  514. goto rescan;
  515. } else {
  516. /*
  517. * we can not ignore partitions of broken tables
  518. * created by for example camera firmware, but
  519. * we limit them to the end of the disk to avoid
  520. * creating invalid block devices
  521. */
  522. size = get_capacity(disk) - from;
  523. }
  524. }
  525. /*
  526. * On a zoned block device, partitions should be aligned on the
  527. * device zone size (i.e. zone boundary crossing not allowed).
  528. * Otherwise, resetting the write pointer of the last zone of
  529. * one partition may impact the following partition.
  530. */
  531. if (bdev_is_zoned(bdev) &&
  532. !part_zone_aligned(disk, bdev, from, size)) {
  533. printk(KERN_WARNING
  534. "%s: p%d start %llu+%llu is not zone aligned\n",
  535. disk->disk_name, p, (unsigned long long) from,
  536. (unsigned long long) size);
  537. continue;
  538. }
  539. part = add_partition(disk, p, from, size,
  540. state->parts[p].flags,
  541. &state->parts[p].info);
  542. if (IS_ERR(part)) {
  543. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  544. disk->disk_name, p, -PTR_ERR(part));
  545. continue;
  546. }
  547. #ifdef CONFIG_BLK_DEV_MD
  548. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  549. md_autodetect_dev(part_to_dev(part)->devt);
  550. #endif
  551. }
  552. free_partitions(state);
  553. return 0;
  554. }
  555. int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
  556. {
  557. int res;
  558. if (!bdev->bd_invalidated)
  559. return 0;
  560. res = drop_partitions(disk, bdev);
  561. if (res)
  562. return res;
  563. set_capacity(disk, 0);
  564. check_disk_size_change(disk, bdev, false);
  565. bdev->bd_invalidated = 0;
  566. /* tell userspace that the media / partition table may have changed */
  567. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  568. return 0;
  569. }
  570. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  571. {
  572. struct address_space *mapping = bdev->bd_inode->i_mapping;
  573. struct page *page;
  574. page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_SHIFT-9)), NULL);
  575. if (!IS_ERR(page)) {
  576. if (PageError(page))
  577. goto fail;
  578. p->v = page;
  579. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << 9);
  580. fail:
  581. put_page(page);
  582. }
  583. p->v = NULL;
  584. return NULL;
  585. }
  586. EXPORT_SYMBOL(read_dev_sector);