genhd.c 27 KB

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