genhd.c 44 KB

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