genhd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. static DEFINE_MUTEX(block_class_lock);
  19. #ifndef CONFIG_SYSFS_DEPRECATED
  20. struct kobject *block_depr;
  21. #endif
  22. /*
  23. * Can be deleted altogether. Later.
  24. *
  25. */
  26. static struct blk_major_name {
  27. struct blk_major_name *next;
  28. int major;
  29. char name[16];
  30. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  31. /* index in the above - for now: assume no multimajor ranges */
  32. static inline int major_to_index(int major)
  33. {
  34. return major % BLKDEV_MAJOR_HASH_SIZE;
  35. }
  36. #ifdef CONFIG_PROC_FS
  37. void blkdev_show(struct seq_file *f, off_t offset)
  38. {
  39. struct blk_major_name *dp;
  40. if (offset < BLKDEV_MAJOR_HASH_SIZE) {
  41. mutex_lock(&block_class_lock);
  42. for (dp = major_names[offset]; dp; dp = dp->next)
  43. seq_printf(f, "%3d %s\n", dp->major, dp->name);
  44. mutex_unlock(&block_class_lock);
  45. }
  46. }
  47. #endif /* CONFIG_PROC_FS */
  48. int register_blkdev(unsigned int major, const char *name)
  49. {
  50. struct blk_major_name **n, *p;
  51. int index, ret = 0;
  52. mutex_lock(&block_class_lock);
  53. /* temporary */
  54. if (major == 0) {
  55. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  56. if (major_names[index] == NULL)
  57. break;
  58. }
  59. if (index == 0) {
  60. printk("register_blkdev: failed to get major for %s\n",
  61. name);
  62. ret = -EBUSY;
  63. goto out;
  64. }
  65. major = index;
  66. ret = major;
  67. }
  68. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  69. if (p == NULL) {
  70. ret = -ENOMEM;
  71. goto out;
  72. }
  73. p->major = major;
  74. strlcpy(p->name, name, sizeof(p->name));
  75. p->next = NULL;
  76. index = major_to_index(major);
  77. for (n = &major_names[index]; *n; n = &(*n)->next) {
  78. if ((*n)->major == major)
  79. break;
  80. }
  81. if (!*n)
  82. *n = p;
  83. else
  84. ret = -EBUSY;
  85. if (ret < 0) {
  86. printk("register_blkdev: cannot get major %d for %s\n",
  87. major, name);
  88. kfree(p);
  89. }
  90. out:
  91. mutex_unlock(&block_class_lock);
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(register_blkdev);
  95. void unregister_blkdev(unsigned int major, const char *name)
  96. {
  97. struct blk_major_name **n;
  98. struct blk_major_name *p = NULL;
  99. int index = major_to_index(major);
  100. mutex_lock(&block_class_lock);
  101. for (n = &major_names[index]; *n; n = &(*n)->next)
  102. if ((*n)->major == major)
  103. break;
  104. if (!*n || strcmp((*n)->name, name)) {
  105. WARN_ON(1);
  106. } else {
  107. p = *n;
  108. *n = p->next;
  109. }
  110. mutex_unlock(&block_class_lock);
  111. kfree(p);
  112. }
  113. EXPORT_SYMBOL(unregister_blkdev);
  114. static struct kobj_map *bdev_map;
  115. /*
  116. * Register device numbers dev..(dev+range-1)
  117. * range must be nonzero
  118. * The hash chain is sorted on range, so that subranges can override.
  119. */
  120. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  121. struct kobject *(*probe)(dev_t, int *, void *),
  122. int (*lock)(dev_t, void *), void *data)
  123. {
  124. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  125. }
  126. EXPORT_SYMBOL(blk_register_region);
  127. void blk_unregister_region(dev_t devt, unsigned long range)
  128. {
  129. kobj_unmap(bdev_map, devt, range);
  130. }
  131. EXPORT_SYMBOL(blk_unregister_region);
  132. static struct kobject *exact_match(dev_t devt, int *part, void *data)
  133. {
  134. struct gendisk *p = data;
  135. return &p->dev.kobj;
  136. }
  137. static int exact_lock(dev_t devt, void *data)
  138. {
  139. struct gendisk *p = data;
  140. if (!get_disk(p))
  141. return -1;
  142. return 0;
  143. }
  144. /**
  145. * add_disk - add partitioning information to kernel list
  146. * @disk: per-device partitioning information
  147. *
  148. * This function registers the partitioning information in @disk
  149. * with the kernel.
  150. */
  151. void add_disk(struct gendisk *disk)
  152. {
  153. disk->flags |= GENHD_FL_UP;
  154. blk_register_region(MKDEV(disk->major, disk->first_minor),
  155. disk->minors, NULL, exact_match, exact_lock, disk);
  156. register_disk(disk);
  157. blk_register_queue(disk);
  158. }
  159. EXPORT_SYMBOL(add_disk);
  160. EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
  161. void unlink_gendisk(struct gendisk *disk)
  162. {
  163. blk_unregister_queue(disk);
  164. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  165. disk->minors);
  166. }
  167. /**
  168. * get_gendisk - get partitioning information for a given device
  169. * @dev: device to get partitioning information for
  170. *
  171. * This function gets the structure containing partitioning
  172. * information for the given device @dev.
  173. */
  174. struct gendisk *get_gendisk(dev_t devt, int *part)
  175. {
  176. struct kobject *kobj = kobj_lookup(bdev_map, devt, part);
  177. struct device *dev = kobj_to_dev(kobj);
  178. return kobj ? dev_to_disk(dev) : NULL;
  179. }
  180. /*
  181. * print a full list of all partitions - intended for places where the root
  182. * filesystem can't be mounted and thus to give the victim some idea of what
  183. * went wrong
  184. */
  185. void __init printk_all_partitions(void)
  186. {
  187. struct device *dev;
  188. struct gendisk *sgp;
  189. char buf[BDEVNAME_SIZE];
  190. int n;
  191. mutex_lock(&block_class_lock);
  192. /* For each block device... */
  193. list_for_each_entry(dev, &block_class.devices, node) {
  194. if (dev->type != &disk_type)
  195. continue;
  196. sgp = dev_to_disk(dev);
  197. /*
  198. * Don't show empty devices or things that have been surpressed
  199. */
  200. if (get_capacity(sgp) == 0 ||
  201. (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  202. continue;
  203. /*
  204. * Note, unlike /proc/partitions, I am showing the numbers in
  205. * hex - the same format as the root= option takes.
  206. */
  207. printk("%02x%02x %10llu %s",
  208. sgp->major, sgp->first_minor,
  209. (unsigned long long)get_capacity(sgp) >> 1,
  210. disk_name(sgp, 0, buf));
  211. if (sgp->driverfs_dev != NULL &&
  212. sgp->driverfs_dev->driver != NULL)
  213. printk(" driver: %s\n",
  214. sgp->driverfs_dev->driver->name);
  215. else
  216. printk(" (driver?)\n");
  217. /* now show the partitions */
  218. for (n = 0; n < sgp->minors - 1; ++n) {
  219. if (sgp->part[n] == NULL)
  220. continue;
  221. if (sgp->part[n]->nr_sects == 0)
  222. continue;
  223. printk(" %02x%02x %10llu %s\n",
  224. sgp->major, n + 1 + sgp->first_minor,
  225. (unsigned long long)sgp->part[n]->nr_sects >> 1,
  226. disk_name(sgp, n + 1, buf));
  227. }
  228. }
  229. mutex_unlock(&block_class_lock);
  230. }
  231. #ifdef CONFIG_PROC_FS
  232. /* iterator */
  233. static void *part_start(struct seq_file *part, loff_t *pos)
  234. {
  235. loff_t k = *pos;
  236. struct device *dev;
  237. mutex_lock(&block_class_lock);
  238. list_for_each_entry(dev, &block_class.devices, node) {
  239. if (dev->type != &disk_type)
  240. continue;
  241. if (!k--)
  242. return dev_to_disk(dev);
  243. }
  244. return NULL;
  245. }
  246. static void *part_next(struct seq_file *part, void *v, loff_t *pos)
  247. {
  248. struct gendisk *gp = v;
  249. struct device *dev;
  250. ++*pos;
  251. list_for_each_entry(dev, &gp->dev.node, node) {
  252. if (&dev->node == &block_class.devices)
  253. return NULL;
  254. if (dev->type == &disk_type)
  255. return dev_to_disk(dev);
  256. }
  257. return NULL;
  258. }
  259. static void part_stop(struct seq_file *part, void *v)
  260. {
  261. mutex_unlock(&block_class_lock);
  262. }
  263. static int show_partition(struct seq_file *part, void *v)
  264. {
  265. struct gendisk *sgp = v;
  266. int n;
  267. char buf[BDEVNAME_SIZE];
  268. if (&sgp->dev.node == block_class.devices.next)
  269. seq_puts(part, "major minor #blocks name\n\n");
  270. /* Don't show non-partitionable removeable devices or empty devices */
  271. if (!get_capacity(sgp) ||
  272. (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
  273. return 0;
  274. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  275. return 0;
  276. /* show the full disk and all non-0 size partitions of it */
  277. seq_printf(part, "%4d %4d %10llu %s\n",
  278. sgp->major, sgp->first_minor,
  279. (unsigned long long)get_capacity(sgp) >> 1,
  280. disk_name(sgp, 0, buf));
  281. for (n = 0; n < sgp->minors - 1; n++) {
  282. if (!sgp->part[n])
  283. continue;
  284. if (sgp->part[n]->nr_sects == 0)
  285. continue;
  286. seq_printf(part, "%4d %4d %10llu %s\n",
  287. sgp->major, n + 1 + sgp->first_minor,
  288. (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
  289. disk_name(sgp, n + 1, buf));
  290. }
  291. return 0;
  292. }
  293. struct seq_operations partitions_op = {
  294. .start = part_start,
  295. .next = part_next,
  296. .stop = part_stop,
  297. .show = show_partition
  298. };
  299. #endif
  300. extern int blk_dev_init(void);
  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. seq_printf(s, "%4d %4d %s %u %u %u %u\n",
  502. gp->major, n + gp->first_minor + 1,
  503. disk_name(gp, n + 1, buf),
  504. hd->ios[0], hd->sectors[0],
  505. hd->ios[1], hd->sectors[1]);
  506. }
  507. return 0;
  508. }
  509. struct seq_operations diskstats_op = {
  510. .start = diskstats_start,
  511. .next = diskstats_next,
  512. .stop = diskstats_stop,
  513. .show = diskstats_show
  514. };
  515. static void media_change_notify_thread(struct work_struct *work)
  516. {
  517. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  518. char event[] = "MEDIA_CHANGE=1";
  519. char *envp[] = { event, NULL };
  520. /*
  521. * set enviroment vars to indicate which event this is for
  522. * so that user space will know to go check the media status.
  523. */
  524. kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
  525. put_device(gd->driverfs_dev);
  526. }
  527. void genhd_media_change_notify(struct gendisk *disk)
  528. {
  529. get_device(disk->driverfs_dev);
  530. schedule_work(&disk->async_notify);
  531. }
  532. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  533. dev_t blk_lookup_devt(const char *name)
  534. {
  535. struct device *dev;
  536. dev_t devt = MKDEV(0, 0);
  537. mutex_lock(&block_class_lock);
  538. list_for_each_entry(dev, &block_class.devices, node) {
  539. if (strcmp(dev->bus_id, name) == 0) {
  540. devt = dev->devt;
  541. break;
  542. }
  543. }
  544. mutex_unlock(&block_class_lock);
  545. return devt;
  546. }
  547. EXPORT_SYMBOL(blk_lookup_devt);
  548. struct gendisk *alloc_disk(int minors)
  549. {
  550. return alloc_disk_node(minors, -1);
  551. }
  552. struct gendisk *alloc_disk_node(int minors, int node_id)
  553. {
  554. struct gendisk *disk;
  555. disk = kmalloc_node(sizeof(struct gendisk),
  556. GFP_KERNEL | __GFP_ZERO, node_id);
  557. if (disk) {
  558. if (!init_disk_stats(disk)) {
  559. kfree(disk);
  560. return NULL;
  561. }
  562. if (minors > 1) {
  563. int size = (minors - 1) * sizeof(struct hd_struct *);
  564. disk->part = kmalloc_node(size,
  565. GFP_KERNEL | __GFP_ZERO, node_id);
  566. if (!disk->part) {
  567. free_disk_stats(disk);
  568. kfree(disk);
  569. return NULL;
  570. }
  571. }
  572. disk->minors = minors;
  573. rand_initialize_disk(disk);
  574. disk->dev.class = &block_class;
  575. disk->dev.type = &disk_type;
  576. device_initialize(&disk->dev);
  577. INIT_WORK(&disk->async_notify,
  578. media_change_notify_thread);
  579. }
  580. return disk;
  581. }
  582. EXPORT_SYMBOL(alloc_disk);
  583. EXPORT_SYMBOL(alloc_disk_node);
  584. struct kobject *get_disk(struct gendisk *disk)
  585. {
  586. struct module *owner;
  587. struct kobject *kobj;
  588. if (!disk->fops)
  589. return NULL;
  590. owner = disk->fops->owner;
  591. if (owner && !try_module_get(owner))
  592. return NULL;
  593. kobj = kobject_get(&disk->dev.kobj);
  594. if (kobj == NULL) {
  595. module_put(owner);
  596. return NULL;
  597. }
  598. return kobj;
  599. }
  600. EXPORT_SYMBOL(get_disk);
  601. void put_disk(struct gendisk *disk)
  602. {
  603. if (disk)
  604. kobject_put(&disk->dev.kobj);
  605. }
  606. EXPORT_SYMBOL(put_disk);
  607. void set_device_ro(struct block_device *bdev, int flag)
  608. {
  609. if (bdev->bd_contains != bdev)
  610. bdev->bd_part->policy = flag;
  611. else
  612. bdev->bd_disk->policy = flag;
  613. }
  614. EXPORT_SYMBOL(set_device_ro);
  615. void set_disk_ro(struct gendisk *disk, int flag)
  616. {
  617. int i;
  618. disk->policy = flag;
  619. for (i = 0; i < disk->minors - 1; i++)
  620. if (disk->part[i]) disk->part[i]->policy = flag;
  621. }
  622. EXPORT_SYMBOL(set_disk_ro);
  623. int bdev_read_only(struct block_device *bdev)
  624. {
  625. if (!bdev)
  626. return 0;
  627. else if (bdev->bd_contains != bdev)
  628. return bdev->bd_part->policy;
  629. else
  630. return bdev->bd_disk->policy;
  631. }
  632. EXPORT_SYMBOL(bdev_read_only);
  633. int invalidate_partition(struct gendisk *disk, int index)
  634. {
  635. int res = 0;
  636. struct block_device *bdev = bdget_disk(disk, index);
  637. if (bdev) {
  638. fsync_bdev(bdev);
  639. res = __invalidate_device(bdev);
  640. bdput(bdev);
  641. }
  642. return res;
  643. }
  644. EXPORT_SYMBOL(invalidate_partition);