uuid.patch 7.6 KB

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