genhd.c 28 KB

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