genhd.c 44 KB

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