partition-generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
  188. {
  189. struct hd_struct *part = dev_to_part(dev);
  190. add_uevent_var(env, "PARTN=%u", part->partno);
  191. if (part->info && part->info->volname[0])
  192. add_uevent_var(env, "PARTNAME=%s", part->info->volname);
  193. return 0;
  194. }
  195. struct device_type part_type = {
  196. .name = "partition",
  197. .groups = part_attr_groups,
  198. .release = part_release,
  199. .uevent = part_uevent,
  200. };
  201. static void delete_partition_rcu_cb(struct rcu_head *head)
  202. {
  203. struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
  204. part->start_sect = 0;
  205. part->nr_sects = 0;
  206. part_stat_set_all(part, 0);
  207. put_device(part_to_dev(part));
  208. }
  209. void __delete_partition(struct percpu_ref *ref)
  210. {
  211. struct hd_struct *part = container_of(ref, struct hd_struct, ref);
  212. call_rcu(&part->rcu_head, delete_partition_rcu_cb);
  213. }
  214. void delete_partition(struct gendisk *disk, int partno)
  215. {
  216. struct disk_part_tbl *ptbl = disk->part_tbl;
  217. struct hd_struct *part;
  218. if (partno >= ptbl->len)
  219. return;
  220. part = ptbl->part[partno];
  221. if (!part)
  222. return;
  223. rcu_assign_pointer(ptbl->part[partno], NULL);
  224. rcu_assign_pointer(ptbl->last_lookup, NULL);
  225. kobject_put(part->holder_dir);
  226. device_del(part_to_dev(part));
  227. hd_struct_kill(part);
  228. }
  229. static ssize_t whole_disk_show(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. return 0;
  233. }
  234. static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
  235. whole_disk_show, NULL);
  236. struct hd_struct *add_partition(struct gendisk *disk, int partno,
  237. sector_t start, sector_t len, int flags,
  238. struct partition_meta_info *info)
  239. {
  240. struct hd_struct *p;
  241. dev_t devt = MKDEV(0, 0);
  242. struct device *ddev = disk_to_dev(disk);
  243. struct device *pdev;
  244. struct disk_part_tbl *ptbl;
  245. const char *dname;
  246. int err;
  247. err = disk_expand_part_tbl(disk, partno);
  248. if (err)
  249. return ERR_PTR(err);
  250. ptbl = disk->part_tbl;
  251. if (ptbl->part[partno])
  252. return ERR_PTR(-EBUSY);
  253. p = kzalloc(sizeof(*p), GFP_KERNEL);
  254. if (!p)
  255. return ERR_PTR(-EBUSY);
  256. if (!init_part_stats(p)) {
  257. err = -ENOMEM;
  258. goto out_free;
  259. }
  260. seqcount_init(&p->nr_sects_seq);
  261. pdev = part_to_dev(p);
  262. p->start_sect = start;
  263. p->alignment_offset =
  264. queue_limit_alignment_offset(&disk->queue->limits, start);
  265. p->discard_alignment =
  266. queue_limit_discard_alignment(&disk->queue->limits, start);
  267. p->nr_sects = len;
  268. p->partno = partno;
  269. p->policy = get_disk_ro(disk);
  270. if (info) {
  271. struct partition_meta_info *pinfo = alloc_part_info(disk);
  272. if (!pinfo)
  273. goto out_free_stats;
  274. memcpy(pinfo, info, sizeof(*info));
  275. p->info = pinfo;
  276. }
  277. dname = dev_name(ddev);
  278. if (isdigit(dname[strlen(dname) - 1]))
  279. dev_set_name(pdev, "%sp%d", dname, partno);
  280. else
  281. dev_set_name(pdev, "%s%d", dname, partno);
  282. device_initialize(pdev);
  283. pdev->class = &block_class;
  284. pdev->type = &part_type;
  285. pdev->parent = ddev;
  286. err = blk_alloc_devt(p, &devt);
  287. if (err)
  288. goto out_free_info;
  289. pdev->devt = devt;
  290. /* delay uevent until 'holders' subdir is created */
  291. dev_set_uevent_suppress(pdev, 1);
  292. err = device_add(pdev);
  293. if (err)
  294. goto out_put;
  295. err = -ENOMEM;
  296. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  297. if (!p->holder_dir)
  298. goto out_del;
  299. dev_set_uevent_suppress(pdev, 0);
  300. if (flags & ADDPART_FLAG_WHOLEDISK) {
  301. err = device_create_file(pdev, &dev_attr_whole_disk);
  302. if (err)
  303. goto out_del;
  304. }
  305. err = hd_ref_init(p);
  306. if (err) {
  307. if (flags & ADDPART_FLAG_WHOLEDISK)
  308. goto out_remove_file;
  309. goto out_del;
  310. }
  311. /* everything is up and running, commence */
  312. rcu_assign_pointer(ptbl->part[partno], p);
  313. /* suppress uevent if the disk suppresses it */
  314. if (!dev_get_uevent_suppress(ddev))
  315. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  316. return p;
  317. out_free_info:
  318. free_part_info(p);
  319. out_free_stats:
  320. free_part_stats(p);
  321. out_free:
  322. kfree(p);
  323. return ERR_PTR(err);
  324. out_remove_file:
  325. device_remove_file(pdev, &dev_attr_whole_disk);
  326. out_del:
  327. kobject_put(p->holder_dir);
  328. device_del(pdev);
  329. out_put:
  330. put_device(pdev);
  331. blk_free_devt(devt);
  332. return ERR_PTR(err);
  333. }
  334. static bool disk_unlock_native_capacity(struct gendisk *disk)
  335. {
  336. const struct block_device_operations *bdops = disk->fops;
  337. if (bdops->unlock_native_capacity &&
  338. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  339. printk(KERN_CONT "enabling native capacity\n");
  340. bdops->unlock_native_capacity(disk);
  341. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  342. return true;
  343. } else {
  344. printk(KERN_CONT "truncated\n");
  345. return false;
  346. }
  347. }
  348. static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
  349. {
  350. struct disk_part_iter piter;
  351. struct hd_struct *part;
  352. int res;
  353. if (bdev->bd_part_count || bdev->bd_super)
  354. return -EBUSY;
  355. res = invalidate_partition(disk, 0);
  356. if (res)
  357. return res;
  358. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  359. while ((part = disk_part_iter_next(&piter)))
  360. delete_partition(disk, part->partno);
  361. disk_part_iter_exit(&piter);
  362. return 0;
  363. }
  364. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  365. {
  366. struct parsed_partitions *state = NULL;
  367. struct hd_struct *part;
  368. int p, highest, res;
  369. rescan:
  370. if (state && !IS_ERR(state)) {
  371. free_partitions(state);
  372. state = NULL;
  373. }
  374. res = drop_partitions(disk, bdev);
  375. if (res)
  376. return res;
  377. if (disk->fops->revalidate_disk)
  378. disk->fops->revalidate_disk(disk);
  379. check_disk_size_change(disk, bdev);
  380. bdev->bd_invalidated = 0;
  381. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  382. return 0;
  383. if (IS_ERR(state)) {
  384. /*
  385. * I/O error reading the partition table. If any
  386. * partition code tried to read beyond EOD, retry
  387. * after unlocking native capacity.
  388. */
  389. if (PTR_ERR(state) == -ENOSPC) {
  390. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  391. disk->disk_name);
  392. if (disk_unlock_native_capacity(disk))
  393. goto rescan;
  394. }
  395. return -EIO;
  396. }
  397. /*
  398. * If any partition code tried to read beyond EOD, try
  399. * unlocking native capacity even if partition table is
  400. * successfully read as we could be missing some partitions.
  401. */
  402. if (state->access_beyond_eod) {
  403. printk(KERN_WARNING
  404. "%s: partition table partially beyond EOD, ",
  405. disk->disk_name);
  406. if (disk_unlock_native_capacity(disk))
  407. goto rescan;
  408. }
  409. /* tell userspace that the media / partition table may have changed */
  410. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  411. /* Detect the highest partition number and preallocate
  412. * disk->part_tbl. This is an optimization and not strictly
  413. * necessary.
  414. */
  415. for (p = 1, highest = 0; p < state->limit; p++)
  416. if (state->parts[p].size)
  417. highest = p;
  418. disk_expand_part_tbl(disk, highest);
  419. /* add partitions */
  420. for (p = 1; p < state->limit; p++) {
  421. sector_t size, from;
  422. size = state->parts[p].size;
  423. if (!size)
  424. continue;
  425. from = state->parts[p].from;
  426. if (from >= get_capacity(disk)) {
  427. printk(KERN_WARNING
  428. "%s: p%d start %llu is beyond EOD, ",
  429. disk->disk_name, p, (unsigned long long) from);
  430. if (disk_unlock_native_capacity(disk))
  431. goto rescan;
  432. continue;
  433. }
  434. if (from + size > get_capacity(disk)) {
  435. printk(KERN_WARNING
  436. "%s: p%d size %llu extends beyond EOD, ",
  437. disk->disk_name, p, (unsigned long long) size);
  438. if (disk_unlock_native_capacity(disk)) {
  439. /* free state and restart */
  440. goto rescan;
  441. } else {
  442. /*
  443. * we can not ignore partitions of broken tables
  444. * created by for example camera firmware, but
  445. * we limit them to the end of the disk to avoid
  446. * creating invalid block devices
  447. */
  448. size = get_capacity(disk) - from;
  449. }
  450. }
  451. part = add_partition(disk, p, from, size,
  452. state->parts[p].flags,
  453. &state->parts[p].info);
  454. if (IS_ERR(part)) {
  455. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  456. disk->disk_name, p, -PTR_ERR(part));
  457. continue;
  458. }
  459. #ifdef CONFIG_BLK_DEV_MD
  460. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  461. md_autodetect_dev(part_to_dev(part)->devt);
  462. #endif
  463. }
  464. free_partitions(state);
  465. return 0;
  466. }
  467. int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
  468. {
  469. int res;
  470. if (!bdev->bd_invalidated)
  471. return 0;
  472. res = drop_partitions(disk, bdev);
  473. if (res)
  474. return res;
  475. set_capacity(disk, 0);
  476. check_disk_size_change(disk, bdev);
  477. bdev->bd_invalidated = 0;
  478. /* tell userspace that the media / partition table may have changed */
  479. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  480. return 0;
  481. }
  482. static struct page *read_pagecache_sector(struct block_device *bdev, sector_t n)
  483. {
  484. struct address_space *mapping = bdev->bd_inode->i_mapping;
  485. return read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_SHIFT-9)),
  486. NULL);
  487. }
  488. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  489. {
  490. struct page *page;
  491. /* don't populate page cache for dax capable devices */
  492. if (IS_DAX(bdev->bd_inode))
  493. page = read_dax_sector(bdev, n);
  494. else
  495. page = read_pagecache_sector(bdev, n);
  496. if (!IS_ERR(page)) {
  497. if (PageError(page))
  498. goto fail;
  499. p->v = page;
  500. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << 9);
  501. fail:
  502. put_page(page);
  503. }
  504. p->v = NULL;
  505. return NULL;
  506. }
  507. EXPORT_SYMBOL(read_dev_sector);