genhd.c 44 KB

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