genhd.c 27 KB

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