check.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * fs/partitions/check.c
  3. *
  4. * Code extracted from drivers/block/genhd.c
  5. * Copyright (C) 1991-1998 Linus Torvalds
  6. * Re-organised Feb 1998 Russell King
  7. *
  8. * We now have independent partition support from the
  9. * block drivers, which allows all the partition code to
  10. * be grouped in one location, and it to be mostly self
  11. * contained.
  12. *
  13. * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl}
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/slab.h>
  19. #include <linux/kmod.h>
  20. #include <linux/ctype.h>
  21. #include <linux/genhd.h>
  22. #include <linux/blktrace_api.h>
  23. #include "check.h"
  24. #include "acorn.h"
  25. #include "amiga.h"
  26. #include "atari.h"
  27. #include "ldm.h"
  28. #include "mac.h"
  29. #include "msdos.h"
  30. #include "osf.h"
  31. #include "sgi.h"
  32. #include "sun.h"
  33. #include "ibm.h"
  34. #include "ultrix.h"
  35. #include "efi.h"
  36. #include "karma.h"
  37. #include "sysv68.h"
  38. #ifdef CONFIG_BLK_DEV_MD
  39. extern void md_autodetect_dev(dev_t dev);
  40. #endif
  41. int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
  42. static int (*check_part[])(struct parsed_partitions *) = {
  43. /*
  44. * Probe partition formats with tables at disk address 0
  45. * that also have an ADFS boot block at 0xdc0.
  46. */
  47. #ifdef CONFIG_ACORN_PARTITION_ICS
  48. adfspart_check_ICS,
  49. #endif
  50. #ifdef CONFIG_ACORN_PARTITION_POWERTEC
  51. adfspart_check_POWERTEC,
  52. #endif
  53. #ifdef CONFIG_ACORN_PARTITION_EESOX
  54. adfspart_check_EESOX,
  55. #endif
  56. /*
  57. * Now move on to formats that only have partition info at
  58. * disk address 0xdc0. Since these may also have stale
  59. * PC/BIOS partition tables, they need to come before
  60. * the msdos entry.
  61. */
  62. #ifdef CONFIG_ACORN_PARTITION_CUMANA
  63. adfspart_check_CUMANA,
  64. #endif
  65. #ifdef CONFIG_ACORN_PARTITION_ADFS
  66. adfspart_check_ADFS,
  67. #endif
  68. #ifdef CONFIG_EFI_PARTITION
  69. efi_partition, /* this must come before msdos */
  70. #endif
  71. #ifdef CONFIG_SGI_PARTITION
  72. sgi_partition,
  73. #endif
  74. #ifdef CONFIG_LDM_PARTITION
  75. ldm_partition, /* this must come before msdos */
  76. #endif
  77. #ifdef CONFIG_MSDOS_PARTITION
  78. msdos_partition,
  79. #endif
  80. #ifdef CONFIG_OSF_PARTITION
  81. osf_partition,
  82. #endif
  83. #ifdef CONFIG_SUN_PARTITION
  84. sun_partition,
  85. #endif
  86. #ifdef CONFIG_AMIGA_PARTITION
  87. amiga_partition,
  88. #endif
  89. #ifdef CONFIG_ATARI_PARTITION
  90. atari_partition,
  91. #endif
  92. #ifdef CONFIG_MAC_PARTITION
  93. mac_partition,
  94. #endif
  95. #ifdef CONFIG_ULTRIX_PARTITION
  96. ultrix_partition,
  97. #endif
  98. #ifdef CONFIG_IBM_PARTITION
  99. ibm_partition,
  100. #endif
  101. #ifdef CONFIG_KARMA_PARTITION
  102. karma_partition,
  103. #endif
  104. #ifdef CONFIG_SYSV68_PARTITION
  105. sysv68_partition,
  106. #endif
  107. NULL
  108. };
  109. /*
  110. * disk_name() is used by partition check code and the genhd driver.
  111. * It formats the devicename of the indicated disk into
  112. * the supplied buffer (of size at least 32), and returns
  113. * a pointer to that same buffer (for convenience).
  114. */
  115. char *disk_name(struct gendisk *hd, int partno, char *buf)
  116. {
  117. if (!partno)
  118. snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
  119. else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
  120. snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
  121. else
  122. snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
  123. return buf;
  124. }
  125. const char *bdevname(struct block_device *bdev, char *buf)
  126. {
  127. return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
  128. }
  129. EXPORT_SYMBOL(bdevname);
  130. /*
  131. * There's very little reason to use this, you should really
  132. * have a struct block_device just about everywhere and use
  133. * bdevname() instead.
  134. */
  135. const char *__bdevname(dev_t dev, char *buffer)
  136. {
  137. scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
  138. MAJOR(dev), MINOR(dev));
  139. return buffer;
  140. }
  141. EXPORT_SYMBOL(__bdevname);
  142. static struct parsed_partitions *
  143. check_partition(struct gendisk *hd, struct block_device *bdev)
  144. {
  145. struct parsed_partitions *state;
  146. int i, res, err;
  147. state = kzalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
  148. if (!state)
  149. return NULL;
  150. state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
  151. if (!state->pp_buf) {
  152. kfree(state);
  153. return NULL;
  154. }
  155. state->pp_buf[0] = '\0';
  156. state->bdev = bdev;
  157. disk_name(hd, 0, state->name);
  158. snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
  159. if (isdigit(state->name[strlen(state->name)-1]))
  160. sprintf(state->name, "p");
  161. state->limit = disk_max_parts(hd);
  162. i = res = err = 0;
  163. while (!res && check_part[i]) {
  164. memset(&state->parts, 0, sizeof(state->parts));
  165. res = check_part[i++](state);
  166. if (res < 0) {
  167. /* We have hit an I/O error which we don't report now.
  168. * But record it, and let the others do their job.
  169. */
  170. err = res;
  171. res = 0;
  172. }
  173. }
  174. if (res > 0) {
  175. printk(KERN_INFO "%s", state->pp_buf);
  176. free_page((unsigned long)state->pp_buf);
  177. return state;
  178. }
  179. if (state->access_beyond_eod)
  180. err = -ENOSPC;
  181. if (err)
  182. /* The partition is unrecognized. So report I/O errors if there were any */
  183. res = err;
  184. if (!res)
  185. strlcat(state->pp_buf, " unknown partition table\n", PAGE_SIZE);
  186. else if (warn_no_part)
  187. strlcat(state->pp_buf, " unable to read partition table\n", PAGE_SIZE);
  188. printk(KERN_INFO "%s", state->pp_buf);
  189. free_page((unsigned long)state->pp_buf);
  190. kfree(state);
  191. return ERR_PTR(res);
  192. }
  193. static ssize_t part_partition_show(struct device *dev,
  194. struct device_attribute *attr, char *buf)
  195. {
  196. struct hd_struct *p = dev_to_part(dev);
  197. return sprintf(buf, "%d\n", p->partno);
  198. }
  199. static ssize_t part_start_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct hd_struct *p = dev_to_part(dev);
  203. return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
  204. }
  205. ssize_t part_size_show(struct device *dev,
  206. struct device_attribute *attr, char *buf)
  207. {
  208. struct hd_struct *p = dev_to_part(dev);
  209. return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
  210. }
  211. ssize_t part_ro_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct hd_struct *p = dev_to_part(dev);
  215. return sprintf(buf, "%d\n", p->policy ? 1 : 0);
  216. }
  217. ssize_t part_alignment_offset_show(struct device *dev,
  218. struct device_attribute *attr, char *buf)
  219. {
  220. struct hd_struct *p = dev_to_part(dev);
  221. return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
  222. }
  223. ssize_t part_discard_alignment_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct hd_struct *p = dev_to_part(dev);
  227. struct gendisk *disk = dev_to_disk(dev);
  228. return sprintf(buf, "%u\n",
  229. (unsigned long long)queue_limit_discard_alignment(
  230. &disk->queue->limits,
  231. p->start_sect));
  232. }
  233. ssize_t part_stat_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct hd_struct *p = dev_to_part(dev);
  237. int cpu;
  238. cpu = part_stat_lock();
  239. part_round_stats(cpu, p);
  240. part_stat_unlock();
  241. return sprintf(buf,
  242. "%8lu %8lu %8llu %8u "
  243. "%8lu %8lu %8llu %8u "
  244. "%8u %8u %8u"
  245. "\n",
  246. part_stat_read(p, ios[READ]),
  247. part_stat_read(p, merges[READ]),
  248. (unsigned long long)part_stat_read(p, sectors[READ]),
  249. jiffies_to_msecs(part_stat_read(p, ticks[READ])),
  250. part_stat_read(p, ios[WRITE]),
  251. part_stat_read(p, merges[WRITE]),
  252. (unsigned long long)part_stat_read(p, sectors[WRITE]),
  253. jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
  254. part_in_flight(p),
  255. jiffies_to_msecs(part_stat_read(p, io_ticks)),
  256. jiffies_to_msecs(part_stat_read(p, time_in_queue)));
  257. }
  258. ssize_t part_inflight_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct hd_struct *p = dev_to_part(dev);
  262. return sprintf(buf, "%8u %8u\n", atomic_read(&p->in_flight[0]),
  263. atomic_read(&p->in_flight[1]));
  264. }
  265. #ifdef CONFIG_FAIL_MAKE_REQUEST
  266. ssize_t part_fail_show(struct device *dev,
  267. struct device_attribute *attr, char *buf)
  268. {
  269. struct hd_struct *p = dev_to_part(dev);
  270. return sprintf(buf, "%d\n", p->make_it_fail);
  271. }
  272. ssize_t part_fail_store(struct device *dev,
  273. struct device_attribute *attr,
  274. const char *buf, size_t count)
  275. {
  276. struct hd_struct *p = dev_to_part(dev);
  277. int i;
  278. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  279. p->make_it_fail = (i == 0) ? 0 : 1;
  280. return count;
  281. }
  282. #endif
  283. static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
  284. static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
  285. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  286. static DEVICE_ATTR(ro, S_IRUGO, part_ro_show, NULL);
  287. static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
  288. static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
  289. NULL);
  290. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  291. static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
  292. #ifdef CONFIG_FAIL_MAKE_REQUEST
  293. static struct device_attribute dev_attr_fail =
  294. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  295. #endif
  296. static struct attribute *part_attrs[] = {
  297. &dev_attr_partition.attr,
  298. &dev_attr_start.attr,
  299. &dev_attr_size.attr,
  300. &dev_attr_ro.attr,
  301. &dev_attr_alignment_offset.attr,
  302. &dev_attr_discard_alignment.attr,
  303. &dev_attr_stat.attr,
  304. &dev_attr_inflight.attr,
  305. #ifdef CONFIG_FAIL_MAKE_REQUEST
  306. &dev_attr_fail.attr,
  307. #endif
  308. NULL
  309. };
  310. static struct attribute_group part_attr_group = {
  311. .attrs = part_attrs,
  312. };
  313. static const struct attribute_group *part_attr_groups[] = {
  314. &part_attr_group,
  315. #ifdef CONFIG_BLK_DEV_IO_TRACE
  316. &blk_trace_attr_group,
  317. #endif
  318. NULL
  319. };
  320. static void part_release(struct device *dev)
  321. {
  322. struct hd_struct *p = dev_to_part(dev);
  323. free_part_stats(p);
  324. free_part_info(p);
  325. kfree(p);
  326. }
  327. struct device_type part_type = {
  328. .name = "partition",
  329. .groups = part_attr_groups,
  330. .release = part_release,
  331. };
  332. static void delete_partition_rcu_cb(struct rcu_head *head)
  333. {
  334. struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
  335. part->start_sect = 0;
  336. part->nr_sects = 0;
  337. part_stat_set_all(part, 0);
  338. put_device(part_to_dev(part));
  339. }
  340. void __delete_partition(struct hd_struct *part)
  341. {
  342. call_rcu(&part->rcu_head, delete_partition_rcu_cb);
  343. }
  344. void delete_partition(struct gendisk *disk, int partno)
  345. {
  346. struct disk_part_tbl *ptbl = disk->part_tbl;
  347. struct hd_struct *part;
  348. if (partno >= ptbl->len)
  349. return;
  350. part = ptbl->part[partno];
  351. if (!part)
  352. return;
  353. blk_free_devt(part_devt(part));
  354. rcu_assign_pointer(ptbl->part[partno], NULL);
  355. rcu_assign_pointer(ptbl->last_lookup, NULL);
  356. kobject_put(part->holder_dir);
  357. device_del(part_to_dev(part));
  358. hd_struct_put(part);
  359. }
  360. static ssize_t whole_disk_show(struct device *dev,
  361. struct device_attribute *attr, char *buf)
  362. {
  363. return 0;
  364. }
  365. static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
  366. whole_disk_show, NULL);
  367. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  368. sector_t start, sector_t len, int flags,
  369. struct partition_meta_info *info)
  370. {
  371. struct hd_struct *p;
  372. dev_t devt = MKDEV(0, 0);
  373. struct device *ddev = disk_to_dev(disk);
  374. struct device *pdev;
  375. struct disk_part_tbl *ptbl;
  376. const char *dname;
  377. int err;
  378. err = disk_expand_part_tbl(disk, partno);
  379. if (err)
  380. return ERR_PTR(err);
  381. ptbl = disk->part_tbl;
  382. if (ptbl->part[partno])
  383. return ERR_PTR(-EBUSY);
  384. p = kzalloc(sizeof(*p), GFP_KERNEL);
  385. if (!p)
  386. return ERR_PTR(-EBUSY);
  387. if (!init_part_stats(p)) {
  388. err = -ENOMEM;
  389. goto out_free;
  390. }
  391. pdev = part_to_dev(p);
  392. p->start_sect = start;
  393. p->alignment_offset =
  394. queue_limit_alignment_offset(&disk->queue->limits, start);
  395. p->nr_sects = len;
  396. p->partno = partno;
  397. p->policy = get_disk_ro(disk);
  398. if (info) {
  399. struct partition_meta_info *pinfo = alloc_part_info(disk);
  400. if (!pinfo)
  401. goto out_free_stats;
  402. memcpy(pinfo, info, sizeof(*info));
  403. p->info = pinfo;
  404. }
  405. dname = dev_name(ddev);
  406. if (isdigit(dname[strlen(dname) - 1]))
  407. dev_set_name(pdev, "%sp%d", dname, partno);
  408. else
  409. dev_set_name(pdev, "%s%d", dname, partno);
  410. device_initialize(pdev);
  411. pdev->class = &block_class;
  412. pdev->type = &part_type;
  413. pdev->parent = ddev;
  414. err = blk_alloc_devt(p, &devt);
  415. if (err)
  416. goto out_free_info;
  417. pdev->devt = devt;
  418. /* delay uevent until 'holders' subdir is created */
  419. dev_set_uevent_suppress(pdev, 1);
  420. err = device_add(pdev);
  421. if (err)
  422. goto out_put;
  423. err = -ENOMEM;
  424. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  425. if (!p->holder_dir)
  426. goto out_del;
  427. dev_set_uevent_suppress(pdev, 0);
  428. if (flags & ADDPART_FLAG_WHOLEDISK) {
  429. err = device_create_file(pdev, &dev_attr_whole_disk);
  430. if (err)
  431. goto out_del;
  432. }
  433. /* everything is up and running, commence */
  434. rcu_assign_pointer(ptbl->part[partno], p);
  435. /* suppress uevent if the disk suppresses it */
  436. if (!dev_get_uevent_suppress(ddev))
  437. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  438. hd_ref_init(p);
  439. return p;
  440. out_free_info:
  441. free_part_info(p);
  442. out_free_stats:
  443. free_part_stats(p);
  444. out_free:
  445. kfree(p);
  446. return ERR_PTR(err);
  447. out_del:
  448. kobject_put(p->holder_dir);
  449. device_del(pdev);
  450. out_put:
  451. put_device(pdev);
  452. blk_free_devt(devt);
  453. return ERR_PTR(err);
  454. }
  455. static bool disk_unlock_native_capacity(struct gendisk *disk)
  456. {
  457. const struct block_device_operations *bdops = disk->fops;
  458. if (bdops->unlock_native_capacity &&
  459. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  460. printk(KERN_CONT "enabling native capacity\n");
  461. bdops->unlock_native_capacity(disk);
  462. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  463. return true;
  464. } else {
  465. printk(KERN_CONT "truncated\n");
  466. return false;
  467. }
  468. }
  469. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  470. {
  471. struct parsed_partitions *state = NULL;
  472. struct disk_part_iter piter;
  473. struct hd_struct *part;
  474. int p, highest, res;
  475. rescan:
  476. if (state && !IS_ERR(state)) {
  477. kfree(state);
  478. state = NULL;
  479. }
  480. if (bdev->bd_part_count)
  481. return -EBUSY;
  482. res = invalidate_partition(disk, 0);
  483. if (res)
  484. return res;
  485. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  486. while ((part = disk_part_iter_next(&piter)))
  487. delete_partition(disk, part->partno);
  488. disk_part_iter_exit(&piter);
  489. if (disk->fops->revalidate_disk)
  490. disk->fops->revalidate_disk(disk);
  491. check_disk_size_change(disk, bdev);
  492. bdev->bd_invalidated = 0;
  493. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  494. return 0;
  495. if (IS_ERR(state)) {
  496. /*
  497. * I/O error reading the partition table. If any
  498. * partition code tried to read beyond EOD, retry
  499. * after unlocking native capacity.
  500. */
  501. if (PTR_ERR(state) == -ENOSPC) {
  502. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  503. disk->disk_name);
  504. if (disk_unlock_native_capacity(disk))
  505. goto rescan;
  506. }
  507. return -EIO;
  508. }
  509. /*
  510. * If any partition code tried to read beyond EOD, try
  511. * unlocking native capacity even if partition table is
  512. * successfully read as we could be missing some partitions.
  513. */
  514. if (state->access_beyond_eod) {
  515. printk(KERN_WARNING
  516. "%s: partition table partially beyond EOD, ",
  517. disk->disk_name);
  518. if (disk_unlock_native_capacity(disk))
  519. goto rescan;
  520. }
  521. /* tell userspace that the media / partition table may have changed */
  522. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  523. /* Detect the highest partition number and preallocate
  524. * disk->part_tbl. This is an optimization and not strictly
  525. * necessary.
  526. */
  527. for (p = 1, highest = 0; p < state->limit; p++)
  528. if (state->parts[p].size)
  529. highest = p;
  530. disk_expand_part_tbl(disk, highest);
  531. /* add partitions */
  532. for (p = 1; p < state->limit; p++) {
  533. sector_t size, from;
  534. struct partition_meta_info *info = NULL;
  535. size = state->parts[p].size;
  536. if (!size)
  537. continue;
  538. from = state->parts[p].from;
  539. if (from >= get_capacity(disk)) {
  540. printk(KERN_WARNING
  541. "%s: p%d start %llu is beyond EOD, ",
  542. disk->disk_name, p, (unsigned long long) from);
  543. if (disk_unlock_native_capacity(disk))
  544. goto rescan;
  545. continue;
  546. }
  547. if (from + size > get_capacity(disk)) {
  548. printk(KERN_WARNING
  549. "%s: p%d size %llu extends beyond EOD, ",
  550. disk->disk_name, p, (unsigned long long) size);
  551. if (disk_unlock_native_capacity(disk)) {
  552. /* free state and restart */
  553. goto rescan;
  554. } else {
  555. /*
  556. * we can not ignore partitions of broken tables
  557. * created by for example camera firmware, but
  558. * we limit them to the end of the disk to avoid
  559. * creating invalid block devices
  560. */
  561. size = get_capacity(disk) - from;
  562. }
  563. }
  564. if (state->parts[p].has_info)
  565. info = &state->parts[p].info;
  566. part = add_partition(disk, p, from, size,
  567. state->parts[p].flags,
  568. &state->parts[p].info);
  569. if (IS_ERR(part)) {
  570. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  571. disk->disk_name, p, -PTR_ERR(part));
  572. continue;
  573. }
  574. #ifdef CONFIG_BLK_DEV_MD
  575. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  576. md_autodetect_dev(part_to_dev(part)->devt);
  577. #endif
  578. }
  579. kfree(state);
  580. return 0;
  581. }
  582. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  583. {
  584. struct address_space *mapping = bdev->bd_inode->i_mapping;
  585. struct page *page;
  586. page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
  587. NULL);
  588. if (!IS_ERR(page)) {
  589. if (PageError(page))
  590. goto fail;
  591. p->v = page;
  592. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
  593. fail:
  594. page_cache_release(page);
  595. }
  596. p->v = NULL;
  597. return NULL;
  598. }
  599. EXPORT_SYMBOL(read_dev_sector);