genhd.c 18 KB

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