partition-generic.c 14 KB

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