genhd.c 31 KB

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