genhd.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. * @gfp_mask: memory allocation flag
  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. * @part: 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. 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. #endif
  629. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  630. {
  631. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  632. /* Make old-style 2.4 aliases work */
  633. request_module("block-major-%d", MAJOR(devt));
  634. return NULL;
  635. }
  636. static int __init genhd_device_init(void)
  637. {
  638. int error;
  639. block_class.dev_kobj = sysfs_dev_block_kobj;
  640. error = class_register(&block_class);
  641. if (unlikely(error))
  642. return error;
  643. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  644. blk_dev_init();
  645. #ifndef CONFIG_SYSFS_DEPRECATED
  646. /* create top-level block dir */
  647. block_depr = kobject_create_and_add("block", NULL);
  648. #endif
  649. return 0;
  650. }
  651. subsys_initcall(genhd_device_init);
  652. static ssize_t disk_range_show(struct device *dev,
  653. struct device_attribute *attr, char *buf)
  654. {
  655. struct gendisk *disk = dev_to_disk(dev);
  656. return sprintf(buf, "%d\n", disk->minors);
  657. }
  658. static ssize_t disk_ext_range_show(struct device *dev,
  659. struct device_attribute *attr, char *buf)
  660. {
  661. struct gendisk *disk = dev_to_disk(dev);
  662. return sprintf(buf, "%d\n", disk_max_parts(disk));
  663. }
  664. static ssize_t disk_removable_show(struct device *dev,
  665. struct device_attribute *attr, char *buf)
  666. {
  667. struct gendisk *disk = dev_to_disk(dev);
  668. return sprintf(buf, "%d\n",
  669. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  670. }
  671. static ssize_t disk_ro_show(struct device *dev,
  672. struct device_attribute *attr, char *buf)
  673. {
  674. struct gendisk *disk = dev_to_disk(dev);
  675. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  676. }
  677. static ssize_t disk_capability_show(struct device *dev,
  678. struct device_attribute *attr, char *buf)
  679. {
  680. struct gendisk *disk = dev_to_disk(dev);
  681. return sprintf(buf, "%x\n", disk->flags);
  682. }
  683. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  684. static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
  685. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  686. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  687. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  688. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  689. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  690. #ifdef CONFIG_FAIL_MAKE_REQUEST
  691. static struct device_attribute dev_attr_fail =
  692. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  693. #endif
  694. #ifdef CONFIG_FAIL_IO_TIMEOUT
  695. static struct device_attribute dev_attr_fail_timeout =
  696. __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
  697. part_timeout_store);
  698. #endif
  699. static struct attribute *disk_attrs[] = {
  700. &dev_attr_range.attr,
  701. &dev_attr_ext_range.attr,
  702. &dev_attr_removable.attr,
  703. &dev_attr_ro.attr,
  704. &dev_attr_size.attr,
  705. &dev_attr_capability.attr,
  706. &dev_attr_stat.attr,
  707. #ifdef CONFIG_FAIL_MAKE_REQUEST
  708. &dev_attr_fail.attr,
  709. #endif
  710. #ifdef CONFIG_FAIL_IO_TIMEOUT
  711. &dev_attr_fail_timeout.attr,
  712. #endif
  713. NULL
  714. };
  715. static struct attribute_group disk_attr_group = {
  716. .attrs = disk_attrs,
  717. };
  718. static struct attribute_group *disk_attr_groups[] = {
  719. &disk_attr_group,
  720. NULL
  721. };
  722. static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
  723. {
  724. struct disk_part_tbl *ptbl =
  725. container_of(head, struct disk_part_tbl, rcu_head);
  726. kfree(ptbl);
  727. }
  728. /**
  729. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  730. * @disk: disk to replace part_tbl for
  731. * @new_ptbl: new part_tbl to install
  732. *
  733. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  734. * original ptbl is freed using RCU callback.
  735. *
  736. * LOCKING:
  737. * Matching bd_mutx locked.
  738. */
  739. static void disk_replace_part_tbl(struct gendisk *disk,
  740. struct disk_part_tbl *new_ptbl)
  741. {
  742. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  743. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  744. if (old_ptbl)
  745. call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
  746. }
  747. /**
  748. * disk_expand_part_tbl - expand disk->part_tbl
  749. * @disk: disk to expand part_tbl for
  750. * @partno: expand such that this partno can fit in
  751. *
  752. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  753. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  754. *
  755. * LOCKING:
  756. * Matching bd_mutex locked, might sleep.
  757. *
  758. * RETURNS:
  759. * 0 on success, -errno on failure.
  760. */
  761. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  762. {
  763. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  764. struct disk_part_tbl *new_ptbl;
  765. int len = old_ptbl ? old_ptbl->len : 0;
  766. int target = partno + 1;
  767. size_t size;
  768. int i;
  769. /* disk_max_parts() is zero during initialization, ignore if so */
  770. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  771. return -EINVAL;
  772. if (target <= len)
  773. return 0;
  774. size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
  775. new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
  776. if (!new_ptbl)
  777. return -ENOMEM;
  778. INIT_RCU_HEAD(&new_ptbl->rcu_head);
  779. new_ptbl->len = target;
  780. for (i = 0; i < len; i++)
  781. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  782. disk_replace_part_tbl(disk, new_ptbl);
  783. return 0;
  784. }
  785. static void disk_release(struct device *dev)
  786. {
  787. struct gendisk *disk = dev_to_disk(dev);
  788. kfree(disk->random);
  789. disk_replace_part_tbl(disk, NULL);
  790. free_part_stats(&disk->part0);
  791. kfree(disk);
  792. }
  793. struct class block_class = {
  794. .name = "block",
  795. };
  796. static struct device_type disk_type = {
  797. .name = "disk",
  798. .groups = disk_attr_groups,
  799. .release = disk_release,
  800. };
  801. #ifdef CONFIG_PROC_FS
  802. /*
  803. * aggregate disk stat collector. Uses the same stats that the sysfs
  804. * entries do, above, but makes them available through one seq_file.
  805. *
  806. * The output looks suspiciously like /proc/partitions with a bunch of
  807. * extra fields.
  808. */
  809. static int diskstats_show(struct seq_file *seqf, void *v)
  810. {
  811. struct gendisk *gp = v;
  812. struct disk_part_iter piter;
  813. struct hd_struct *hd;
  814. char buf[BDEVNAME_SIZE];
  815. int cpu;
  816. /*
  817. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  818. seq_puts(seqf, "major minor name"
  819. " rio rmerge rsect ruse wio wmerge "
  820. "wsect wuse running use aveq"
  821. "\n\n");
  822. */
  823. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
  824. while ((hd = disk_part_iter_next(&piter))) {
  825. cpu = part_stat_lock();
  826. part_round_stats(cpu, hd);
  827. part_stat_unlock();
  828. seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
  829. "%u %lu %lu %llu %u %u %u %u\n",
  830. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  831. disk_name(gp, hd->partno, buf),
  832. part_stat_read(hd, ios[0]),
  833. part_stat_read(hd, merges[0]),
  834. (unsigned long long)part_stat_read(hd, sectors[0]),
  835. jiffies_to_msecs(part_stat_read(hd, ticks[0])),
  836. part_stat_read(hd, ios[1]),
  837. part_stat_read(hd, merges[1]),
  838. (unsigned long long)part_stat_read(hd, sectors[1]),
  839. jiffies_to_msecs(part_stat_read(hd, ticks[1])),
  840. hd->in_flight,
  841. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  842. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  843. );
  844. }
  845. disk_part_iter_exit(&piter);
  846. return 0;
  847. }
  848. const struct seq_operations diskstats_op = {
  849. .start = disk_seqf_start,
  850. .next = disk_seqf_next,
  851. .stop = disk_seqf_stop,
  852. .show = diskstats_show
  853. };
  854. #endif /* CONFIG_PROC_FS */
  855. static void media_change_notify_thread(struct work_struct *work)
  856. {
  857. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  858. char event[] = "MEDIA_CHANGE=1";
  859. char *envp[] = { event, NULL };
  860. /*
  861. * set enviroment vars to indicate which event this is for
  862. * so that user space will know to go check the media status.
  863. */
  864. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  865. put_device(gd->driverfs_dev);
  866. }
  867. #if 0
  868. void genhd_media_change_notify(struct gendisk *disk)
  869. {
  870. get_device(disk->driverfs_dev);
  871. schedule_work(&disk->async_notify);
  872. }
  873. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  874. #endif /* 0 */
  875. dev_t blk_lookup_devt(const char *name, int partno)
  876. {
  877. dev_t devt = MKDEV(0, 0);
  878. struct class_dev_iter iter;
  879. struct device *dev;
  880. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  881. while ((dev = class_dev_iter_next(&iter))) {
  882. struct gendisk *disk = dev_to_disk(dev);
  883. struct hd_struct *part;
  884. if (strcmp(dev->bus_id, name))
  885. continue;
  886. part = disk_get_part(disk, partno);
  887. if (part) {
  888. devt = part_devt(part);
  889. disk_put_part(part);
  890. break;
  891. }
  892. disk_put_part(part);
  893. }
  894. class_dev_iter_exit(&iter);
  895. return devt;
  896. }
  897. EXPORT_SYMBOL(blk_lookup_devt);
  898. struct gendisk *alloc_disk(int minors)
  899. {
  900. return alloc_disk_node(minors, -1);
  901. }
  902. EXPORT_SYMBOL(alloc_disk);
  903. struct gendisk *alloc_disk_node(int minors, int node_id)
  904. {
  905. struct gendisk *disk;
  906. disk = kmalloc_node(sizeof(struct gendisk),
  907. GFP_KERNEL | __GFP_ZERO, node_id);
  908. if (disk) {
  909. if (!init_part_stats(&disk->part0)) {
  910. kfree(disk);
  911. return NULL;
  912. }
  913. if (disk_expand_part_tbl(disk, 0)) {
  914. free_part_stats(&disk->part0);
  915. kfree(disk);
  916. return NULL;
  917. }
  918. disk->part_tbl->part[0] = &disk->part0;
  919. disk->minors = minors;
  920. rand_initialize_disk(disk);
  921. disk_to_dev(disk)->class = &block_class;
  922. disk_to_dev(disk)->type = &disk_type;
  923. device_initialize(disk_to_dev(disk));
  924. INIT_WORK(&disk->async_notify,
  925. media_change_notify_thread);
  926. disk->node_id = node_id;
  927. }
  928. return disk;
  929. }
  930. EXPORT_SYMBOL(alloc_disk_node);
  931. struct kobject *get_disk(struct gendisk *disk)
  932. {
  933. struct module *owner;
  934. struct kobject *kobj;
  935. if (!disk->fops)
  936. return NULL;
  937. owner = disk->fops->owner;
  938. if (owner && !try_module_get(owner))
  939. return NULL;
  940. kobj = kobject_get(&disk_to_dev(disk)->kobj);
  941. if (kobj == NULL) {
  942. module_put(owner);
  943. return NULL;
  944. }
  945. return kobj;
  946. }
  947. EXPORT_SYMBOL(get_disk);
  948. void put_disk(struct gendisk *disk)
  949. {
  950. if (disk)
  951. kobject_put(&disk_to_dev(disk)->kobj);
  952. }
  953. EXPORT_SYMBOL(put_disk);
  954. void set_device_ro(struct block_device *bdev, int flag)
  955. {
  956. bdev->bd_part->policy = flag;
  957. }
  958. EXPORT_SYMBOL(set_device_ro);
  959. void set_disk_ro(struct gendisk *disk, int flag)
  960. {
  961. struct disk_part_iter piter;
  962. struct hd_struct *part;
  963. disk_part_iter_init(&piter, disk,
  964. DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
  965. while ((part = disk_part_iter_next(&piter)))
  966. part->policy = flag;
  967. disk_part_iter_exit(&piter);
  968. }
  969. EXPORT_SYMBOL(set_disk_ro);
  970. int bdev_read_only(struct block_device *bdev)
  971. {
  972. if (!bdev)
  973. return 0;
  974. return bdev->bd_part->policy;
  975. }
  976. EXPORT_SYMBOL(bdev_read_only);
  977. int invalidate_partition(struct gendisk *disk, int partno)
  978. {
  979. int res = 0;
  980. struct block_device *bdev = bdget_disk(disk, partno);
  981. if (bdev) {
  982. fsync_bdev(bdev);
  983. res = __invalidate_device(bdev);
  984. bdput(bdev);
  985. }
  986. return res;
  987. }
  988. EXPORT_SYMBOL(invalidate_partition);