genhd.c 19 KB

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