genhd.c 46 KB

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