genhd.c 50 KB

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