aix.c 6.4 KB

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