genhd.c 43 KB

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