genhd.c 20 KB

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