genhd.c 44 KB

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