aix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/partitions/aix.c
  4. *
  5. * Copyright (C) 2012-2013 Philippe De Muyter <[email protected]>
  6. */
  7. #include "check.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. * read_lba(): Read bytes from disk, starting at given LBA
  62. * @state
  63. * @lba
  64. * @buffer
  65. * @count
  66. *
  67. * Description: Reads @count bytes from @state->disk into @buffer.
  68. * Returns number of bytes read on success, 0 on error.
  69. */
  70. static size_t read_lba(struct parsed_partitions *state, u64 lba, u8 *buffer,
  71. size_t count)
  72. {
  73. size_t totalreadcount = 0;
  74. if (!buffer || lba + count / 512 > get_capacity(state->disk) - 1ULL)
  75. return 0;
  76. while (count) {
  77. int copied = 512;
  78. Sector sect;
  79. unsigned char *data = read_part_sector(state, lba++, &sect);
  80. if (!data)
  81. break;
  82. if (copied > count)
  83. copied = count;
  84. memcpy(buffer, data, copied);
  85. put_dev_sector(sect);
  86. buffer += copied;
  87. totalreadcount += copied;
  88. count -= copied;
  89. }
  90. return totalreadcount;
  91. }
  92. /**
  93. * alloc_pvd(): reads physical volume descriptor
  94. * @state
  95. * @lba
  96. *
  97. * Description: Returns pvd on success, NULL on error.
  98. * Allocates space for pvd and fill it with disk blocks at @lba
  99. * Notes: remember to free pvd when you're done!
  100. */
  101. static struct pvd *alloc_pvd(struct parsed_partitions *state, u32 lba)
  102. {
  103. size_t count = sizeof(struct pvd);
  104. struct pvd *p;
  105. p = kmalloc(count, GFP_KERNEL);
  106. if (!p)
  107. return NULL;
  108. if (read_lba(state, lba, (u8 *) p, count) < count) {
  109. kfree(p);
  110. return NULL;
  111. }
  112. return p;
  113. }
  114. /**
  115. * alloc_lvn(): reads logical volume names
  116. * @state
  117. * @lba
  118. *
  119. * Description: Returns lvn on success, NULL on error.
  120. * Allocates space for lvn and fill it with disk blocks at @lba
  121. * Notes: remember to free lvn when you're done!
  122. */
  123. static struct lvname *alloc_lvn(struct parsed_partitions *state, u32 lba)
  124. {
  125. size_t count = sizeof(struct lvname) * LVM_MAXLVS;
  126. struct lvname *p;
  127. p = kmalloc(count, GFP_KERNEL);
  128. if (!p)
  129. return NULL;
  130. if (read_lba(state, lba, (u8 *) p, count) < count) {
  131. kfree(p);
  132. return NULL;
  133. }
  134. return p;
  135. }
  136. int aix_partition(struct parsed_partitions *state)
  137. {
  138. int ret = 0;
  139. Sector sect;
  140. unsigned char *d;
  141. u32 pp_bytes_size;
  142. u32 pp_blocks_size = 0;
  143. u32 vgda_sector = 0;
  144. u32 vgda_len = 0;
  145. int numlvs = 0;
  146. struct pvd *pvd = NULL;
  147. struct lv_info {
  148. unsigned short pps_per_lv;
  149. unsigned short pps_found;
  150. unsigned char lv_is_contiguous;
  151. } *lvip;
  152. struct lvname *n = NULL;
  153. d = read_part_sector(state, 7, &sect);
  154. if (d) {
  155. struct lvm_rec *p = (struct lvm_rec *)d;
  156. u16 lvm_version = be16_to_cpu(p->version);
  157. char tmp[64];
  158. if (lvm_version == 1) {
  159. int pp_size_log2 = be16_to_cpu(p->pp_size);
  160. pp_bytes_size = 1 << pp_size_log2;
  161. pp_blocks_size = pp_bytes_size / 512;
  162. snprintf(tmp, sizeof(tmp),
  163. " AIX LVM header version %u found\n",
  164. lvm_version);
  165. vgda_len = be32_to_cpu(p->vgda_len);
  166. vgda_sector = be32_to_cpu(p->vgda_psn[0]);
  167. } else {
  168. snprintf(tmp, sizeof(tmp),
  169. " unsupported AIX LVM version %d found\n",
  170. lvm_version);
  171. }
  172. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  173. put_dev_sector(sect);
  174. }
  175. if (vgda_sector && (d = read_part_sector(state, vgda_sector, &sect))) {
  176. struct vgda *p = (struct vgda *)d;
  177. numlvs = be16_to_cpu(p->numlvs);
  178. put_dev_sector(sect);
  179. }
  180. lvip = kcalloc(state->limit, sizeof(struct lv_info), GFP_KERNEL);
  181. if (!lvip)
  182. return 0;
  183. if (numlvs && (d = read_part_sector(state, vgda_sector + 1, &sect))) {
  184. struct lvd *p = (struct lvd *)d;
  185. int i;
  186. n = alloc_lvn(state, vgda_sector + vgda_len - 33);
  187. if (n) {
  188. int foundlvs = 0;
  189. for (i = 0; foundlvs < numlvs && i < state->limit; i += 1) {
  190. lvip[i].pps_per_lv = be16_to_cpu(p[i].num_lps);
  191. if (lvip[i].pps_per_lv)
  192. foundlvs += 1;
  193. }
  194. /* pvd loops depend on n[].name and lvip[].pps_per_lv */
  195. pvd = alloc_pvd(state, vgda_sector + 17);
  196. }
  197. put_dev_sector(sect);
  198. }
  199. if (pvd) {
  200. int numpps = be16_to_cpu(pvd->pp_count);
  201. int psn_part1 = be32_to_cpu(pvd->psn_part1);
  202. int i;
  203. int cur_lv_ix = -1;
  204. int next_lp_ix = 1;
  205. int lp_ix;
  206. for (i = 0; i < numpps; i += 1) {
  207. struct ppe *p = pvd->ppe + i;
  208. unsigned int lv_ix;
  209. lp_ix = be16_to_cpu(p->lp_ix);
  210. if (!lp_ix) {
  211. next_lp_ix = 1;
  212. continue;
  213. }
  214. lv_ix = be16_to_cpu(p->lv_ix) - 1;
  215. if (lv_ix >= state->limit) {
  216. cur_lv_ix = -1;
  217. continue;
  218. }
  219. lvip[lv_ix].pps_found += 1;
  220. if (lp_ix == 1) {
  221. cur_lv_ix = lv_ix;
  222. next_lp_ix = 1;
  223. } else if (lv_ix != cur_lv_ix || lp_ix != next_lp_ix) {
  224. next_lp_ix = 1;
  225. continue;
  226. }
  227. if (lp_ix == lvip[lv_ix].pps_per_lv) {
  228. char tmp[70];
  229. put_partition(state, lv_ix + 1,
  230. (i + 1 - lp_ix) * pp_blocks_size + psn_part1,
  231. lvip[lv_ix].pps_per_lv * pp_blocks_size);
  232. snprintf(tmp, sizeof(tmp), " <%s>\n",
  233. n[lv_ix].name);
  234. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  235. lvip[lv_ix].lv_is_contiguous = 1;
  236. ret = 1;
  237. next_lp_ix = 1;
  238. } else
  239. next_lp_ix += 1;
  240. }
  241. for (i = 0; i < state->limit; i += 1)
  242. if (lvip[i].pps_found && !lvip[i].lv_is_contiguous) {
  243. char tmp[sizeof(n[i].name) + 1]; // null char
  244. snprintf(tmp, sizeof(tmp), "%s", n[i].name);
  245. pr_warn("partition %s (%u pp's found) is "
  246. "not contiguous\n",
  247. tmp, lvip[i].pps_found);
  248. }
  249. kfree(pvd);
  250. }
  251. kfree(n);
  252. kfree(lvip);
  253. return ret;
  254. }