partition-generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Code extracted from drivers/block/genhd.c
  3. * Copyright (C) 1991-1998 Linus Torvalds
  4. * Re-organised Feb 1998 Russell King
  5. *
  6. * We now have independent partition support from the
  7. * block drivers, which allows all the partition code to
  8. * be grouped in one location, and it to be mostly self
  9. * contained.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/slab.h>
  15. #include <linux/kmod.h>
  16. #include <linux/ctype.h>
  17. #include <linux/genhd.h>
  18. #include <linux/blktrace_api.h>
  19. #include "partitions/check.h"
  20. #ifdef CONFIG_BLK_DEV_MD
  21. extern void md_autodetect_dev(dev_t dev);
  22. #endif
  23. /*
  24. * disk_name() is used by partition check code and the genhd driver.
  25. * It formats the devicename of the indicated disk into
  26. * the supplied buffer (of size at least 32), and returns
  27. * a pointer to that same buffer (for convenience).
  28. */
  29. char *disk_name(struct gendisk *hd, int partno, char *buf)
  30. {
  31. if (!partno)
  32. snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
  33. else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
  34. snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
  35. else
  36. snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
  37. return buf;
  38. }
  39. const char *bdevname(struct block_device *bdev, char *buf)
  40. {
  41. return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
  42. }
  43. EXPORT_SYMBOL(bdevname);
  44. /*
  45. * There's very little reason to use this, you should really
  46. * have a struct block_device just about everywhere and use
  47. * bdevname() instead.
  48. */
  49. const char *__bdevname(dev_t dev, char *buffer)
  50. {
  51. scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
  52. MAJOR(dev), MINOR(dev));
  53. return buffer;
  54. }
  55. EXPORT_SYMBOL(__bdevname);
  56. static ssize_t part_partition_show(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct hd_struct *p = dev_to_part(dev);
  60. return sprintf(buf, "%d\n", p->partno);
  61. }
  62. static ssize_t part_start_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, "%llu\n",(unsigned long long)p->start_sect);
  67. }
  68. ssize_t part_size_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)part_nr_sects_read(p));
  73. }
  74. static ssize_t part_ro_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, "%d\n", p->policy ? 1 : 0);
  79. }
  80. static ssize_t part_alignment_offset_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, "%llu\n", (unsigned long long)p->alignment_offset);
  85. }
  86. static ssize_t part_discard_alignment_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, "%u\n", p->discard_alignment);
  91. }
  92. ssize_t part_stat_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct hd_struct *p = dev_to_part(dev);
  96. int cpu;
  97. cpu = part_stat_lock();
  98. part_round_stats(cpu, p);
  99. part_stat_unlock();
  100. return sprintf(buf,
  101. "%8lu %8lu %8llu %8u "
  102. "%8lu %8lu %8llu %8u "
  103. "%8u %8u %8u"
  104. "\n",
  105. part_stat_read(p, ios[READ]),
  106. part_stat_read(p, merges[READ]),
  107. (unsigned long long)part_stat_read(p, sectors[READ]),
  108. jiffies_to_msecs(part_stat_read(p, ticks[READ])),
  109. part_stat_read(p, ios[WRITE]),
  110. part_stat_read(p, merges[WRITE]),
  111. (unsigned long long)part_stat_read(p, sectors[WRITE]),
  112. jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
  113. part_in_flight(p),
  114. jiffies_to_msecs(part_stat_read(p, io_ticks)),
  115. jiffies_to_msecs(part_stat_read(p, time_in_queue)));
  116. }
  117. ssize_t part_inflight_show(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct hd_struct *p = dev_to_part(dev);
  121. return sprintf(buf, "%8u %8u\n", atomic_read(&p->in_flight[0]),
  122. atomic_read(&p->in_flight[1]));
  123. }
  124. #ifdef CONFIG_FAIL_MAKE_REQUEST
  125. ssize_t part_fail_show(struct device *dev,
  126. struct device_attribute *attr, char *buf)
  127. {
  128. struct hd_struct *p = dev_to_part(dev);
  129. return sprintf(buf, "%d\n", p->make_it_fail);
  130. }
  131. ssize_t part_fail_store(struct device *dev,
  132. struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. struct hd_struct *p = dev_to_part(dev);
  136. int i;
  137. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  138. p->make_it_fail = (i == 0) ? 0 : 1;
  139. return count;
  140. }
  141. #endif
  142. static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
  143. static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
  144. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  145. static DEVICE_ATTR(ro, S_IRUGO, part_ro_show, NULL);
  146. static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
  147. static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
  148. NULL);
  149. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  150. static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
  151. #ifdef CONFIG_FAIL_MAKE_REQUEST
  152. static struct device_attribute dev_attr_fail =
  153. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  154. #endif
  155. static struct attribute *part_attrs[] = {
  156. &dev_attr_partition.attr,
  157. &dev_attr_start.attr,
  158. &dev_attr_size.attr,
  159. &dev_attr_ro.attr,
  160. &dev_attr_alignment_offset.attr,
  161. &dev_attr_discard_alignment.attr,
  162. &dev_attr_stat.attr,
  163. &dev_attr_inflight.attr,
  164. #ifdef CONFIG_FAIL_MAKE_REQUEST
  165. &dev_attr_fail.attr,
  166. #endif
  167. NULL
  168. };
  169. static struct attribute_group part_attr_group = {
  170. .attrs = part_attrs,
  171. };
  172. static const struct attribute_group *part_attr_groups[] = {
  173. &part_attr_group,
  174. #ifdef CONFIG_BLK_DEV_IO_TRACE
  175. &blk_trace_attr_group,
  176. #endif
  177. NULL
  178. };
  179. static void part_release(struct device *dev)
  180. {
  181. struct hd_struct *p = dev_to_part(dev);
  182. blk_free_devt(dev->devt);
  183. free_part_stats(p);
  184. free_part_info(p);
  185. kfree(p);
  186. }
  187. struct device_type part_type = {
  188. .name = "partition",
  189. .groups = part_attr_groups,
  190. .release = part_release,
  191. };
  192. static void delete_partition_rcu_cb(struct rcu_head *head)
  193. {
  194. struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
  195. part->start_sect = 0;
  196. part->nr_sects = 0;
  197. part_stat_set_all(part, 0);
  198. put_device(part_to_dev(part));
  199. }
  200. void __delete_partition(struct hd_struct *part)
  201. {
  202. call_rcu(&part->rcu_head, delete_partition_rcu_cb);
  203. }
  204. void delete_partition(struct gendisk *disk, int partno)
  205. {
  206. struct disk_part_tbl *ptbl = disk->part_tbl;
  207. struct hd_struct *part;
  208. if (partno >= ptbl->len)
  209. return;
  210. part = ptbl->part[partno];
  211. if (!part)
  212. return;
  213. rcu_assign_pointer(ptbl->part[partno], NULL);
  214. rcu_assign_pointer(ptbl->last_lookup, NULL);
  215. kobject_put(part->holder_dir);
  216. device_del(part_to_dev(part));
  217. hd_struct_put(part);
  218. }
  219. static ssize_t whole_disk_show(struct device *dev,
  220. struct device_attribute *attr, char *buf)
  221. {
  222. return 0;
  223. }
  224. static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
  225. whole_disk_show, NULL);
  226. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  227. sector_t start, sector_t len, int flags,
  228. struct partition_meta_info *info)
  229. {
  230. struct hd_struct *p;
  231. dev_t devt = MKDEV(0, 0);
  232. struct device *ddev = disk_to_dev(disk);
  233. struct device *pdev;
  234. struct disk_part_tbl *ptbl;
  235. const char *dname;
  236. int err;
  237. err = disk_expand_part_tbl(disk, partno);
  238. if (err)
  239. return ERR_PTR(err);
  240. ptbl = disk->part_tbl;
  241. if (ptbl->part[partno])
  242. return ERR_PTR(-EBUSY);
  243. p = kzalloc(sizeof(*p), GFP_KERNEL);
  244. if (!p)
  245. return ERR_PTR(-EBUSY);
  246. if (!init_part_stats(p)) {
  247. err = -ENOMEM;
  248. goto out_free;
  249. }
  250. seqcount_init(&p->nr_sects_seq);
  251. pdev = part_to_dev(p);
  252. p->start_sect = start;
  253. p->alignment_offset =
  254. queue_limit_alignment_offset(&disk->queue->limits, start);
  255. p->discard_alignment =
  256. queue_limit_discard_alignment(&disk->queue->limits, start);
  257. p->nr_sects = len;
  258. p->partno = partno;
  259. p->policy = get_disk_ro(disk);
  260. if (info) {
  261. struct partition_meta_info *pinfo = alloc_part_info(disk);
  262. if (!pinfo)
  263. goto out_free_stats;
  264. memcpy(pinfo, info, sizeof(*info));
  265. p->info = pinfo;
  266. }
  267. dname = dev_name(ddev);
  268. if (isdigit(dname[strlen(dname) - 1]))
  269. dev_set_name(pdev, "%sp%d", dname, partno);
  270. else
  271. dev_set_name(pdev, "%s%d", dname, partno);
  272. device_initialize(pdev);
  273. pdev->class = &block_class;
  274. pdev->type = &part_type;
  275. pdev->parent = ddev;
  276. err = blk_alloc_devt(p, &devt);
  277. if (err)
  278. goto out_free_info;
  279. pdev->devt = devt;
  280. /* delay uevent until 'holders' subdir is created */
  281. dev_set_uevent_suppress(pdev, 1);
  282. err = device_add(pdev);
  283. if (err)
  284. goto out_put;
  285. err = -ENOMEM;
  286. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  287. if (!p->holder_dir)
  288. goto out_del;
  289. dev_set_uevent_suppress(pdev, 0);
  290. if (flags & ADDPART_FLAG_WHOLEDISK) {
  291. err = device_create_file(pdev, &dev_attr_whole_disk);
  292. if (err)
  293. goto out_del;
  294. }
  295. /* everything is up and running, commence */
  296. rcu_assign_pointer(ptbl->part[partno], p);
  297. /* suppress uevent if the disk suppresses it */
  298. if (!dev_get_uevent_suppress(ddev))
  299. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  300. hd_ref_init(p);
  301. return p;
  302. out_free_info:
  303. free_part_info(p);
  304. out_free_stats:
  305. free_part_stats(p);
  306. out_free:
  307. kfree(p);
  308. return ERR_PTR(err);
  309. out_del:
  310. kobject_put(p->holder_dir);
  311. device_del(pdev);
  312. out_put:
  313. put_device(pdev);
  314. blk_free_devt(devt);
  315. return ERR_PTR(err);
  316. }
  317. static bool disk_unlock_native_capacity(struct gendisk *disk)
  318. {
  319. const struct block_device_operations *bdops = disk->fops;
  320. if (bdops->unlock_native_capacity &&
  321. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  322. printk(KERN_CONT "enabling native capacity\n");
  323. bdops->unlock_native_capacity(disk);
  324. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  325. return true;
  326. } else {
  327. printk(KERN_CONT "truncated\n");
  328. return false;
  329. }
  330. }
  331. static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
  332. {
  333. struct disk_part_iter piter;
  334. struct hd_struct *part;
  335. int res;
  336. if (bdev->bd_part_count)
  337. return -EBUSY;
  338. res = invalidate_partition(disk, 0);
  339. if (res)
  340. return res;
  341. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  342. while ((part = disk_part_iter_next(&piter)))
  343. delete_partition(disk, part->partno);
  344. disk_part_iter_exit(&piter);
  345. return 0;
  346. }
  347. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  348. {
  349. struct parsed_partitions *state = NULL;
  350. struct hd_struct *part;
  351. int p, highest, res;
  352. rescan:
  353. if (state && !IS_ERR(state)) {
  354. free_partitions(state);
  355. state = NULL;
  356. }
  357. res = drop_partitions(disk, bdev);
  358. if (res)
  359. return res;
  360. if (disk->fops->revalidate_disk)
  361. disk->fops->revalidate_disk(disk);
  362. check_disk_size_change(disk, bdev);
  363. bdev->bd_invalidated = 0;
  364. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  365. return 0;
  366. if (IS_ERR(state)) {
  367. /*
  368. * I/O error reading the partition table. If any
  369. * partition code tried to read beyond EOD, retry
  370. * after unlocking native capacity.
  371. */
  372. if (PTR_ERR(state) == -ENOSPC) {
  373. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  374. disk->disk_name);
  375. if (disk_unlock_native_capacity(disk))
  376. goto rescan;
  377. }
  378. return -EIO;
  379. }
  380. /*
  381. * If any partition code tried to read beyond EOD, try
  382. * unlocking native capacity even if partition table is
  383. * successfully read as we could be missing some partitions.
  384. */
  385. if (state->access_beyond_eod) {
  386. printk(KERN_WARNING
  387. "%s: partition table partially beyond EOD, ",
  388. disk->disk_name);
  389. if (disk_unlock_native_capacity(disk))
  390. goto rescan;
  391. }
  392. /* tell userspace that the media / partition table may have changed */
  393. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  394. /* Detect the highest partition number and preallocate
  395. * disk->part_tbl. This is an optimization and not strictly
  396. * necessary.
  397. */
  398. for (p = 1, highest = 0; p < state->limit; p++)
  399. if (state->parts[p].size)
  400. highest = p;
  401. disk_expand_part_tbl(disk, highest);
  402. /* add partitions */
  403. for (p = 1; p < state->limit; p++) {
  404. sector_t size, from;
  405. struct partition_meta_info *info = NULL;
  406. size = state->parts[p].size;
  407. if (!size)
  408. continue;
  409. from = state->parts[p].from;
  410. if (from >= get_capacity(disk)) {
  411. printk(KERN_WARNING
  412. "%s: p%d start %llu is beyond EOD, ",
  413. disk->disk_name, p, (unsigned long long) from);
  414. if (disk_unlock_native_capacity(disk))
  415. goto rescan;
  416. continue;
  417. }
  418. if (from + size > get_capacity(disk)) {
  419. printk(KERN_WARNING
  420. "%s: p%d size %llu extends beyond EOD, ",
  421. disk->disk_name, p, (unsigned long long) size);
  422. if (disk_unlock_native_capacity(disk)) {
  423. /* free state and restart */
  424. goto rescan;
  425. } else {
  426. /*
  427. * we can not ignore partitions of broken tables
  428. * created by for example camera firmware, but
  429. * we limit them to the end of the disk to avoid
  430. * creating invalid block devices
  431. */
  432. size = get_capacity(disk) - from;
  433. }
  434. }
  435. if (state->parts[p].has_info)
  436. info = &state->parts[p].info;
  437. part = add_partition(disk, p, from, size,
  438. state->parts[p].flags,
  439. &state->parts[p].info);
  440. if (IS_ERR(part)) {
  441. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  442. disk->disk_name, p, -PTR_ERR(part));
  443. continue;
  444. }
  445. #ifdef CONFIG_BLK_DEV_MD
  446. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  447. md_autodetect_dev(part_to_dev(part)->devt);
  448. #endif
  449. }
  450. free_partitions(state);
  451. return 0;
  452. }
  453. int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
  454. {
  455. int res;
  456. if (!bdev->bd_invalidated)
  457. return 0;
  458. res = drop_partitions(disk, bdev);
  459. if (res)
  460. return res;
  461. set_capacity(disk, 0);
  462. check_disk_size_change(disk, bdev);
  463. bdev->bd_invalidated = 0;
  464. /* tell userspace that the media / partition table may have changed */
  465. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  466. return 0;
  467. }
  468. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  469. {
  470. struct address_space *mapping = bdev->bd_inode->i_mapping;
  471. struct page *page;
  472. page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
  473. NULL);
  474. if (!IS_ERR(page)) {
  475. if (PageError(page))
  476. goto fail;
  477. p->v = page;
  478. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
  479. fail:
  480. page_cache_release(page);
  481. }
  482. p->v = NULL;
  483. return NULL;
  484. }
  485. EXPORT_SYMBOL(read_dev_sector);