genhd.c 30 KB

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