genhd.c 18 KB

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