genhd.c 27 KB

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