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 "check.h"
  22. #include "acorn.h"
  23. #include "amiga.h"
  24. #include "atari.h"
  25. #include "ldm.h"
  26. #include "mac.h"
  27. #include "msdos.h"
  28. #include "osf.h"
  29. #include "sgi.h"
  30. #include "sun.h"
  31. #include "ibm.h"
  32. #include "ultrix.h"
  33. #include "efi.h"
  34. #include "karma.h"
  35. #include "sysv68.h"
  36. #ifdef CONFIG_BLK_DEV_MD
  37. extern void md_autodetect_dev(dev_t dev);
  38. #endif
  39. int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
  40. static int (*check_part[])(struct parsed_partitions *, struct block_device *) = {
  41. /*
  42. * Probe partition formats with tables at disk address 0
  43. * that also have an ADFS boot block at 0xdc0.
  44. */
  45. #ifdef CONFIG_ACORN_PARTITION_ICS
  46. adfspart_check_ICS,
  47. #endif
  48. #ifdef CONFIG_ACORN_PARTITION_POWERTEC
  49. adfspart_check_POWERTEC,
  50. #endif
  51. #ifdef CONFIG_ACORN_PARTITION_EESOX
  52. adfspart_check_EESOX,
  53. #endif
  54. /*
  55. * Now move on to formats that only have partition info at
  56. * disk address 0xdc0. Since these may also have stale
  57. * PC/BIOS partition tables, they need to come before
  58. * the msdos entry.
  59. */
  60. #ifdef CONFIG_ACORN_PARTITION_CUMANA
  61. adfspart_check_CUMANA,
  62. #endif
  63. #ifdef CONFIG_ACORN_PARTITION_ADFS
  64. adfspart_check_ADFS,
  65. #endif
  66. #ifdef CONFIG_EFI_PARTITION
  67. efi_partition, /* this must come before msdos */
  68. #endif
  69. #ifdef CONFIG_SGI_PARTITION
  70. sgi_partition,
  71. #endif
  72. #ifdef CONFIG_LDM_PARTITION
  73. ldm_partition, /* this must come before msdos */
  74. #endif
  75. #ifdef CONFIG_MSDOS_PARTITION
  76. msdos_partition,
  77. #endif
  78. #ifdef CONFIG_OSF_PARTITION
  79. osf_partition,
  80. #endif
  81. #ifdef CONFIG_SUN_PARTITION
  82. sun_partition,
  83. #endif
  84. #ifdef CONFIG_AMIGA_PARTITION
  85. amiga_partition,
  86. #endif
  87. #ifdef CONFIG_ATARI_PARTITION
  88. atari_partition,
  89. #endif
  90. #ifdef CONFIG_MAC_PARTITION
  91. mac_partition,
  92. #endif
  93. #ifdef CONFIG_ULTRIX_PARTITION
  94. ultrix_partition,
  95. #endif
  96. #ifdef CONFIG_IBM_PARTITION
  97. ibm_partition,
  98. #endif
  99. #ifdef CONFIG_KARMA_PARTITION
  100. karma_partition,
  101. #endif
  102. #ifdef CONFIG_SYSV68_PARTITION
  103. sysv68_partition,
  104. #endif
  105. NULL
  106. };
  107. /*
  108. * disk_name() is used by partition check code and the genhd driver.
  109. * It formats the devicename of the indicated disk into
  110. * the supplied buffer (of size at least 32), and returns
  111. * a pointer to that same buffer (for convenience).
  112. */
  113. char *disk_name(struct gendisk *hd, int partno, char *buf)
  114. {
  115. if (!partno)
  116. snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
  117. else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
  118. snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
  119. else
  120. snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
  121. return buf;
  122. }
  123. const char *bdevname(struct block_device *bdev, char *buf)
  124. {
  125. return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
  126. }
  127. EXPORT_SYMBOL(bdevname);
  128. /*
  129. * There's very little reason to use this, you should really
  130. * have a struct block_device just about everywhere and use
  131. * bdevname() instead.
  132. */
  133. const char *__bdevname(dev_t dev, char *buffer)
  134. {
  135. scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
  136. MAJOR(dev), MINOR(dev));
  137. return buffer;
  138. }
  139. EXPORT_SYMBOL(__bdevname);
  140. static struct parsed_partitions *
  141. check_partition(struct gendisk *hd, struct block_device *bdev)
  142. {
  143. struct parsed_partitions *state;
  144. int i, res, err;
  145. state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
  146. if (!state)
  147. return NULL;
  148. disk_name(hd, 0, state->name);
  149. printk(KERN_INFO " %s:", state->name);
  150. if (isdigit(state->name[strlen(state->name)-1]))
  151. sprintf(state->name, "p");
  152. state->limit = disk_max_parts(hd);
  153. i = res = err = 0;
  154. while (!res && check_part[i]) {
  155. memset(&state->parts, 0, sizeof(state->parts));
  156. res = check_part[i++](state, bdev);
  157. if (res < 0) {
  158. /* We have hit an I/O error which we don't report now.
  159. * But record it, and let the others do their job.
  160. */
  161. err = res;
  162. res = 0;
  163. }
  164. }
  165. if (res > 0)
  166. return state;
  167. if (err)
  168. /* The partition is unrecognized. So report I/O errors if there were any */
  169. res = err;
  170. if (!res)
  171. printk(" unknown partition table\n");
  172. else if (warn_no_part)
  173. printk(" unable to read partition table\n");
  174. kfree(state);
  175. return ERR_PTR(res);
  176. }
  177. static ssize_t part_partition_show(struct device *dev,
  178. struct device_attribute *attr, char *buf)
  179. {
  180. struct hd_struct *p = dev_to_part(dev);
  181. return sprintf(buf, "%d\n", p->partno);
  182. }
  183. static ssize_t part_start_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct hd_struct *p = dev_to_part(dev);
  187. return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
  188. }
  189. ssize_t part_size_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct hd_struct *p = dev_to_part(dev);
  193. return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
  194. }
  195. ssize_t part_stat_show(struct device *dev,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct hd_struct *p = dev_to_part(dev);
  199. int cpu;
  200. cpu = part_stat_lock();
  201. part_round_stats(cpu, p);
  202. part_stat_unlock();
  203. return sprintf(buf,
  204. "%8lu %8lu %8llu %8u "
  205. "%8lu %8lu %8llu %8u "
  206. "%8u %8u %8u"
  207. "\n",
  208. part_stat_read(p, ios[READ]),
  209. part_stat_read(p, merges[READ]),
  210. (unsigned long long)part_stat_read(p, sectors[READ]),
  211. jiffies_to_msecs(part_stat_read(p, ticks[READ])),
  212. part_stat_read(p, ios[WRITE]),
  213. part_stat_read(p, merges[WRITE]),
  214. (unsigned long long)part_stat_read(p, sectors[WRITE]),
  215. jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
  216. p->in_flight,
  217. jiffies_to_msecs(part_stat_read(p, io_ticks)),
  218. jiffies_to_msecs(part_stat_read(p, time_in_queue)));
  219. }
  220. #ifdef CONFIG_FAIL_MAKE_REQUEST
  221. ssize_t part_fail_show(struct device *dev,
  222. struct device_attribute *attr, char *buf)
  223. {
  224. struct hd_struct *p = dev_to_part(dev);
  225. return sprintf(buf, "%d\n", p->make_it_fail);
  226. }
  227. ssize_t part_fail_store(struct device *dev,
  228. struct device_attribute *attr,
  229. const char *buf, size_t count)
  230. {
  231. struct hd_struct *p = dev_to_part(dev);
  232. int i;
  233. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  234. p->make_it_fail = (i == 0) ? 0 : 1;
  235. return count;
  236. }
  237. #endif
  238. static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
  239. static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
  240. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  241. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  242. #ifdef CONFIG_FAIL_MAKE_REQUEST
  243. static struct device_attribute dev_attr_fail =
  244. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  245. #endif
  246. static struct attribute *part_attrs[] = {
  247. &dev_attr_partition.attr,
  248. &dev_attr_start.attr,
  249. &dev_attr_size.attr,
  250. &dev_attr_stat.attr,
  251. #ifdef CONFIG_FAIL_MAKE_REQUEST
  252. &dev_attr_fail.attr,
  253. #endif
  254. NULL
  255. };
  256. static struct attribute_group part_attr_group = {
  257. .attrs = part_attrs,
  258. };
  259. static struct attribute_group *part_attr_groups[] = {
  260. &part_attr_group,
  261. NULL
  262. };
  263. static void part_release(struct device *dev)
  264. {
  265. struct hd_struct *p = dev_to_part(dev);
  266. free_part_stats(p);
  267. kfree(p);
  268. }
  269. struct device_type part_type = {
  270. .name = "partition",
  271. .groups = part_attr_groups,
  272. .release = part_release,
  273. };
  274. static void delete_partition_rcu_cb(struct rcu_head *head)
  275. {
  276. struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
  277. part->start_sect = 0;
  278. part->nr_sects = 0;
  279. part_stat_set_all(part, 0);
  280. put_device(part_to_dev(part));
  281. }
  282. void delete_partition(struct gendisk *disk, int partno)
  283. {
  284. struct disk_part_tbl *ptbl = disk->part_tbl;
  285. struct hd_struct *part;
  286. if (partno >= ptbl->len)
  287. return;
  288. part = ptbl->part[partno];
  289. if (!part)
  290. return;
  291. blk_free_devt(part_devt(part));
  292. rcu_assign_pointer(ptbl->part[partno], NULL);
  293. kobject_put(part->holder_dir);
  294. device_del(part_to_dev(part));
  295. call_rcu(&part->rcu_head, delete_partition_rcu_cb);
  296. }
  297. static ssize_t whole_disk_show(struct device *dev,
  298. struct device_attribute *attr, char *buf)
  299. {
  300. return 0;
  301. }
  302. static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
  303. whole_disk_show, NULL);
  304. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  305. sector_t start, sector_t len, int flags)
  306. {
  307. struct hd_struct *p;
  308. dev_t devt = MKDEV(0, 0);
  309. struct device *ddev = disk_to_dev(disk);
  310. struct device *pdev;
  311. struct disk_part_tbl *ptbl;
  312. const char *dname;
  313. int err;
  314. err = disk_expand_part_tbl(disk, partno);
  315. if (err)
  316. return ERR_PTR(err);
  317. ptbl = disk->part_tbl;
  318. if (ptbl->part[partno])
  319. return ERR_PTR(-EBUSY);
  320. p = kzalloc(sizeof(*p), GFP_KERNEL);
  321. if (!p)
  322. return ERR_PTR(-EBUSY);
  323. if (!init_part_stats(p)) {
  324. err = -ENOMEM;
  325. goto out_free;
  326. }
  327. pdev = part_to_dev(p);
  328. p->start_sect = start;
  329. p->nr_sects = len;
  330. p->partno = partno;
  331. p->policy = get_disk_ro(disk);
  332. dname = dev_name(ddev);
  333. if (isdigit(dname[strlen(dname) - 1]))
  334. snprintf(pdev->bus_id, BUS_ID_SIZE, "%sp%d", dname, partno);
  335. else
  336. snprintf(pdev->bus_id, BUS_ID_SIZE, "%s%d", dname, partno);
  337. device_initialize(pdev);
  338. pdev->class = &block_class;
  339. pdev->type = &part_type;
  340. pdev->parent = ddev;
  341. err = blk_alloc_devt(p, &devt);
  342. if (err)
  343. goto out_free_stats;
  344. pdev->devt = devt;
  345. /* delay uevent until 'holders' subdir is created */
  346. pdev->uevent_suppress = 1;
  347. err = device_add(pdev);
  348. if (err)
  349. goto out_put;
  350. err = -ENOMEM;
  351. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  352. if (!p->holder_dir)
  353. goto out_del;
  354. pdev->uevent_suppress = 0;
  355. if (flags & ADDPART_FLAG_WHOLEDISK) {
  356. err = device_create_file(pdev, &dev_attr_whole_disk);
  357. if (err)
  358. goto out_del;
  359. }
  360. /* everything is up and running, commence */
  361. INIT_RCU_HEAD(&p->rcu_head);
  362. rcu_assign_pointer(ptbl->part[partno], p);
  363. /* suppress uevent if the disk supresses it */
  364. if (!ddev->uevent_suppress)
  365. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  366. return p;
  367. out_free_stats:
  368. free_part_stats(p);
  369. out_free:
  370. kfree(p);
  371. return ERR_PTR(err);
  372. out_del:
  373. kobject_put(p->holder_dir);
  374. device_del(pdev);
  375. out_put:
  376. put_device(pdev);
  377. blk_free_devt(devt);
  378. return ERR_PTR(err);
  379. }
  380. /* Not exported, helper to add_disk(). */
  381. void register_disk(struct gendisk *disk)
  382. {
  383. struct device *ddev = disk_to_dev(disk);
  384. struct block_device *bdev;
  385. struct disk_part_iter piter;
  386. struct hd_struct *part;
  387. char *s;
  388. int err;
  389. ddev->parent = disk->driverfs_dev;
  390. strlcpy(ddev->bus_id, disk->disk_name, BUS_ID_SIZE);
  391. /* ewww... some of these buggers have / in the name... */
  392. s = strchr(ddev->bus_id, '/');
  393. if (s)
  394. *s = '!';
  395. /* delay uevents, until we scanned partition table */
  396. ddev->uevent_suppress = 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. ddev->uevent_suppress = 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. }