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