partition-generic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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_rcu_cb(struct rcu_head *head)
  216. {
  217. struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
  218. part->start_sect = 0;
  219. part->nr_sects = 0;
  220. part_stat_set_all(part, 0);
  221. put_device(part_to_dev(part));
  222. }
  223. void __delete_partition(struct percpu_ref *ref)
  224. {
  225. struct hd_struct *part = container_of(ref, struct hd_struct, ref);
  226. call_rcu(&part->rcu_head, delete_partition_rcu_cb);
  227. }
  228. /*
  229. * Must be called either with bd_mutex held, before a disk can be opened or
  230. * after all disk users are gone.
  231. */
  232. void delete_partition(struct gendisk *disk, int partno)
  233. {
  234. struct disk_part_tbl *ptbl =
  235. rcu_dereference_protected(disk->part_tbl, 1);
  236. struct hd_struct *part;
  237. if (partno >= ptbl->len)
  238. return;
  239. part = rcu_dereference_protected(ptbl->part[partno], 1);
  240. if (!part)
  241. return;
  242. rcu_assign_pointer(ptbl->part[partno], NULL);
  243. rcu_assign_pointer(ptbl->last_lookup, NULL);
  244. kobject_put(part->holder_dir);
  245. device_del(part_to_dev(part));
  246. hd_struct_kill(part);
  247. }
  248. static ssize_t whole_disk_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. return 0;
  252. }
  253. static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
  254. /*
  255. * Must be called either with bd_mutex held, before a disk can be opened or
  256. * after all disk users are gone.
  257. */
  258. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  259. sector_t start, sector_t len, int flags,
  260. struct partition_meta_info *info)
  261. {
  262. struct hd_struct *p;
  263. dev_t devt = MKDEV(0, 0);
  264. struct device *ddev = disk_to_dev(disk);
  265. struct device *pdev;
  266. struct disk_part_tbl *ptbl;
  267. const char *dname;
  268. int err;
  269. err = disk_expand_part_tbl(disk, partno);
  270. if (err)
  271. return ERR_PTR(err);
  272. ptbl = rcu_dereference_protected(disk->part_tbl, 1);
  273. if (ptbl->part[partno])
  274. return ERR_PTR(-EBUSY);
  275. p = kzalloc(sizeof(*p), GFP_KERNEL);
  276. if (!p)
  277. return ERR_PTR(-EBUSY);
  278. if (!init_part_stats(p)) {
  279. err = -ENOMEM;
  280. goto out_free;
  281. }
  282. seqcount_init(&p->nr_sects_seq);
  283. pdev = part_to_dev(p);
  284. p->start_sect = start;
  285. p->alignment_offset =
  286. queue_limit_alignment_offset(&disk->queue->limits, start);
  287. p->discard_alignment =
  288. queue_limit_discard_alignment(&disk->queue->limits, start);
  289. p->nr_sects = len;
  290. p->partno = partno;
  291. p->policy = get_disk_ro(disk);
  292. if (info) {
  293. struct partition_meta_info *pinfo = alloc_part_info(disk);
  294. if (!pinfo) {
  295. err = -ENOMEM;
  296. goto out_free_stats;
  297. }
  298. memcpy(pinfo, info, sizeof(*info));
  299. p->info = pinfo;
  300. }
  301. dname = dev_name(ddev);
  302. if (isdigit(dname[strlen(dname) - 1]))
  303. dev_set_name(pdev, "%sp%d", dname, partno);
  304. else
  305. dev_set_name(pdev, "%s%d", dname, partno);
  306. device_initialize(pdev);
  307. pdev->class = &block_class;
  308. pdev->type = &part_type;
  309. pdev->parent = ddev;
  310. err = blk_alloc_devt(p, &devt);
  311. if (err)
  312. goto out_free_info;
  313. pdev->devt = devt;
  314. /* delay uevent until 'holders' subdir is created */
  315. dev_set_uevent_suppress(pdev, 1);
  316. err = device_add(pdev);
  317. if (err)
  318. goto out_put;
  319. err = -ENOMEM;
  320. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  321. if (!p->holder_dir)
  322. goto out_del;
  323. dev_set_uevent_suppress(pdev, 0);
  324. if (flags & ADDPART_FLAG_WHOLEDISK) {
  325. err = device_create_file(pdev, &dev_attr_whole_disk);
  326. if (err)
  327. goto out_del;
  328. }
  329. err = hd_ref_init(p);
  330. if (err) {
  331. if (flags & ADDPART_FLAG_WHOLEDISK)
  332. goto out_remove_file;
  333. goto out_del;
  334. }
  335. /* everything is up and running, commence */
  336. rcu_assign_pointer(ptbl->part[partno], p);
  337. /* suppress uevent if the disk suppresses it */
  338. if (!dev_get_uevent_suppress(ddev))
  339. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  340. return p;
  341. out_free_info:
  342. free_part_info(p);
  343. out_free_stats:
  344. free_part_stats(p);
  345. out_free:
  346. kfree(p);
  347. return ERR_PTR(err);
  348. out_remove_file:
  349. device_remove_file(pdev, &dev_attr_whole_disk);
  350. out_del:
  351. kobject_put(p->holder_dir);
  352. device_del(pdev);
  353. out_put:
  354. put_device(pdev);
  355. return ERR_PTR(err);
  356. }
  357. static bool disk_unlock_native_capacity(struct gendisk *disk)
  358. {
  359. const struct block_device_operations *bdops = disk->fops;
  360. if (bdops->unlock_native_capacity &&
  361. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  362. printk(KERN_CONT "enabling native capacity\n");
  363. bdops->unlock_native_capacity(disk);
  364. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  365. return true;
  366. } else {
  367. printk(KERN_CONT "truncated\n");
  368. return false;
  369. }
  370. }
  371. static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
  372. {
  373. struct disk_part_iter piter;
  374. struct hd_struct *part;
  375. int res;
  376. if (bdev->bd_part_count || bdev->bd_super)
  377. return -EBUSY;
  378. res = invalidate_partition(disk, 0);
  379. if (res)
  380. return res;
  381. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  382. while ((part = disk_part_iter_next(&piter)))
  383. delete_partition(disk, part->partno);
  384. disk_part_iter_exit(&piter);
  385. return 0;
  386. }
  387. static bool part_zone_aligned(struct gendisk *disk,
  388. struct block_device *bdev,
  389. sector_t from, sector_t size)
  390. {
  391. unsigned int zone_sectors = bdev_zone_sectors(bdev);
  392. /*
  393. * If this function is called, then the disk is a zoned block device
  394. * (host-aware or host-managed). This can be detected even if the
  395. * zoned block device support is disabled (CONFIG_BLK_DEV_ZONED not
  396. * set). In this case, however, only host-aware devices will be seen
  397. * as a block device is not created for host-managed devices. Without
  398. * zoned block device support, host-aware drives can still be used as
  399. * regular block devices (no zone operation) and their zone size will
  400. * be reported as 0. Allow this case.
  401. */
  402. if (!zone_sectors)
  403. return true;
  404. /*
  405. * Check partition start and size alignement. If the drive has a
  406. * smaller last runt zone, ignore it and allow the partition to
  407. * use it. Check the zone size too: it should be a power of 2 number
  408. * of sectors.
  409. */
  410. if (WARN_ON_ONCE(!is_power_of_2(zone_sectors))) {
  411. u32 rem;
  412. div_u64_rem(from, zone_sectors, &rem);
  413. if (rem)
  414. return false;
  415. if ((from + size) < get_capacity(disk)) {
  416. div_u64_rem(size, zone_sectors, &rem);
  417. if (rem)
  418. return false;
  419. }
  420. } else {
  421. if (from & (zone_sectors - 1))
  422. return false;
  423. if ((from + size) < get_capacity(disk) &&
  424. (size & (zone_sectors - 1)))
  425. return false;
  426. }
  427. return true;
  428. }
  429. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  430. {
  431. struct parsed_partitions *state = NULL;
  432. struct hd_struct *part;
  433. int p, highest, res;
  434. rescan:
  435. if (state && !IS_ERR(state)) {
  436. free_partitions(state);
  437. state = NULL;
  438. }
  439. res = drop_partitions(disk, bdev);
  440. if (res)
  441. return res;
  442. if (disk->fops->revalidate_disk)
  443. disk->fops->revalidate_disk(disk);
  444. check_disk_size_change(disk, bdev, true);
  445. bdev->bd_invalidated = 0;
  446. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  447. return 0;
  448. if (IS_ERR(state)) {
  449. /*
  450. * I/O error reading the partition table. If any
  451. * partition code tried to read beyond EOD, retry
  452. * after unlocking native capacity.
  453. */
  454. if (PTR_ERR(state) == -ENOSPC) {
  455. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  456. disk->disk_name);
  457. if (disk_unlock_native_capacity(disk))
  458. goto rescan;
  459. }
  460. return -EIO;
  461. }
  462. /*
  463. * If any partition code tried to read beyond EOD, try
  464. * unlocking native capacity even if partition table is
  465. * successfully read as we could be missing some partitions.
  466. */
  467. if (state->access_beyond_eod) {
  468. printk(KERN_WARNING
  469. "%s: partition table partially beyond EOD, ",
  470. disk->disk_name);
  471. if (disk_unlock_native_capacity(disk))
  472. goto rescan;
  473. }
  474. /* tell userspace that the media / partition table may have changed */
  475. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  476. /* Detect the highest partition number and preallocate
  477. * disk->part_tbl. This is an optimization and not strictly
  478. * necessary.
  479. */
  480. for (p = 1, highest = 0; p < state->limit; p++)
  481. if (state->parts[p].size)
  482. highest = p;
  483. disk_expand_part_tbl(disk, highest);
  484. /* add partitions */
  485. for (p = 1; p < state->limit; p++) {
  486. sector_t size, from;
  487. size = state->parts[p].size;
  488. if (!size)
  489. continue;
  490. from = state->parts[p].from;
  491. if (from >= get_capacity(disk)) {
  492. printk(KERN_WARNING
  493. "%s: p%d start %llu is beyond EOD, ",
  494. disk->disk_name, p, (unsigned long long) from);
  495. if (disk_unlock_native_capacity(disk))
  496. goto rescan;
  497. continue;
  498. }
  499. if (from + size > get_capacity(disk)) {
  500. printk(KERN_WARNING
  501. "%s: p%d size %llu extends beyond EOD, ",
  502. disk->disk_name, p, (unsigned long long) size);
  503. if (disk_unlock_native_capacity(disk)) {
  504. /* free state and restart */
  505. goto rescan;
  506. } else {
  507. /*
  508. * we can not ignore partitions of broken tables
  509. * created by for example camera firmware, but
  510. * we limit them to the end of the disk to avoid
  511. * creating invalid block devices
  512. */
  513. size = get_capacity(disk) - from;
  514. }
  515. }
  516. /*
  517. * On a zoned block device, partitions should be aligned on the
  518. * device zone size (i.e. zone boundary crossing not allowed).
  519. * Otherwise, resetting the write pointer of the last zone of
  520. * one partition may impact the following partition.
  521. */
  522. if (bdev_is_zoned(bdev) &&
  523. !part_zone_aligned(disk, bdev, from, size)) {
  524. printk(KERN_WARNING
  525. "%s: p%d start %llu+%llu is not zone aligned\n",
  526. disk->disk_name, p, (unsigned long long) from,
  527. (unsigned long long) size);
  528. continue;
  529. }
  530. part = add_partition(disk, p, from, size,
  531. state->parts[p].flags,
  532. &state->parts[p].info);
  533. if (IS_ERR(part)) {
  534. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  535. disk->disk_name, p, -PTR_ERR(part));
  536. continue;
  537. }
  538. #ifdef CONFIG_BLK_DEV_MD
  539. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  540. md_autodetect_dev(part_to_dev(part)->devt);
  541. #endif
  542. }
  543. free_partitions(state);
  544. return 0;
  545. }
  546. int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
  547. {
  548. int res;
  549. if (!bdev->bd_invalidated)
  550. return 0;
  551. res = drop_partitions(disk, bdev);
  552. if (res)
  553. return res;
  554. set_capacity(disk, 0);
  555. check_disk_size_change(disk, bdev, false);
  556. bdev->bd_invalidated = 0;
  557. /* tell userspace that the media / partition table may have changed */
  558. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  559. return 0;
  560. }
  561. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  562. {
  563. struct address_space *mapping = bdev->bd_inode->i_mapping;
  564. struct page *page;
  565. page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_SHIFT-9)), NULL);
  566. if (!IS_ERR(page)) {
  567. if (PageError(page))
  568. goto fail;
  569. p->v = page;
  570. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << 9);
  571. fail:
  572. put_page(page);
  573. }
  574. p->v = NULL;
  575. return NULL;
  576. }
  577. EXPORT_SYMBOL(read_dev_sector);