genhd.c 50 KB

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