genhd.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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. delete_partition(disk, part->partno);
  553. }
  554. disk_part_iter_exit(&piter);
  555. invalidate_partition(disk, 0);
  556. set_capacity(disk, 0);
  557. disk->flags &= ~GENHD_FL_UP;
  558. sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
  559. blk_unregister_queue(disk);
  560. blk_unregister_region(disk_devt(disk), disk->minors);
  561. part_stat_set_all(&disk->part0, 0);
  562. disk->part0.stamp = 0;
  563. kobject_put(disk->part0.holder_dir);
  564. kobject_put(disk->slave_dir);
  565. if (!sysfs_deprecated)
  566. sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
  567. pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
  568. device_del(disk_to_dev(disk));
  569. }
  570. EXPORT_SYMBOL(del_gendisk);
  571. /* sysfs access to bad-blocks list. */
  572. static ssize_t disk_badblocks_show(struct device *dev,
  573. struct device_attribute *attr,
  574. char *page)
  575. {
  576. struct gendisk *disk = dev_to_disk(dev);
  577. if (!disk->bb)
  578. return sprintf(page, "\n");
  579. return badblocks_show(disk->bb, page, 0);
  580. }
  581. static ssize_t disk_badblocks_store(struct device *dev,
  582. struct device_attribute *attr,
  583. const char *page, size_t len)
  584. {
  585. struct gendisk *disk = dev_to_disk(dev);
  586. if (!disk->bb)
  587. return -ENXIO;
  588. return badblocks_store(disk->bb, page, len, 0);
  589. }
  590. /**
  591. * get_gendisk - get partitioning information for a given device
  592. * @devt: device to get partitioning information for
  593. * @partno: returned partition index
  594. *
  595. * This function gets the structure containing partitioning
  596. * information for the given device @devt.
  597. */
  598. struct gendisk *get_gendisk(dev_t devt, int *partno)
  599. {
  600. struct gendisk *disk = NULL;
  601. if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
  602. struct kobject *kobj;
  603. kobj = kobj_lookup(bdev_map, devt, partno);
  604. if (kobj)
  605. disk = dev_to_disk(kobj_to_dev(kobj));
  606. } else {
  607. struct hd_struct *part;
  608. spin_lock_bh(&ext_devt_lock);
  609. part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  610. if (part && get_disk(part_to_disk(part))) {
  611. *partno = part->partno;
  612. disk = part_to_disk(part);
  613. }
  614. spin_unlock_bh(&ext_devt_lock);
  615. }
  616. return disk;
  617. }
  618. EXPORT_SYMBOL(get_gendisk);
  619. /**
  620. * bdget_disk - do bdget() by gendisk and partition number
  621. * @disk: gendisk of interest
  622. * @partno: partition number
  623. *
  624. * Find partition @partno from @disk, do bdget() on it.
  625. *
  626. * CONTEXT:
  627. * Don't care.
  628. *
  629. * RETURNS:
  630. * Resulting block_device on success, NULL on failure.
  631. */
  632. struct block_device *bdget_disk(struct gendisk *disk, int partno)
  633. {
  634. struct hd_struct *part;
  635. struct block_device *bdev = NULL;
  636. part = disk_get_part(disk, partno);
  637. if (part)
  638. bdev = bdget(part_devt(part));
  639. disk_put_part(part);
  640. return bdev;
  641. }
  642. EXPORT_SYMBOL(bdget_disk);
  643. /*
  644. * print a full list of all partitions - intended for places where the root
  645. * filesystem can't be mounted and thus to give the victim some idea of what
  646. * went wrong
  647. */
  648. void __init printk_all_partitions(void)
  649. {
  650. struct class_dev_iter iter;
  651. struct device *dev;
  652. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  653. while ((dev = class_dev_iter_next(&iter))) {
  654. struct gendisk *disk = dev_to_disk(dev);
  655. struct disk_part_iter piter;
  656. struct hd_struct *part;
  657. char name_buf[BDEVNAME_SIZE];
  658. char devt_buf[BDEVT_SIZE];
  659. /*
  660. * Don't show empty devices or things that have been
  661. * suppressed
  662. */
  663. if (get_capacity(disk) == 0 ||
  664. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  665. continue;
  666. /*
  667. * Note, unlike /proc/partitions, I am showing the
  668. * numbers in hex - the same format as the root=
  669. * option takes.
  670. */
  671. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  672. while ((part = disk_part_iter_next(&piter))) {
  673. bool is_part0 = part == &disk->part0;
  674. printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
  675. bdevt_str(part_devt(part), devt_buf),
  676. (unsigned long long)part_nr_sects_read(part) >> 1
  677. , disk_name(disk, part->partno, name_buf),
  678. part->info ? part->info->uuid : "");
  679. if (is_part0) {
  680. if (dev->parent && dev->parent->driver)
  681. printk(" driver: %s\n",
  682. dev->parent->driver->name);
  683. else
  684. printk(" (driver?)\n");
  685. } else
  686. printk("\n");
  687. }
  688. disk_part_iter_exit(&piter);
  689. }
  690. class_dev_iter_exit(&iter);
  691. }
  692. #ifdef CONFIG_PROC_FS
  693. /* iterator */
  694. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  695. {
  696. loff_t skip = *pos;
  697. struct class_dev_iter *iter;
  698. struct device *dev;
  699. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  700. if (!iter)
  701. return ERR_PTR(-ENOMEM);
  702. seqf->private = iter;
  703. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  704. do {
  705. dev = class_dev_iter_next(iter);
  706. if (!dev)
  707. return NULL;
  708. } while (skip--);
  709. return dev_to_disk(dev);
  710. }
  711. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  712. {
  713. struct device *dev;
  714. (*pos)++;
  715. dev = class_dev_iter_next(seqf->private);
  716. if (dev)
  717. return dev_to_disk(dev);
  718. return NULL;
  719. }
  720. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  721. {
  722. struct class_dev_iter *iter = seqf->private;
  723. /* stop is called even after start failed :-( */
  724. if (iter) {
  725. class_dev_iter_exit(iter);
  726. kfree(iter);
  727. seqf->private = NULL;
  728. }
  729. }
  730. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  731. {
  732. void *p;
  733. p = disk_seqf_start(seqf, pos);
  734. if (!IS_ERR_OR_NULL(p) && !*pos)
  735. seq_puts(seqf, "major minor #blocks name\n\n");
  736. return p;
  737. }
  738. static int show_partition(struct seq_file *seqf, void *v)
  739. {
  740. struct gendisk *sgp = v;
  741. struct disk_part_iter piter;
  742. struct hd_struct *part;
  743. char buf[BDEVNAME_SIZE];
  744. /* Don't show non-partitionable removeable devices or empty devices */
  745. if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
  746. (sgp->flags & GENHD_FL_REMOVABLE)))
  747. return 0;
  748. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  749. return 0;
  750. /* show the full disk and all non-0 size partitions of it */
  751. disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
  752. while ((part = disk_part_iter_next(&piter)))
  753. seq_printf(seqf, "%4d %7d %10llu %s\n",
  754. MAJOR(part_devt(part)), MINOR(part_devt(part)),
  755. (unsigned long long)part_nr_sects_read(part) >> 1,
  756. disk_name(sgp, part->partno, buf));
  757. disk_part_iter_exit(&piter);
  758. return 0;
  759. }
  760. static const struct seq_operations partitions_op = {
  761. .start = show_partition_start,
  762. .next = disk_seqf_next,
  763. .stop = disk_seqf_stop,
  764. .show = show_partition
  765. };
  766. static int partitions_open(struct inode *inode, struct file *file)
  767. {
  768. return seq_open(file, &partitions_op);
  769. }
  770. static const struct file_operations proc_partitions_operations = {
  771. .open = partitions_open,
  772. .read = seq_read,
  773. .llseek = seq_lseek,
  774. .release = seq_release,
  775. };
  776. #endif
  777. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  778. {
  779. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  780. /* Make old-style 2.4 aliases work */
  781. request_module("block-major-%d", MAJOR(devt));
  782. return NULL;
  783. }
  784. static int __init genhd_device_init(void)
  785. {
  786. int error;
  787. block_class.dev_kobj = sysfs_dev_block_kobj;
  788. error = class_register(&block_class);
  789. if (unlikely(error))
  790. return error;
  791. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  792. blk_dev_init();
  793. register_blkdev(BLOCK_EXT_MAJOR, "blkext");
  794. /* create top-level block dir */
  795. if (!sysfs_deprecated)
  796. block_depr = kobject_create_and_add("block", NULL);
  797. return 0;
  798. }
  799. subsys_initcall(genhd_device_init);
  800. static ssize_t disk_range_show(struct device *dev,
  801. struct device_attribute *attr, char *buf)
  802. {
  803. struct gendisk *disk = dev_to_disk(dev);
  804. return sprintf(buf, "%d\n", disk->minors);
  805. }
  806. static ssize_t disk_ext_range_show(struct device *dev,
  807. struct device_attribute *attr, char *buf)
  808. {
  809. struct gendisk *disk = dev_to_disk(dev);
  810. return sprintf(buf, "%d\n", disk_max_parts(disk));
  811. }
  812. static ssize_t disk_removable_show(struct device *dev,
  813. struct device_attribute *attr, char *buf)
  814. {
  815. struct gendisk *disk = dev_to_disk(dev);
  816. return sprintf(buf, "%d\n",
  817. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  818. }
  819. static ssize_t disk_ro_show(struct device *dev,
  820. struct device_attribute *attr, char *buf)
  821. {
  822. struct gendisk *disk = dev_to_disk(dev);
  823. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  824. }
  825. static ssize_t disk_capability_show(struct device *dev,
  826. struct device_attribute *attr, char *buf)
  827. {
  828. struct gendisk *disk = dev_to_disk(dev);
  829. return sprintf(buf, "%x\n", disk->flags);
  830. }
  831. static ssize_t disk_alignment_offset_show(struct device *dev,
  832. struct device_attribute *attr,
  833. char *buf)
  834. {
  835. struct gendisk *disk = dev_to_disk(dev);
  836. return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
  837. }
  838. static ssize_t disk_discard_alignment_show(struct device *dev,
  839. struct device_attribute *attr,
  840. char *buf)
  841. {
  842. struct gendisk *disk = dev_to_disk(dev);
  843. return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
  844. }
  845. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  846. static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
  847. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  848. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  849. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  850. static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
  851. static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
  852. NULL);
  853. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  854. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  855. static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
  856. static DEVICE_ATTR(badblocks, S_IRUGO | S_IWUSR, disk_badblocks_show,
  857. disk_badblocks_store);
  858. #ifdef CONFIG_FAIL_MAKE_REQUEST
  859. static struct device_attribute dev_attr_fail =
  860. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  861. #endif
  862. #ifdef CONFIG_FAIL_IO_TIMEOUT
  863. static struct device_attribute dev_attr_fail_timeout =
  864. __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
  865. part_timeout_store);
  866. #endif
  867. static struct attribute *disk_attrs[] = {
  868. &dev_attr_range.attr,
  869. &dev_attr_ext_range.attr,
  870. &dev_attr_removable.attr,
  871. &dev_attr_ro.attr,
  872. &dev_attr_size.attr,
  873. &dev_attr_alignment_offset.attr,
  874. &dev_attr_discard_alignment.attr,
  875. &dev_attr_capability.attr,
  876. &dev_attr_stat.attr,
  877. &dev_attr_inflight.attr,
  878. &dev_attr_badblocks.attr,
  879. #ifdef CONFIG_FAIL_MAKE_REQUEST
  880. &dev_attr_fail.attr,
  881. #endif
  882. #ifdef CONFIG_FAIL_IO_TIMEOUT
  883. &dev_attr_fail_timeout.attr,
  884. #endif
  885. NULL
  886. };
  887. static struct attribute_group disk_attr_group = {
  888. .attrs = disk_attrs,
  889. };
  890. static const struct attribute_group *disk_attr_groups[] = {
  891. &disk_attr_group,
  892. NULL
  893. };
  894. /**
  895. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  896. * @disk: disk to replace part_tbl for
  897. * @new_ptbl: new part_tbl to install
  898. *
  899. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  900. * original ptbl is freed using RCU callback.
  901. *
  902. * LOCKING:
  903. * Matching bd_mutx locked.
  904. */
  905. static void disk_replace_part_tbl(struct gendisk *disk,
  906. struct disk_part_tbl *new_ptbl)
  907. {
  908. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  909. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  910. if (old_ptbl) {
  911. rcu_assign_pointer(old_ptbl->last_lookup, NULL);
  912. kfree_rcu(old_ptbl, rcu_head);
  913. }
  914. }
  915. /**
  916. * disk_expand_part_tbl - expand disk->part_tbl
  917. * @disk: disk to expand part_tbl for
  918. * @partno: expand such that this partno can fit in
  919. *
  920. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  921. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  922. *
  923. * LOCKING:
  924. * Matching bd_mutex locked, might sleep.
  925. *
  926. * RETURNS:
  927. * 0 on success, -errno on failure.
  928. */
  929. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  930. {
  931. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  932. struct disk_part_tbl *new_ptbl;
  933. int len = old_ptbl ? old_ptbl->len : 0;
  934. int i, target;
  935. size_t size;
  936. /*
  937. * check for int overflow, since we can get here from blkpg_ioctl()
  938. * with a user passed 'partno'.
  939. */
  940. target = partno + 1;
  941. if (target < 0)
  942. return -EINVAL;
  943. /* disk_max_parts() is zero during initialization, ignore if so */
  944. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  945. return -EINVAL;
  946. if (target <= len)
  947. return 0;
  948. size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
  949. new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
  950. if (!new_ptbl)
  951. return -ENOMEM;
  952. new_ptbl->len = target;
  953. for (i = 0; i < len; i++)
  954. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  955. disk_replace_part_tbl(disk, new_ptbl);
  956. return 0;
  957. }
  958. static void disk_release(struct device *dev)
  959. {
  960. struct gendisk *disk = dev_to_disk(dev);
  961. blk_free_devt(dev->devt);
  962. disk_release_events(disk);
  963. kfree(disk->random);
  964. disk_replace_part_tbl(disk, NULL);
  965. hd_free_part(&disk->part0);
  966. if (disk->queue)
  967. blk_put_queue(disk->queue);
  968. kfree(disk);
  969. }
  970. struct class block_class = {
  971. .name = "block",
  972. };
  973. static char *block_devnode(struct device *dev, umode_t *mode,
  974. kuid_t *uid, kgid_t *gid)
  975. {
  976. struct gendisk *disk = dev_to_disk(dev);
  977. if (disk->devnode)
  978. return disk->devnode(disk, mode);
  979. return NULL;
  980. }
  981. static struct device_type disk_type = {
  982. .name = "disk",
  983. .groups = disk_attr_groups,
  984. .release = disk_release,
  985. .devnode = block_devnode,
  986. };
  987. #ifdef CONFIG_PROC_FS
  988. /*
  989. * aggregate disk stat collector. Uses the same stats that the sysfs
  990. * entries do, above, but makes them available through one seq_file.
  991. *
  992. * The output looks suspiciously like /proc/partitions with a bunch of
  993. * extra fields.
  994. */
  995. static int diskstats_show(struct seq_file *seqf, void *v)
  996. {
  997. struct gendisk *gp = v;
  998. struct disk_part_iter piter;
  999. struct hd_struct *hd;
  1000. char buf[BDEVNAME_SIZE];
  1001. int cpu;
  1002. /*
  1003. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  1004. seq_puts(seqf, "major minor name"
  1005. " rio rmerge rsect ruse wio wmerge "
  1006. "wsect wuse running use aveq"
  1007. "\n\n");
  1008. */
  1009. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
  1010. while ((hd = disk_part_iter_next(&piter))) {
  1011. cpu = part_stat_lock();
  1012. part_round_stats(cpu, hd);
  1013. part_stat_unlock();
  1014. seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
  1015. "%u %lu %lu %lu %u %u %u %u\n",
  1016. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  1017. disk_name(gp, hd->partno, buf),
  1018. part_stat_read(hd, ios[READ]),
  1019. part_stat_read(hd, merges[READ]),
  1020. part_stat_read(hd, sectors[READ]),
  1021. jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
  1022. part_stat_read(hd, ios[WRITE]),
  1023. part_stat_read(hd, merges[WRITE]),
  1024. part_stat_read(hd, sectors[WRITE]),
  1025. jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
  1026. part_in_flight(hd),
  1027. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  1028. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  1029. );
  1030. }
  1031. disk_part_iter_exit(&piter);
  1032. return 0;
  1033. }
  1034. static const struct seq_operations diskstats_op = {
  1035. .start = disk_seqf_start,
  1036. .next = disk_seqf_next,
  1037. .stop = disk_seqf_stop,
  1038. .show = diskstats_show
  1039. };
  1040. static int diskstats_open(struct inode *inode, struct file *file)
  1041. {
  1042. return seq_open(file, &diskstats_op);
  1043. }
  1044. static const struct file_operations proc_diskstats_operations = {
  1045. .open = diskstats_open,
  1046. .read = seq_read,
  1047. .llseek = seq_lseek,
  1048. .release = seq_release,
  1049. };
  1050. static int __init proc_genhd_init(void)
  1051. {
  1052. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  1053. proc_create("partitions", 0, NULL, &proc_partitions_operations);
  1054. return 0;
  1055. }
  1056. module_init(proc_genhd_init);
  1057. #endif /* CONFIG_PROC_FS */
  1058. dev_t blk_lookup_devt(const char *name, int partno)
  1059. {
  1060. dev_t devt = MKDEV(0, 0);
  1061. struct class_dev_iter iter;
  1062. struct device *dev;
  1063. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  1064. while ((dev = class_dev_iter_next(&iter))) {
  1065. struct gendisk *disk = dev_to_disk(dev);
  1066. struct hd_struct *part;
  1067. if (strcmp(dev_name(dev), name))
  1068. continue;
  1069. if (partno < disk->minors) {
  1070. /* We need to return the right devno, even
  1071. * if the partition doesn't exist yet.
  1072. */
  1073. devt = MKDEV(MAJOR(dev->devt),
  1074. MINOR(dev->devt) + partno);
  1075. break;
  1076. }
  1077. part = disk_get_part(disk, partno);
  1078. if (part) {
  1079. devt = part_devt(part);
  1080. disk_put_part(part);
  1081. break;
  1082. }
  1083. disk_put_part(part);
  1084. }
  1085. class_dev_iter_exit(&iter);
  1086. return devt;
  1087. }
  1088. EXPORT_SYMBOL(blk_lookup_devt);
  1089. struct gendisk *alloc_disk(int minors)
  1090. {
  1091. return alloc_disk_node(minors, NUMA_NO_NODE);
  1092. }
  1093. EXPORT_SYMBOL(alloc_disk);
  1094. struct gendisk *alloc_disk_node(int minors, int node_id)
  1095. {
  1096. struct gendisk *disk;
  1097. disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
  1098. if (disk) {
  1099. if (!init_part_stats(&disk->part0)) {
  1100. kfree(disk);
  1101. return NULL;
  1102. }
  1103. disk->node_id = node_id;
  1104. if (disk_expand_part_tbl(disk, 0)) {
  1105. free_part_stats(&disk->part0);
  1106. kfree(disk);
  1107. return NULL;
  1108. }
  1109. disk->part_tbl->part[0] = &disk->part0;
  1110. /*
  1111. * set_capacity() and get_capacity() currently don't use
  1112. * seqcounter to read/update the part0->nr_sects. Still init
  1113. * the counter as we can read the sectors in IO submission
  1114. * patch using seqence counters.
  1115. *
  1116. * TODO: Ideally set_capacity() and get_capacity() should be
  1117. * converted to make use of bd_mutex and sequence counters.
  1118. */
  1119. seqcount_init(&disk->part0.nr_sects_seq);
  1120. if (hd_ref_init(&disk->part0)) {
  1121. hd_free_part(&disk->part0);
  1122. kfree(disk);
  1123. return NULL;
  1124. }
  1125. disk->minors = minors;
  1126. rand_initialize_disk(disk);
  1127. disk_to_dev(disk)->class = &block_class;
  1128. disk_to_dev(disk)->type = &disk_type;
  1129. device_initialize(disk_to_dev(disk));
  1130. }
  1131. return disk;
  1132. }
  1133. EXPORT_SYMBOL(alloc_disk_node);
  1134. struct kobject *get_disk(struct gendisk *disk)
  1135. {
  1136. struct module *owner;
  1137. struct kobject *kobj;
  1138. if (!disk->fops)
  1139. return NULL;
  1140. owner = disk->fops->owner;
  1141. if (owner && !try_module_get(owner))
  1142. return NULL;
  1143. kobj = kobject_get(&disk_to_dev(disk)->kobj);
  1144. if (kobj == NULL) {
  1145. module_put(owner);
  1146. return NULL;
  1147. }
  1148. return kobj;
  1149. }
  1150. EXPORT_SYMBOL(get_disk);
  1151. void put_disk(struct gendisk *disk)
  1152. {
  1153. if (disk)
  1154. kobject_put(&disk_to_dev(disk)->kobj);
  1155. }
  1156. EXPORT_SYMBOL(put_disk);
  1157. static void set_disk_ro_uevent(struct gendisk *gd, int ro)
  1158. {
  1159. char event[] = "DISK_RO=1";
  1160. char *envp[] = { event, NULL };
  1161. if (!ro)
  1162. event[8] = '0';
  1163. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  1164. }
  1165. void set_device_ro(struct block_device *bdev, int flag)
  1166. {
  1167. bdev->bd_part->policy = flag;
  1168. }
  1169. EXPORT_SYMBOL(set_device_ro);
  1170. void set_disk_ro(struct gendisk *disk, int flag)
  1171. {
  1172. struct disk_part_iter piter;
  1173. struct hd_struct *part;
  1174. if (disk->part0.policy != flag) {
  1175. set_disk_ro_uevent(disk, flag);
  1176. disk->part0.policy = flag;
  1177. }
  1178. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
  1179. while ((part = disk_part_iter_next(&piter)))
  1180. part->policy = flag;
  1181. disk_part_iter_exit(&piter);
  1182. }
  1183. EXPORT_SYMBOL(set_disk_ro);
  1184. int bdev_read_only(struct block_device *bdev)
  1185. {
  1186. if (!bdev)
  1187. return 0;
  1188. return bdev->bd_part->policy;
  1189. }
  1190. EXPORT_SYMBOL(bdev_read_only);
  1191. int invalidate_partition(struct gendisk *disk, int partno)
  1192. {
  1193. int res = 0;
  1194. struct block_device *bdev = bdget_disk(disk, partno);
  1195. if (bdev) {
  1196. fsync_bdev(bdev);
  1197. res = __invalidate_device(bdev, true);
  1198. bdput(bdev);
  1199. }
  1200. return res;
  1201. }
  1202. EXPORT_SYMBOL(invalidate_partition);
  1203. /*
  1204. * Disk events - monitor disk events like media change and eject request.
  1205. */
  1206. struct disk_events {
  1207. struct list_head node; /* all disk_event's */
  1208. struct gendisk *disk; /* the associated disk */
  1209. spinlock_t lock;
  1210. struct mutex block_mutex; /* protects blocking */
  1211. int block; /* event blocking depth */
  1212. unsigned int pending; /* events already sent out */
  1213. unsigned int clearing; /* events being cleared */
  1214. long poll_msecs; /* interval, -1 for default */
  1215. struct delayed_work dwork;
  1216. };
  1217. static const char *disk_events_strs[] = {
  1218. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
  1219. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
  1220. };
  1221. static char *disk_uevents[] = {
  1222. [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
  1223. [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
  1224. };
  1225. /* list of all disk_events */
  1226. static DEFINE_MUTEX(disk_events_mutex);
  1227. static LIST_HEAD(disk_events);
  1228. /* disable in-kernel polling by default */
  1229. static unsigned long disk_events_dfl_poll_msecs;
  1230. static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
  1231. {
  1232. struct disk_events *ev = disk->ev;
  1233. long intv_msecs = 0;
  1234. /*
  1235. * If device-specific poll interval is set, always use it. If
  1236. * the default is being used, poll iff there are events which
  1237. * can't be monitored asynchronously.
  1238. */
  1239. if (ev->poll_msecs >= 0)
  1240. intv_msecs = ev->poll_msecs;
  1241. else if (disk->events & ~disk->async_events)
  1242. intv_msecs = disk_events_dfl_poll_msecs;
  1243. return msecs_to_jiffies(intv_msecs);
  1244. }
  1245. /**
  1246. * disk_block_events - block and flush disk event checking
  1247. * @disk: disk to block events for
  1248. *
  1249. * On return from this function, it is guaranteed that event checking
  1250. * isn't in progress and won't happen until unblocked by
  1251. * disk_unblock_events(). Events blocking is counted and the actual
  1252. * unblocking happens after the matching number of unblocks are done.
  1253. *
  1254. * Note that this intentionally does not block event checking from
  1255. * disk_clear_events().
  1256. *
  1257. * CONTEXT:
  1258. * Might sleep.
  1259. */
  1260. void disk_block_events(struct gendisk *disk)
  1261. {
  1262. struct disk_events *ev = disk->ev;
  1263. unsigned long flags;
  1264. bool cancel;
  1265. if (!ev)
  1266. return;
  1267. /*
  1268. * Outer mutex ensures that the first blocker completes canceling
  1269. * the event work before further blockers are allowed to finish.
  1270. */
  1271. mutex_lock(&ev->block_mutex);
  1272. spin_lock_irqsave(&ev->lock, flags);
  1273. cancel = !ev->block++;
  1274. spin_unlock_irqrestore(&ev->lock, flags);
  1275. if (cancel)
  1276. cancel_delayed_work_sync(&disk->ev->dwork);
  1277. mutex_unlock(&ev->block_mutex);
  1278. }
  1279. static void __disk_unblock_events(struct gendisk *disk, bool check_now)
  1280. {
  1281. struct disk_events *ev = disk->ev;
  1282. unsigned long intv;
  1283. unsigned long flags;
  1284. spin_lock_irqsave(&ev->lock, flags);
  1285. if (WARN_ON_ONCE(ev->block <= 0))
  1286. goto out_unlock;
  1287. if (--ev->block)
  1288. goto out_unlock;
  1289. intv = disk_events_poll_jiffies(disk);
  1290. if (check_now)
  1291. queue_delayed_work(system_freezable_power_efficient_wq,
  1292. &ev->dwork, 0);
  1293. else if (intv)
  1294. queue_delayed_work(system_freezable_power_efficient_wq,
  1295. &ev->dwork, intv);
  1296. out_unlock:
  1297. spin_unlock_irqrestore(&ev->lock, flags);
  1298. }
  1299. /**
  1300. * disk_unblock_events - unblock disk event checking
  1301. * @disk: disk to unblock events for
  1302. *
  1303. * Undo disk_block_events(). When the block count reaches zero, it
  1304. * starts events polling if configured.
  1305. *
  1306. * CONTEXT:
  1307. * Don't care. Safe to call from irq context.
  1308. */
  1309. void disk_unblock_events(struct gendisk *disk)
  1310. {
  1311. if (disk->ev)
  1312. __disk_unblock_events(disk, false);
  1313. }
  1314. /**
  1315. * disk_flush_events - schedule immediate event checking and flushing
  1316. * @disk: disk to check and flush events for
  1317. * @mask: events to flush
  1318. *
  1319. * Schedule immediate event checking on @disk if not blocked. Events in
  1320. * @mask are scheduled to be cleared from the driver. Note that this
  1321. * doesn't clear the events from @disk->ev.
  1322. *
  1323. * CONTEXT:
  1324. * If @mask is non-zero must be called with bdev->bd_mutex held.
  1325. */
  1326. void disk_flush_events(struct gendisk *disk, unsigned int mask)
  1327. {
  1328. struct disk_events *ev = disk->ev;
  1329. if (!ev)
  1330. return;
  1331. spin_lock_irq(&ev->lock);
  1332. ev->clearing |= mask;
  1333. if (!ev->block)
  1334. mod_delayed_work(system_freezable_power_efficient_wq,
  1335. &ev->dwork, 0);
  1336. spin_unlock_irq(&ev->lock);
  1337. }
  1338. /**
  1339. * disk_clear_events - synchronously check, clear and return pending events
  1340. * @disk: disk to fetch and clear events from
  1341. * @mask: mask of events to be fetched and cleared
  1342. *
  1343. * Disk events are synchronously checked and pending events in @mask
  1344. * are cleared and returned. This ignores the block count.
  1345. *
  1346. * CONTEXT:
  1347. * Might sleep.
  1348. */
  1349. unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
  1350. {
  1351. const struct block_device_operations *bdops = disk->fops;
  1352. struct disk_events *ev = disk->ev;
  1353. unsigned int pending;
  1354. unsigned int clearing = mask;
  1355. if (!ev) {
  1356. /* for drivers still using the old ->media_changed method */
  1357. if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
  1358. bdops->media_changed && bdops->media_changed(disk))
  1359. return DISK_EVENT_MEDIA_CHANGE;
  1360. return 0;
  1361. }
  1362. disk_block_events(disk);
  1363. /*
  1364. * store the union of mask and ev->clearing on the stack so that the
  1365. * race with disk_flush_events does not cause ambiguity (ev->clearing
  1366. * can still be modified even if events are blocked).
  1367. */
  1368. spin_lock_irq(&ev->lock);
  1369. clearing |= ev->clearing;
  1370. ev->clearing = 0;
  1371. spin_unlock_irq(&ev->lock);
  1372. disk_check_events(ev, &clearing);
  1373. /*
  1374. * if ev->clearing is not 0, the disk_flush_events got called in the
  1375. * middle of this function, so we want to run the workfn without delay.
  1376. */
  1377. __disk_unblock_events(disk, ev->clearing ? true : false);
  1378. /* then, fetch and clear pending events */
  1379. spin_lock_irq(&ev->lock);
  1380. pending = ev->pending & mask;
  1381. ev->pending &= ~mask;
  1382. spin_unlock_irq(&ev->lock);
  1383. WARN_ON_ONCE(clearing & mask);
  1384. return pending;
  1385. }
  1386. /*
  1387. * Separate this part out so that a different pointer for clearing_ptr can be
  1388. * passed in for disk_clear_events.
  1389. */
  1390. static void disk_events_workfn(struct work_struct *work)
  1391. {
  1392. struct delayed_work *dwork = to_delayed_work(work);
  1393. struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
  1394. disk_check_events(ev, &ev->clearing);
  1395. }
  1396. static void disk_check_events(struct disk_events *ev,
  1397. unsigned int *clearing_ptr)
  1398. {
  1399. struct gendisk *disk = ev->disk;
  1400. char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
  1401. unsigned int clearing = *clearing_ptr;
  1402. unsigned int events;
  1403. unsigned long intv;
  1404. int nr_events = 0, i;
  1405. /* check events */
  1406. events = disk->fops->check_events(disk, clearing);
  1407. /* accumulate pending events and schedule next poll if necessary */
  1408. spin_lock_irq(&ev->lock);
  1409. events &= ~ev->pending;
  1410. ev->pending |= events;
  1411. *clearing_ptr &= ~clearing;
  1412. intv = disk_events_poll_jiffies(disk);
  1413. if (!ev->block && intv)
  1414. queue_delayed_work(system_freezable_power_efficient_wq,
  1415. &ev->dwork, intv);
  1416. spin_unlock_irq(&ev->lock);
  1417. /*
  1418. * Tell userland about new events. Only the events listed in
  1419. * @disk->events are reported. Unlisted events are processed the
  1420. * same internally but never get reported to userland.
  1421. */
  1422. for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
  1423. if (events & disk->events & (1 << i))
  1424. envp[nr_events++] = disk_uevents[i];
  1425. if (nr_events)
  1426. kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
  1427. }
  1428. /*
  1429. * A disk events enabled device has the following sysfs nodes under
  1430. * its /sys/block/X/ directory.
  1431. *
  1432. * events : list of all supported events
  1433. * events_async : list of events which can be detected w/o polling
  1434. * events_poll_msecs : polling interval, 0: disable, -1: system default
  1435. */
  1436. static ssize_t __disk_events_show(unsigned int events, char *buf)
  1437. {
  1438. const char *delim = "";
  1439. ssize_t pos = 0;
  1440. int i;
  1441. for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
  1442. if (events & (1 << i)) {
  1443. pos += sprintf(buf + pos, "%s%s",
  1444. delim, disk_events_strs[i]);
  1445. delim = " ";
  1446. }
  1447. if (pos)
  1448. pos += sprintf(buf + pos, "\n");
  1449. return pos;
  1450. }
  1451. static ssize_t disk_events_show(struct device *dev,
  1452. struct device_attribute *attr, char *buf)
  1453. {
  1454. struct gendisk *disk = dev_to_disk(dev);
  1455. return __disk_events_show(disk->events, buf);
  1456. }
  1457. static ssize_t disk_events_async_show(struct device *dev,
  1458. struct device_attribute *attr, char *buf)
  1459. {
  1460. struct gendisk *disk = dev_to_disk(dev);
  1461. return __disk_events_show(disk->async_events, buf);
  1462. }
  1463. static ssize_t disk_events_poll_msecs_show(struct device *dev,
  1464. struct device_attribute *attr,
  1465. char *buf)
  1466. {
  1467. struct gendisk *disk = dev_to_disk(dev);
  1468. return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
  1469. }
  1470. static ssize_t disk_events_poll_msecs_store(struct device *dev,
  1471. struct device_attribute *attr,
  1472. const char *buf, size_t count)
  1473. {
  1474. struct gendisk *disk = dev_to_disk(dev);
  1475. long intv;
  1476. if (!count || !sscanf(buf, "%ld", &intv))
  1477. return -EINVAL;
  1478. if (intv < 0 && intv != -1)
  1479. return -EINVAL;
  1480. disk_block_events(disk);
  1481. disk->ev->poll_msecs = intv;
  1482. __disk_unblock_events(disk, true);
  1483. return count;
  1484. }
  1485. static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
  1486. static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
  1487. static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
  1488. disk_events_poll_msecs_show,
  1489. disk_events_poll_msecs_store);
  1490. static const struct attribute *disk_events_attrs[] = {
  1491. &dev_attr_events.attr,
  1492. &dev_attr_events_async.attr,
  1493. &dev_attr_events_poll_msecs.attr,
  1494. NULL,
  1495. };
  1496. /*
  1497. * The default polling interval can be specified by the kernel
  1498. * parameter block.events_dfl_poll_msecs which defaults to 0
  1499. * (disable). This can also be modified runtime by writing to
  1500. * /sys/module/block/events_dfl_poll_msecs.
  1501. */
  1502. static int disk_events_set_dfl_poll_msecs(const char *val,
  1503. const struct kernel_param *kp)
  1504. {
  1505. struct disk_events *ev;
  1506. int ret;
  1507. ret = param_set_ulong(val, kp);
  1508. if (ret < 0)
  1509. return ret;
  1510. mutex_lock(&disk_events_mutex);
  1511. list_for_each_entry(ev, &disk_events, node)
  1512. disk_flush_events(ev->disk, 0);
  1513. mutex_unlock(&disk_events_mutex);
  1514. return 0;
  1515. }
  1516. static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
  1517. .set = disk_events_set_dfl_poll_msecs,
  1518. .get = param_get_ulong,
  1519. };
  1520. #undef MODULE_PARAM_PREFIX
  1521. #define MODULE_PARAM_PREFIX "block."
  1522. module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
  1523. &disk_events_dfl_poll_msecs, 0644);
  1524. /*
  1525. * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
  1526. */
  1527. static void disk_alloc_events(struct gendisk *disk)
  1528. {
  1529. struct disk_events *ev;
  1530. if (!disk->fops->check_events)
  1531. return;
  1532. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  1533. if (!ev) {
  1534. pr_warn("%s: failed to initialize events\n", disk->disk_name);
  1535. return;
  1536. }
  1537. INIT_LIST_HEAD(&ev->node);
  1538. ev->disk = disk;
  1539. spin_lock_init(&ev->lock);
  1540. mutex_init(&ev->block_mutex);
  1541. ev->block = 1;
  1542. ev->poll_msecs = -1;
  1543. INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
  1544. disk->ev = ev;
  1545. }
  1546. static void disk_add_events(struct gendisk *disk)
  1547. {
  1548. if (!disk->ev)
  1549. return;
  1550. /* FIXME: error handling */
  1551. if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
  1552. pr_warn("%s: failed to create sysfs files for events\n",
  1553. disk->disk_name);
  1554. mutex_lock(&disk_events_mutex);
  1555. list_add_tail(&disk->ev->node, &disk_events);
  1556. mutex_unlock(&disk_events_mutex);
  1557. /*
  1558. * Block count is initialized to 1 and the following initial
  1559. * unblock kicks it into action.
  1560. */
  1561. __disk_unblock_events(disk, true);
  1562. }
  1563. static void disk_del_events(struct gendisk *disk)
  1564. {
  1565. if (!disk->ev)
  1566. return;
  1567. disk_block_events(disk);
  1568. mutex_lock(&disk_events_mutex);
  1569. list_del_init(&disk->ev->node);
  1570. mutex_unlock(&disk_events_mutex);
  1571. sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
  1572. }
  1573. static void disk_release_events(struct gendisk *disk)
  1574. {
  1575. /* the block count should be 1 from disk_del_events() */
  1576. WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
  1577. kfree(disk->ev);
  1578. }