partition-generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. err = -ENOMEM;
  274. goto out_free_stats;
  275. }
  276. memcpy(pinfo, info, sizeof(*info));
  277. p->info = pinfo;
  278. }
  279. dname = dev_name(ddev);
  280. if (isdigit(dname[strlen(dname) - 1]))
  281. dev_set_name(pdev, "%sp%d", dname, partno);
  282. else
  283. dev_set_name(pdev, "%s%d", dname, partno);
  284. device_initialize(pdev);
  285. pdev->class = &block_class;
  286. pdev->type = &part_type;
  287. pdev->parent = ddev;
  288. err = blk_alloc_devt(p, &devt);
  289. if (err)
  290. goto out_free_info;
  291. pdev->devt = devt;
  292. /* delay uevent until 'holders' subdir is created */
  293. dev_set_uevent_suppress(pdev, 1);
  294. err = device_add(pdev);
  295. if (err)
  296. goto out_put;
  297. err = -ENOMEM;
  298. p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
  299. if (!p->holder_dir)
  300. goto out_del;
  301. dev_set_uevent_suppress(pdev, 0);
  302. if (flags & ADDPART_FLAG_WHOLEDISK) {
  303. err = device_create_file(pdev, &dev_attr_whole_disk);
  304. if (err)
  305. goto out_del;
  306. }
  307. err = hd_ref_init(p);
  308. if (err) {
  309. if (flags & ADDPART_FLAG_WHOLEDISK)
  310. goto out_remove_file;
  311. goto out_del;
  312. }
  313. /* everything is up and running, commence */
  314. rcu_assign_pointer(ptbl->part[partno], p);
  315. /* suppress uevent if the disk suppresses it */
  316. if (!dev_get_uevent_suppress(ddev))
  317. kobject_uevent(&pdev->kobj, KOBJ_ADD);
  318. return p;
  319. out_free_info:
  320. free_part_info(p);
  321. out_free_stats:
  322. free_part_stats(p);
  323. out_free:
  324. kfree(p);
  325. return ERR_PTR(err);
  326. out_remove_file:
  327. device_remove_file(pdev, &dev_attr_whole_disk);
  328. out_del:
  329. kobject_put(p->holder_dir);
  330. device_del(pdev);
  331. out_put:
  332. put_device(pdev);
  333. blk_free_devt(devt);
  334. return ERR_PTR(err);
  335. }
  336. static bool disk_unlock_native_capacity(struct gendisk *disk)
  337. {
  338. const struct block_device_operations *bdops = disk->fops;
  339. if (bdops->unlock_native_capacity &&
  340. !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
  341. printk(KERN_CONT "enabling native capacity\n");
  342. bdops->unlock_native_capacity(disk);
  343. disk->flags |= GENHD_FL_NATIVE_CAPACITY;
  344. return true;
  345. } else {
  346. printk(KERN_CONT "truncated\n");
  347. return false;
  348. }
  349. }
  350. static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
  351. {
  352. struct disk_part_iter piter;
  353. struct hd_struct *part;
  354. int res;
  355. if (bdev->bd_part_count || bdev->bd_super)
  356. return -EBUSY;
  357. res = invalidate_partition(disk, 0);
  358. if (res)
  359. return res;
  360. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  361. while ((part = disk_part_iter_next(&piter)))
  362. delete_partition(disk, part->partno);
  363. disk_part_iter_exit(&piter);
  364. return 0;
  365. }
  366. int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
  367. {
  368. struct parsed_partitions *state = NULL;
  369. struct hd_struct *part;
  370. int p, highest, res;
  371. rescan:
  372. if (state && !IS_ERR(state)) {
  373. free_partitions(state);
  374. state = NULL;
  375. }
  376. res = drop_partitions(disk, bdev);
  377. if (res)
  378. return res;
  379. if (disk->fops->revalidate_disk)
  380. disk->fops->revalidate_disk(disk);
  381. check_disk_size_change(disk, bdev);
  382. bdev->bd_invalidated = 0;
  383. if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
  384. return 0;
  385. if (IS_ERR(state)) {
  386. /*
  387. * I/O error reading the partition table. If any
  388. * partition code tried to read beyond EOD, retry
  389. * after unlocking native capacity.
  390. */
  391. if (PTR_ERR(state) == -ENOSPC) {
  392. printk(KERN_WARNING "%s: partition table beyond EOD, ",
  393. disk->disk_name);
  394. if (disk_unlock_native_capacity(disk))
  395. goto rescan;
  396. }
  397. return -EIO;
  398. }
  399. /*
  400. * If any partition code tried to read beyond EOD, try
  401. * unlocking native capacity even if partition table is
  402. * successfully read as we could be missing some partitions.
  403. */
  404. if (state->access_beyond_eod) {
  405. printk(KERN_WARNING
  406. "%s: partition table partially beyond EOD, ",
  407. disk->disk_name);
  408. if (disk_unlock_native_capacity(disk))
  409. goto rescan;
  410. }
  411. /* tell userspace that the media / partition table may have changed */
  412. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  413. /* Detect the highest partition number and preallocate
  414. * disk->part_tbl. This is an optimization and not strictly
  415. * necessary.
  416. */
  417. for (p = 1, highest = 0; p < state->limit; p++)
  418. if (state->parts[p].size)
  419. highest = p;
  420. disk_expand_part_tbl(disk, highest);
  421. /* add partitions */
  422. for (p = 1; p < state->limit; p++) {
  423. sector_t size, from;
  424. size = state->parts[p].size;
  425. if (!size)
  426. continue;
  427. from = state->parts[p].from;
  428. if (from >= get_capacity(disk)) {
  429. printk(KERN_WARNING
  430. "%s: p%d start %llu is beyond EOD, ",
  431. disk->disk_name, p, (unsigned long long) from);
  432. if (disk_unlock_native_capacity(disk))
  433. goto rescan;
  434. continue;
  435. }
  436. if (from + size > get_capacity(disk)) {
  437. printk(KERN_WARNING
  438. "%s: p%d size %llu extends beyond EOD, ",
  439. disk->disk_name, p, (unsigned long long) size);
  440. if (disk_unlock_native_capacity(disk)) {
  441. /* free state and restart */
  442. goto rescan;
  443. } else {
  444. /*
  445. * we can not ignore partitions of broken tables
  446. * created by for example camera firmware, but
  447. * we limit them to the end of the disk to avoid
  448. * creating invalid block devices
  449. */
  450. size = get_capacity(disk) - from;
  451. }
  452. }
  453. part = add_partition(disk, p, from, size,
  454. state->parts[p].flags,
  455. &state->parts[p].info);
  456. if (IS_ERR(part)) {
  457. printk(KERN_ERR " %s: p%d could not be added: %ld\n",
  458. disk->disk_name, p, -PTR_ERR(part));
  459. continue;
  460. }
  461. #ifdef CONFIG_BLK_DEV_MD
  462. if (state->parts[p].flags & ADDPART_FLAG_RAID)
  463. md_autodetect_dev(part_to_dev(part)->devt);
  464. #endif
  465. }
  466. free_partitions(state);
  467. return 0;
  468. }
  469. int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
  470. {
  471. int res;
  472. if (!bdev->bd_invalidated)
  473. return 0;
  474. res = drop_partitions(disk, bdev);
  475. if (res)
  476. return res;
  477. set_capacity(disk, 0);
  478. check_disk_size_change(disk, bdev);
  479. bdev->bd_invalidated = 0;
  480. /* tell userspace that the media / partition table may have changed */
  481. kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
  482. return 0;
  483. }
  484. static struct page *read_pagecache_sector(struct block_device *bdev, sector_t n)
  485. {
  486. struct address_space *mapping = bdev->bd_inode->i_mapping;
  487. return read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_SHIFT-9)),
  488. NULL);
  489. }
  490. unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
  491. {
  492. struct page *page;
  493. /* don't populate page cache for dax capable devices */
  494. if (IS_DAX(bdev->bd_inode))
  495. page = read_dax_sector(bdev, n);
  496. else
  497. page = read_pagecache_sector(bdev, n);
  498. if (!IS_ERR(page)) {
  499. if (PageError(page))
  500. goto fail;
  501. p->v = page;
  502. return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << 9);
  503. fail:
  504. put_page(page);
  505. }
  506. p->v = NULL;
  507. return NULL;
  508. }
  509. EXPORT_SYMBOL(read_dev_sector);