genhd.c 30 KB

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