genhd.c 30 KB

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