check.c 15 KB

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