genhd.c 30 KB

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