genhd.c 48 KB

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