genhd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 <linux/idr.h>
  19. #include "blk.h"
  20. static DEFINE_MUTEX(block_class_lock);
  21. #ifndef CONFIG_SYSFS_DEPRECATED
  22. struct kobject *block_depr;
  23. #endif
  24. /* for extended dynamic devt allocation, currently only one major is used */
  25. #define MAX_EXT_DEVT (1 << MINORBITS)
  26. /* For extended devt allocation. ext_devt_mutex prevents look up
  27. * results from going away underneath its user.
  28. */
  29. static DEFINE_MUTEX(ext_devt_mutex);
  30. static DEFINE_IDR(ext_devt_idr);
  31. static struct device_type disk_type;
  32. /**
  33. * disk_get_part - get partition
  34. * @disk: disk to look partition from
  35. * @partno: partition number
  36. *
  37. * Look for partition @partno from @disk. If found, increment
  38. * reference count and return it.
  39. *
  40. * CONTEXT:
  41. * Don't care.
  42. *
  43. * RETURNS:
  44. * Pointer to the found partition on success, NULL if not found.
  45. */
  46. struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
  47. {
  48. struct hd_struct *part = NULL;
  49. struct disk_part_tbl *ptbl;
  50. if (unlikely(partno < 0))
  51. return NULL;
  52. rcu_read_lock();
  53. ptbl = rcu_dereference(disk->part_tbl);
  54. if (likely(partno < ptbl->len)) {
  55. part = rcu_dereference(ptbl->part[partno]);
  56. if (part)
  57. get_device(part_to_dev(part));
  58. }
  59. rcu_read_unlock();
  60. return part;
  61. }
  62. EXPORT_SYMBOL_GPL(disk_get_part);
  63. /**
  64. * disk_part_iter_init - initialize partition iterator
  65. * @piter: iterator to initialize
  66. * @disk: disk to iterate over
  67. * @flags: DISK_PITER_* flags
  68. *
  69. * Initialize @piter so that it iterates over partitions of @disk.
  70. *
  71. * CONTEXT:
  72. * Don't care.
  73. */
  74. void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
  75. unsigned int flags)
  76. {
  77. struct disk_part_tbl *ptbl;
  78. rcu_read_lock();
  79. ptbl = rcu_dereference(disk->part_tbl);
  80. piter->disk = disk;
  81. piter->part = NULL;
  82. if (flags & DISK_PITER_REVERSE)
  83. piter->idx = ptbl->len - 1;
  84. else if (flags & DISK_PITER_INCL_PART0)
  85. piter->idx = 0;
  86. else
  87. piter->idx = 1;
  88. piter->flags = flags;
  89. rcu_read_unlock();
  90. }
  91. EXPORT_SYMBOL_GPL(disk_part_iter_init);
  92. /**
  93. * disk_part_iter_next - proceed iterator to the next partition and return it
  94. * @piter: iterator of interest
  95. *
  96. * Proceed @piter to the next partition and return it.
  97. *
  98. * CONTEXT:
  99. * Don't care.
  100. */
  101. struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
  102. {
  103. struct disk_part_tbl *ptbl;
  104. int inc, end;
  105. /* put the last partition */
  106. disk_put_part(piter->part);
  107. piter->part = NULL;
  108. /* get part_tbl */
  109. rcu_read_lock();
  110. ptbl = rcu_dereference(piter->disk->part_tbl);
  111. /* determine iteration parameters */
  112. if (piter->flags & DISK_PITER_REVERSE) {
  113. inc = -1;
  114. if (piter->flags & DISK_PITER_INCL_PART0)
  115. end = -1;
  116. else
  117. end = 0;
  118. } else {
  119. inc = 1;
  120. end = ptbl->len;
  121. }
  122. /* iterate to the next partition */
  123. for (; piter->idx != end; piter->idx += inc) {
  124. struct hd_struct *part;
  125. part = rcu_dereference(ptbl->part[piter->idx]);
  126. if (!part)
  127. continue;
  128. if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
  129. continue;
  130. get_device(part_to_dev(part));
  131. piter->part = part;
  132. piter->idx += inc;
  133. break;
  134. }
  135. rcu_read_unlock();
  136. return piter->part;
  137. }
  138. EXPORT_SYMBOL_GPL(disk_part_iter_next);
  139. /**
  140. * disk_part_iter_exit - finish up partition iteration
  141. * @piter: iter of interest
  142. *
  143. * Called when iteration is over. Cleans up @piter.
  144. *
  145. * CONTEXT:
  146. * Don't care.
  147. */
  148. void disk_part_iter_exit(struct disk_part_iter *piter)
  149. {
  150. disk_put_part(piter->part);
  151. piter->part = NULL;
  152. }
  153. EXPORT_SYMBOL_GPL(disk_part_iter_exit);
  154. /**
  155. * disk_map_sector_rcu - map sector to partition
  156. * @disk: gendisk of interest
  157. * @sector: sector to map
  158. *
  159. * Find out which partition @sector maps to on @disk. This is
  160. * primarily used for stats accounting.
  161. *
  162. * CONTEXT:
  163. * RCU read locked. The returned partition pointer is valid only
  164. * while preemption is disabled.
  165. *
  166. * RETURNS:
  167. * Found partition on success, part0 is returned if no partition matches
  168. */
  169. struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
  170. {
  171. struct disk_part_tbl *ptbl;
  172. int i;
  173. ptbl = rcu_dereference(disk->part_tbl);
  174. for (i = 1; i < ptbl->len; i++) {
  175. struct hd_struct *part = rcu_dereference(ptbl->part[i]);
  176. if (part && part->start_sect <= sector &&
  177. sector < part->start_sect + part->nr_sects)
  178. return part;
  179. }
  180. return &disk->part0;
  181. }
  182. EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
  183. /*
  184. * Can be deleted altogether. Later.
  185. *
  186. */
  187. static struct blk_major_name {
  188. struct blk_major_name *next;
  189. int major;
  190. char name[16];
  191. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  192. /* index in the above - for now: assume no multimajor ranges */
  193. static inline int major_to_index(int major)
  194. {
  195. return major % BLKDEV_MAJOR_HASH_SIZE;
  196. }
  197. #ifdef CONFIG_PROC_FS
  198. void blkdev_show(struct seq_file *seqf, off_t offset)
  199. {
  200. struct blk_major_name *dp;
  201. if (offset < BLKDEV_MAJOR_HASH_SIZE) {
  202. mutex_lock(&block_class_lock);
  203. for (dp = major_names[offset]; dp; dp = dp->next)
  204. seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
  205. mutex_unlock(&block_class_lock);
  206. }
  207. }
  208. #endif /* CONFIG_PROC_FS */
  209. int register_blkdev(unsigned int major, const char *name)
  210. {
  211. struct blk_major_name **n, *p;
  212. int index, ret = 0;
  213. mutex_lock(&block_class_lock);
  214. /* temporary */
  215. if (major == 0) {
  216. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  217. if (major_names[index] == NULL)
  218. break;
  219. }
  220. if (index == 0) {
  221. printk("register_blkdev: failed to get major for %s\n",
  222. name);
  223. ret = -EBUSY;
  224. goto out;
  225. }
  226. major = index;
  227. ret = major;
  228. }
  229. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  230. if (p == NULL) {
  231. ret = -ENOMEM;
  232. goto out;
  233. }
  234. p->major = major;
  235. strlcpy(p->name, name, sizeof(p->name));
  236. p->next = NULL;
  237. index = major_to_index(major);
  238. for (n = &major_names[index]; *n; n = &(*n)->next) {
  239. if ((*n)->major == major)
  240. break;
  241. }
  242. if (!*n)
  243. *n = p;
  244. else
  245. ret = -EBUSY;
  246. if (ret < 0) {
  247. printk("register_blkdev: cannot get major %d for %s\n",
  248. major, name);
  249. kfree(p);
  250. }
  251. out:
  252. mutex_unlock(&block_class_lock);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(register_blkdev);
  256. void unregister_blkdev(unsigned int major, const char *name)
  257. {
  258. struct blk_major_name **n;
  259. struct blk_major_name *p = NULL;
  260. int index = major_to_index(major);
  261. mutex_lock(&block_class_lock);
  262. for (n = &major_names[index]; *n; n = &(*n)->next)
  263. if ((*n)->major == major)
  264. break;
  265. if (!*n || strcmp((*n)->name, name)) {
  266. WARN_ON(1);
  267. } else {
  268. p = *n;
  269. *n = p->next;
  270. }
  271. mutex_unlock(&block_class_lock);
  272. kfree(p);
  273. }
  274. EXPORT_SYMBOL(unregister_blkdev);
  275. static struct kobj_map *bdev_map;
  276. /**
  277. * blk_mangle_minor - scatter minor numbers apart
  278. * @minor: minor number to mangle
  279. *
  280. * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
  281. * is enabled. Mangling twice gives the original value.
  282. *
  283. * RETURNS:
  284. * Mangled value.
  285. *
  286. * CONTEXT:
  287. * Don't care.
  288. */
  289. static int blk_mangle_minor(int minor)
  290. {
  291. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  292. int i;
  293. for (i = 0; i < MINORBITS / 2; i++) {
  294. int low = minor & (1 << i);
  295. int high = minor & (1 << (MINORBITS - 1 - i));
  296. int distance = MINORBITS - 1 - 2 * i;
  297. minor ^= low | high; /* clear both bits */
  298. low <<= distance; /* swap the positions */
  299. high >>= distance;
  300. minor |= low | high; /* and set */
  301. }
  302. #endif
  303. return minor;
  304. }
  305. /**
  306. * blk_alloc_devt - allocate a dev_t for a partition
  307. * @part: partition to allocate dev_t for
  308. * @devt: out parameter for resulting dev_t
  309. *
  310. * Allocate a dev_t for block device.
  311. *
  312. * RETURNS:
  313. * 0 on success, allocated dev_t is returned in *@devt. -errno on
  314. * failure.
  315. *
  316. * CONTEXT:
  317. * Might sleep.
  318. */
  319. int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
  320. {
  321. struct gendisk *disk = part_to_disk(part);
  322. int idx, rc;
  323. /* in consecutive minor range? */
  324. if (part->partno < disk->minors) {
  325. *devt = MKDEV(disk->major, disk->first_minor + part->partno);
  326. return 0;
  327. }
  328. /* allocate ext devt */
  329. do {
  330. if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
  331. return -ENOMEM;
  332. rc = idr_get_new(&ext_devt_idr, part, &idx);
  333. } while (rc == -EAGAIN);
  334. if (rc)
  335. return rc;
  336. if (idx > MAX_EXT_DEVT) {
  337. idr_remove(&ext_devt_idr, idx);
  338. return -EBUSY;
  339. }
  340. *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
  341. return 0;
  342. }
  343. /**
  344. * blk_free_devt - free a dev_t
  345. * @devt: dev_t to free
  346. *
  347. * Free @devt which was allocated using blk_alloc_devt().
  348. *
  349. * CONTEXT:
  350. * Might sleep.
  351. */
  352. void blk_free_devt(dev_t devt)
  353. {
  354. might_sleep();
  355. if (devt == MKDEV(0, 0))
  356. return;
  357. if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
  358. mutex_lock(&ext_devt_mutex);
  359. idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  360. mutex_unlock(&ext_devt_mutex);
  361. }
  362. }
  363. static char *bdevt_str(dev_t devt, char *buf)
  364. {
  365. if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
  366. char tbuf[BDEVT_SIZE];
  367. snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
  368. snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
  369. } else
  370. snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
  371. return buf;
  372. }
  373. /*
  374. * Register device numbers dev..(dev+range-1)
  375. * range must be nonzero
  376. * The hash chain is sorted on range, so that subranges can override.
  377. */
  378. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  379. struct kobject *(*probe)(dev_t, int *, void *),
  380. int (*lock)(dev_t, void *), void *data)
  381. {
  382. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  383. }
  384. EXPORT_SYMBOL(blk_register_region);
  385. void blk_unregister_region(dev_t devt, unsigned long range)
  386. {
  387. kobj_unmap(bdev_map, devt, range);
  388. }
  389. EXPORT_SYMBOL(blk_unregister_region);
  390. static struct kobject *exact_match(dev_t devt, int *partno, void *data)
  391. {
  392. struct gendisk *p = data;
  393. return &disk_to_dev(p)->kobj;
  394. }
  395. static int exact_lock(dev_t devt, void *data)
  396. {
  397. struct gendisk *p = data;
  398. if (!get_disk(p))
  399. return -1;
  400. return 0;
  401. }
  402. /**
  403. * add_disk - add partitioning information to kernel list
  404. * @disk: per-device partitioning information
  405. *
  406. * This function registers the partitioning information in @disk
  407. * with the kernel.
  408. *
  409. * FIXME: error handling
  410. */
  411. void add_disk(struct gendisk *disk)
  412. {
  413. struct backing_dev_info *bdi;
  414. dev_t devt;
  415. int retval;
  416. /* minors == 0 indicates to use ext devt from part0 and should
  417. * be accompanied with EXT_DEVT flag. Make sure all
  418. * parameters make sense.
  419. */
  420. WARN_ON(disk->minors && !(disk->major || disk->first_minor));
  421. WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
  422. disk->flags |= GENHD_FL_UP;
  423. retval = blk_alloc_devt(&disk->part0, &devt);
  424. if (retval) {
  425. WARN_ON(1);
  426. return;
  427. }
  428. disk_to_dev(disk)->devt = devt;
  429. /* ->major and ->first_minor aren't supposed to be
  430. * dereferenced from here on, but set them just in case.
  431. */
  432. disk->major = MAJOR(devt);
  433. disk->first_minor = MINOR(devt);
  434. blk_register_region(disk_devt(disk), disk->minors, NULL,
  435. exact_match, exact_lock, disk);
  436. register_disk(disk);
  437. blk_register_queue(disk);
  438. bdi = &disk->queue->backing_dev_info;
  439. bdi_register_dev(bdi, disk_devt(disk));
  440. retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
  441. "bdi");
  442. WARN_ON(retval);
  443. }
  444. EXPORT_SYMBOL(add_disk);
  445. EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
  446. void unlink_gendisk(struct gendisk *disk)
  447. {
  448. sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
  449. bdi_unregister(&disk->queue->backing_dev_info);
  450. blk_unregister_queue(disk);
  451. blk_unregister_region(disk_devt(disk), disk->minors);
  452. }
  453. /**
  454. * get_gendisk - get partitioning information for a given device
  455. * @devt: device to get partitioning information for
  456. * @partno: returned partition index
  457. *
  458. * This function gets the structure containing partitioning
  459. * information for the given device @devt.
  460. */
  461. struct gendisk *get_gendisk(dev_t devt, int *partno)
  462. {
  463. struct gendisk *disk = NULL;
  464. if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
  465. struct kobject *kobj;
  466. kobj = kobj_lookup(bdev_map, devt, partno);
  467. if (kobj)
  468. disk = dev_to_disk(kobj_to_dev(kobj));
  469. } else {
  470. struct hd_struct *part;
  471. mutex_lock(&ext_devt_mutex);
  472. part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  473. if (part && get_disk(part_to_disk(part))) {
  474. *partno = part->partno;
  475. disk = part_to_disk(part);
  476. }
  477. mutex_unlock(&ext_devt_mutex);
  478. }
  479. return disk;
  480. }
  481. /**
  482. * bdget_disk - do bdget() by gendisk and partition number
  483. * @disk: gendisk of interest
  484. * @partno: partition number
  485. *
  486. * Find partition @partno from @disk, do bdget() on it.
  487. *
  488. * CONTEXT:
  489. * Don't care.
  490. *
  491. * RETURNS:
  492. * Resulting block_device on success, NULL on failure.
  493. */
  494. struct block_device *bdget_disk(struct gendisk *disk, int partno)
  495. {
  496. struct hd_struct *part;
  497. struct block_device *bdev = NULL;
  498. part = disk_get_part(disk, partno);
  499. if (part)
  500. bdev = bdget(part_devt(part));
  501. disk_put_part(part);
  502. return bdev;
  503. }
  504. EXPORT_SYMBOL(bdget_disk);
  505. /*
  506. * print a full list of all partitions - intended for places where the root
  507. * filesystem can't be mounted and thus to give the victim some idea of what
  508. * went wrong
  509. */
  510. void __init printk_all_partitions(void)
  511. {
  512. struct class_dev_iter iter;
  513. struct device *dev;
  514. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  515. while ((dev = class_dev_iter_next(&iter))) {
  516. struct gendisk *disk = dev_to_disk(dev);
  517. struct disk_part_iter piter;
  518. struct hd_struct *part;
  519. char name_buf[BDEVNAME_SIZE];
  520. char devt_buf[BDEVT_SIZE];
  521. /*
  522. * Don't show empty devices or things that have been
  523. * surpressed
  524. */
  525. if (get_capacity(disk) == 0 ||
  526. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  527. continue;
  528. /*
  529. * Note, unlike /proc/partitions, I am showing the
  530. * numbers in hex - the same format as the root=
  531. * option takes.
  532. */
  533. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  534. while ((part = disk_part_iter_next(&piter))) {
  535. bool is_part0 = part == &disk->part0;
  536. printk("%s%s %10llu %s", is_part0 ? "" : " ",
  537. bdevt_str(part_devt(part), devt_buf),
  538. (unsigned long long)part->nr_sects >> 1,
  539. disk_name(disk, part->partno, name_buf));
  540. if (is_part0) {
  541. if (disk->driverfs_dev != NULL &&
  542. disk->driverfs_dev->driver != NULL)
  543. printk(" driver: %s\n",
  544. disk->driverfs_dev->driver->name);
  545. else
  546. printk(" (driver?)\n");
  547. } else
  548. printk("\n");
  549. }
  550. disk_part_iter_exit(&piter);
  551. }
  552. class_dev_iter_exit(&iter);
  553. }
  554. #ifdef CONFIG_PROC_FS
  555. /* iterator */
  556. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  557. {
  558. loff_t skip = *pos;
  559. struct class_dev_iter *iter;
  560. struct device *dev;
  561. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  562. if (!iter)
  563. return ERR_PTR(-ENOMEM);
  564. seqf->private = iter;
  565. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  566. do {
  567. dev = class_dev_iter_next(iter);
  568. if (!dev)
  569. return NULL;
  570. } while (skip--);
  571. return dev_to_disk(dev);
  572. }
  573. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  574. {
  575. struct device *dev;
  576. (*pos)++;
  577. dev = class_dev_iter_next(seqf->private);
  578. if (dev)
  579. return dev_to_disk(dev);
  580. return NULL;
  581. }
  582. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  583. {
  584. struct class_dev_iter *iter = seqf->private;
  585. /* stop is called even after start failed :-( */
  586. if (iter) {
  587. class_dev_iter_exit(iter);
  588. kfree(iter);
  589. }
  590. }
  591. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  592. {
  593. static void *p;
  594. p = disk_seqf_start(seqf, pos);
  595. if (!IS_ERR(p) && p && !*pos)
  596. seq_puts(seqf, "major minor #blocks name\n\n");
  597. return p;
  598. }
  599. static int show_partition(struct seq_file *seqf, void *v)
  600. {
  601. struct gendisk *sgp = v;
  602. struct disk_part_iter piter;
  603. struct hd_struct *part;
  604. char buf[BDEVNAME_SIZE];
  605. /* Don't show non-partitionable removeable devices or empty devices */
  606. if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
  607. (sgp->flags & GENHD_FL_REMOVABLE)))
  608. return 0;
  609. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  610. return 0;
  611. /* show the full disk and all non-0 size partitions of it */
  612. disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
  613. while ((part = disk_part_iter_next(&piter)))
  614. seq_printf(seqf, "%4d %7d %10llu %s\n",
  615. MAJOR(part_devt(part)), MINOR(part_devt(part)),
  616. (unsigned long long)part->nr_sects >> 1,
  617. disk_name(sgp, part->partno, buf));
  618. disk_part_iter_exit(&piter);
  619. return 0;
  620. }
  621. const struct seq_operations partitions_op = {
  622. .start = show_partition_start,
  623. .next = disk_seqf_next,
  624. .stop = disk_seqf_stop,
  625. .show = show_partition
  626. };
  627. #endif
  628. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  629. {
  630. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  631. /* Make old-style 2.4 aliases work */
  632. request_module("block-major-%d", MAJOR(devt));
  633. return NULL;
  634. }
  635. static int __init genhd_device_init(void)
  636. {
  637. int error;
  638. block_class.dev_kobj = sysfs_dev_block_kobj;
  639. error = class_register(&block_class);
  640. if (unlikely(error))
  641. return error;
  642. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  643. blk_dev_init();
  644. #ifndef CONFIG_SYSFS_DEPRECATED
  645. /* create top-level block dir */
  646. block_depr = kobject_create_and_add("block", NULL);
  647. #endif
  648. return 0;
  649. }
  650. subsys_initcall(genhd_device_init);
  651. static ssize_t disk_range_show(struct device *dev,
  652. struct device_attribute *attr, char *buf)
  653. {
  654. struct gendisk *disk = dev_to_disk(dev);
  655. return sprintf(buf, "%d\n", disk->minors);
  656. }
  657. static ssize_t disk_ext_range_show(struct device *dev,
  658. struct device_attribute *attr, char *buf)
  659. {
  660. struct gendisk *disk = dev_to_disk(dev);
  661. return sprintf(buf, "%d\n", disk_max_parts(disk));
  662. }
  663. static ssize_t disk_removable_show(struct device *dev,
  664. struct device_attribute *attr, char *buf)
  665. {
  666. struct gendisk *disk = dev_to_disk(dev);
  667. return sprintf(buf, "%d\n",
  668. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  669. }
  670. static ssize_t disk_ro_show(struct device *dev,
  671. struct device_attribute *attr, char *buf)
  672. {
  673. struct gendisk *disk = dev_to_disk(dev);
  674. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  675. }
  676. static ssize_t disk_capability_show(struct device *dev,
  677. struct device_attribute *attr, char *buf)
  678. {
  679. struct gendisk *disk = dev_to_disk(dev);
  680. return sprintf(buf, "%x\n", disk->flags);
  681. }
  682. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  683. static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
  684. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  685. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  686. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  687. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  688. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  689. #ifdef CONFIG_FAIL_MAKE_REQUEST
  690. static struct device_attribute dev_attr_fail =
  691. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  692. #endif
  693. #ifdef CONFIG_FAIL_IO_TIMEOUT
  694. static struct device_attribute dev_attr_fail_timeout =
  695. __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
  696. part_timeout_store);
  697. #endif
  698. static struct attribute *disk_attrs[] = {
  699. &dev_attr_range.attr,
  700. &dev_attr_ext_range.attr,
  701. &dev_attr_removable.attr,
  702. &dev_attr_ro.attr,
  703. &dev_attr_size.attr,
  704. &dev_attr_capability.attr,
  705. &dev_attr_stat.attr,
  706. #ifdef CONFIG_FAIL_MAKE_REQUEST
  707. &dev_attr_fail.attr,
  708. #endif
  709. #ifdef CONFIG_FAIL_IO_TIMEOUT
  710. &dev_attr_fail_timeout.attr,
  711. #endif
  712. NULL
  713. };
  714. static struct attribute_group disk_attr_group = {
  715. .attrs = disk_attrs,
  716. };
  717. static struct attribute_group *disk_attr_groups[] = {
  718. &disk_attr_group,
  719. NULL
  720. };
  721. static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
  722. {
  723. struct disk_part_tbl *ptbl =
  724. container_of(head, struct disk_part_tbl, rcu_head);
  725. kfree(ptbl);
  726. }
  727. /**
  728. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  729. * @disk: disk to replace part_tbl for
  730. * @new_ptbl: new part_tbl to install
  731. *
  732. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  733. * original ptbl is freed using RCU callback.
  734. *
  735. * LOCKING:
  736. * Matching bd_mutx locked.
  737. */
  738. static void disk_replace_part_tbl(struct gendisk *disk,
  739. struct disk_part_tbl *new_ptbl)
  740. {
  741. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  742. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  743. if (old_ptbl)
  744. call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
  745. }
  746. /**
  747. * disk_expand_part_tbl - expand disk->part_tbl
  748. * @disk: disk to expand part_tbl for
  749. * @partno: expand such that this partno can fit in
  750. *
  751. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  752. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  753. *
  754. * LOCKING:
  755. * Matching bd_mutex locked, might sleep.
  756. *
  757. * RETURNS:
  758. * 0 on success, -errno on failure.
  759. */
  760. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  761. {
  762. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  763. struct disk_part_tbl *new_ptbl;
  764. int len = old_ptbl ? old_ptbl->len : 0;
  765. int target = partno + 1;
  766. size_t size;
  767. int i;
  768. /* disk_max_parts() is zero during initialization, ignore if so */
  769. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  770. return -EINVAL;
  771. if (target <= len)
  772. return 0;
  773. size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
  774. new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
  775. if (!new_ptbl)
  776. return -ENOMEM;
  777. INIT_RCU_HEAD(&new_ptbl->rcu_head);
  778. new_ptbl->len = target;
  779. for (i = 0; i < len; i++)
  780. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  781. disk_replace_part_tbl(disk, new_ptbl);
  782. return 0;
  783. }
  784. static void disk_release(struct device *dev)
  785. {
  786. struct gendisk *disk = dev_to_disk(dev);
  787. kfree(disk->random);
  788. disk_replace_part_tbl(disk, NULL);
  789. free_part_stats(&disk->part0);
  790. kfree(disk);
  791. }
  792. struct class block_class = {
  793. .name = "block",
  794. };
  795. static struct device_type disk_type = {
  796. .name = "disk",
  797. .groups = disk_attr_groups,
  798. .release = disk_release,
  799. };
  800. #ifdef CONFIG_PROC_FS
  801. /*
  802. * aggregate disk stat collector. Uses the same stats that the sysfs
  803. * entries do, above, but makes them available through one seq_file.
  804. *
  805. * The output looks suspiciously like /proc/partitions with a bunch of
  806. * extra fields.
  807. */
  808. static int diskstats_show(struct seq_file *seqf, void *v)
  809. {
  810. struct gendisk *gp = v;
  811. struct disk_part_iter piter;
  812. struct hd_struct *hd;
  813. char buf[BDEVNAME_SIZE];
  814. int cpu;
  815. /*
  816. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  817. seq_puts(seqf, "major minor name"
  818. " rio rmerge rsect ruse wio wmerge "
  819. "wsect wuse running use aveq"
  820. "\n\n");
  821. */
  822. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
  823. while ((hd = disk_part_iter_next(&piter))) {
  824. cpu = part_stat_lock();
  825. part_round_stats(cpu, hd);
  826. part_stat_unlock();
  827. seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
  828. "%u %lu %lu %llu %u %u %u %u\n",
  829. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  830. disk_name(gp, hd->partno, buf),
  831. part_stat_read(hd, ios[0]),
  832. part_stat_read(hd, merges[0]),
  833. (unsigned long long)part_stat_read(hd, sectors[0]),
  834. jiffies_to_msecs(part_stat_read(hd, ticks[0])),
  835. part_stat_read(hd, ios[1]),
  836. part_stat_read(hd, merges[1]),
  837. (unsigned long long)part_stat_read(hd, sectors[1]),
  838. jiffies_to_msecs(part_stat_read(hd, ticks[1])),
  839. hd->in_flight,
  840. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  841. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  842. );
  843. }
  844. disk_part_iter_exit(&piter);
  845. return 0;
  846. }
  847. const struct seq_operations diskstats_op = {
  848. .start = disk_seqf_start,
  849. .next = disk_seqf_next,
  850. .stop = disk_seqf_stop,
  851. .show = diskstats_show
  852. };
  853. #endif /* CONFIG_PROC_FS */
  854. static void media_change_notify_thread(struct work_struct *work)
  855. {
  856. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  857. char event[] = "MEDIA_CHANGE=1";
  858. char *envp[] = { event, NULL };
  859. /*
  860. * set enviroment vars to indicate which event this is for
  861. * so that user space will know to go check the media status.
  862. */
  863. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  864. put_device(gd->driverfs_dev);
  865. }
  866. #if 0
  867. void genhd_media_change_notify(struct gendisk *disk)
  868. {
  869. get_device(disk->driverfs_dev);
  870. schedule_work(&disk->async_notify);
  871. }
  872. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  873. #endif /* 0 */
  874. dev_t blk_lookup_devt(const char *name, int partno)
  875. {
  876. dev_t devt = MKDEV(0, 0);
  877. struct class_dev_iter iter;
  878. struct device *dev;
  879. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  880. while ((dev = class_dev_iter_next(&iter))) {
  881. struct gendisk *disk = dev_to_disk(dev);
  882. struct hd_struct *part;
  883. if (strcmp(dev->bus_id, name))
  884. continue;
  885. part = disk_get_part(disk, partno);
  886. if (part) {
  887. devt = part_devt(part);
  888. disk_put_part(part);
  889. break;
  890. }
  891. disk_put_part(part);
  892. }
  893. class_dev_iter_exit(&iter);
  894. return devt;
  895. }
  896. EXPORT_SYMBOL(blk_lookup_devt);
  897. struct gendisk *alloc_disk(int minors)
  898. {
  899. return alloc_disk_node(minors, -1);
  900. }
  901. EXPORT_SYMBOL(alloc_disk);
  902. struct gendisk *alloc_disk_node(int minors, int node_id)
  903. {
  904. struct gendisk *disk;
  905. disk = kmalloc_node(sizeof(struct gendisk),
  906. GFP_KERNEL | __GFP_ZERO, node_id);
  907. if (disk) {
  908. if (!init_part_stats(&disk->part0)) {
  909. kfree(disk);
  910. return NULL;
  911. }
  912. if (disk_expand_part_tbl(disk, 0)) {
  913. free_part_stats(&disk->part0);
  914. kfree(disk);
  915. return NULL;
  916. }
  917. disk->part_tbl->part[0] = &disk->part0;
  918. disk->minors = minors;
  919. rand_initialize_disk(disk);
  920. disk_to_dev(disk)->class = &block_class;
  921. disk_to_dev(disk)->type = &disk_type;
  922. device_initialize(disk_to_dev(disk));
  923. INIT_WORK(&disk->async_notify,
  924. media_change_notify_thread);
  925. disk->node_id = node_id;
  926. }
  927. return disk;
  928. }
  929. EXPORT_SYMBOL(alloc_disk_node);
  930. struct kobject *get_disk(struct gendisk *disk)
  931. {
  932. struct module *owner;
  933. struct kobject *kobj;
  934. if (!disk->fops)
  935. return NULL;
  936. owner = disk->fops->owner;
  937. if (owner && !try_module_get(owner))
  938. return NULL;
  939. kobj = kobject_get(&disk_to_dev(disk)->kobj);
  940. if (kobj == NULL) {
  941. module_put(owner);
  942. return NULL;
  943. }
  944. return kobj;
  945. }
  946. EXPORT_SYMBOL(get_disk);
  947. void put_disk(struct gendisk *disk)
  948. {
  949. if (disk)
  950. kobject_put(&disk_to_dev(disk)->kobj);
  951. }
  952. EXPORT_SYMBOL(put_disk);
  953. void set_device_ro(struct block_device *bdev, int flag)
  954. {
  955. bdev->bd_part->policy = flag;
  956. }
  957. EXPORT_SYMBOL(set_device_ro);
  958. void set_disk_ro(struct gendisk *disk, int flag)
  959. {
  960. struct disk_part_iter piter;
  961. struct hd_struct *part;
  962. disk_part_iter_init(&piter, disk,
  963. DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
  964. while ((part = disk_part_iter_next(&piter)))
  965. part->policy = flag;
  966. disk_part_iter_exit(&piter);
  967. }
  968. EXPORT_SYMBOL(set_disk_ro);
  969. int bdev_read_only(struct block_device *bdev)
  970. {
  971. if (!bdev)
  972. return 0;
  973. return bdev->bd_part->policy;
  974. }
  975. EXPORT_SYMBOL(bdev_read_only);
  976. int invalidate_partition(struct gendisk *disk, int partno)
  977. {
  978. int res = 0;
  979. struct block_device *bdev = bdget_disk(disk, partno);
  980. if (bdev) {
  981. fsync_bdev(bdev);
  982. res = __invalidate_device(bdev);
  983. bdput(bdev);
  984. }
  985. return res;
  986. }
  987. EXPORT_SYMBOL(invalidate_partition);