genhd.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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 <linux/log2.h>
  21. #include <linux/ctype.h>
  22. #include "blk.h"
  23. static DEFINE_MUTEX(block_class_lock);
  24. struct kobject *block_depr;
  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. static void disk_add_events(struct gendisk *disk);
  34. static void disk_del_events(struct gendisk *disk);
  35. static void disk_release_events(struct gendisk *disk);
  36. /**
  37. * disk_get_part - get partition
  38. * @disk: disk to look partition from
  39. * @partno: partition number
  40. *
  41. * Look for partition @partno from @disk. If found, increment
  42. * reference count and return it.
  43. *
  44. * CONTEXT:
  45. * Don't care.
  46. *
  47. * RETURNS:
  48. * Pointer to the found partition on success, NULL if not found.
  49. */
  50. struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
  51. {
  52. struct hd_struct *part = NULL;
  53. struct disk_part_tbl *ptbl;
  54. if (unlikely(partno < 0))
  55. return NULL;
  56. rcu_read_lock();
  57. ptbl = rcu_dereference(disk->part_tbl);
  58. if (likely(partno < ptbl->len)) {
  59. part = rcu_dereference(ptbl->part[partno]);
  60. if (part)
  61. get_device(part_to_dev(part));
  62. }
  63. rcu_read_unlock();
  64. return part;
  65. }
  66. EXPORT_SYMBOL_GPL(disk_get_part);
  67. /**
  68. * disk_part_iter_init - initialize partition iterator
  69. * @piter: iterator to initialize
  70. * @disk: disk to iterate over
  71. * @flags: DISK_PITER_* flags
  72. *
  73. * Initialize @piter so that it iterates over partitions of @disk.
  74. *
  75. * CONTEXT:
  76. * Don't care.
  77. */
  78. void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
  79. unsigned int flags)
  80. {
  81. struct disk_part_tbl *ptbl;
  82. rcu_read_lock();
  83. ptbl = rcu_dereference(disk->part_tbl);
  84. piter->disk = disk;
  85. piter->part = NULL;
  86. if (flags & DISK_PITER_REVERSE)
  87. piter->idx = ptbl->len - 1;
  88. else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
  89. piter->idx = 0;
  90. else
  91. piter->idx = 1;
  92. piter->flags = flags;
  93. rcu_read_unlock();
  94. }
  95. EXPORT_SYMBOL_GPL(disk_part_iter_init);
  96. /**
  97. * disk_part_iter_next - proceed iterator to the next partition and return it
  98. * @piter: iterator of interest
  99. *
  100. * Proceed @piter to the next partition and return it.
  101. *
  102. * CONTEXT:
  103. * Don't care.
  104. */
  105. struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
  106. {
  107. struct disk_part_tbl *ptbl;
  108. int inc, end;
  109. /* put the last partition */
  110. disk_put_part(piter->part);
  111. piter->part = NULL;
  112. /* get part_tbl */
  113. rcu_read_lock();
  114. ptbl = rcu_dereference(piter->disk->part_tbl);
  115. /* determine iteration parameters */
  116. if (piter->flags & DISK_PITER_REVERSE) {
  117. inc = -1;
  118. if (piter->flags & (DISK_PITER_INCL_PART0 |
  119. DISK_PITER_INCL_EMPTY_PART0))
  120. end = -1;
  121. else
  122. end = 0;
  123. } else {
  124. inc = 1;
  125. end = ptbl->len;
  126. }
  127. /* iterate to the next partition */
  128. for (; piter->idx != end; piter->idx += inc) {
  129. struct hd_struct *part;
  130. part = rcu_dereference(ptbl->part[piter->idx]);
  131. if (!part)
  132. continue;
  133. if (!part->nr_sects &&
  134. !(piter->flags & DISK_PITER_INCL_EMPTY) &&
  135. !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
  136. piter->idx == 0))
  137. continue;
  138. get_device(part_to_dev(part));
  139. piter->part = part;
  140. piter->idx += inc;
  141. break;
  142. }
  143. rcu_read_unlock();
  144. return piter->part;
  145. }
  146. EXPORT_SYMBOL_GPL(disk_part_iter_next);
  147. /**
  148. * disk_part_iter_exit - finish up partition iteration
  149. * @piter: iter of interest
  150. *
  151. * Called when iteration is over. Cleans up @piter.
  152. *
  153. * CONTEXT:
  154. * Don't care.
  155. */
  156. void disk_part_iter_exit(struct disk_part_iter *piter)
  157. {
  158. disk_put_part(piter->part);
  159. piter->part = NULL;
  160. }
  161. EXPORT_SYMBOL_GPL(disk_part_iter_exit);
  162. static inline int sector_in_part(struct hd_struct *part, sector_t sector)
  163. {
  164. return part->start_sect <= sector &&
  165. sector < part->start_sect + part->nr_sects;
  166. }
  167. /**
  168. * disk_map_sector_rcu - map sector to partition
  169. * @disk: gendisk of interest
  170. * @sector: sector to map
  171. *
  172. * Find out which partition @sector maps to on @disk. This is
  173. * primarily used for stats accounting.
  174. *
  175. * CONTEXT:
  176. * RCU read locked. The returned partition pointer is valid only
  177. * while preemption is disabled.
  178. *
  179. * RETURNS:
  180. * Found partition on success, part0 is returned if no partition matches
  181. */
  182. struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
  183. {
  184. struct disk_part_tbl *ptbl;
  185. struct hd_struct *part;
  186. int i;
  187. ptbl = rcu_dereference(disk->part_tbl);
  188. part = rcu_dereference(ptbl->last_lookup);
  189. if (part && sector_in_part(part, sector))
  190. return part;
  191. for (i = 1; i < ptbl->len; i++) {
  192. part = rcu_dereference(ptbl->part[i]);
  193. if (part && sector_in_part(part, sector)) {
  194. rcu_assign_pointer(ptbl->last_lookup, part);
  195. return part;
  196. }
  197. }
  198. return &disk->part0;
  199. }
  200. EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
  201. /*
  202. * Can be deleted altogether. Later.
  203. *
  204. */
  205. static struct blk_major_name {
  206. struct blk_major_name *next;
  207. int major;
  208. char name[16];
  209. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  210. /* index in the above - for now: assume no multimajor ranges */
  211. static inline int major_to_index(unsigned major)
  212. {
  213. return major % BLKDEV_MAJOR_HASH_SIZE;
  214. }
  215. #ifdef CONFIG_PROC_FS
  216. void blkdev_show(struct seq_file *seqf, off_t offset)
  217. {
  218. struct blk_major_name *dp;
  219. if (offset < BLKDEV_MAJOR_HASH_SIZE) {
  220. mutex_lock(&block_class_lock);
  221. for (dp = major_names[offset]; dp; dp = dp->next)
  222. seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
  223. mutex_unlock(&block_class_lock);
  224. }
  225. }
  226. #endif /* CONFIG_PROC_FS */
  227. /**
  228. * register_blkdev - register a new block device
  229. *
  230. * @major: the requested major device number [1..255]. If @major=0, try to
  231. * allocate any unused major number.
  232. * @name: the name of the new block device as a zero terminated string
  233. *
  234. * The @name must be unique within the system.
  235. *
  236. * The return value depends on the @major input parameter.
  237. * - if a major device number was requested in range [1..255] then the
  238. * function returns zero on success, or a negative error code
  239. * - if any unused major number was requested with @major=0 parameter
  240. * then the return value is the allocated major number in range
  241. * [1..255] or a negative error code otherwise
  242. */
  243. int register_blkdev(unsigned int major, const char *name)
  244. {
  245. struct blk_major_name **n, *p;
  246. int index, ret = 0;
  247. mutex_lock(&block_class_lock);
  248. /* temporary */
  249. if (major == 0) {
  250. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  251. if (major_names[index] == NULL)
  252. break;
  253. }
  254. if (index == 0) {
  255. printk("register_blkdev: failed to get major for %s\n",
  256. name);
  257. ret = -EBUSY;
  258. goto out;
  259. }
  260. major = index;
  261. ret = major;
  262. }
  263. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  264. if (p == NULL) {
  265. ret = -ENOMEM;
  266. goto out;
  267. }
  268. p->major = major;
  269. strlcpy(p->name, name, sizeof(p->name));
  270. p->next = NULL;
  271. index = major_to_index(major);
  272. for (n = &major_names[index]; *n; n = &(*n)->next) {
  273. if ((*n)->major == major)
  274. break;
  275. }
  276. if (!*n)
  277. *n = p;
  278. else
  279. ret = -EBUSY;
  280. if (ret < 0) {
  281. printk("register_blkdev: cannot get major %d for %s\n",
  282. major, name);
  283. kfree(p);
  284. }
  285. out:
  286. mutex_unlock(&block_class_lock);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(register_blkdev);
  290. void unregister_blkdev(unsigned int major, const char *name)
  291. {
  292. struct blk_major_name **n;
  293. struct blk_major_name *p = NULL;
  294. int index = major_to_index(major);
  295. mutex_lock(&block_class_lock);
  296. for (n = &major_names[index]; *n; n = &(*n)->next)
  297. if ((*n)->major == major)
  298. break;
  299. if (!*n || strcmp((*n)->name, name)) {
  300. WARN_ON(1);
  301. } else {
  302. p = *n;
  303. *n = p->next;
  304. }
  305. mutex_unlock(&block_class_lock);
  306. kfree(p);
  307. }
  308. EXPORT_SYMBOL(unregister_blkdev);
  309. static struct kobj_map *bdev_map;
  310. /**
  311. * blk_mangle_minor - scatter minor numbers apart
  312. * @minor: minor number to mangle
  313. *
  314. * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
  315. * is enabled. Mangling twice gives the original value.
  316. *
  317. * RETURNS:
  318. * Mangled value.
  319. *
  320. * CONTEXT:
  321. * Don't care.
  322. */
  323. static int blk_mangle_minor(int minor)
  324. {
  325. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  326. int i;
  327. for (i = 0; i < MINORBITS / 2; i++) {
  328. int low = minor & (1 << i);
  329. int high = minor & (1 << (MINORBITS - 1 - i));
  330. int distance = MINORBITS - 1 - 2 * i;
  331. minor ^= low | high; /* clear both bits */
  332. low <<= distance; /* swap the positions */
  333. high >>= distance;
  334. minor |= low | high; /* and set */
  335. }
  336. #endif
  337. return minor;
  338. }
  339. /**
  340. * blk_alloc_devt - allocate a dev_t for a partition
  341. * @part: partition to allocate dev_t for
  342. * @devt: out parameter for resulting dev_t
  343. *
  344. * Allocate a dev_t for block device.
  345. *
  346. * RETURNS:
  347. * 0 on success, allocated dev_t is returned in *@devt. -errno on
  348. * failure.
  349. *
  350. * CONTEXT:
  351. * Might sleep.
  352. */
  353. int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
  354. {
  355. struct gendisk *disk = part_to_disk(part);
  356. int idx, rc;
  357. /* in consecutive minor range? */
  358. if (part->partno < disk->minors) {
  359. *devt = MKDEV(disk->major, disk->first_minor + part->partno);
  360. return 0;
  361. }
  362. /* allocate ext devt */
  363. do {
  364. if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
  365. return -ENOMEM;
  366. rc = idr_get_new(&ext_devt_idr, part, &idx);
  367. } while (rc == -EAGAIN);
  368. if (rc)
  369. return rc;
  370. if (idx > MAX_EXT_DEVT) {
  371. idr_remove(&ext_devt_idr, idx);
  372. return -EBUSY;
  373. }
  374. *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
  375. return 0;
  376. }
  377. /**
  378. * blk_free_devt - free a dev_t
  379. * @devt: dev_t to free
  380. *
  381. * Free @devt which was allocated using blk_alloc_devt().
  382. *
  383. * CONTEXT:
  384. * Might sleep.
  385. */
  386. void blk_free_devt(dev_t devt)
  387. {
  388. might_sleep();
  389. if (devt == MKDEV(0, 0))
  390. return;
  391. if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
  392. mutex_lock(&ext_devt_mutex);
  393. idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  394. mutex_unlock(&ext_devt_mutex);
  395. }
  396. }
  397. static char *bdevt_str(dev_t devt, char *buf)
  398. {
  399. if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
  400. char tbuf[BDEVT_SIZE];
  401. snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
  402. snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
  403. } else
  404. snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
  405. return buf;
  406. }
  407. /*
  408. * Register device numbers dev..(dev+range-1)
  409. * range must be nonzero
  410. * The hash chain is sorted on range, so that subranges can override.
  411. */
  412. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  413. struct kobject *(*probe)(dev_t, int *, void *),
  414. int (*lock)(dev_t, void *), void *data)
  415. {
  416. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  417. }
  418. EXPORT_SYMBOL(blk_register_region);
  419. void blk_unregister_region(dev_t devt, unsigned long range)
  420. {
  421. kobj_unmap(bdev_map, devt, range);
  422. }
  423. EXPORT_SYMBOL(blk_unregister_region);
  424. static struct kobject *exact_match(dev_t devt, int *partno, void *data)
  425. {
  426. struct gendisk *p = data;
  427. return &disk_to_dev(p)->kobj;
  428. }
  429. static int exact_lock(dev_t devt, void *data)
  430. {
  431. struct gendisk *p = data;
  432. if (!get_disk(p))
  433. return -1;
  434. return 0;
  435. }
  436. void register_disk(struct gendisk *disk)
  437. {
  438. struct device *ddev = disk_to_dev(disk);
  439. struct block_device *bdev;
  440. struct disk_part_iter piter;
  441. struct hd_struct *part;
  442. int err;
  443. ddev->parent = disk->driverfs_dev;
  444. dev_set_name(ddev, disk->disk_name);
  445. /* delay uevents, until we scanned partition table */
  446. dev_set_uevent_suppress(ddev, 1);
  447. if (device_add(ddev))
  448. return;
  449. if (!sysfs_deprecated) {
  450. err = sysfs_create_link(block_depr, &ddev->kobj,
  451. kobject_name(&ddev->kobj));
  452. if (err) {
  453. device_del(ddev);
  454. return;
  455. }
  456. }
  457. disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
  458. disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
  459. /* No minors to use for partitions */
  460. if (!disk_part_scan_enabled(disk))
  461. goto exit;
  462. /* No such device (e.g., media were just removed) */
  463. if (!get_capacity(disk))
  464. goto exit;
  465. bdev = bdget_disk(disk, 0);
  466. if (!bdev)
  467. goto exit;
  468. bdev->bd_invalidated = 1;
  469. err = blkdev_get(bdev, FMODE_READ, NULL);
  470. if (err < 0)
  471. goto exit;
  472. blkdev_put(bdev, FMODE_READ);
  473. exit:
  474. /* announce disk after possible partitions are created */
  475. dev_set_uevent_suppress(ddev, 0);
  476. kobject_uevent(&ddev->kobj, KOBJ_ADD);
  477. /* announce possible partitions */
  478. disk_part_iter_init(&piter, disk, 0);
  479. while ((part = disk_part_iter_next(&piter)))
  480. kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
  481. disk_part_iter_exit(&piter);
  482. }
  483. /**
  484. * add_disk - add partitioning information to kernel list
  485. * @disk: per-device partitioning information
  486. *
  487. * This function registers the partitioning information in @disk
  488. * with the kernel.
  489. *
  490. * FIXME: error handling
  491. */
  492. void add_disk(struct gendisk *disk)
  493. {
  494. struct backing_dev_info *bdi;
  495. dev_t devt;
  496. int retval;
  497. /* minors == 0 indicates to use ext devt from part0 and should
  498. * be accompanied with EXT_DEVT flag. Make sure all
  499. * parameters make sense.
  500. */
  501. WARN_ON(disk->minors && !(disk->major || disk->first_minor));
  502. WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
  503. disk->flags |= GENHD_FL_UP;
  504. retval = blk_alloc_devt(&disk->part0, &devt);
  505. if (retval) {
  506. WARN_ON(1);
  507. return;
  508. }
  509. disk_to_dev(disk)->devt = devt;
  510. /* ->major and ->first_minor aren't supposed to be
  511. * dereferenced from here on, but set them just in case.
  512. */
  513. disk->major = MAJOR(devt);
  514. disk->first_minor = MINOR(devt);
  515. /* Register BDI before referencing it from bdev */
  516. bdi = &disk->queue->backing_dev_info;
  517. bdi_register_dev(bdi, disk_devt(disk));
  518. blk_register_region(disk_devt(disk), disk->minors, NULL,
  519. exact_match, exact_lock, disk);
  520. register_disk(disk);
  521. blk_register_queue(disk);
  522. /*
  523. * Take an extra ref on queue which will be put on disk_release()
  524. * so that it sticks around as long as @disk is there.
  525. */
  526. WARN_ON_ONCE(blk_get_queue(disk->queue));
  527. retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
  528. "bdi");
  529. WARN_ON(retval);
  530. disk_add_events(disk);
  531. }
  532. EXPORT_SYMBOL(add_disk);
  533. void del_gendisk(struct gendisk *disk)
  534. {
  535. struct disk_part_iter piter;
  536. struct hd_struct *part;
  537. disk_del_events(disk);
  538. /* invalidate stuff */
  539. disk_part_iter_init(&piter, disk,
  540. DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
  541. while ((part = disk_part_iter_next(&piter))) {
  542. invalidate_partition(disk, part->partno);
  543. delete_partition(disk, part->partno);
  544. }
  545. disk_part_iter_exit(&piter);
  546. invalidate_partition(disk, 0);
  547. blk_free_devt(disk_to_dev(disk)->devt);
  548. set_capacity(disk, 0);
  549. disk->flags &= ~GENHD_FL_UP;
  550. sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
  551. bdi_unregister(&disk->queue->backing_dev_info);
  552. blk_unregister_queue(disk);
  553. blk_unregister_region(disk_devt(disk), disk->minors);
  554. part_stat_set_all(&disk->part0, 0);
  555. disk->part0.stamp = 0;
  556. kobject_put(disk->part0.holder_dir);
  557. kobject_put(disk->slave_dir);
  558. disk->driverfs_dev = NULL;
  559. if (!sysfs_deprecated)
  560. sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
  561. device_del(disk_to_dev(disk));
  562. }
  563. EXPORT_SYMBOL(del_gendisk);
  564. /**
  565. * get_gendisk - get partitioning information for a given device
  566. * @devt: device to get partitioning information for
  567. * @partno: returned partition index
  568. *
  569. * This function gets the structure containing partitioning
  570. * information for the given device @devt.
  571. */
  572. struct gendisk *get_gendisk(dev_t devt, int *partno)
  573. {
  574. struct gendisk *disk = NULL;
  575. if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
  576. struct kobject *kobj;
  577. kobj = kobj_lookup(bdev_map, devt, partno);
  578. if (kobj)
  579. disk = dev_to_disk(kobj_to_dev(kobj));
  580. } else {
  581. struct hd_struct *part;
  582. mutex_lock(&ext_devt_mutex);
  583. part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  584. if (part && get_disk(part_to_disk(part))) {
  585. *partno = part->partno;
  586. disk = part_to_disk(part);
  587. }
  588. mutex_unlock(&ext_devt_mutex);
  589. }
  590. return disk;
  591. }
  592. EXPORT_SYMBOL(get_gendisk);
  593. /**
  594. * bdget_disk - do bdget() by gendisk and partition number
  595. * @disk: gendisk of interest
  596. * @partno: partition number
  597. *
  598. * Find partition @partno from @disk, do bdget() on it.
  599. *
  600. * CONTEXT:
  601. * Don't care.
  602. *
  603. * RETURNS:
  604. * Resulting block_device on success, NULL on failure.
  605. */
  606. struct block_device *bdget_disk(struct gendisk *disk, int partno)
  607. {
  608. struct hd_struct *part;
  609. struct block_device *bdev = NULL;
  610. part = disk_get_part(disk, partno);
  611. if (part)
  612. bdev = bdget(part_devt(part));
  613. disk_put_part(part);
  614. return bdev;
  615. }
  616. EXPORT_SYMBOL(bdget_disk);
  617. /*
  618. * print a full list of all partitions - intended for places where the root
  619. * filesystem can't be mounted and thus to give the victim some idea of what
  620. * went wrong
  621. */
  622. void __init printk_all_partitions(void)
  623. {
  624. struct class_dev_iter iter;
  625. struct device *dev;
  626. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  627. while ((dev = class_dev_iter_next(&iter))) {
  628. struct gendisk *disk = dev_to_disk(dev);
  629. struct disk_part_iter piter;
  630. struct hd_struct *part;
  631. char name_buf[BDEVNAME_SIZE];
  632. char devt_buf[BDEVT_SIZE];
  633. u8 uuid[PARTITION_META_INFO_UUIDLTH * 2 + 1];
  634. /*
  635. * Don't show empty devices or things that have been
  636. * suppressed
  637. */
  638. if (get_capacity(disk) == 0 ||
  639. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  640. continue;
  641. /*
  642. * Note, unlike /proc/partitions, I am showing the
  643. * numbers in hex - the same format as the root=
  644. * option takes.
  645. */
  646. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  647. while ((part = disk_part_iter_next(&piter))) {
  648. bool is_part0 = part == &disk->part0;
  649. uuid[0] = 0;
  650. if (part->info)
  651. part_unpack_uuid(part->info->uuid, uuid);
  652. printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
  653. bdevt_str(part_devt(part), devt_buf),
  654. (unsigned long long)part->nr_sects >> 1,
  655. disk_name(disk, part->partno, name_buf), uuid);
  656. if (is_part0) {
  657. if (disk->driverfs_dev != NULL &&
  658. disk->driverfs_dev->driver != NULL)
  659. printk(" driver: %s\n",
  660. disk->driverfs_dev->driver->name);
  661. else
  662. printk(" (driver?)\n");
  663. } else
  664. printk("\n");
  665. }
  666. disk_part_iter_exit(&piter);
  667. }
  668. class_dev_iter_exit(&iter);
  669. }
  670. #ifdef CONFIG_PROC_FS
  671. /* iterator */
  672. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  673. {
  674. loff_t skip = *pos;
  675. struct class_dev_iter *iter;
  676. struct device *dev;
  677. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  678. if (!iter)
  679. return ERR_PTR(-ENOMEM);
  680. seqf->private = iter;
  681. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  682. do {
  683. dev = class_dev_iter_next(iter);
  684. if (!dev)
  685. return NULL;
  686. } while (skip--);
  687. return dev_to_disk(dev);
  688. }
  689. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  690. {
  691. struct device *dev;
  692. (*pos)++;
  693. dev = class_dev_iter_next(seqf->private);
  694. if (dev)
  695. return dev_to_disk(dev);
  696. return NULL;
  697. }
  698. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  699. {
  700. struct class_dev_iter *iter = seqf->private;
  701. /* stop is called even after start failed :-( */
  702. if (iter) {
  703. class_dev_iter_exit(iter);
  704. kfree(iter);
  705. }
  706. }
  707. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  708. {
  709. static void *p;
  710. p = disk_seqf_start(seqf, pos);
  711. if (!IS_ERR_OR_NULL(p) && !*pos)
  712. seq_puts(seqf, "major minor #blocks name\n\n");
  713. return p;
  714. }
  715. static int show_partition(struct seq_file *seqf, void *v)
  716. {
  717. struct gendisk *sgp = v;
  718. struct disk_part_iter piter;
  719. struct hd_struct *part;
  720. char buf[BDEVNAME_SIZE];
  721. /* Don't show non-partitionable removeable devices or empty devices */
  722. if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
  723. (sgp->flags & GENHD_FL_REMOVABLE)))
  724. return 0;
  725. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  726. return 0;
  727. /* show the full disk and all non-0 size partitions of it */
  728. disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
  729. while ((part = disk_part_iter_next(&piter)))
  730. seq_printf(seqf, "%4d %7d %10llu %s\n",
  731. MAJOR(part_devt(part)), MINOR(part_devt(part)),
  732. (unsigned long long)part->nr_sects >> 1,
  733. disk_name(sgp, part->partno, buf));
  734. disk_part_iter_exit(&piter);
  735. return 0;
  736. }
  737. static const struct seq_operations partitions_op = {
  738. .start = show_partition_start,
  739. .next = disk_seqf_next,
  740. .stop = disk_seqf_stop,
  741. .show = show_partition
  742. };
  743. static int partitions_open(struct inode *inode, struct file *file)
  744. {
  745. return seq_open(file, &partitions_op);
  746. }
  747. static const struct file_operations proc_partitions_operations = {
  748. .open = partitions_open,
  749. .read = seq_read,
  750. .llseek = seq_lseek,
  751. .release = seq_release,
  752. };
  753. #endif
  754. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  755. {
  756. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  757. /* Make old-style 2.4 aliases work */
  758. request_module("block-major-%d", MAJOR(devt));
  759. return NULL;
  760. }
  761. static int __init genhd_device_init(void)
  762. {
  763. int error;
  764. block_class.dev_kobj = sysfs_dev_block_kobj;
  765. error = class_register(&block_class);
  766. if (unlikely(error))
  767. return error;
  768. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  769. blk_dev_init();
  770. register_blkdev(BLOCK_EXT_MAJOR, "blkext");
  771. /* create top-level block dir */
  772. if (!sysfs_deprecated)
  773. block_depr = kobject_create_and_add("block", NULL);
  774. return 0;
  775. }
  776. subsys_initcall(genhd_device_init);
  777. static ssize_t alias_show(struct device *dev,
  778. struct device_attribute *attr, char *buf)
  779. {
  780. struct gendisk *disk = dev_to_disk(dev);
  781. ssize_t ret = 0;
  782. if (disk->alias)
  783. ret = snprintf(buf, ALIAS_LEN, "%s\n", disk->alias);
  784. return ret;
  785. }
  786. static ssize_t alias_store(struct device *dev, struct device_attribute *attr,
  787. const char *buf, size_t count)
  788. {
  789. struct gendisk *disk = dev_to_disk(dev);
  790. char *alias;
  791. char *envp[] = { NULL, NULL };
  792. unsigned char c;
  793. int i;
  794. ssize_t ret = count;
  795. if (!count)
  796. return -EINVAL;
  797. if (count >= ALIAS_LEN) {
  798. printk(KERN_ERR "alias: alias is too long\n");
  799. return -EINVAL;
  800. }
  801. /* Validation check */
  802. for (i = 0; i < count; i++) {
  803. c = buf[i];
  804. if (i == count - 1 && c == '\n')
  805. break;
  806. if (!isalnum(c) && c != '_' && c != '-') {
  807. printk(KERN_ERR "alias: invalid alias\n");
  808. return -EINVAL;
  809. }
  810. }
  811. if (disk->alias) {
  812. printk(KERN_INFO "alias: %s is already assigned (%s)\n",
  813. disk->disk_name, disk->alias);
  814. return -EINVAL;
  815. }
  816. alias = kasprintf(GFP_KERNEL, "%s", buf);
  817. if (!alias)
  818. return -ENOMEM;
  819. if (alias[count - 1] == '\n')
  820. alias[count - 1] = '\0';
  821. envp[0] = kasprintf(GFP_KERNEL, "ALIAS=%s", alias);
  822. if (!envp[0]) {
  823. kfree(alias);
  824. return -ENOMEM;
  825. }
  826. disk->alias = alias;
  827. printk(KERN_INFO "alias: assigned %s to %s\n", alias, disk->disk_name);
  828. kobject_uevent_env(&dev->kobj, KOBJ_ADD, envp);
  829. kfree(envp[0]);
  830. return ret;
  831. }
  832. static ssize_t disk_range_show(struct device *dev,
  833. struct device_attribute *attr, char *buf)
  834. {
  835. struct gendisk *disk = dev_to_disk(dev);
  836. return sprintf(buf, "%d\n", disk->minors);
  837. }
  838. static ssize_t disk_ext_range_show(struct device *dev,
  839. struct device_attribute *attr, char *buf)
  840. {
  841. struct gendisk *disk = dev_to_disk(dev);
  842. return sprintf(buf, "%d\n", disk_max_parts(disk));
  843. }
  844. static ssize_t disk_removable_show(struct device *dev,
  845. struct device_attribute *attr, char *buf)
  846. {
  847. struct gendisk *disk = dev_to_disk(dev);
  848. return sprintf(buf, "%d\n",
  849. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  850. }
  851. static ssize_t disk_ro_show(struct device *dev,
  852. struct device_attribute *attr, char *buf)
  853. {
  854. struct gendisk *disk = dev_to_disk(dev);
  855. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  856. }
  857. static ssize_t disk_capability_show(struct device *dev,
  858. struct device_attribute *attr, char *buf)
  859. {
  860. struct gendisk *disk = dev_to_disk(dev);
  861. return sprintf(buf, "%x\n", disk->flags);
  862. }
  863. static ssize_t disk_alignment_offset_show(struct device *dev,
  864. struct device_attribute *attr,
  865. char *buf)
  866. {
  867. struct gendisk *disk = dev_to_disk(dev);
  868. return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
  869. }
  870. static ssize_t disk_discard_alignment_show(struct device *dev,
  871. struct device_attribute *attr,
  872. char *buf)
  873. {
  874. struct gendisk *disk = dev_to_disk(dev);
  875. return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
  876. }
  877. static DEVICE_ATTR(alias, S_IRUGO|S_IWUSR, alias_show, alias_store);
  878. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  879. static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
  880. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  881. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  882. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  883. static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
  884. static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
  885. NULL);
  886. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  887. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  888. static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
  889. #ifdef CONFIG_FAIL_MAKE_REQUEST
  890. static struct device_attribute dev_attr_fail =
  891. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  892. #endif
  893. #ifdef CONFIG_FAIL_IO_TIMEOUT
  894. static struct device_attribute dev_attr_fail_timeout =
  895. __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
  896. part_timeout_store);
  897. #endif
  898. static struct attribute *disk_attrs[] = {
  899. &dev_attr_alias.attr,
  900. &dev_attr_range.attr,
  901. &dev_attr_ext_range.attr,
  902. &dev_attr_removable.attr,
  903. &dev_attr_ro.attr,
  904. &dev_attr_size.attr,
  905. &dev_attr_alignment_offset.attr,
  906. &dev_attr_discard_alignment.attr,
  907. &dev_attr_capability.attr,
  908. &dev_attr_stat.attr,
  909. &dev_attr_inflight.attr,
  910. #ifdef CONFIG_FAIL_MAKE_REQUEST
  911. &dev_attr_fail.attr,
  912. #endif
  913. #ifdef CONFIG_FAIL_IO_TIMEOUT
  914. &dev_attr_fail_timeout.attr,
  915. #endif
  916. NULL
  917. };
  918. static struct attribute_group disk_attr_group = {
  919. .attrs = disk_attrs,
  920. };
  921. static const struct attribute_group *disk_attr_groups[] = {
  922. &disk_attr_group,
  923. NULL
  924. };
  925. /**
  926. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  927. * @disk: disk to replace part_tbl for
  928. * @new_ptbl: new part_tbl to install
  929. *
  930. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  931. * original ptbl is freed using RCU callback.
  932. *
  933. * LOCKING:
  934. * Matching bd_mutx locked.
  935. */
  936. static void disk_replace_part_tbl(struct gendisk *disk,
  937. struct disk_part_tbl *new_ptbl)
  938. {
  939. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  940. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  941. if (old_ptbl) {
  942. rcu_assign_pointer(old_ptbl->last_lookup, NULL);
  943. kfree_rcu(old_ptbl, rcu_head);
  944. }
  945. }
  946. /**
  947. * disk_expand_part_tbl - expand disk->part_tbl
  948. * @disk: disk to expand part_tbl for
  949. * @partno: expand such that this partno can fit in
  950. *
  951. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  952. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  953. *
  954. * LOCKING:
  955. * Matching bd_mutex locked, might sleep.
  956. *
  957. * RETURNS:
  958. * 0 on success, -errno on failure.
  959. */
  960. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  961. {
  962. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  963. struct disk_part_tbl *new_ptbl;
  964. int len = old_ptbl ? old_ptbl->len : 0;
  965. int target = partno + 1;
  966. size_t size;
  967. int i;
  968. /* disk_max_parts() is zero during initialization, ignore if so */
  969. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  970. return -EINVAL;
  971. if (target <= len)
  972. return 0;
  973. size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
  974. new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
  975. if (!new_ptbl)
  976. return -ENOMEM;
  977. new_ptbl->len = target;
  978. for (i = 0; i < len; i++)
  979. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  980. disk_replace_part_tbl(disk, new_ptbl);
  981. return 0;
  982. }
  983. static void disk_release(struct device *dev)
  984. {
  985. struct gendisk *disk = dev_to_disk(dev);
  986. disk_release_events(disk);
  987. kfree(disk->random);
  988. disk_replace_part_tbl(disk, NULL);
  989. free_part_stats(&disk->part0);
  990. free_part_info(&disk->part0);
  991. if (disk->queue)
  992. blk_put_queue(disk->queue);
  993. kfree(disk);
  994. }
  995. struct class block_class = {
  996. .name = "block",
  997. };
  998. static char *block_devnode(struct device *dev, mode_t *mode)
  999. {
  1000. struct gendisk *disk = dev_to_disk(dev);
  1001. if (disk->devnode)
  1002. return disk->devnode(disk, mode);
  1003. return NULL;
  1004. }
  1005. static struct device_type disk_type = {
  1006. .name = "disk",
  1007. .groups = disk_attr_groups,
  1008. .release = disk_release,
  1009. .devnode = block_devnode,
  1010. };
  1011. #ifdef CONFIG_PROC_FS
  1012. /*
  1013. * aggregate disk stat collector. Uses the same stats that the sysfs
  1014. * entries do, above, but makes them available through one seq_file.
  1015. *
  1016. * The output looks suspiciously like /proc/partitions with a bunch of
  1017. * extra fields.
  1018. */
  1019. static int diskstats_show(struct seq_file *seqf, void *v)
  1020. {
  1021. struct gendisk *gp = v;
  1022. struct disk_part_iter piter;
  1023. struct hd_struct *hd;
  1024. char buf[BDEVNAME_SIZE];
  1025. int cpu;
  1026. /*
  1027. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  1028. seq_puts(seqf, "major minor name"
  1029. " rio rmerge rsect ruse wio wmerge "
  1030. "wsect wuse running use aveq"
  1031. "\n\n");
  1032. */
  1033. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
  1034. while ((hd = disk_part_iter_next(&piter))) {
  1035. cpu = part_stat_lock();
  1036. part_round_stats(cpu, hd);
  1037. part_stat_unlock();
  1038. seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
  1039. "%u %lu %lu %lu %u %u %u %u\n",
  1040. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  1041. disk_name(gp, hd->partno, buf),
  1042. part_stat_read(hd, ios[READ]),
  1043. part_stat_read(hd, merges[READ]),
  1044. part_stat_read(hd, sectors[READ]),
  1045. jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
  1046. part_stat_read(hd, ios[WRITE]),
  1047. part_stat_read(hd, merges[WRITE]),
  1048. part_stat_read(hd, sectors[WRITE]),
  1049. jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
  1050. part_in_flight(hd),
  1051. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  1052. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  1053. );
  1054. }
  1055. disk_part_iter_exit(&piter);
  1056. return 0;
  1057. }
  1058. static const struct seq_operations diskstats_op = {
  1059. .start = disk_seqf_start,
  1060. .next = disk_seqf_next,
  1061. .stop = disk_seqf_stop,
  1062. .show = diskstats_show
  1063. };
  1064. static int diskstats_open(struct inode *inode, struct file *file)
  1065. {
  1066. return seq_open(file, &diskstats_op);
  1067. }
  1068. static const struct file_operations proc_diskstats_operations = {
  1069. .open = diskstats_open,
  1070. .read = seq_read,
  1071. .llseek = seq_lseek,
  1072. .release = seq_release,
  1073. };
  1074. static int __init proc_genhd_init(void)
  1075. {
  1076. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  1077. proc_create("partitions", 0, NULL, &proc_partitions_operations);
  1078. return 0;
  1079. }
  1080. module_init(proc_genhd_init);
  1081. #endif /* CONFIG_PROC_FS */
  1082. dev_t blk_lookup_devt(const char *name, int partno)
  1083. {
  1084. dev_t devt = MKDEV(0, 0);
  1085. struct class_dev_iter iter;
  1086. struct device *dev;
  1087. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  1088. while ((dev = class_dev_iter_next(&iter))) {
  1089. struct gendisk *disk = dev_to_disk(dev);
  1090. struct hd_struct *part;
  1091. if (strcmp(dev_name(dev), name))
  1092. continue;
  1093. if (partno < disk->minors) {
  1094. /* We need to return the right devno, even
  1095. * if the partition doesn't exist yet.
  1096. */
  1097. devt = MKDEV(MAJOR(dev->devt),
  1098. MINOR(dev->devt) + partno);
  1099. break;
  1100. }
  1101. part = disk_get_part(disk, partno);
  1102. if (part) {
  1103. devt = part_devt(part);
  1104. disk_put_part(part);
  1105. break;
  1106. }
  1107. disk_put_part(part);
  1108. }
  1109. class_dev_iter_exit(&iter);
  1110. return devt;
  1111. }
  1112. EXPORT_SYMBOL(blk_lookup_devt);
  1113. struct gendisk *alloc_disk(int minors)
  1114. {
  1115. return alloc_disk_node(minors, -1);
  1116. }
  1117. EXPORT_SYMBOL(alloc_disk);
  1118. struct gendisk *alloc_disk_node(int minors, int node_id)
  1119. {
  1120. struct gendisk *disk;
  1121. disk = kmalloc_node(sizeof(struct gendisk),
  1122. GFP_KERNEL | __GFP_ZERO, node_id);
  1123. if (disk) {
  1124. if (!init_part_stats(&disk->part0)) {
  1125. kfree(disk);
  1126. return NULL;
  1127. }
  1128. disk->node_id = node_id;
  1129. if (disk_expand_part_tbl(disk, 0)) {
  1130. free_part_stats(&disk->part0);
  1131. kfree(disk);
  1132. return NULL;
  1133. }
  1134. disk->part_tbl->part[0] = &disk->part0;
  1135. hd_ref_init(&disk->part0);
  1136. disk->minors = minors;
  1137. rand_initialize_disk(disk);
  1138. disk_to_dev(disk)->class = &block_class;
  1139. disk_to_dev(disk)->type = &disk_type;
  1140. device_initialize(disk_to_dev(disk));
  1141. }
  1142. return disk;
  1143. }
  1144. EXPORT_SYMBOL(alloc_disk_node);
  1145. struct kobject *get_disk(struct gendisk *disk)
  1146. {
  1147. struct module *owner;
  1148. struct kobject *kobj;
  1149. if (!disk->fops)
  1150. return NULL;
  1151. owner = disk->fops->owner;
  1152. if (owner && !try_module_get(owner))
  1153. return NULL;
  1154. kobj = kobject_get(&disk_to_dev(disk)->kobj);
  1155. if (kobj == NULL) {
  1156. module_put(owner);
  1157. return NULL;
  1158. }
  1159. return kobj;
  1160. }
  1161. EXPORT_SYMBOL(get_disk);
  1162. void put_disk(struct gendisk *disk)
  1163. {
  1164. if (disk)
  1165. kobject_put(&disk_to_dev(disk)->kobj);
  1166. }
  1167. EXPORT_SYMBOL(put_disk);
  1168. static void set_disk_ro_uevent(struct gendisk *gd, int ro)
  1169. {
  1170. char event[] = "DISK_RO=1";
  1171. char *envp[] = { event, NULL };
  1172. if (!ro)
  1173. event[8] = '0';
  1174. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  1175. }
  1176. void set_device_ro(struct block_device *bdev, int flag)
  1177. {
  1178. bdev->bd_part->policy = flag;
  1179. }
  1180. EXPORT_SYMBOL(set_device_ro);
  1181. void set_disk_ro(struct gendisk *disk, int flag)
  1182. {
  1183. struct disk_part_iter piter;
  1184. struct hd_struct *part;
  1185. if (disk->part0.policy != flag) {
  1186. set_disk_ro_uevent(disk, flag);
  1187. disk->part0.policy = flag;
  1188. }
  1189. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  1190. while ((part = disk_part_iter_next(&piter)))
  1191. part->policy = flag;
  1192. disk_part_iter_exit(&piter);
  1193. }
  1194. EXPORT_SYMBOL(set_disk_ro);
  1195. int bdev_read_only(struct block_device *bdev)
  1196. {
  1197. if (!bdev)
  1198. return 0;
  1199. return bdev->bd_part->policy;
  1200. }
  1201. EXPORT_SYMBOL(bdev_read_only);
  1202. int invalidate_partition(struct gendisk *disk, int partno)
  1203. {
  1204. int res = 0;
  1205. struct block_device *bdev = bdget_disk(disk, partno);
  1206. if (bdev) {
  1207. fsync_bdev(bdev);
  1208. res = __invalidate_device(bdev, true);
  1209. bdput(bdev);
  1210. }
  1211. return res;
  1212. }
  1213. EXPORT_SYMBOL(invalidate_partition);
  1214. /*
  1215. * Disk events - monitor disk events like media change and eject request.
  1216. */
  1217. struct disk_events {
  1218. struct list_head node; /* all disk_event's */
  1219. struct gendisk *disk; /* the associated disk */
  1220. spinlock_t lock;
  1221. struct mutex block_mutex; /* protects blocking */
  1222. int block; /* event blocking depth */
  1223. unsigned int pending; /* events already sent out */
  1224. unsigned int clearing; /* events being cleared */
  1225. long poll_msecs; /* interval, -1 for default */
  1226. struct delayed_work dwork;
  1227. };
  1228. static const char *disk_events_strs[] = {
  1229. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
  1230. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
  1231. };
  1232. static char *disk_uevents[] = {
  1233. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
  1234. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
  1235. };
  1236. /* list of all disk_events */
  1237. static DEFINE_MUTEX(disk_events_mutex);
  1238. static LIST_HEAD(disk_events);
  1239. /* disable in-kernel polling by default */
  1240. static unsigned long disk_events_dfl_poll_msecs = 0;
  1241. static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
  1242. {
  1243. struct disk_events *ev = disk->ev;
  1244. long intv_msecs = 0;
  1245. /*
  1246. * If device-specific poll interval is set, always use it. If
  1247. * the default is being used, poll iff there are events which
  1248. * can't be monitored asynchronously.
  1249. */
  1250. if (ev->poll_msecs >= 0)
  1251. intv_msecs = ev->poll_msecs;
  1252. else if (disk->events & ~disk->async_events)
  1253. intv_msecs = disk_events_dfl_poll_msecs;
  1254. return msecs_to_jiffies(intv_msecs);
  1255. }
  1256. /**
  1257. * disk_block_events - block and flush disk event checking
  1258. * @disk: disk to block events for
  1259. *
  1260. * On return from this function, it is guaranteed that event checking
  1261. * isn't in progress and won't happen until unblocked by
  1262. * disk_unblock_events(). Events blocking is counted and the actual
  1263. * unblocking happens after the matching number of unblocks are done.
  1264. *
  1265. * Note that this intentionally does not block event checking from
  1266. * disk_clear_events().
  1267. *
  1268. * CONTEXT:
  1269. * Might sleep.
  1270. */
  1271. void disk_block_events(struct gendisk *disk)
  1272. {
  1273. struct disk_events *ev = disk->ev;
  1274. unsigned long flags;
  1275. bool cancel;
  1276. if (!ev)
  1277. return;
  1278. /*
  1279. * Outer mutex ensures that the first blocker completes canceling
  1280. * the event work before further blockers are allowed to finish.
  1281. */
  1282. mutex_lock(&ev->block_mutex);
  1283. spin_lock_irqsave(&ev->lock, flags);
  1284. cancel = !ev->block++;
  1285. spin_unlock_irqrestore(&ev->lock, flags);
  1286. if (cancel)
  1287. cancel_delayed_work_sync(&disk->ev->dwork);
  1288. mutex_unlock(&ev->block_mutex);
  1289. }
  1290. static void __disk_unblock_events(struct gendisk *disk, bool check_now)
  1291. {
  1292. struct disk_events *ev = disk->ev;
  1293. unsigned long intv;
  1294. unsigned long flags;
  1295. spin_lock_irqsave(&ev->lock, flags);
  1296. if (WARN_ON_ONCE(ev->block <= 0))
  1297. goto out_unlock;
  1298. if (--ev->block)
  1299. goto out_unlock;
  1300. /*
  1301. * Not exactly a latency critical operation, set poll timer
  1302. * slack to 25% and kick event check.
  1303. */
  1304. intv = disk_events_poll_jiffies(disk);
  1305. set_timer_slack(&ev->dwork.timer, intv / 4);
  1306. if (check_now)
  1307. queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
  1308. else if (intv)
  1309. queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
  1310. out_unlock:
  1311. spin_unlock_irqrestore(&ev->lock, flags);
  1312. }
  1313. /**
  1314. * disk_unblock_events - unblock disk event checking
  1315. * @disk: disk to unblock events for
  1316. *
  1317. * Undo disk_block_events(). When the block count reaches zero, it
  1318. * starts events polling if configured.
  1319. *
  1320. * CONTEXT:
  1321. * Don't care. Safe to call from irq context.
  1322. */
  1323. void disk_unblock_events(struct gendisk *disk)
  1324. {
  1325. if (disk->ev)
  1326. __disk_unblock_events(disk, false);
  1327. }
  1328. /**
  1329. * disk_flush_events - schedule immediate event checking and flushing
  1330. * @disk: disk to check and flush events for
  1331. * @mask: events to flush
  1332. *
  1333. * Schedule immediate event checking on @disk if not blocked. Events in
  1334. * @mask are scheduled to be cleared from the driver. Note that this
  1335. * doesn't clear the events from @disk->ev.
  1336. *
  1337. * CONTEXT:
  1338. * If @mask is non-zero must be called with bdev->bd_mutex held.
  1339. */
  1340. void disk_flush_events(struct gendisk *disk, unsigned int mask)
  1341. {
  1342. struct disk_events *ev = disk->ev;
  1343. if (!ev)
  1344. return;
  1345. spin_lock_irq(&ev->lock);
  1346. ev->clearing |= mask;
  1347. if (!ev->block) {
  1348. cancel_delayed_work(&ev->dwork);
  1349. queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
  1350. }
  1351. spin_unlock_irq(&ev->lock);
  1352. }
  1353. /**
  1354. * disk_clear_events - synchronously check, clear and return pending events
  1355. * @disk: disk to fetch and clear events from
  1356. * @mask: mask of events to be fetched and clearted
  1357. *
  1358. * Disk events are synchronously checked and pending events in @mask
  1359. * are cleared and returned. This ignores the block count.
  1360. *
  1361. * CONTEXT:
  1362. * Might sleep.
  1363. */
  1364. unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
  1365. {
  1366. const struct block_device_operations *bdops = disk->fops;
  1367. struct disk_events *ev = disk->ev;
  1368. unsigned int pending;
  1369. if (!ev) {
  1370. /* for drivers still using the old ->media_changed method */
  1371. if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
  1372. bdops->media_changed && bdops->media_changed(disk))
  1373. return DISK_EVENT_MEDIA_CHANGE;
  1374. return 0;
  1375. }
  1376. /* tell the workfn about the events being cleared */
  1377. spin_lock_irq(&ev->lock);
  1378. ev->clearing |= mask;
  1379. spin_unlock_irq(&ev->lock);
  1380. /* uncondtionally schedule event check and wait for it to finish */
  1381. disk_block_events(disk);
  1382. queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
  1383. flush_delayed_work(&ev->dwork);
  1384. __disk_unblock_events(disk, false);
  1385. /* then, fetch and clear pending events */
  1386. spin_lock_irq(&ev->lock);
  1387. WARN_ON_ONCE(ev->clearing & mask); /* cleared by workfn */
  1388. pending = ev->pending & mask;
  1389. ev->pending &= ~mask;
  1390. spin_unlock_irq(&ev->lock);
  1391. return pending;
  1392. }
  1393. static void disk_events_workfn(struct work_struct *work)
  1394. {
  1395. struct delayed_work *dwork = to_delayed_work(work);
  1396. struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
  1397. struct gendisk *disk = ev->disk;
  1398. char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
  1399. unsigned int clearing = ev->clearing;
  1400. unsigned int events;
  1401. unsigned long intv;
  1402. int nr_events = 0, i;
  1403. /* check events */
  1404. events = disk->fops->check_events(disk, clearing);
  1405. /* accumulate pending events and schedule next poll if necessary */
  1406. spin_lock_irq(&ev->lock);
  1407. events &= ~ev->pending;
  1408. ev->pending |= events;
  1409. ev->clearing &= ~clearing;
  1410. intv = disk_events_poll_jiffies(disk);
  1411. if (!ev->block && intv)
  1412. queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
  1413. spin_unlock_irq(&ev->lock);
  1414. /*
  1415. * Tell userland about new events. Only the events listed in
  1416. * @disk->events are reported. Unlisted events are processed the
  1417. * same internally but never get reported to userland.
  1418. */
  1419. for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
  1420. if (events & disk->events & (1 << i))
  1421. envp[nr_events++] = disk_uevents[i];
  1422. if (nr_events)
  1423. kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
  1424. }
  1425. /*
  1426. * A disk events enabled device has the following sysfs nodes under
  1427. * its /sys/block/X/ directory.
  1428. *
  1429. * events : list of all supported events
  1430. * events_async : list of events which can be detected w/o polling
  1431. * events_poll_msecs : polling interval, 0: disable, -1: system default
  1432. */
  1433. static ssize_t __disk_events_show(unsigned int events, char *buf)
  1434. {
  1435. const char *delim = "";
  1436. ssize_t pos = 0;
  1437. int i;
  1438. for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
  1439. if (events & (1 << i)) {
  1440. pos += sprintf(buf + pos, "%s%s",
  1441. delim, disk_events_strs[i]);
  1442. delim = " ";
  1443. }
  1444. if (pos)
  1445. pos += sprintf(buf + pos, "\n");
  1446. return pos;
  1447. }
  1448. static ssize_t disk_events_show(struct device *dev,
  1449. struct device_attribute *attr, char *buf)
  1450. {
  1451. struct gendisk *disk = dev_to_disk(dev);
  1452. return __disk_events_show(disk->events, buf);
  1453. }
  1454. static ssize_t disk_events_async_show(struct device *dev,
  1455. struct device_attribute *attr, char *buf)
  1456. {
  1457. struct gendisk *disk = dev_to_disk(dev);
  1458. return __disk_events_show(disk->async_events, buf);
  1459. }
  1460. static ssize_t disk_events_poll_msecs_show(struct device *dev,
  1461. struct device_attribute *attr,
  1462. char *buf)
  1463. {
  1464. struct gendisk *disk = dev_to_disk(dev);
  1465. return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
  1466. }
  1467. static ssize_t disk_events_poll_msecs_store(struct device *dev,
  1468. struct device_attribute *attr,
  1469. const char *buf, size_t count)
  1470. {
  1471. struct gendisk *disk = dev_to_disk(dev);
  1472. long intv;
  1473. if (!count || !sscanf(buf, "%ld", &intv))
  1474. return -EINVAL;
  1475. if (intv < 0 && intv != -1)
  1476. return -EINVAL;
  1477. disk_block_events(disk);
  1478. disk->ev->poll_msecs = intv;
  1479. __disk_unblock_events(disk, true);
  1480. return count;
  1481. }
  1482. static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
  1483. static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
  1484. static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
  1485. disk_events_poll_msecs_show,
  1486. disk_events_poll_msecs_store);
  1487. static const struct attribute *disk_events_attrs[] = {
  1488. &dev_attr_events.attr,
  1489. &dev_attr_events_async.attr,
  1490. &dev_attr_events_poll_msecs.attr,
  1491. NULL,
  1492. };
  1493. /*
  1494. * The default polling interval can be specified by the kernel
  1495. * parameter block.events_dfl_poll_msecs which defaults to 0
  1496. * (disable). This can also be modified runtime by writing to
  1497. * /sys/module/block/events_dfl_poll_msecs.
  1498. */
  1499. static int disk_events_set_dfl_poll_msecs(const char *val,
  1500. const struct kernel_param *kp)
  1501. {
  1502. struct disk_events *ev;
  1503. int ret;
  1504. ret = param_set_ulong(val, kp);
  1505. if (ret < 0)
  1506. return ret;
  1507. mutex_lock(&disk_events_mutex);
  1508. list_for_each_entry(ev, &disk_events, node)
  1509. disk_flush_events(ev->disk, 0);
  1510. mutex_unlock(&disk_events_mutex);
  1511. return 0;
  1512. }
  1513. static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
  1514. .set = disk_events_set_dfl_poll_msecs,
  1515. .get = param_get_ulong,
  1516. };
  1517. #undef MODULE_PARAM_PREFIX
  1518. #define MODULE_PARAM_PREFIX "block."
  1519. module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
  1520. &disk_events_dfl_poll_msecs, 0644);
  1521. /*
  1522. * disk_{add|del|release}_events - initialize and destroy disk_events.
  1523. */
  1524. static void disk_add_events(struct gendisk *disk)
  1525. {
  1526. struct disk_events *ev;
  1527. if (!disk->fops->check_events)
  1528. return;
  1529. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  1530. if (!ev) {
  1531. pr_warn("%s: failed to initialize events\n", disk->disk_name);
  1532. return;
  1533. }
  1534. if (sysfs_create_files(&disk_to_dev(disk)->kobj,
  1535. disk_events_attrs) < 0) {
  1536. pr_warn("%s: failed to create sysfs files for events\n",
  1537. disk->disk_name);
  1538. kfree(ev);
  1539. return;
  1540. }
  1541. disk->ev = ev;
  1542. INIT_LIST_HEAD(&ev->node);
  1543. ev->disk = disk;
  1544. spin_lock_init(&ev->lock);
  1545. mutex_init(&ev->block_mutex);
  1546. ev->block = 1;
  1547. ev->poll_msecs = -1;
  1548. INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
  1549. mutex_lock(&disk_events_mutex);
  1550. list_add_tail(&ev->node, &disk_events);
  1551. mutex_unlock(&disk_events_mutex);
  1552. /*
  1553. * Block count is initialized to 1 and the following initial
  1554. * unblock kicks it into action.
  1555. */
  1556. __disk_unblock_events(disk, true);
  1557. }
  1558. static void disk_del_events(struct gendisk *disk)
  1559. {
  1560. if (!disk->ev)
  1561. return;
  1562. disk_block_events(disk);
  1563. mutex_lock(&disk_events_mutex);
  1564. list_del_init(&disk->ev->node);
  1565. mutex_unlock(&disk_events_mutex);
  1566. sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
  1567. }
  1568. static void disk_release_events(struct gendisk *disk)
  1569. {
  1570. /* the block count should be 1 from disk_del_events() */
  1571. WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
  1572. kfree(disk->ev);
  1573. }