genhd.c 44 KB

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