genhd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. class_register(&block_class);
  312. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  313. blk_dev_init();
  314. #ifndef CONFIG_SYSFS_DEPRECATED
  315. /* create top-level block dir */
  316. block_depr = kobject_create_and_add("block", NULL);
  317. #endif
  318. return 0;
  319. }
  320. subsys_initcall(genhd_device_init);
  321. static ssize_t disk_range_show(struct device *dev,
  322. struct device_attribute *attr, char *buf)
  323. {
  324. struct gendisk *disk = dev_to_disk(dev);
  325. return sprintf(buf, "%d\n", disk->minors);
  326. }
  327. static ssize_t disk_removable_show(struct device *dev,
  328. struct device_attribute *attr, char *buf)
  329. {
  330. struct gendisk *disk = dev_to_disk(dev);
  331. return sprintf(buf, "%d\n",
  332. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  333. }
  334. static ssize_t disk_size_show(struct device *dev,
  335. struct device_attribute *attr, char *buf)
  336. {
  337. struct gendisk *disk = dev_to_disk(dev);
  338. return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
  339. }
  340. static ssize_t disk_capability_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. struct gendisk *disk = dev_to_disk(dev);
  344. return sprintf(buf, "%x\n", disk->flags);
  345. }
  346. static ssize_t disk_stat_show(struct device *dev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. struct gendisk *disk = dev_to_disk(dev);
  350. preempt_disable();
  351. disk_round_stats(disk);
  352. preempt_enable();
  353. return sprintf(buf,
  354. "%8lu %8lu %8llu %8u "
  355. "%8lu %8lu %8llu %8u "
  356. "%8u %8u %8u"
  357. "\n",
  358. disk_stat_read(disk, ios[READ]),
  359. disk_stat_read(disk, merges[READ]),
  360. (unsigned long long)disk_stat_read(disk, sectors[READ]),
  361. jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
  362. disk_stat_read(disk, ios[WRITE]),
  363. disk_stat_read(disk, merges[WRITE]),
  364. (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
  365. jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
  366. disk->in_flight,
  367. jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
  368. jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
  369. }
  370. #ifdef CONFIG_FAIL_MAKE_REQUEST
  371. static ssize_t disk_fail_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct gendisk *disk = dev_to_disk(dev);
  375. return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
  376. }
  377. static ssize_t disk_fail_store(struct device *dev,
  378. struct device_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. struct gendisk *disk = dev_to_disk(dev);
  382. int i;
  383. if (count > 0 && sscanf(buf, "%d", &i) > 0) {
  384. if (i == 0)
  385. disk->flags &= ~GENHD_FL_FAIL;
  386. else
  387. disk->flags |= GENHD_FL_FAIL;
  388. }
  389. return count;
  390. }
  391. #endif
  392. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  393. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  394. static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
  395. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  396. static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
  397. #ifdef CONFIG_FAIL_MAKE_REQUEST
  398. static struct device_attribute dev_attr_fail =
  399. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
  400. #endif
  401. static struct attribute *disk_attrs[] = {
  402. &dev_attr_range.attr,
  403. &dev_attr_removable.attr,
  404. &dev_attr_size.attr,
  405. &dev_attr_capability.attr,
  406. &dev_attr_stat.attr,
  407. #ifdef CONFIG_FAIL_MAKE_REQUEST
  408. &dev_attr_fail.attr,
  409. #endif
  410. NULL
  411. };
  412. static struct attribute_group disk_attr_group = {
  413. .attrs = disk_attrs,
  414. };
  415. static struct attribute_group *disk_attr_groups[] = {
  416. &disk_attr_group,
  417. NULL
  418. };
  419. static void disk_release(struct device *dev)
  420. {
  421. struct gendisk *disk = dev_to_disk(dev);
  422. kfree(disk->random);
  423. kfree(disk->part);
  424. free_disk_stats(disk);
  425. kfree(disk);
  426. }
  427. struct class block_class = {
  428. .name = "block",
  429. };
  430. static struct device_type disk_type = {
  431. .name = "disk",
  432. .groups = disk_attr_groups,
  433. .release = disk_release,
  434. };
  435. /*
  436. * aggregate disk stat collector. Uses the same stats that the sysfs
  437. * entries do, above, but makes them available through one seq_file.
  438. *
  439. * The output looks suspiciously like /proc/partitions with a bunch of
  440. * extra fields.
  441. */
  442. static void *diskstats_start(struct seq_file *part, loff_t *pos)
  443. {
  444. loff_t k = *pos;
  445. struct device *dev;
  446. mutex_lock(&block_class_lock);
  447. list_for_each_entry(dev, &block_class.devices, node) {
  448. if (dev->type != &disk_type)
  449. continue;
  450. if (!k--)
  451. return dev_to_disk(dev);
  452. }
  453. return NULL;
  454. }
  455. static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
  456. {
  457. struct gendisk *gp = v;
  458. struct device *dev;
  459. ++*pos;
  460. list_for_each_entry(dev, &gp->dev.node, node) {
  461. if (&dev->node == &block_class.devices)
  462. return NULL;
  463. if (dev->type == &disk_type)
  464. return dev_to_disk(dev);
  465. }
  466. return NULL;
  467. }
  468. static void diskstats_stop(struct seq_file *part, void *v)
  469. {
  470. mutex_unlock(&block_class_lock);
  471. }
  472. static int diskstats_show(struct seq_file *s, void *v)
  473. {
  474. struct gendisk *gp = v;
  475. char buf[BDEVNAME_SIZE];
  476. int n = 0;
  477. /*
  478. if (&gp->dev.kobj.entry == block_class.devices.next)
  479. seq_puts(s, "major minor name"
  480. " rio rmerge rsect ruse wio wmerge "
  481. "wsect wuse running use aveq"
  482. "\n\n");
  483. */
  484. preempt_disable();
  485. disk_round_stats(gp);
  486. preempt_enable();
  487. seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
  488. gp->major, n + gp->first_minor, disk_name(gp, n, buf),
  489. disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
  490. (unsigned long long)disk_stat_read(gp, sectors[0]),
  491. jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
  492. disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
  493. (unsigned long long)disk_stat_read(gp, sectors[1]),
  494. jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
  495. gp->in_flight,
  496. jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
  497. jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
  498. /* now show all non-0 size partitions of it */
  499. for (n = 0; n < gp->minors - 1; n++) {
  500. struct hd_struct *hd = gp->part[n];
  501. if (!hd || !hd->nr_sects)
  502. continue;
  503. preempt_disable();
  504. part_round_stats(hd);
  505. preempt_enable();
  506. seq_printf(s, "%4d %4d %s %lu %lu %llu "
  507. "%u %lu %lu %llu %u %u %u %u\n",
  508. gp->major, n + gp->first_minor + 1,
  509. disk_name(gp, n + 1, buf),
  510. part_stat_read(hd, ios[0]),
  511. part_stat_read(hd, merges[0]),
  512. (unsigned long long)part_stat_read(hd, sectors[0]),
  513. jiffies_to_msecs(part_stat_read(hd, ticks[0])),
  514. part_stat_read(hd, ios[1]),
  515. part_stat_read(hd, merges[1]),
  516. (unsigned long long)part_stat_read(hd, sectors[1]),
  517. jiffies_to_msecs(part_stat_read(hd, ticks[1])),
  518. hd->in_flight,
  519. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  520. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  521. );
  522. }
  523. return 0;
  524. }
  525. const struct seq_operations diskstats_op = {
  526. .start = diskstats_start,
  527. .next = diskstats_next,
  528. .stop = diskstats_stop,
  529. .show = diskstats_show
  530. };
  531. static void media_change_notify_thread(struct work_struct *work)
  532. {
  533. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  534. char event[] = "MEDIA_CHANGE=1";
  535. char *envp[] = { event, NULL };
  536. /*
  537. * set enviroment vars to indicate which event this is for
  538. * so that user space will know to go check the media status.
  539. */
  540. kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
  541. put_device(gd->driverfs_dev);
  542. }
  543. #if 0
  544. void genhd_media_change_notify(struct gendisk *disk)
  545. {
  546. get_device(disk->driverfs_dev);
  547. schedule_work(&disk->async_notify);
  548. }
  549. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  550. #endif /* 0 */
  551. dev_t blk_lookup_devt(const char *name)
  552. {
  553. struct device *dev;
  554. dev_t devt = MKDEV(0, 0);
  555. mutex_lock(&block_class_lock);
  556. list_for_each_entry(dev, &block_class.devices, node) {
  557. if (strcmp(dev->bus_id, name) == 0) {
  558. devt = dev->devt;
  559. break;
  560. }
  561. }
  562. mutex_unlock(&block_class_lock);
  563. return devt;
  564. }
  565. EXPORT_SYMBOL(blk_lookup_devt);
  566. struct gendisk *alloc_disk(int minors)
  567. {
  568. return alloc_disk_node(minors, -1);
  569. }
  570. struct gendisk *alloc_disk_node(int minors, int node_id)
  571. {
  572. struct gendisk *disk;
  573. disk = kmalloc_node(sizeof(struct gendisk),
  574. GFP_KERNEL | __GFP_ZERO, node_id);
  575. if (disk) {
  576. if (!init_disk_stats(disk)) {
  577. kfree(disk);
  578. return NULL;
  579. }
  580. if (minors > 1) {
  581. int size = (minors - 1) * sizeof(struct hd_struct *);
  582. disk->part = kmalloc_node(size,
  583. GFP_KERNEL | __GFP_ZERO, node_id);
  584. if (!disk->part) {
  585. free_disk_stats(disk);
  586. kfree(disk);
  587. return NULL;
  588. }
  589. }
  590. disk->minors = minors;
  591. rand_initialize_disk(disk);
  592. disk->dev.class = &block_class;
  593. disk->dev.type = &disk_type;
  594. device_initialize(&disk->dev);
  595. INIT_WORK(&disk->async_notify,
  596. media_change_notify_thread);
  597. }
  598. return disk;
  599. }
  600. EXPORT_SYMBOL(alloc_disk);
  601. EXPORT_SYMBOL(alloc_disk_node);
  602. struct kobject *get_disk(struct gendisk *disk)
  603. {
  604. struct module *owner;
  605. struct kobject *kobj;
  606. if (!disk->fops)
  607. return NULL;
  608. owner = disk->fops->owner;
  609. if (owner && !try_module_get(owner))
  610. return NULL;
  611. kobj = kobject_get(&disk->dev.kobj);
  612. if (kobj == NULL) {
  613. module_put(owner);
  614. return NULL;
  615. }
  616. return kobj;
  617. }
  618. EXPORT_SYMBOL(get_disk);
  619. void put_disk(struct gendisk *disk)
  620. {
  621. if (disk)
  622. kobject_put(&disk->dev.kobj);
  623. }
  624. EXPORT_SYMBOL(put_disk);
  625. void set_device_ro(struct block_device *bdev, int flag)
  626. {
  627. if (bdev->bd_contains != bdev)
  628. bdev->bd_part->policy = flag;
  629. else
  630. bdev->bd_disk->policy = flag;
  631. }
  632. EXPORT_SYMBOL(set_device_ro);
  633. void set_disk_ro(struct gendisk *disk, int flag)
  634. {
  635. int i;
  636. disk->policy = flag;
  637. for (i = 0; i < disk->minors - 1; i++)
  638. if (disk->part[i]) disk->part[i]->policy = flag;
  639. }
  640. EXPORT_SYMBOL(set_disk_ro);
  641. int bdev_read_only(struct block_device *bdev)
  642. {
  643. if (!bdev)
  644. return 0;
  645. else if (bdev->bd_contains != bdev)
  646. return bdev->bd_part->policy;
  647. else
  648. return bdev->bd_disk->policy;
  649. }
  650. EXPORT_SYMBOL(bdev_read_only);
  651. int invalidate_partition(struct gendisk *disk, int index)
  652. {
  653. int res = 0;
  654. struct block_device *bdev = bdget_disk(disk, index);
  655. if (bdev) {
  656. fsync_bdev(bdev);
  657. res = __invalidate_device(bdev);
  658. bdput(bdev);
  659. }
  660. return res;
  661. }
  662. EXPORT_SYMBOL(invalidate_partition);