genhd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 "blk.h"
  21. static DEFINE_MUTEX(block_class_lock);
  22. #ifndef CONFIG_SYSFS_DEPRECATED
  23. struct kobject *block_depr;
  24. #endif
  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. /**
  34. * disk_get_part - get partition
  35. * @disk: disk to look partition from
  36. * @partno: partition number
  37. *
  38. * Look for partition @partno from @disk. If found, increment
  39. * reference count and return it.
  40. *
  41. * CONTEXT:
  42. * Don't care.
  43. *
  44. * RETURNS:
  45. * Pointer to the found partition on success, NULL if not found.
  46. */
  47. struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
  48. {
  49. struct hd_struct *part = NULL;
  50. struct disk_part_tbl *ptbl;
  51. if (unlikely(partno < 0))
  52. return NULL;
  53. rcu_read_lock();
  54. ptbl = rcu_dereference(disk->part_tbl);
  55. if (likely(partno < ptbl->len)) {
  56. part = rcu_dereference(ptbl->part[partno]);
  57. if (part)
  58. get_device(part_to_dev(part));
  59. }
  60. rcu_read_unlock();
  61. return part;
  62. }
  63. EXPORT_SYMBOL_GPL(disk_get_part);
  64. /**
  65. * disk_part_iter_init - initialize partition iterator
  66. * @piter: iterator to initialize
  67. * @disk: disk to iterate over
  68. * @flags: DISK_PITER_* flags
  69. *
  70. * Initialize @piter so that it iterates over partitions of @disk.
  71. *
  72. * CONTEXT:
  73. * Don't care.
  74. */
  75. void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
  76. unsigned int flags)
  77. {
  78. struct disk_part_tbl *ptbl;
  79. rcu_read_lock();
  80. ptbl = rcu_dereference(disk->part_tbl);
  81. piter->disk = disk;
  82. piter->part = NULL;
  83. if (flags & DISK_PITER_REVERSE)
  84. piter->idx = ptbl->len - 1;
  85. else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
  86. piter->idx = 0;
  87. else
  88. piter->idx = 1;
  89. piter->flags = flags;
  90. rcu_read_unlock();
  91. }
  92. EXPORT_SYMBOL_GPL(disk_part_iter_init);
  93. /**
  94. * disk_part_iter_next - proceed iterator to the next partition and return it
  95. * @piter: iterator of interest
  96. *
  97. * Proceed @piter to the next partition and return it.
  98. *
  99. * CONTEXT:
  100. * Don't care.
  101. */
  102. struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
  103. {
  104. struct disk_part_tbl *ptbl;
  105. int inc, end;
  106. /* put the last partition */
  107. disk_put_part(piter->part);
  108. piter->part = NULL;
  109. /* get part_tbl */
  110. rcu_read_lock();
  111. ptbl = rcu_dereference(piter->disk->part_tbl);
  112. /* determine iteration parameters */
  113. if (piter->flags & DISK_PITER_REVERSE) {
  114. inc = -1;
  115. if (piter->flags & (DISK_PITER_INCL_PART0 |
  116. DISK_PITER_INCL_EMPTY_PART0))
  117. end = -1;
  118. else
  119. end = 0;
  120. } else {
  121. inc = 1;
  122. end = ptbl->len;
  123. }
  124. /* iterate to the next partition */
  125. for (; piter->idx != end; piter->idx += inc) {
  126. struct hd_struct *part;
  127. part = rcu_dereference(ptbl->part[piter->idx]);
  128. if (!part)
  129. continue;
  130. if (!part->nr_sects &&
  131. !(piter->flags & DISK_PITER_INCL_EMPTY) &&
  132. !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
  133. piter->idx == 0))
  134. continue;
  135. get_device(part_to_dev(part));
  136. piter->part = part;
  137. piter->idx += inc;
  138. break;
  139. }
  140. rcu_read_unlock();
  141. return piter->part;
  142. }
  143. EXPORT_SYMBOL_GPL(disk_part_iter_next);
  144. /**
  145. * disk_part_iter_exit - finish up partition iteration
  146. * @piter: iter of interest
  147. *
  148. * Called when iteration is over. Cleans up @piter.
  149. *
  150. * CONTEXT:
  151. * Don't care.
  152. */
  153. void disk_part_iter_exit(struct disk_part_iter *piter)
  154. {
  155. disk_put_part(piter->part);
  156. piter->part = NULL;
  157. }
  158. EXPORT_SYMBOL_GPL(disk_part_iter_exit);
  159. static inline int sector_in_part(struct hd_struct *part, sector_t sector)
  160. {
  161. return part->start_sect <= sector &&
  162. sector < part->start_sect + part->nr_sects;
  163. }
  164. /**
  165. * disk_map_sector_rcu - map sector to partition
  166. * @disk: gendisk of interest
  167. * @sector: sector to map
  168. *
  169. * Find out which partition @sector maps to on @disk. This is
  170. * primarily used for stats accounting.
  171. *
  172. * CONTEXT:
  173. * RCU read locked. The returned partition pointer is valid only
  174. * while preemption is disabled.
  175. *
  176. * RETURNS:
  177. * Found partition on success, part0 is returned if no partition matches
  178. */
  179. struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
  180. {
  181. struct disk_part_tbl *ptbl;
  182. struct hd_struct *part;
  183. int i;
  184. ptbl = rcu_dereference(disk->part_tbl);
  185. part = rcu_dereference(ptbl->last_lookup);
  186. if (part && sector_in_part(part, sector))
  187. return part;
  188. for (i = 1; i < ptbl->len; i++) {
  189. part = rcu_dereference(ptbl->part[i]);
  190. if (part && sector_in_part(part, sector)) {
  191. rcu_assign_pointer(ptbl->last_lookup, part);
  192. return part;
  193. }
  194. }
  195. return &disk->part0;
  196. }
  197. EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
  198. /*
  199. * Can be deleted altogether. Later.
  200. *
  201. */
  202. static struct blk_major_name {
  203. struct blk_major_name *next;
  204. int major;
  205. char name[16];
  206. } *major_names[BLKDEV_MAJOR_HASH_SIZE];
  207. /* index in the above - for now: assume no multimajor ranges */
  208. static inline int major_to_index(int major)
  209. {
  210. return major % BLKDEV_MAJOR_HASH_SIZE;
  211. }
  212. #ifdef CONFIG_PROC_FS
  213. void blkdev_show(struct seq_file *seqf, off_t offset)
  214. {
  215. struct blk_major_name *dp;
  216. if (offset < BLKDEV_MAJOR_HASH_SIZE) {
  217. mutex_lock(&block_class_lock);
  218. for (dp = major_names[offset]; dp; dp = dp->next)
  219. seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
  220. mutex_unlock(&block_class_lock);
  221. }
  222. }
  223. #endif /* CONFIG_PROC_FS */
  224. /**
  225. * register_blkdev - register a new block device
  226. *
  227. * @major: the requested major device number [1..255]. If @major=0, try to
  228. * allocate any unused major number.
  229. * @name: the name of the new block device as a zero terminated string
  230. *
  231. * The @name must be unique within the system.
  232. *
  233. * The return value depends on the @major input parameter.
  234. * - if a major device number was requested in range [1..255] then the
  235. * function returns zero on success, or a negative error code
  236. * - if any unused major number was requested with @major=0 parameter
  237. * then the return value is the allocated major number in range
  238. * [1..255] or a negative error code otherwise
  239. */
  240. int register_blkdev(unsigned int major, const char *name)
  241. {
  242. struct blk_major_name **n, *p;
  243. int index, ret = 0;
  244. mutex_lock(&block_class_lock);
  245. /* temporary */
  246. if (major == 0) {
  247. for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
  248. if (major_names[index] == NULL)
  249. break;
  250. }
  251. if (index == 0) {
  252. printk("register_blkdev: failed to get major for %s\n",
  253. name);
  254. ret = -EBUSY;
  255. goto out;
  256. }
  257. major = index;
  258. ret = major;
  259. }
  260. p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
  261. if (p == NULL) {
  262. ret = -ENOMEM;
  263. goto out;
  264. }
  265. p->major = major;
  266. strlcpy(p->name, name, sizeof(p->name));
  267. p->next = NULL;
  268. index = major_to_index(major);
  269. for (n = &major_names[index]; *n; n = &(*n)->next) {
  270. if ((*n)->major == major)
  271. break;
  272. }
  273. if (!*n)
  274. *n = p;
  275. else
  276. ret = -EBUSY;
  277. if (ret < 0) {
  278. printk("register_blkdev: cannot get major %d for %s\n",
  279. major, name);
  280. kfree(p);
  281. }
  282. out:
  283. mutex_unlock(&block_class_lock);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL(register_blkdev);
  287. void unregister_blkdev(unsigned int major, const char *name)
  288. {
  289. struct blk_major_name **n;
  290. struct blk_major_name *p = NULL;
  291. int index = major_to_index(major);
  292. mutex_lock(&block_class_lock);
  293. for (n = &major_names[index]; *n; n = &(*n)->next)
  294. if ((*n)->major == major)
  295. break;
  296. if (!*n || strcmp((*n)->name, name)) {
  297. WARN_ON(1);
  298. } else {
  299. p = *n;
  300. *n = p->next;
  301. }
  302. mutex_unlock(&block_class_lock);
  303. kfree(p);
  304. }
  305. EXPORT_SYMBOL(unregister_blkdev);
  306. static struct kobj_map *bdev_map;
  307. /**
  308. * blk_mangle_minor - scatter minor numbers apart
  309. * @minor: minor number to mangle
  310. *
  311. * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
  312. * is enabled. Mangling twice gives the original value.
  313. *
  314. * RETURNS:
  315. * Mangled value.
  316. *
  317. * CONTEXT:
  318. * Don't care.
  319. */
  320. static int blk_mangle_minor(int minor)
  321. {
  322. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  323. int i;
  324. for (i = 0; i < MINORBITS / 2; i++) {
  325. int low = minor & (1 << i);
  326. int high = minor & (1 << (MINORBITS - 1 - i));
  327. int distance = MINORBITS - 1 - 2 * i;
  328. minor ^= low | high; /* clear both bits */
  329. low <<= distance; /* swap the positions */
  330. high >>= distance;
  331. minor |= low | high; /* and set */
  332. }
  333. #endif
  334. return minor;
  335. }
  336. /**
  337. * blk_alloc_devt - allocate a dev_t for a partition
  338. * @part: partition to allocate dev_t for
  339. * @devt: out parameter for resulting dev_t
  340. *
  341. * Allocate a dev_t for block device.
  342. *
  343. * RETURNS:
  344. * 0 on success, allocated dev_t is returned in *@devt. -errno on
  345. * failure.
  346. *
  347. * CONTEXT:
  348. * Might sleep.
  349. */
  350. int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
  351. {
  352. struct gendisk *disk = part_to_disk(part);
  353. int idx, rc;
  354. /* in consecutive minor range? */
  355. if (part->partno < disk->minors) {
  356. *devt = MKDEV(disk->major, disk->first_minor + part->partno);
  357. return 0;
  358. }
  359. /* allocate ext devt */
  360. do {
  361. if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
  362. return -ENOMEM;
  363. rc = idr_get_new(&ext_devt_idr, part, &idx);
  364. } while (rc == -EAGAIN);
  365. if (rc)
  366. return rc;
  367. if (idx > MAX_EXT_DEVT) {
  368. idr_remove(&ext_devt_idr, idx);
  369. return -EBUSY;
  370. }
  371. *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
  372. return 0;
  373. }
  374. /**
  375. * blk_free_devt - free a dev_t
  376. * @devt: dev_t to free
  377. *
  378. * Free @devt which was allocated using blk_alloc_devt().
  379. *
  380. * CONTEXT:
  381. * Might sleep.
  382. */
  383. void blk_free_devt(dev_t devt)
  384. {
  385. might_sleep();
  386. if (devt == MKDEV(0, 0))
  387. return;
  388. if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
  389. mutex_lock(&ext_devt_mutex);
  390. idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  391. mutex_unlock(&ext_devt_mutex);
  392. }
  393. }
  394. static char *bdevt_str(dev_t devt, char *buf)
  395. {
  396. if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
  397. char tbuf[BDEVT_SIZE];
  398. snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
  399. snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
  400. } else
  401. snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
  402. return buf;
  403. }
  404. /*
  405. * Register device numbers dev..(dev+range-1)
  406. * range must be nonzero
  407. * The hash chain is sorted on range, so that subranges can override.
  408. */
  409. void blk_register_region(dev_t devt, unsigned long range, struct module *module,
  410. struct kobject *(*probe)(dev_t, int *, void *),
  411. int (*lock)(dev_t, void *), void *data)
  412. {
  413. kobj_map(bdev_map, devt, range, module, probe, lock, data);
  414. }
  415. EXPORT_SYMBOL(blk_register_region);
  416. void blk_unregister_region(dev_t devt, unsigned long range)
  417. {
  418. kobj_unmap(bdev_map, devt, range);
  419. }
  420. EXPORT_SYMBOL(blk_unregister_region);
  421. static struct kobject *exact_match(dev_t devt, int *partno, void *data)
  422. {
  423. struct gendisk *p = data;
  424. return &disk_to_dev(p)->kobj;
  425. }
  426. static int exact_lock(dev_t devt, void *data)
  427. {
  428. struct gendisk *p = data;
  429. if (!get_disk(p))
  430. return -1;
  431. return 0;
  432. }
  433. /**
  434. * add_disk - add partitioning information to kernel list
  435. * @disk: per-device partitioning information
  436. *
  437. * This function registers the partitioning information in @disk
  438. * with the kernel.
  439. *
  440. * FIXME: error handling
  441. */
  442. void add_disk(struct gendisk *disk)
  443. {
  444. struct backing_dev_info *bdi;
  445. dev_t devt;
  446. int retval;
  447. /* minors == 0 indicates to use ext devt from part0 and should
  448. * be accompanied with EXT_DEVT flag. Make sure all
  449. * parameters make sense.
  450. */
  451. WARN_ON(disk->minors && !(disk->major || disk->first_minor));
  452. WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
  453. disk->flags |= GENHD_FL_UP;
  454. retval = blk_alloc_devt(&disk->part0, &devt);
  455. if (retval) {
  456. WARN_ON(1);
  457. return;
  458. }
  459. disk_to_dev(disk)->devt = devt;
  460. /* ->major and ->first_minor aren't supposed to be
  461. * dereferenced from here on, but set them just in case.
  462. */
  463. disk->major = MAJOR(devt);
  464. disk->first_minor = MINOR(devt);
  465. blk_register_region(disk_devt(disk), disk->minors, NULL,
  466. exact_match, exact_lock, disk);
  467. register_disk(disk);
  468. blk_register_queue(disk);
  469. bdi = &disk->queue->backing_dev_info;
  470. bdi_register_dev(bdi, disk_devt(disk));
  471. retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
  472. "bdi");
  473. WARN_ON(retval);
  474. }
  475. EXPORT_SYMBOL(add_disk);
  476. EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
  477. void unlink_gendisk(struct gendisk *disk)
  478. {
  479. sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
  480. bdi_unregister(&disk->queue->backing_dev_info);
  481. blk_unregister_queue(disk);
  482. blk_unregister_region(disk_devt(disk), disk->minors);
  483. }
  484. /**
  485. * get_gendisk - get partitioning information for a given device
  486. * @devt: device to get partitioning information for
  487. * @partno: returned partition index
  488. *
  489. * This function gets the structure containing partitioning
  490. * information for the given device @devt.
  491. */
  492. struct gendisk *get_gendisk(dev_t devt, int *partno)
  493. {
  494. struct gendisk *disk = NULL;
  495. if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
  496. struct kobject *kobj;
  497. kobj = kobj_lookup(bdev_map, devt, partno);
  498. if (kobj)
  499. disk = dev_to_disk(kobj_to_dev(kobj));
  500. } else {
  501. struct hd_struct *part;
  502. mutex_lock(&ext_devt_mutex);
  503. part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
  504. if (part && get_disk(part_to_disk(part))) {
  505. *partno = part->partno;
  506. disk = part_to_disk(part);
  507. }
  508. mutex_unlock(&ext_devt_mutex);
  509. }
  510. return disk;
  511. }
  512. /**
  513. * bdget_disk - do bdget() by gendisk and partition number
  514. * @disk: gendisk of interest
  515. * @partno: partition number
  516. *
  517. * Find partition @partno from @disk, do bdget() on it.
  518. *
  519. * CONTEXT:
  520. * Don't care.
  521. *
  522. * RETURNS:
  523. * Resulting block_device on success, NULL on failure.
  524. */
  525. struct block_device *bdget_disk(struct gendisk *disk, int partno)
  526. {
  527. struct hd_struct *part;
  528. struct block_device *bdev = NULL;
  529. part = disk_get_part(disk, partno);
  530. if (part)
  531. bdev = bdget(part_devt(part));
  532. disk_put_part(part);
  533. return bdev;
  534. }
  535. EXPORT_SYMBOL(bdget_disk);
  536. /*
  537. * print a full list of all partitions - intended for places where the root
  538. * filesystem can't be mounted and thus to give the victim some idea of what
  539. * went wrong
  540. */
  541. void __init printk_all_partitions(void)
  542. {
  543. struct class_dev_iter iter;
  544. struct device *dev;
  545. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  546. while ((dev = class_dev_iter_next(&iter))) {
  547. struct gendisk *disk = dev_to_disk(dev);
  548. struct disk_part_iter piter;
  549. struct hd_struct *part;
  550. char name_buf[BDEVNAME_SIZE];
  551. char devt_buf[BDEVT_SIZE];
  552. /*
  553. * Don't show empty devices or things that have been
  554. * surpressed
  555. */
  556. if (get_capacity(disk) == 0 ||
  557. (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
  558. continue;
  559. /*
  560. * Note, unlike /proc/partitions, I am showing the
  561. * numbers in hex - the same format as the root=
  562. * option takes.
  563. */
  564. disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  565. while ((part = disk_part_iter_next(&piter))) {
  566. bool is_part0 = part == &disk->part0;
  567. printk("%s%s %10llu %s", is_part0 ? "" : " ",
  568. bdevt_str(part_devt(part), devt_buf),
  569. (unsigned long long)part->nr_sects >> 1,
  570. disk_name(disk, part->partno, name_buf));
  571. if (is_part0) {
  572. if (disk->driverfs_dev != NULL &&
  573. disk->driverfs_dev->driver != NULL)
  574. printk(" driver: %s\n",
  575. disk->driverfs_dev->driver->name);
  576. else
  577. printk(" (driver?)\n");
  578. } else
  579. printk("\n");
  580. }
  581. disk_part_iter_exit(&piter);
  582. }
  583. class_dev_iter_exit(&iter);
  584. }
  585. #ifdef CONFIG_PROC_FS
  586. /* iterator */
  587. static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
  588. {
  589. loff_t skip = *pos;
  590. struct class_dev_iter *iter;
  591. struct device *dev;
  592. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  593. if (!iter)
  594. return ERR_PTR(-ENOMEM);
  595. seqf->private = iter;
  596. class_dev_iter_init(iter, &block_class, NULL, &disk_type);
  597. do {
  598. dev = class_dev_iter_next(iter);
  599. if (!dev)
  600. return NULL;
  601. } while (skip--);
  602. return dev_to_disk(dev);
  603. }
  604. static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
  605. {
  606. struct device *dev;
  607. (*pos)++;
  608. dev = class_dev_iter_next(seqf->private);
  609. if (dev)
  610. return dev_to_disk(dev);
  611. return NULL;
  612. }
  613. static void disk_seqf_stop(struct seq_file *seqf, void *v)
  614. {
  615. struct class_dev_iter *iter = seqf->private;
  616. /* stop is called even after start failed :-( */
  617. if (iter) {
  618. class_dev_iter_exit(iter);
  619. kfree(iter);
  620. }
  621. }
  622. static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
  623. {
  624. static void *p;
  625. p = disk_seqf_start(seqf, pos);
  626. if (!IS_ERR(p) && p && !*pos)
  627. seq_puts(seqf, "major minor #blocks name\n\n");
  628. return p;
  629. }
  630. static int show_partition(struct seq_file *seqf, void *v)
  631. {
  632. struct gendisk *sgp = v;
  633. struct disk_part_iter piter;
  634. struct hd_struct *part;
  635. char buf[BDEVNAME_SIZE];
  636. /* Don't show non-partitionable removeable devices or empty devices */
  637. if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
  638. (sgp->flags & GENHD_FL_REMOVABLE)))
  639. return 0;
  640. if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
  641. return 0;
  642. /* show the full disk and all non-0 size partitions of it */
  643. disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
  644. while ((part = disk_part_iter_next(&piter)))
  645. seq_printf(seqf, "%4d %7d %10llu %s\n",
  646. MAJOR(part_devt(part)), MINOR(part_devt(part)),
  647. (unsigned long long)part->nr_sects >> 1,
  648. disk_name(sgp, part->partno, buf));
  649. disk_part_iter_exit(&piter);
  650. return 0;
  651. }
  652. static const struct seq_operations partitions_op = {
  653. .start = show_partition_start,
  654. .next = disk_seqf_next,
  655. .stop = disk_seqf_stop,
  656. .show = show_partition
  657. };
  658. static int partitions_open(struct inode *inode, struct file *file)
  659. {
  660. return seq_open(file, &partitions_op);
  661. }
  662. static const struct file_operations proc_partitions_operations = {
  663. .open = partitions_open,
  664. .read = seq_read,
  665. .llseek = seq_lseek,
  666. .release = seq_release,
  667. };
  668. #endif
  669. static struct kobject *base_probe(dev_t devt, int *partno, void *data)
  670. {
  671. if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
  672. /* Make old-style 2.4 aliases work */
  673. request_module("block-major-%d", MAJOR(devt));
  674. return NULL;
  675. }
  676. static int __init genhd_device_init(void)
  677. {
  678. int error;
  679. block_class.dev_kobj = sysfs_dev_block_kobj;
  680. error = class_register(&block_class);
  681. if (unlikely(error))
  682. return error;
  683. bdev_map = kobj_map_init(base_probe, &block_class_lock);
  684. blk_dev_init();
  685. register_blkdev(BLOCK_EXT_MAJOR, "blkext");
  686. #ifndef CONFIG_SYSFS_DEPRECATED
  687. /* create top-level block dir */
  688. block_depr = kobject_create_and_add("block", NULL);
  689. #endif
  690. return 0;
  691. }
  692. subsys_initcall(genhd_device_init);
  693. static ssize_t disk_range_show(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. struct gendisk *disk = dev_to_disk(dev);
  697. return sprintf(buf, "%d\n", disk->minors);
  698. }
  699. static ssize_t disk_ext_range_show(struct device *dev,
  700. struct device_attribute *attr, char *buf)
  701. {
  702. struct gendisk *disk = dev_to_disk(dev);
  703. return sprintf(buf, "%d\n", disk_max_parts(disk));
  704. }
  705. static ssize_t disk_removable_show(struct device *dev,
  706. struct device_attribute *attr, char *buf)
  707. {
  708. struct gendisk *disk = dev_to_disk(dev);
  709. return sprintf(buf, "%d\n",
  710. (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
  711. }
  712. static ssize_t disk_ro_show(struct device *dev,
  713. struct device_attribute *attr, char *buf)
  714. {
  715. struct gendisk *disk = dev_to_disk(dev);
  716. return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
  717. }
  718. static ssize_t disk_capability_show(struct device *dev,
  719. struct device_attribute *attr, char *buf)
  720. {
  721. struct gendisk *disk = dev_to_disk(dev);
  722. return sprintf(buf, "%x\n", disk->flags);
  723. }
  724. static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
  725. static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
  726. static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
  727. static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
  728. static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
  729. static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
  730. static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
  731. #ifdef CONFIG_FAIL_MAKE_REQUEST
  732. static struct device_attribute dev_attr_fail =
  733. __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
  734. #endif
  735. #ifdef CONFIG_FAIL_IO_TIMEOUT
  736. static struct device_attribute dev_attr_fail_timeout =
  737. __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
  738. part_timeout_store);
  739. #endif
  740. static struct attribute *disk_attrs[] = {
  741. &dev_attr_range.attr,
  742. &dev_attr_ext_range.attr,
  743. &dev_attr_removable.attr,
  744. &dev_attr_ro.attr,
  745. &dev_attr_size.attr,
  746. &dev_attr_capability.attr,
  747. &dev_attr_stat.attr,
  748. #ifdef CONFIG_FAIL_MAKE_REQUEST
  749. &dev_attr_fail.attr,
  750. #endif
  751. #ifdef CONFIG_FAIL_IO_TIMEOUT
  752. &dev_attr_fail_timeout.attr,
  753. #endif
  754. NULL
  755. };
  756. static struct attribute_group disk_attr_group = {
  757. .attrs = disk_attrs,
  758. };
  759. static struct attribute_group *disk_attr_groups[] = {
  760. &disk_attr_group,
  761. NULL
  762. };
  763. static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
  764. {
  765. struct disk_part_tbl *ptbl =
  766. container_of(head, struct disk_part_tbl, rcu_head);
  767. kfree(ptbl);
  768. }
  769. /**
  770. * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
  771. * @disk: disk to replace part_tbl for
  772. * @new_ptbl: new part_tbl to install
  773. *
  774. * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
  775. * original ptbl is freed using RCU callback.
  776. *
  777. * LOCKING:
  778. * Matching bd_mutx locked.
  779. */
  780. static void disk_replace_part_tbl(struct gendisk *disk,
  781. struct disk_part_tbl *new_ptbl)
  782. {
  783. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  784. rcu_assign_pointer(disk->part_tbl, new_ptbl);
  785. if (old_ptbl) {
  786. rcu_assign_pointer(old_ptbl->last_lookup, NULL);
  787. call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
  788. }
  789. }
  790. /**
  791. * disk_expand_part_tbl - expand disk->part_tbl
  792. * @disk: disk to expand part_tbl for
  793. * @partno: expand such that this partno can fit in
  794. *
  795. * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
  796. * uses RCU to allow unlocked dereferencing for stats and other stuff.
  797. *
  798. * LOCKING:
  799. * Matching bd_mutex locked, might sleep.
  800. *
  801. * RETURNS:
  802. * 0 on success, -errno on failure.
  803. */
  804. int disk_expand_part_tbl(struct gendisk *disk, int partno)
  805. {
  806. struct disk_part_tbl *old_ptbl = disk->part_tbl;
  807. struct disk_part_tbl *new_ptbl;
  808. int len = old_ptbl ? old_ptbl->len : 0;
  809. int target = partno + 1;
  810. size_t size;
  811. int i;
  812. /* disk_max_parts() is zero during initialization, ignore if so */
  813. if (disk_max_parts(disk) && target > disk_max_parts(disk))
  814. return -EINVAL;
  815. if (target <= len)
  816. return 0;
  817. size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
  818. new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
  819. if (!new_ptbl)
  820. return -ENOMEM;
  821. INIT_RCU_HEAD(&new_ptbl->rcu_head);
  822. new_ptbl->len = target;
  823. for (i = 0; i < len; i++)
  824. rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
  825. disk_replace_part_tbl(disk, new_ptbl);
  826. return 0;
  827. }
  828. static void disk_release(struct device *dev)
  829. {
  830. struct gendisk *disk = dev_to_disk(dev);
  831. kfree(disk->random);
  832. disk_replace_part_tbl(disk, NULL);
  833. free_part_stats(&disk->part0);
  834. kfree(disk);
  835. }
  836. struct class block_class = {
  837. .name = "block",
  838. };
  839. static struct device_type disk_type = {
  840. .name = "disk",
  841. .groups = disk_attr_groups,
  842. .release = disk_release,
  843. };
  844. #ifdef CONFIG_PROC_FS
  845. /*
  846. * aggregate disk stat collector. Uses the same stats that the sysfs
  847. * entries do, above, but makes them available through one seq_file.
  848. *
  849. * The output looks suspiciously like /proc/partitions with a bunch of
  850. * extra fields.
  851. */
  852. static int diskstats_show(struct seq_file *seqf, void *v)
  853. {
  854. struct gendisk *gp = v;
  855. struct disk_part_iter piter;
  856. struct hd_struct *hd;
  857. char buf[BDEVNAME_SIZE];
  858. int cpu;
  859. /*
  860. if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
  861. seq_puts(seqf, "major minor name"
  862. " rio rmerge rsect ruse wio wmerge "
  863. "wsect wuse running use aveq"
  864. "\n\n");
  865. */
  866. disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
  867. while ((hd = disk_part_iter_next(&piter))) {
  868. cpu = part_stat_lock();
  869. part_round_stats(cpu, hd);
  870. part_stat_unlock();
  871. seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
  872. "%u %lu %lu %llu %u %u %u %u\n",
  873. MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
  874. disk_name(gp, hd->partno, buf),
  875. part_stat_read(hd, ios[0]),
  876. part_stat_read(hd, merges[0]),
  877. (unsigned long long)part_stat_read(hd, sectors[0]),
  878. jiffies_to_msecs(part_stat_read(hd, ticks[0])),
  879. part_stat_read(hd, ios[1]),
  880. part_stat_read(hd, merges[1]),
  881. (unsigned long long)part_stat_read(hd, sectors[1]),
  882. jiffies_to_msecs(part_stat_read(hd, ticks[1])),
  883. hd->in_flight,
  884. jiffies_to_msecs(part_stat_read(hd, io_ticks)),
  885. jiffies_to_msecs(part_stat_read(hd, time_in_queue))
  886. );
  887. }
  888. disk_part_iter_exit(&piter);
  889. return 0;
  890. }
  891. static const struct seq_operations diskstats_op = {
  892. .start = disk_seqf_start,
  893. .next = disk_seqf_next,
  894. .stop = disk_seqf_stop,
  895. .show = diskstats_show
  896. };
  897. static int diskstats_open(struct inode *inode, struct file *file)
  898. {
  899. return seq_open(file, &diskstats_op);
  900. }
  901. static const struct file_operations proc_diskstats_operations = {
  902. .open = diskstats_open,
  903. .read = seq_read,
  904. .llseek = seq_lseek,
  905. .release = seq_release,
  906. };
  907. static int __init proc_genhd_init(void)
  908. {
  909. proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
  910. proc_create("partitions", 0, NULL, &proc_partitions_operations);
  911. return 0;
  912. }
  913. module_init(proc_genhd_init);
  914. #endif /* CONFIG_PROC_FS */
  915. static void media_change_notify_thread(struct work_struct *work)
  916. {
  917. struct gendisk *gd = container_of(work, struct gendisk, async_notify);
  918. char event[] = "MEDIA_CHANGE=1";
  919. char *envp[] = { event, NULL };
  920. /*
  921. * set enviroment vars to indicate which event this is for
  922. * so that user space will know to go check the media status.
  923. */
  924. kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
  925. put_device(gd->driverfs_dev);
  926. }
  927. #if 0
  928. void genhd_media_change_notify(struct gendisk *disk)
  929. {
  930. get_device(disk->driverfs_dev);
  931. schedule_work(&disk->async_notify);
  932. }
  933. EXPORT_SYMBOL_GPL(genhd_media_change_notify);
  934. #endif /* 0 */
  935. dev_t blk_lookup_devt(const char *name, int partno)
  936. {
  937. dev_t devt = MKDEV(0, 0);
  938. struct class_dev_iter iter;
  939. struct device *dev;
  940. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  941. while ((dev = class_dev_iter_next(&iter))) {
  942. struct gendisk *disk = dev_to_disk(dev);
  943. struct hd_struct *part;
  944. if (strcmp(dev_name(dev), name))
  945. continue;
  946. if (partno < disk->minors) {
  947. /* We need to return the right devno, even
  948. * if the partition doesn't exist yet.
  949. */
  950. devt = MKDEV(MAJOR(dev->devt),
  951. MINOR(dev->devt) + partno);
  952. break;
  953. }
  954. part = disk_get_part(disk, partno);
  955. if (part) {
  956. devt = part_devt(part);
  957. disk_put_part(part);
  958. break;
  959. }
  960. disk_put_part(part);
  961. }
  962. class_dev_iter_exit(&iter);
  963. return devt;
  964. }
  965. EXPORT_SYMBOL(blk_lookup_devt);
  966. struct gendisk *alloc_disk(int minors)
  967. {
  968. return alloc_disk_node(minors, -1);
  969. }
  970. EXPORT_SYMBOL(alloc_disk);
  971. struct gendisk *alloc_disk_node(int minors, int node_id)
  972. {
  973. struct gendisk *disk;
  974. disk = kmalloc_node(sizeof(struct gendisk),
  975. GFP_KERNEL | __GFP_ZERO, node_id);
  976. if (disk) {
  977. if (!init_part_stats(&disk->part0)) {
  978. kfree(disk);
  979. return NULL;
  980. }
  981. disk->node_id = node_id;
  982. if (disk_expand_part_tbl(disk, 0)) {
  983. free_part_stats(&disk->part0);
  984. kfree(disk);
  985. return NULL;
  986. }
  987. disk->part_tbl->part[0] = &disk->part0;
  988. disk->minors = minors;
  989. rand_initialize_disk(disk);
  990. disk_to_dev(disk)->class = &block_class;
  991. disk_to_dev(disk)->type = &disk_type;
  992. device_initialize(disk_to_dev(disk));
  993. INIT_WORK(&disk->async_notify,
  994. media_change_notify_thread);
  995. }
  996. return disk;
  997. }
  998. EXPORT_SYMBOL(alloc_disk_node);
  999. struct kobject *get_disk(struct gendisk *disk)
  1000. {
  1001. struct module *owner;
  1002. struct kobject *kobj;
  1003. if (!disk->fops)
  1004. return NULL;
  1005. owner = disk->fops->owner;
  1006. if (owner && !try_module_get(owner))
  1007. return NULL;
  1008. kobj = kobject_get(&disk_to_dev(disk)->kobj);
  1009. if (kobj == NULL) {
  1010. module_put(owner);
  1011. return NULL;
  1012. }
  1013. return kobj;
  1014. }
  1015. EXPORT_SYMBOL(get_disk);
  1016. void put_disk(struct gendisk *disk)
  1017. {
  1018. if (disk)
  1019. kobject_put(&disk_to_dev(disk)->kobj);
  1020. }
  1021. EXPORT_SYMBOL(put_disk);
  1022. void set_device_ro(struct block_device *bdev, int flag)
  1023. {
  1024. bdev->bd_part->policy = flag;
  1025. }
  1026. EXPORT_SYMBOL(set_device_ro);
  1027. void set_disk_ro(struct gendisk *disk, int flag)
  1028. {
  1029. struct disk_part_iter piter;
  1030. struct hd_struct *part;
  1031. disk_part_iter_init(&piter, disk,
  1032. DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
  1033. while ((part = disk_part_iter_next(&piter)))
  1034. part->policy = flag;
  1035. disk_part_iter_exit(&piter);
  1036. }
  1037. EXPORT_SYMBOL(set_disk_ro);
  1038. int bdev_read_only(struct block_device *bdev)
  1039. {
  1040. if (!bdev)
  1041. return 0;
  1042. return bdev->bd_part->policy;
  1043. }
  1044. EXPORT_SYMBOL(bdev_read_only);
  1045. int invalidate_partition(struct gendisk *disk, int partno)
  1046. {
  1047. int res = 0;
  1048. struct block_device *bdev = bdget_disk(disk, partno);
  1049. if (bdev) {
  1050. fsync_bdev(bdev);
  1051. res = __invalidate_device(bdev);
  1052. bdput(bdev);
  1053. }
  1054. return res;
  1055. }
  1056. EXPORT_SYMBOL(invalidate_partition);