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