genhd.c 44 KB

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