uuid.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. diff -Nur linux-3.4.2.orig/block/genhd.c linux-3.4.2/block/genhd.c
  2. --- linux-3.4.2.orig/block/genhd.c 2012-06-09 17:36:33.000000000 +0200
  3. +++ linux-3.4.2/block/genhd.c 2012-06-15 18:19:16.000000000 +0200
  4. @@ -33,7 +33,7 @@
  5. static DEFINE_MUTEX(ext_devt_mutex);
  6. static DEFINE_IDR(ext_devt_idr);
  7. -static struct device_type disk_type;
  8. +struct device_type disk_type;
  9. static void disk_alloc_events(struct gendisk *disk);
  10. static void disk_add_events(struct gendisk *disk);
  11. @@ -1122,7 +1122,7 @@
  12. return NULL;
  13. }
  14. -static struct device_type disk_type = {
  15. +struct device_type disk_type = {
  16. .name = "disk",
  17. .groups = disk_attr_groups,
  18. .release = disk_release,
  19. diff -Nur linux-3.4.2.orig/init/do_mounts.c linux-3.4.2/init/do_mounts.c
  20. --- linux-3.4.2.orig/init/do_mounts.c 2012-06-09 17:36:33.000000000 +0200
  21. +++ linux-3.4.2/init/do_mounts.c 2012-06-15 18:20:11.000000000 +0200
  22. @@ -21,6 +21,7 @@
  23. #include <linux/nfs_fs_sb.h>
  24. #include <linux/nfs_mount.h>
  25. +#include "../fs/ext2/ext2.h"
  26. #include "do_mounts.h"
  27. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  28. @@ -32,6 +33,132 @@
  29. dev_t ROOT_DEV;
  30. +#ifdef CONFIG_EXT2_FS
  31. +/* support for root=UUID=ce40d6b2-18eb-4a75-aefe-7ddb0995ce63 bootargs */
  32. +
  33. +#include <linux/ext2_fs.h>
  34. +
  35. +__u8 root_dev_uuid[16];
  36. +int root_dev_type; /* 0 = normal (/dev/hda1, 0301); 1 = UUID; 3 = bad */
  37. +
  38. +/* imported from block/genhd.c after removing its static qualifier */
  39. +extern struct device_type disk_type;
  40. +
  41. +/* helper function */
  42. +static __u8 __init fromhex(char c)
  43. +{
  44. + if (c >= '0' && c <= '9')
  45. + return (c - '0');
  46. + c &= ~32;
  47. + if (c >= 'A' && c <= 'F')
  48. + return (c - 'A' + 10);
  49. + return (0xFF);
  50. +}
  51. +
  52. +static void __init parse_uuid(const char *s)
  53. +{
  54. + int i;
  55. + __u8 j, k;
  56. +
  57. + if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' ||
  58. + s[18] != '-' || s[23] != '-')
  59. + goto bad_uuid;
  60. + for (i = 0; i < 16; i++) {
  61. + if (*s == '-')
  62. + ++s;
  63. + j = fromhex(*s++);
  64. + k = fromhex(*s++);
  65. + if (j == 0xFF || k == 0xFF)
  66. + goto bad_uuid;
  67. + root_dev_uuid[i] = (j << 4) | k;
  68. + }
  69. + return;
  70. + bad_uuid:
  71. + /* we cannot panic here, defer */
  72. + root_dev_type = 3;
  73. +}
  74. +
  75. +/* from drivers/md/md.c */
  76. +static void __init initcode_bi_complete(struct bio *bio, int error)
  77. +{
  78. + complete((struct completion*)bio->bi_private);
  79. +}
  80. +
  81. +static int __init initcode_sync_page_read(struct block_device *bdev,
  82. + sector_t sector, int size, struct page *page)
  83. +{
  84. + struct bio *bio = bio_alloc(GFP_NOIO, 1);
  85. + struct completion event;
  86. + int ret, rw = READ;
  87. +
  88. + rw |= REQ_SYNC;
  89. +
  90. + bio->bi_bdev = bdev;
  91. + bio->bi_sector = sector;
  92. + bio_add_page(bio, page, size, 0);
  93. + init_completion(&event);
  94. + bio->bi_private = &event;
  95. + bio->bi_end_io = initcode_bi_complete;
  96. + submit_bio(rw, bio);
  97. + wait_for_completion(&event);
  98. +
  99. + ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
  100. + bio_put(bio);
  101. + /* 0 = failure */
  102. + return ret;
  103. +}
  104. +
  105. +/* most of this taken from fs/ext2/super.c */
  106. +static int __init check_dev(struct gendisk *thedisk, dev_t devt,
  107. + int blocksize, struct page *page)
  108. +{
  109. + struct ext2_super_block * es;
  110. + struct block_device *bdev;
  111. + unsigned long sb_block = 1;
  112. + unsigned long logic_sb_block;
  113. + unsigned long offset = 0;
  114. + int rv = /* not found */ 0;
  115. + char bff[22];
  116. +
  117. + bdev = bdget(devt);
  118. + if (blkdev_get(bdev, FMODE_READ, NULL)) {
  119. + printk(KERN_ERR "VFS: opening block device %s failed!\n",
  120. + format_dev_t(bff, devt));
  121. + return (0);
  122. + }
  123. +
  124. + if (blocksize != BLOCK_SIZE) {
  125. + logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  126. + offset = (sb_block*BLOCK_SIZE) % blocksize;
  127. + } else {
  128. + logic_sb_block = sb_block;
  129. + }
  130. +
  131. +// printk(KERN_ERR "D: attempting to read %d @%lu from "
  132. +// "bdev %p devt %08X %s\n", blocksize, logic_sb_block,
  133. +// bdev, devt, format_dev_t(bff, devt));
  134. + if (!initcode_sync_page_read(bdev, logic_sb_block, blocksize, page)) {
  135. +// printk(KERN_ERR "D: failed!\n");
  136. + goto out;
  137. + }
  138. + es = (struct ext2_super_block *)(((char *)page_address(page)) + offset);
  139. + if (le16_to_cpu(es->s_magic) == EXT2_SUPER_MAGIC) {
  140. +// printk(KERN_ERR "D: has uuid "
  141. +// "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  142. +// es->s_uuid[0], es->s_uuid[1], es->s_uuid[2], es->s_uuid[3],
  143. +// es->s_uuid[4], es->s_uuid[5], es->s_uuid[6], es->s_uuid[7],
  144. +// es->s_uuid[8], es->s_uuid[9], es->s_uuid[10], es->s_uuid[11],
  145. +// es->s_uuid[12], es->s_uuid[13], es->s_uuid[14], es->s_uuid[15]);
  146. + if (!memcmp(es->s_uuid, root_dev_uuid, 16))
  147. + rv = /* found */ 1;
  148. + }
  149. +// else printk(KERN_ERR "D: bad ext2fs magic\n");
  150. + out:
  151. + blkdev_put(bdev, FMODE_READ);
  152. + return (rv);
  153. +}
  154. +#endif /* CONFIG_EXT2_FS for UUID support */
  155. +
  156. static int __init load_ramdisk(char *str)
  157. {
  158. rd_doload = simple_strtol(str,NULL,0) & 3;
  159. @@ -256,6 +383,13 @@
  160. static int __init root_dev_setup(char *line)
  161. {
  162. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  163. +#ifdef CONFIG_EXT2_FS
  164. + root_dev_type = 0;
  165. + if (!strncmp(line, "UUID=", 5)) {
  166. + root_dev_type = 1;
  167. + parse_uuid(line + 5);
  168. + }
  169. +#endif /* CONFIG_EXT2_FS for UUID support */
  170. return 1;
  171. }
  172. @@ -471,6 +605,83 @@
  173. void __init mount_root(void)
  174. {
  175. +#ifdef CONFIG_EXT2_FS
  176. + /* UUID support */
  177. +// printk_all_partitions();
  178. + if (root_dev_type == 1) {
  179. + int blocksize;
  180. +
  181. + /* from block/genhd.c printk_all_partitions */
  182. + struct class_dev_iter iter;
  183. + struct device *dev;
  184. +
  185. + /* from drivers/md/md.c */
  186. + struct page *sb_page;
  187. +
  188. + if (!(sb_page = alloc_page(GFP_KERNEL))) {
  189. + printk(KERN_ERR "VFS: no memory for bio page\n");
  190. + goto nomemforbio;
  191. + }
  192. +
  193. +// printk(KERN_ERR "D: root is: "
  194. +// "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  195. +// root_dev_uuid[0], root_dev_uuid[1], root_dev_uuid[2], root_dev_uuid[3],
  196. +// root_dev_uuid[4], root_dev_uuid[5], root_dev_uuid[6], root_dev_uuid[7],
  197. +// root_dev_uuid[8], root_dev_uuid[9], root_dev_uuid[10], root_dev_uuid[11],
  198. +// root_dev_uuid[12], root_dev_uuid[13], root_dev_uuid[14], root_dev_uuid[15]);
  199. + /* from block/genhd.c printk_all_partitions */
  200. +// printk(KERN_ERR "D: begin iter\n");
  201. + class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  202. + while (root_dev_type && (dev = class_dev_iter_next(&iter))) {
  203. +// char bff[22];
  204. + struct gendisk *disk = dev_to_disk(dev);
  205. + struct disk_part_iter piter;
  206. + struct hd_struct *part;
  207. + if (get_capacity(disk) == 0 ||
  208. + (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) {
  209. +// printk(KERN_ERR "D: ignoring\n");
  210. + continue;
  211. + }
  212. + blocksize = queue_logical_block_size(disk->queue);
  213. +// printk(KERN_ERR "D: gendisk, blocksize %d "
  214. +// "name '%s' devt %08X %s #part %d\n", blocksize,
  215. +// disk->disk_name, dev->devt,
  216. +// format_dev_t(bff, dev->devt),
  217. +// disk_max_parts(disk));
  218. + disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  219. + while (root_dev_type && (part = disk_part_iter_next(&piter))) {
  220. + /* avoid empty or too small partitions */
  221. +// printk(KERN_ERR "D: part #%d start %llu "
  222. +// "nr %llu\n", part->partno,
  223. +// (__u64)part->start_sect,
  224. +// (__u64)part->nr_sects);
  225. + if (part->nr_sects < 8)
  226. + continue;
  227. + if (check_dev(disk, MKDEV(MAJOR(dev->devt),
  228. + MINOR(dev->devt) + part->partno),
  229. + blocksize, sb_page)) {
  230. + ROOT_DEV = part_devt(part);
  231. +// printk(KERN_ERR "D: got match!\n");
  232. + // comment out below for debugging
  233. + root_dev_type = 0;
  234. + }
  235. + }
  236. + disk_part_iter_exit(&piter);
  237. + }
  238. +// printk(KERN_ERR "D: end iter\n");
  239. + class_dev_iter_exit(&iter);
  240. + put_page(sb_page);
  241. + }
  242. + nomemforbio:
  243. + if (root_dev_type == 1)
  244. + printk(KERN_ERR "VFS: Unable to find root by UUID %s.\n",
  245. + saved_root_name + 5);
  246. + else if (root_dev_type == 3)
  247. + /* execute deferred panic from parse_uuid */
  248. + panic("Badly formatted UUID %s was supplied as kernel "
  249. + "parameter root", saved_root_name + 5);
  250. +#endif /* CONFIG_EXT2_FS for UUID support */
  251. +
  252. #ifdef CONFIG_ROOT_NFS
  253. if (ROOT_DEV == Root_NFS) {
  254. if (mount_nfs_root())