genhd.c 44 KB

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