genhd.c 47 KB

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