genhd.c 46 KB

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