partition-generic.c 15 KB

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