genhd.c 43 KB

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