genhd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * gendisk handling
  3. */
  4. #include <linux/module.h>
  5. #include <linux/fs.h>
  6. #include <linux/genhd.h>
  7. #include <linux/kdev_t.h>
  8. #include <linux/kernel.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/init.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. #include <linux/kmod.h>
  15. #include <linux/kobj_map.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/mutex.h>
  18. #include "blk.h"
  19. static DEFINE_MUTEX(block_class_lock);
  20. #ifndef CONFIG_SYSFS_DEPRECATED
  21. struct kobject *block_depr;
  22. #endif
  23. static struct device_type disk_type;
  24. /*
  25. * Can be deleted altogether. Later.
  26. *
  27. */
  28. static struct blk_major_name {
  29. struct blk_major_name *next;
  30. int major;
  31. char name[16];
  32. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  33. /* index in the above - for now: assume no multimajor ranges */
  34. static inline int major_to_index(int major)
  35. {
  36. return major % BLKDEV_MAJOR_HASH_SIZE;
  37. }
  38. #ifdef CONFIG_PROC_FS
  39. void blkdev_show(struct seq_file *f, off_t offset)
  40. {
  41. struct blk_major_name *dp;
  42. if (offset < BLKDEV_MAJOR_HASH_SIZE) {
  43. mutex_lock(&block_class_lock);
  44. for (dp = major_names[offset]; dp; dp = dp->next)
  45. seq_printf(f, "%3d %s\n", dp->major, dp->name);
  46. mutex_unlock(&block_class_lock);
  47. }
  48. }
  49. #endif /* CONFIG_PROC_FS */
  50. int register_blkdev(unsigned int major, const char *name)
  51. {
  52. struct blk_major_name **n, *p;
  53. int index, ret = 0;
  54. mutex_lock(&block_class_lock);
  55. /* temporary */
  56. if (major == 0) {
  57. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  58. if (major_names[index] == NULL)
  59. break;
  60. }
  61. if (index == 0) {
  62. printk("register_blkdev: failed to get major for %s\n",
  63. name);
  64. ret = -EBUSY;
  65. goto out;
  66. }
  67. major = index;
  68. ret = major;
  69. }
  70. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  71. if (p == NULL) {
  72. ret = -ENOMEM;
  73. goto out;
  74. }
  75. p->major = major;
  76. strlcpy(p->name, name, sizeof(p->name));
  77. p->next = NULL;
  78. index = major_to_index(major);
  79. for (n = &major_names[index]; *n; n = &(*n)->next) {
  80. if ((*n)->major == major)
  81. break;
  82. }
  83. if (!*n)
  84. *n = p;
  85. else
  86. ret = -EBUSY;
  87. if (ret < 0) {
  88. printk("register_blkdev: cannot get major %d for %s\n",
  89. major, name);
  90. kfree(p);
  91. }
  92. out:
  93. mutex_unlock(&block_class_lock);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL(register_blkdev);
  97. void unregister_blkdev(unsigned int major, const char *name)
  98. {
  99. struct blk_major_name **n;
  100. struct blk_major_name *p = NULL;
  101. int index = major_to_index(major);
  102. mutex_lock(&block_class_lock);
  103. for (n = &major_names[index]; *n; n = &(*n)->next)
  104. if ((*n)->major == major)
  105. break;
  106. if (!*n || strcmp((*n)->name, name)) {
  107. WARN_ON(1);
  108. } else {
  109. p = *n;
  110. *n = p->next;
  111. }
  112. mutex_unlock(&block_class_lock);
  113. kfree(p);
  114. }
  115. EXPORT_SYMBOL(unregister_blkdev);
  116. static struct kobj_map *bdev_map;
  117. /*
  118. * Register device numbers dev..(dev+range-1)
  119. * range must be nonzero
  120. * The hash chain is sorted on range, so that subranges can override.
  121. */
  122. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  123. struct kobject *(*probe)(dev_t, int *, void *),
  124. int (*lock)(dev_t, void *), void *data)
  125. {
  126. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  127. }
  128. EXPORT_SYMBOL(blk_register_region);
  129. void blk_unregister_region(dev_t devt, unsigned long range)
  130. {
  131. kobj_unmap(bdev_map, devt, range);
  132. }
  133. EXPORT_SYMBOL(blk_unregister_region);
  134. static struct kobject *exact_match(dev_t devt, int *part, void *data)
  135. {
  136. struct gendisk *p = data;
  137. return &p->dev.kobj;
  138. }
  139. static int exact_lock(dev_t devt, void *data)
  140. {
  141. struct gendisk *p = data;
  142. if (!get_disk(p))
  143. return -1;
  144. return 0;
  145. }
  146. /**
  147. * add_disk - add partitioning information to kernel list
  148. * @disk: per-device partitioning information
  149. *
  150. * This function registers the partitioning information in @disk
  151. * with the kernel.
  152. */
  153. void add_disk(struct gendisk *disk)
  154. {
  155. struct backing_dev_info *bdi;
  156. int retval;
  157. disk->flags |= GENHD_FL_UP;
  158. blk_register_region(MKDEV(disk->major, disk->first_minor),
  159. disk->minors, NULL, exact_match, exact_lock, disk);
  160. register_disk(disk);
  161. blk_register_queue(disk);
  162. bdi = &disk->queue->backing_dev_info;
  163. bdi_register_dev(bdi, MKDEV(disk->major, disk->first_minor));
  164. retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
  165. WARN_ON(retval);
  166. }
  167. EXPORT_SYMBOL(add_disk);
  168. EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
  169. void unlink_gendisk(struct gendisk *disk)
  170. {
  171. sysfs_remove_link(&disk->dev.kobj, "bdi");
  172. bdi_unregister(&disk->queue->backing_dev_info);
  173. blk_unregister_queue(disk);
  174. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  175. disk->minors);
  176. }
  177. /**
  178. * get_gendisk - get partitioning information for a given device
  179. * @devt: device to get partitioning information for
  180. * @part: returned partition index
  181. *
  182. * This function gets the structure containing partitioning
  183. * information for the given device @devt.
  184. */
  185. struct gendisk *get_gendisk(dev_t devt, int *part)
  186. {
  187. struct kobject *kobj = kobj_lookup(bdev_map, devt, part);
  188. struct device *dev = kobj_to_dev(kobj);
  189. return kobj ? dev_to_disk(dev) : NULL;
  190. }
  191. /*
  192. * print a full list of all partitions - intended for places where the root
  193. * filesystem can't be mounted and thus to give the victim some idea of what
  194. * went wrong
  195. */
  196. void __init printk_all_partitions(void)
  197. {
  198. struct class_dev_iter iter;
  199. struct device *dev;
  200. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  201. while ((dev = class_dev_iter_next(&iter))) {
  202. struct gendisk *disk = dev_to_disk(dev);
  203. char buf[BDEVNAME_SIZE];
  204. int n;
  205. /*
  206. * Don't show empty devices or things that have been
  207. * surpressed
  208. */
  209. if (get_capacity(disk) == 0 ||
  210. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  211. continue;
  212. /*
  213. * Note, unlike /proc/partitions, I am showing the
  214. * numbers in hex - the same format as the root=
  215. * option takes.
  216. */
  217. printk("%02x%02x %10llu %s",
  218. disk->major, disk->first_minor,
  219. (unsigned long long)get_capacity(disk) >> 1,
  220. disk_name(disk, 0, buf));
  221. if (disk->driverfs_dev != NULL &&
  222. disk->driverfs_dev->driver != NULL)
  223. printk(" driver: %s\n",
  224. disk->driverfs_dev->driver->name);
  225. else
  226. printk(" (driver?)\n");
  227. /* now show the partitions */
  228. for (n = 0; n < disk->minors - 1; ++n) {
  229. if (disk->part[n] == NULL)
  230. continue;
  231. if (disk->part[n]->nr_sects == 0)
  232. continue;
  233. printk(" %02x%02x %10llu %s\n",
  234. disk->major, n + 1 + disk->first_minor,
  235. (unsigned long long)disk->part[n]->nr_sects >> 1,
  236. disk_name(disk, n + 1, buf));
  237. }
  238. }
  239. class_dev_iter_exit(&iter);
  240. }
  241. #ifdef CONFIG_PROC_FS
  242. /* iterator */
  243. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  244. {
  245. loff_t skip = *pos;
  246. struct class_dev_iter *iter;
  247. struct device *dev;
  248. iter = kmalloc(GFP_KERNEL, sizeof(*iter));
  249. if (!iter)
  250. return ERR_PTR(-ENOMEM);
  251. seqf->private = iter;
  252. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  253. do {
  254. dev = class_dev_iter_next(iter);
  255. if (!dev)
  256. return NULL;
  257. } while (skip--);
  258. return dev_to_disk(dev);
  259. }
  260. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  261. {
  262. struct device *dev;
  263. (*pos)++;
  264. dev = class_dev_iter_next(seqf->private);
  265. if (dev)
  266. return dev_to_disk(dev);
  267. return NULL;
  268. }
  269. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  270. {
  271. struct class_dev_iter *iter = seqf->private;
  272. /* stop is called even after start failed :-( */
  273. if (iter) {
  274. class_dev_iter_exit(iter);
  275. kfree(iter);
  276. }
  277. }
  278. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  279. {
  280. static void *p;
  281. p = disk_seqf_start(seqf, pos);
  282. if (!IS_ERR(p) && p)
  283. seq_puts(seqf, "major minor #blocks name\n\n");
  284. return p;
  285. }
  286. static int show_partition(struct seq_file *part, void *v)
  287. {
  288. struct gendisk *sgp = v;
  289. int n;
  290. char buf[BDEVNAME_SIZE];
  291. /*
  292. * Print header if start told us to do. This is to preserve
  293. * the original behavior of not printing header if no
  294. * partition exists. This hackery will be removed later with
  295. * class iteration clean up.
  296. */
  297. if (part->private) {
  298. seq_puts(part, "major minor #blocks name\n\n");
  299. part->private = NULL;
  300. }
  301. /* Don't show non-partitionable removeable devices or empty devices */
  302. if (!get_capacity(sgp) ||
  303. (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
  304. return 0;
  305. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  306. return 0;
  307. /* show the full disk and all non-0 size partitions of it */
  308. seq_printf(part, "%4d %4d %10llu %s\n",
  309. sgp->major, sgp->first_minor,
  310. (unsigned long long)get_capacity(sgp) >> 1,
  311. disk_name(sgp, 0, buf));
  312. for (n = 0; n < sgp->minors - 1; n++) {
  313. if (!sgp->part[n])
  314. continue;
  315. if (sgp->part[n]->nr_sects == 0)
  316. continue;
  317. seq_printf(part, "%4d %4d %10llu %s\n",
  318. sgp->major, n + 1 + sgp->first_minor,
  319. (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
  320. disk_name(sgp, n + 1, buf));
  321. }
  322. return 0;
  323. }
  324. const struct seq_operations partitions_op = {
  325. .start = show_partition_start,
  326. .next = disk_seqf_next,
  327. .stop = disk_seqf_stop,
  328. .show = show_partition
  329. };
  330. #endif
  331. static struct kobject *base_probe(dev_t devt, int *part, void *data)
  332. {
  333. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  334. /* Make old-style 2.4 aliases work */
  335. request_module("block-major-%d", MAJOR(devt));
  336. return NULL;
  337. }
  338. static int __init genhd_device_init(void)
  339. {
  340. int error;
  341. block_class.dev_kobj = sysfs_dev_block_kobj;
  342. error = class_register(&block_class);
  343. if (unlikely(error))
  344. return error;
  345. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  346. blk_dev_init();
  347. #ifndef CONFIG_SYSFS_DEPRECATED
  348. /* create top-level block dir */
  349. block_depr = kobject_create_and_add("block", NULL);
  350. #endif
  351. return 0;
  352. }
  353. subsys_initcall(genhd_device_init);
  354. static ssize_t disk_range_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. struct gendisk *disk = dev_to_disk(dev);
  358. return sprintf(buf, "%d\n", disk->minors);
  359. }
  360. static ssize_t disk_removable_show(struct device *dev,
  361. struct device_attribute *attr, char *buf)
  362. {
  363. struct gendisk *disk = dev_to_disk(dev);
  364. return sprintf(buf, "%d\n",
  365. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  366. }
  367. static ssize_t disk_ro_show(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. struct gendisk *disk = dev_to_disk(dev);
  371. return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
  372. }
  373. static ssize_t disk_size_show(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. struct gendisk *disk = dev_to_disk(dev);
  377. return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
  378. }
  379. static ssize_t disk_capability_show(struct device *dev,
  380. struct device_attribute *attr, char *buf)
  381. {
  382. struct gendisk *disk = dev_to_disk(dev);
  383. return sprintf(buf, "%x\n", disk->flags);
  384. }
  385. static ssize_t disk_stat_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct gendisk *disk = dev_to_disk(dev);
  389. preempt_disable();
  390. disk_round_stats(disk);
  391. preempt_enable();
  392. return sprintf(buf,
  393. "%8lu %8lu %8llu %8u "
  394. "%8lu %8lu %8llu %8u "
  395. "%8u %8u %8u"
  396. "\n",
  397. disk_stat_read(disk, ios[READ]),
  398. disk_stat_read(disk, merges[READ]),
  399. (unsigned long long)disk_stat_read(disk, sectors[READ]),
  400. jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
  401. disk_stat_read(disk, ios[WRITE]),
  402. disk_stat_read(disk, merges[WRITE]),
  403. (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
  404. jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
  405. disk->in_flight,
  406. jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
  407. jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
  408. }
  409. #ifdef CONFIG_FAIL_MAKE_REQUEST
  410. static ssize_t disk_fail_show(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. struct gendisk *disk = dev_to_disk(dev);
  414. return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
  415. }
  416. static ssize_t disk_fail_store(struct device *dev,
  417. struct device_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct gendisk *disk = dev_to_disk(dev);
  421. int i;
  422. if (count > 0 && sscanf(buf, "%d", &i) > 0) {
  423. if (i == 0)
  424. disk->flags &= ~GENHD_FL_FAIL;
  425. else
  426. disk->flags |= GENHD_FL_FAIL;
  427. }
  428. return count;
  429. }
  430. #endif
  431. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  432. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  433. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  434. static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
  435. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  436. static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
  437. #ifdef CONFIG_FAIL_MAKE_REQUEST
  438. static struct device_attribute dev_attr_fail =
  439. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
  440. #endif
  441. static struct attribute *disk_attrs[] = {
  442. &dev_attr_range.attr,
  443. &dev_attr_removable.attr,
  444. &dev_attr_ro.attr,
  445. &dev_attr_size.attr,
  446. &dev_attr_capability.attr,
  447. &dev_attr_stat.attr,
  448. #ifdef CONFIG_FAIL_MAKE_REQUEST
  449. &dev_attr_fail.attr,
  450. #endif
  451. NULL
  452. };
  453. static struct attribute_group disk_attr_group = {
  454. .attrs = disk_attrs,
  455. };
  456. static struct attribute_group *disk_attr_groups[] = {
  457. &disk_attr_group,
  458. NULL
  459. };
  460. static void disk_release(struct device *dev)
  461. {
  462. struct gendisk *disk = dev_to_disk(dev);
  463. kfree(disk->random);
  464. kfree(disk->part);
  465. free_disk_stats(disk);
  466. kfree(disk);
  467. }
  468. struct class block_class = {
  469. .name = "block",
  470. };
  471. static struct device_type disk_type = {
  472. .name = "disk",
  473. .groups = disk_attr_groups,
  474. .release = disk_release,
  475. };
  476. #ifdef CONFIG_PROC_FS
  477. static int diskstats_show(struct seq_file *s, void *v)
  478. {
  479. struct gendisk *gp = v;
  480. char buf[BDEVNAME_SIZE];
  481. int n;
  482. /*
  483. if (&gp->dev.kobj.entry == block_class.devices.next)
  484. seq_puts(s, "major minor name"
  485. " rio rmerge rsect ruse wio wmerge "
  486. "wsect wuse running use aveq"
  487. "\n\n");
  488. */
  489. preempt_disable();
  490. disk_round_stats(gp);
  491. preempt_enable();
  492. seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
  493. gp->major, gp->first_minor, disk_name(gp, 0, buf),
  494. disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
  495. (unsigned long long)disk_stat_read(gp, sectors[0]),
  496. jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
  497. disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
  498. (unsigned long long)disk_stat_read(gp, sectors[1]),
  499. jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
  500. gp->in_flight,
  501. jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
  502. jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
  503. /* now show all non-0 size partitions of it */
  504. for (n = 0; n < gp->minors - 1; n++) {
  505. struct hd_struct *hd = gp->part[n];
  506. if (!hd || !hd->nr_sects)
  507. continue;
  508. preempt_disable();
  509. part_round_stats(hd);
  510. preempt_enable();
  511. seq_printf(s, "%4d %4d %s %lu %lu %llu "
  512. "%u %lu %lu %llu %u %u %u %u\n",
  513. gp->major, n + gp->first_minor + 1,
  514. disk_name(gp, n + 1, buf),
  515. part_stat_read(hd, ios[0]),
  516. part_stat_read(hd, merges[0]),
  517. (unsigned long long)part_stat_read(hd, sectors[0]),
  518. jiffies_to_msecs(part_stat_read(hd, ticks[0])),
  519. part_stat_read(hd, ios[1]),
  520. part_stat_read(hd, merges[1]),
  521. (unsigned long long)part_stat_read(hd, sectors[1]),
  522. jiffies_to_msecs(part_stat_read(hd, ticks[1])),
  523. hd->in_flight,
  524. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  525. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  526. );
  527. }
  528. return 0;
  529. }
  530. const struct seq_operations diskstats_op = {
  531. .start = disk_seqf_start,
  532. .next = disk_seqf_next,
  533. .stop = disk_seqf_stop,
  534. .show = diskstats_show
  535. };
  536. #endif /* CONFIG_PROC_FS */
  537. static void media_change_notify_thread(struct work_struct *work)
  538. {
  539. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  540. char event[] = "MEDIA_CHANGE=1";
  541. char *envp[] = { event, NULL };
  542. /*
  543. * set enviroment vars to indicate which event this is for
  544. * so that user space will know to go check the media status.
  545. */
  546. kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
  547. put_device(gd->driverfs_dev);
  548. }
  549. #if 0
  550. void genhd_media_change_notify(struct gendisk *disk)
  551. {
  552. get_device(disk->driverfs_dev);
  553. schedule_work(&disk->async_notify);
  554. }
  555. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  556. #endif /* 0 */
  557. dev_t blk_lookup_devt(const char *name, int part)
  558. {
  559. dev_t devt = MKDEV(0, 0);
  560. struct class_dev_iter iter;
  561. struct device *dev;
  562. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  563. while ((dev = class_dev_iter_next(&iter))) {
  564. struct gendisk *disk = dev_to_disk(dev);
  565. if (!strcmp(dev->bus_id, name) && part < disk->minors) {
  566. devt = MKDEV(MAJOR(dev->devt),
  567. MINOR(dev->devt) + part);
  568. break;
  569. }
  570. }
  571. class_dev_iter_exit(&iter);
  572. return devt;
  573. }
  574. EXPORT_SYMBOL(blk_lookup_devt);
  575. struct gendisk *alloc_disk(int minors)
  576. {
  577. return alloc_disk_node(minors, -1);
  578. }
  579. struct gendisk *alloc_disk_node(int minors, int node_id)
  580. {
  581. struct gendisk *disk;
  582. disk = kmalloc_node(sizeof(struct gendisk),
  583. GFP_KERNEL | __GFP_ZERO, node_id);
  584. if (disk) {
  585. if (!init_disk_stats(disk)) {
  586. kfree(disk);
  587. return NULL;
  588. }
  589. if (minors > 1) {
  590. int size = (minors - 1) * sizeof(struct hd_struct *);
  591. disk->part = kmalloc_node(size,
  592. GFP_KERNEL | __GFP_ZERO, node_id);
  593. if (!disk->part) {
  594. free_disk_stats(disk);
  595. kfree(disk);
  596. return NULL;
  597. }
  598. }
  599. disk->minors = minors;
  600. rand_initialize_disk(disk);
  601. disk->dev.class = &block_class;
  602. disk->dev.type = &disk_type;
  603. device_initialize(&disk->dev);
  604. INIT_WORK(&disk->async_notify,
  605. media_change_notify_thread);
  606. }
  607. return disk;
  608. }
  609. EXPORT_SYMBOL(alloc_disk);
  610. EXPORT_SYMBOL(alloc_disk_node);
  611. struct kobject *get_disk(struct gendisk *disk)
  612. {
  613. struct module *owner;
  614. struct kobject *kobj;
  615. if (!disk->fops)
  616. return NULL;
  617. owner = disk->fops->owner;
  618. if (owner && !try_module_get(owner))
  619. return NULL;
  620. kobj = kobject_get(&disk->dev.kobj);
  621. if (kobj == NULL) {
  622. module_put(owner);
  623. return NULL;
  624. }
  625. return kobj;
  626. }
  627. EXPORT_SYMBOL(get_disk);
  628. void put_disk(struct gendisk *disk)
  629. {
  630. if (disk)
  631. kobject_put(&disk->dev.kobj);
  632. }
  633. EXPORT_SYMBOL(put_disk);
  634. void set_device_ro(struct block_device *bdev, int flag)
  635. {
  636. if (bdev->bd_contains != bdev)
  637. bdev->bd_part->policy = flag;
  638. else
  639. bdev->bd_disk->policy = flag;
  640. }
  641. EXPORT_SYMBOL(set_device_ro);
  642. void set_disk_ro(struct gendisk *disk, int flag)
  643. {
  644. int i;
  645. disk->policy = flag;
  646. for (i = 0; i < disk->minors - 1; i++)
  647. if (disk->part[i]) disk->part[i]->policy = flag;
  648. }
  649. EXPORT_SYMBOL(set_disk_ro);
  650. int bdev_read_only(struct block_device *bdev)
  651. {
  652. if (!bdev)
  653. return 0;
  654. else if (bdev->bd_contains != bdev)
  655. return bdev->bd_part->policy;
  656. else
  657. return bdev->bd_disk->policy;
  658. }
  659. EXPORT_SYMBOL(bdev_read_only);
  660. int invalidate_partition(struct gendisk *disk, int index)
  661. {
  662. int res = 0;
  663. struct block_device *bdev = bdget_disk(disk, index);
  664. if (bdev) {
  665. fsync_bdev(bdev);
  666. res = __invalidate_device(bdev);
  667. bdput(bdev);
  668. }
  669. return res;
  670. }
  671. EXPORT_SYMBOL(invalidate_partition);