genhd.c 46 KB

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