genhd.c 28 KB

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