aix.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * fs/partitions/aix.c
  3. *
  4. * Copyright (C) 2012-2013 Philippe De Muyter <phdm@macqel.be>
  5. */
  6. #include "check.h"
  7. #include "aix.h"
  8. struct lvm_rec {
  9. char lvm_id[4]; /* "_LVM" */
  10. char reserved4[16];
  11. __be32 lvmarea_len;
  12. __be32 vgda_len;
  13. __be32 vgda_psn[2];
  14. char reserved36[10];
  15. __be16 pp_size; /* log2(pp_size) */
  16. char reserved46[12];
  17. __be16 version;
  18. };
  19. struct vgda {
  20. __be32 secs;
  21. __be32 usec;
  22. char reserved8[16];
  23. __be16 numlvs;
  24. __be16 maxlvs;
  25. __be16 pp_size;
  26. __be16 numpvs;
  27. __be16 total_vgdas;
  28. __be16 vgda_size;
  29. };
  30. struct lvd {
  31. __be16 lv_ix;
  32. __be16 res2;
  33. __be16 res4;
  34. __be16 maxsize;
  35. __be16 lv_state;
  36. __be16 mirror;
  37. __be16 mirror_policy;
  38. __be16 num_lps;
  39. __be16 res10[8];
  40. };
  41. struct lvname {
  42. char name[64];
  43. };
  44. struct ppe {
  45. __be16 lv_ix;
  46. unsigned short res2;
  47. unsigned short res4;
  48. __be16 lp_ix;
  49. unsigned short res8[12];
  50. };
  51. struct pvd {
  52. char reserved0[16];
  53. __be16 pp_count;
  54. char reserved18[2];
  55. __be32 psn_part1;
  56. char reserved24[8];
  57. struct ppe ppe[1016];
  58. };
  59. #define LVM_MAXLVS 256
  60. /**
  61. * last_lba(): return number of last logical block of device
  62. * @bdev: block device
  63. *
  64. * Description: Returns last LBA value on success, 0 on error.
  65. * This is stored (by sd and ide-geometry) in
  66. * the part[0] entry for this disk, and is the number of
  67. * physical sectors available on the disk.
  68. */
  69. static u64 last_lba(struct block_device *bdev)
  70. {
  71. if (!bdev || !bdev->bd_inode)
  72. return 0;
  73. return (bdev->bd_inode->i_size >> 9) - 1ULL;
  74. }
  75. /**
  76. * read_lba(): Read bytes from disk, starting at given LBA
  77. * @state
  78. * @lba
  79. * @buffer
  80. * @count
  81. *
  82. * Description: Reads @count bytes from @state->bdev into @buffer.
  83. * Returns number of bytes read on success, 0 on error.
  84. */
  85. static size_t read_lba(struct parsed_partitions *state, u64 lba, u8 *buffer,
  86. size_t count)
  87. {
  88. size_t totalreadcount = 0;
  89. if (!buffer || lba + count / 512 > last_lba(state->bdev))
  90. return 0;
  91. while (count) {
  92. int copied = 512;
  93. Sector sect;
  94. unsigned char *data = read_part_sector(state, lba++, &sect);
  95. if (!data)
  96. break;
  97. if (copied > count)
  98. copied = count;
  99. memcpy(buffer, data, copied);
  100. put_dev_sector(sect);
  101. buffer += copied;
  102. totalreadcount += copied;
  103. count -= copied;
  104. }
  105. return totalreadcount;
  106. }
  107. /**
  108. * alloc_pvd(): reads physical volume descriptor
  109. * @state
  110. * @lba
  111. *
  112. * Description: Returns pvd on success, NULL on error.
  113. * Allocates space for pvd and fill it with disk blocks at @lba
  114. * Notes: remember to free pvd when you're done!
  115. */
  116. static struct pvd *alloc_pvd(struct parsed_partitions *state, u32 lba)
  117. {
  118. size_t count = sizeof(struct pvd);
  119. struct pvd *p;
  120. p = kmalloc(count, GFP_KERNEL);
  121. if (!p)
  122. return NULL;
  123. if (read_lba(state, lba, (u8 *) p, count) < count) {
  124. kfree(p);
  125. return NULL;
  126. }
  127. return p;
  128. }
  129. /**
  130. * alloc_lvn(): reads logical volume names
  131. * @state
  132. * @lba
  133. *
  134. * Description: Returns lvn on success, NULL on error.
  135. * Allocates space for lvn and fill it with disk blocks at @lba
  136. * Notes: remember to free lvn when you're done!
  137. */
  138. static struct lvname *alloc_lvn(struct parsed_partitions *state, u32 lba)
  139. {
  140. size_t count = sizeof(struct lvname) * LVM_MAXLVS;
  141. struct lvname *p;
  142. p = kmalloc(count, GFP_KERNEL);
  143. if (!p)
  144. return NULL;
  145. if (read_lba(state, lba, (u8 *) p, count) < count) {
  146. kfree(p);
  147. return NULL;
  148. }
  149. return p;
  150. }
  151. int aix_partition(struct parsed_partitions *state)
  152. {
  153. int ret = 0;
  154. Sector sect;
  155. unsigned char *d;
  156. u32 pp_bytes_size;
  157. u32 pp_blocks_size = 0;
  158. u32 vgda_sector = 0;
  159. u32 vgda_len = 0;
  160. int numlvs = 0;
  161. struct pvd *pvd;
  162. struct lv_info {
  163. unsigned short pps_per_lv;
  164. unsigned short pps_found;
  165. unsigned char lv_is_contiguous;
  166. } *lvip;
  167. struct lvname *n = NULL;
  168. d = read_part_sector(state, 7, &sect);
  169. if (d) {
  170. struct lvm_rec *p = (struct lvm_rec *)d;
  171. u16 lvm_version = be16_to_cpu(p->version);
  172. char tmp[64];
  173. if (lvm_version == 1) {
  174. int pp_size_log2 = be16_to_cpu(p->pp_size);
  175. pp_bytes_size = 1 << pp_size_log2;
  176. pp_blocks_size = pp_bytes_size / 512;
  177. snprintf(tmp, sizeof(tmp),
  178. " AIX LVM header version %u found\n",
  179. lvm_version);
  180. vgda_len = be32_to_cpu(p->vgda_len);
  181. vgda_sector = be32_to_cpu(p->vgda_psn[0]);
  182. } else {
  183. snprintf(tmp, sizeof(tmp),
  184. " unsupported AIX LVM version %d found\n",
  185. lvm_version);
  186. }
  187. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  188. put_dev_sector(sect);
  189. }
  190. if (vgda_sector && (d = read_part_sector(state, vgda_sector, &sect))) {
  191. struct vgda *p = (struct vgda *)d;
  192. numlvs = be16_to_cpu(p->numlvs);
  193. put_dev_sector(sect);
  194. }
  195. lvip = kcalloc(state->limit, sizeof(struct lv_info), GFP_KERNEL);
  196. if (!lvip)
  197. return 0;
  198. if (numlvs && (d = read_part_sector(state, vgda_sector + 1, &sect))) {
  199. struct lvd *p = (struct lvd *)d;
  200. int i;
  201. n = alloc_lvn(state, vgda_sector + vgda_len - 33);
  202. if (n) {
  203. int foundlvs = 0;
  204. for (i = 0; foundlvs < numlvs && i < state->limit; i += 1) {
  205. lvip[i].pps_per_lv = be16_to_cpu(p[i].num_lps);
  206. if (lvip[i].pps_per_lv)
  207. foundlvs += 1;
  208. }
  209. }
  210. put_dev_sector(sect);
  211. }
  212. pvd = alloc_pvd(state, vgda_sector + 17);
  213. if (pvd) {
  214. int numpps = be16_to_cpu(pvd->pp_count);
  215. int psn_part1 = be32_to_cpu(pvd->psn_part1);
  216. int i;
  217. int cur_lv_ix = -1;
  218. int next_lp_ix = 1;
  219. int lp_ix;
  220. for (i = 0; i < numpps; i += 1) {
  221. struct ppe *p = pvd->ppe + i;
  222. unsigned int lv_ix;
  223. lp_ix = be16_to_cpu(p->lp_ix);
  224. if (!lp_ix) {
  225. next_lp_ix = 1;
  226. continue;
  227. }
  228. lv_ix = be16_to_cpu(p->lv_ix) - 1;
  229. if (lv_ix >= state->limit) {
  230. cur_lv_ix = -1;
  231. continue;
  232. }
  233. lvip[lv_ix].pps_found += 1;
  234. if (lp_ix == 1) {
  235. cur_lv_ix = lv_ix;
  236. next_lp_ix = 1;
  237. } else if (lv_ix != cur_lv_ix || lp_ix != next_lp_ix) {
  238. next_lp_ix = 1;
  239. continue;
  240. }
  241. if (lp_ix == lvip[lv_ix].pps_per_lv) {
  242. char tmp[70];
  243. put_partition(state, lv_ix + 1,
  244. (i + 1 - lp_ix) * pp_blocks_size + psn_part1,
  245. lvip[lv_ix].pps_per_lv * pp_blocks_size);
  246. snprintf(tmp, sizeof(tmp), " <%s>\n",
  247. n[lv_ix].name);
  248. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  249. lvip[lv_ix].lv_is_contiguous = 1;
  250. ret = 1;
  251. next_lp_ix = 1;
  252. } else
  253. next_lp_ix += 1;
  254. }
  255. for (i = 0; i < state->limit; i += 1)
  256. if (lvip[i].pps_found && !lvip[i].lv_is_contiguous)
  257. pr_warn("partition %s (%u pp's found) is "
  258. "not contiguous\n",
  259. n[i].name, lvip[i].pps_found);
  260. kfree(pvd);
  261. }
  262. kfree(n);
  263. kfree(lvip);
  264. return ret;
  265. }