xref: /openbmc/linux/block/partitions/aix.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
26ceea22bSPhilippe De Muyter /*
36ceea22bSPhilippe De Muyter  *  fs/partitions/aix.c
46ceea22bSPhilippe De Muyter  *
56ceea22bSPhilippe De Muyter  *  Copyright (C) 2012-2013 Philippe De Muyter <phdm@macqel.be>
66ceea22bSPhilippe De Muyter  */
76ceea22bSPhilippe De Muyter 
86ceea22bSPhilippe De Muyter #include "check.h"
96ceea22bSPhilippe De Muyter #include "aix.h"
106ceea22bSPhilippe De Muyter 
116ceea22bSPhilippe De Muyter struct lvm_rec {
126ceea22bSPhilippe De Muyter 	char lvm_id[4]; /* "_LVM" */
136ceea22bSPhilippe De Muyter 	char reserved4[16];
146ceea22bSPhilippe De Muyter 	__be32 lvmarea_len;
156ceea22bSPhilippe De Muyter 	__be32 vgda_len;
166ceea22bSPhilippe De Muyter 	__be32 vgda_psn[2];
176ceea22bSPhilippe De Muyter 	char reserved36[10];
186ceea22bSPhilippe De Muyter 	__be16 pp_size; /* log2(pp_size) */
196ceea22bSPhilippe De Muyter 	char reserved46[12];
206ceea22bSPhilippe De Muyter 	__be16 version;
216ceea22bSPhilippe De Muyter 	};
226ceea22bSPhilippe De Muyter 
236ceea22bSPhilippe De Muyter struct vgda {
246ceea22bSPhilippe De Muyter 	__be32 secs;
256ceea22bSPhilippe De Muyter 	__be32 usec;
266ceea22bSPhilippe De Muyter 	char reserved8[16];
276ceea22bSPhilippe De Muyter 	__be16 numlvs;
286ceea22bSPhilippe De Muyter 	__be16 maxlvs;
296ceea22bSPhilippe De Muyter 	__be16 pp_size;
306ceea22bSPhilippe De Muyter 	__be16 numpvs;
316ceea22bSPhilippe De Muyter 	__be16 total_vgdas;
326ceea22bSPhilippe De Muyter 	__be16 vgda_size;
336ceea22bSPhilippe De Muyter 	};
346ceea22bSPhilippe De Muyter 
356ceea22bSPhilippe De Muyter struct lvd {
366ceea22bSPhilippe De Muyter 	__be16 lv_ix;
376ceea22bSPhilippe De Muyter 	__be16 res2;
386ceea22bSPhilippe De Muyter 	__be16 res4;
396ceea22bSPhilippe De Muyter 	__be16 maxsize;
406ceea22bSPhilippe De Muyter 	__be16 lv_state;
416ceea22bSPhilippe De Muyter 	__be16 mirror;
426ceea22bSPhilippe De Muyter 	__be16 mirror_policy;
436ceea22bSPhilippe De Muyter 	__be16 num_lps;
446ceea22bSPhilippe De Muyter 	__be16 res10[8];
456ceea22bSPhilippe De Muyter 	};
466ceea22bSPhilippe De Muyter 
476ceea22bSPhilippe De Muyter struct lvname {
486ceea22bSPhilippe De Muyter 	char name[64];
496ceea22bSPhilippe De Muyter 	};
506ceea22bSPhilippe De Muyter 
516ceea22bSPhilippe De Muyter struct ppe {
526ceea22bSPhilippe De Muyter 	__be16 lv_ix;
536ceea22bSPhilippe De Muyter 	unsigned short res2;
546ceea22bSPhilippe De Muyter 	unsigned short res4;
556ceea22bSPhilippe De Muyter 	__be16 lp_ix;
566ceea22bSPhilippe De Muyter 	unsigned short res8[12];
576ceea22bSPhilippe De Muyter 	};
586ceea22bSPhilippe De Muyter 
596ceea22bSPhilippe De Muyter struct pvd {
606ceea22bSPhilippe De Muyter 	char reserved0[16];
616ceea22bSPhilippe De Muyter 	__be16 pp_count;
626ceea22bSPhilippe De Muyter 	char reserved18[2];
636ceea22bSPhilippe De Muyter 	__be32 psn_part1;
646ceea22bSPhilippe De Muyter 	char reserved24[8];
656ceea22bSPhilippe De Muyter 	struct ppe ppe[1016];
666ceea22bSPhilippe De Muyter 	};
676ceea22bSPhilippe De Muyter 
686ceea22bSPhilippe De Muyter #define LVM_MAXLVS 256
696ceea22bSPhilippe De Muyter 
706ceea22bSPhilippe De Muyter /**
716ceea22bSPhilippe De Muyter  * last_lba(): return number of last logical block of device
726ceea22bSPhilippe De Muyter  * @bdev: block device
736ceea22bSPhilippe De Muyter  *
746ceea22bSPhilippe De Muyter  * Description: Returns last LBA value on success, 0 on error.
756ceea22bSPhilippe De Muyter  * This is stored (by sd and ide-geometry) in
766ceea22bSPhilippe De Muyter  *  the part[0] entry for this disk, and is the number of
776ceea22bSPhilippe De Muyter  *  physical sectors available on the disk.
786ceea22bSPhilippe De Muyter  */
796ceea22bSPhilippe De Muyter static u64 last_lba(struct block_device *bdev)
806ceea22bSPhilippe De Muyter {
816ceea22bSPhilippe De Muyter 	if (!bdev || !bdev->bd_inode)
826ceea22bSPhilippe De Muyter 		return 0;
836ceea22bSPhilippe De Muyter 	return (bdev->bd_inode->i_size >> 9) - 1ULL;
846ceea22bSPhilippe De Muyter }
856ceea22bSPhilippe De Muyter 
866ceea22bSPhilippe De Muyter /**
876ceea22bSPhilippe De Muyter  * read_lba(): Read bytes from disk, starting at given LBA
886ceea22bSPhilippe De Muyter  * @state
896ceea22bSPhilippe De Muyter  * @lba
906ceea22bSPhilippe De Muyter  * @buffer
916ceea22bSPhilippe De Muyter  * @count
926ceea22bSPhilippe De Muyter  *
936ceea22bSPhilippe De Muyter  * Description:  Reads @count bytes from @state->bdev into @buffer.
946ceea22bSPhilippe De Muyter  * Returns number of bytes read on success, 0 on error.
956ceea22bSPhilippe De Muyter  */
966ceea22bSPhilippe De Muyter static size_t read_lba(struct parsed_partitions *state, u64 lba, u8 *buffer,
976ceea22bSPhilippe De Muyter 			size_t count)
986ceea22bSPhilippe De Muyter {
996ceea22bSPhilippe De Muyter 	size_t totalreadcount = 0;
1006ceea22bSPhilippe De Muyter 
1016ceea22bSPhilippe De Muyter 	if (!buffer || lba + count / 512 > last_lba(state->bdev))
1026ceea22bSPhilippe De Muyter 		return 0;
1036ceea22bSPhilippe De Muyter 
1046ceea22bSPhilippe De Muyter 	while (count) {
1056ceea22bSPhilippe De Muyter 		int copied = 512;
1066ceea22bSPhilippe De Muyter 		Sector sect;
1076ceea22bSPhilippe De Muyter 		unsigned char *data = read_part_sector(state, lba++, &sect);
1086ceea22bSPhilippe De Muyter 		if (!data)
1096ceea22bSPhilippe De Muyter 			break;
1106ceea22bSPhilippe De Muyter 		if (copied > count)
1116ceea22bSPhilippe De Muyter 			copied = count;
1126ceea22bSPhilippe De Muyter 		memcpy(buffer, data, copied);
1136ceea22bSPhilippe De Muyter 		put_dev_sector(sect);
1146ceea22bSPhilippe De Muyter 		buffer += copied;
1156ceea22bSPhilippe De Muyter 		totalreadcount += copied;
1166ceea22bSPhilippe De Muyter 		count -= copied;
1176ceea22bSPhilippe De Muyter 	}
1186ceea22bSPhilippe De Muyter 	return totalreadcount;
1196ceea22bSPhilippe De Muyter }
1206ceea22bSPhilippe De Muyter 
1216ceea22bSPhilippe De Muyter /**
1226ceea22bSPhilippe De Muyter  * alloc_pvd(): reads physical volume descriptor
1236ceea22bSPhilippe De Muyter  * @state
1246ceea22bSPhilippe De Muyter  * @lba
1256ceea22bSPhilippe De Muyter  *
1266ceea22bSPhilippe De Muyter  * Description: Returns pvd on success,  NULL on error.
1276ceea22bSPhilippe De Muyter  * Allocates space for pvd and fill it with disk blocks at @lba
1286ceea22bSPhilippe De Muyter  * Notes: remember to free pvd when you're done!
1296ceea22bSPhilippe De Muyter  */
1306ceea22bSPhilippe De Muyter static struct pvd *alloc_pvd(struct parsed_partitions *state, u32 lba)
1316ceea22bSPhilippe De Muyter {
1326ceea22bSPhilippe De Muyter 	size_t count = sizeof(struct pvd);
1336ceea22bSPhilippe De Muyter 	struct pvd *p;
1346ceea22bSPhilippe De Muyter 
1356ceea22bSPhilippe De Muyter 	p = kmalloc(count, GFP_KERNEL);
1366ceea22bSPhilippe De Muyter 	if (!p)
1376ceea22bSPhilippe De Muyter 		return NULL;
1386ceea22bSPhilippe De Muyter 
1396ceea22bSPhilippe De Muyter 	if (read_lba(state, lba, (u8 *) p, count) < count) {
1406ceea22bSPhilippe De Muyter 		kfree(p);
1416ceea22bSPhilippe De Muyter 		return NULL;
1426ceea22bSPhilippe De Muyter 	}
1436ceea22bSPhilippe De Muyter 	return p;
1446ceea22bSPhilippe De Muyter }
1456ceea22bSPhilippe De Muyter 
1466ceea22bSPhilippe De Muyter /**
1476ceea22bSPhilippe De Muyter  * alloc_lvn(): reads logical volume names
1486ceea22bSPhilippe De Muyter  * @state
1496ceea22bSPhilippe De Muyter  * @lba
1506ceea22bSPhilippe De Muyter  *
1516ceea22bSPhilippe De Muyter  * Description: Returns lvn on success,  NULL on error.
1526ceea22bSPhilippe De Muyter  * Allocates space for lvn and fill it with disk blocks at @lba
1536ceea22bSPhilippe De Muyter  * Notes: remember to free lvn when you're done!
1546ceea22bSPhilippe De Muyter  */
1556ceea22bSPhilippe De Muyter static struct lvname *alloc_lvn(struct parsed_partitions *state, u32 lba)
1566ceea22bSPhilippe De Muyter {
1576ceea22bSPhilippe De Muyter 	size_t count = sizeof(struct lvname) * LVM_MAXLVS;
1586ceea22bSPhilippe De Muyter 	struct lvname *p;
1596ceea22bSPhilippe De Muyter 
1606ceea22bSPhilippe De Muyter 	p = kmalloc(count, GFP_KERNEL);
1616ceea22bSPhilippe De Muyter 	if (!p)
1626ceea22bSPhilippe De Muyter 		return NULL;
1636ceea22bSPhilippe De Muyter 
1646ceea22bSPhilippe De Muyter 	if (read_lba(state, lba, (u8 *) p, count) < count) {
1656ceea22bSPhilippe De Muyter 		kfree(p);
1666ceea22bSPhilippe De Muyter 		return NULL;
1676ceea22bSPhilippe De Muyter 	}
1686ceea22bSPhilippe De Muyter 	return p;
1696ceea22bSPhilippe De Muyter }
1706ceea22bSPhilippe De Muyter 
1716ceea22bSPhilippe De Muyter int aix_partition(struct parsed_partitions *state)
1726ceea22bSPhilippe De Muyter {
1736ceea22bSPhilippe De Muyter 	int ret = 0;
1746ceea22bSPhilippe De Muyter 	Sector sect;
1756ceea22bSPhilippe De Muyter 	unsigned char *d;
1766ceea22bSPhilippe De Muyter 	u32 pp_bytes_size;
1776ceea22bSPhilippe De Muyter 	u32 pp_blocks_size = 0;
1786ceea22bSPhilippe De Muyter 	u32 vgda_sector = 0;
1796ceea22bSPhilippe De Muyter 	u32 vgda_len = 0;
1806ceea22bSPhilippe De Muyter 	int numlvs = 0;
1816ceea22bSPhilippe De Muyter 	struct pvd *pvd;
1826ceea22bSPhilippe De Muyter 	struct lv_info {
1836ceea22bSPhilippe De Muyter 		unsigned short pps_per_lv;
1846ceea22bSPhilippe De Muyter 		unsigned short pps_found;
1856ceea22bSPhilippe De Muyter 		unsigned char lv_is_contiguous;
1866ceea22bSPhilippe De Muyter 	} *lvip;
1876ceea22bSPhilippe De Muyter 	struct lvname *n = NULL;
1886ceea22bSPhilippe De Muyter 
1896ceea22bSPhilippe De Muyter 	d = read_part_sector(state, 7, &sect);
1906ceea22bSPhilippe De Muyter 	if (d) {
1916ceea22bSPhilippe De Muyter 		struct lvm_rec *p = (struct lvm_rec *)d;
1926ceea22bSPhilippe De Muyter 		u16 lvm_version = be16_to_cpu(p->version);
1936ceea22bSPhilippe De Muyter 		char tmp[64];
1946ceea22bSPhilippe De Muyter 
1956ceea22bSPhilippe De Muyter 		if (lvm_version == 1) {
1966ceea22bSPhilippe De Muyter 			int pp_size_log2 = be16_to_cpu(p->pp_size);
1976ceea22bSPhilippe De Muyter 
1986ceea22bSPhilippe De Muyter 			pp_bytes_size = 1 << pp_size_log2;
1996ceea22bSPhilippe De Muyter 			pp_blocks_size = pp_bytes_size / 512;
2006ceea22bSPhilippe De Muyter 			snprintf(tmp, sizeof(tmp),
2016ceea22bSPhilippe De Muyter 				" AIX LVM header version %u found\n",
2026ceea22bSPhilippe De Muyter 				lvm_version);
2036ceea22bSPhilippe De Muyter 			vgda_len = be32_to_cpu(p->vgda_len);
2046ceea22bSPhilippe De Muyter 			vgda_sector = be32_to_cpu(p->vgda_psn[0]);
2056ceea22bSPhilippe De Muyter 		} else {
2066ceea22bSPhilippe De Muyter 			snprintf(tmp, sizeof(tmp),
2076ceea22bSPhilippe De Muyter 				" unsupported AIX LVM version %d found\n",
2086ceea22bSPhilippe De Muyter 				lvm_version);
2096ceea22bSPhilippe De Muyter 		}
2106ceea22bSPhilippe De Muyter 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2116ceea22bSPhilippe De Muyter 		put_dev_sector(sect);
2126ceea22bSPhilippe De Muyter 	}
2136ceea22bSPhilippe De Muyter 	if (vgda_sector && (d = read_part_sector(state, vgda_sector, &sect))) {
2146ceea22bSPhilippe De Muyter 		struct vgda *p = (struct vgda *)d;
2156ceea22bSPhilippe De Muyter 
2166ceea22bSPhilippe De Muyter 		numlvs = be16_to_cpu(p->numlvs);
2176ceea22bSPhilippe De Muyter 		put_dev_sector(sect);
2186ceea22bSPhilippe De Muyter 	}
219472d5e2aSFabian Frederick 	lvip = kcalloc(state->limit, sizeof(struct lv_info), GFP_KERNEL);
2206ceea22bSPhilippe De Muyter 	if (!lvip)
2216ceea22bSPhilippe De Muyter 		return 0;
2226ceea22bSPhilippe De Muyter 	if (numlvs && (d = read_part_sector(state, vgda_sector + 1, &sect))) {
2236ceea22bSPhilippe De Muyter 		struct lvd *p = (struct lvd *)d;
2246ceea22bSPhilippe De Muyter 		int i;
2256ceea22bSPhilippe De Muyter 
2266ceea22bSPhilippe De Muyter 		n = alloc_lvn(state, vgda_sector + vgda_len - 33);
2276ceea22bSPhilippe De Muyter 		if (n) {
2286ceea22bSPhilippe De Muyter 			int foundlvs = 0;
2296ceea22bSPhilippe De Muyter 
2306ceea22bSPhilippe De Muyter 			for (i = 0; foundlvs < numlvs && i < state->limit; i += 1) {
2316ceea22bSPhilippe De Muyter 				lvip[i].pps_per_lv = be16_to_cpu(p[i].num_lps);
2326ceea22bSPhilippe De Muyter 				if (lvip[i].pps_per_lv)
2336ceea22bSPhilippe De Muyter 					foundlvs += 1;
2346ceea22bSPhilippe De Muyter 			}
2356ceea22bSPhilippe De Muyter 		}
2366ceea22bSPhilippe De Muyter 		put_dev_sector(sect);
2376ceea22bSPhilippe De Muyter 	}
2386ceea22bSPhilippe De Muyter 	pvd = alloc_pvd(state, vgda_sector + 17);
2396ceea22bSPhilippe De Muyter 	if (pvd) {
2406ceea22bSPhilippe De Muyter 		int numpps = be16_to_cpu(pvd->pp_count);
2416ceea22bSPhilippe De Muyter 		int psn_part1 = be32_to_cpu(pvd->psn_part1);
2426ceea22bSPhilippe De Muyter 		int i;
2436ceea22bSPhilippe De Muyter 		int cur_lv_ix = -1;
2446ceea22bSPhilippe De Muyter 		int next_lp_ix = 1;
2456ceea22bSPhilippe De Muyter 		int lp_ix;
2466ceea22bSPhilippe De Muyter 
2476ceea22bSPhilippe De Muyter 		for (i = 0; i < numpps; i += 1) {
2486ceea22bSPhilippe De Muyter 			struct ppe *p = pvd->ppe + i;
2496ceea22bSPhilippe De Muyter 			unsigned int lv_ix;
2506ceea22bSPhilippe De Muyter 
2516ceea22bSPhilippe De Muyter 			lp_ix = be16_to_cpu(p->lp_ix);
2526ceea22bSPhilippe De Muyter 			if (!lp_ix) {
2536ceea22bSPhilippe De Muyter 				next_lp_ix = 1;
2546ceea22bSPhilippe De Muyter 				continue;
2556ceea22bSPhilippe De Muyter 			}
2566ceea22bSPhilippe De Muyter 			lv_ix = be16_to_cpu(p->lv_ix) - 1;
257d97a86c1SDan Carpenter 			if (lv_ix >= state->limit) {
2586ceea22bSPhilippe De Muyter 				cur_lv_ix = -1;
2596ceea22bSPhilippe De Muyter 				continue;
2606ceea22bSPhilippe De Muyter 			}
2616ceea22bSPhilippe De Muyter 			lvip[lv_ix].pps_found += 1;
2626ceea22bSPhilippe De Muyter 			if (lp_ix == 1) {
2636ceea22bSPhilippe De Muyter 				cur_lv_ix = lv_ix;
2646ceea22bSPhilippe De Muyter 				next_lp_ix = 1;
2656ceea22bSPhilippe De Muyter 			} else if (lv_ix != cur_lv_ix || lp_ix != next_lp_ix) {
2666ceea22bSPhilippe De Muyter 				next_lp_ix = 1;
2676ceea22bSPhilippe De Muyter 				continue;
2686ceea22bSPhilippe De Muyter 			}
2696ceea22bSPhilippe De Muyter 			if (lp_ix == lvip[lv_ix].pps_per_lv) {
2706ceea22bSPhilippe De Muyter 				char tmp[70];
2716ceea22bSPhilippe De Muyter 
2726ceea22bSPhilippe De Muyter 				put_partition(state, lv_ix + 1,
2736ceea22bSPhilippe De Muyter 				  (i + 1 - lp_ix) * pp_blocks_size + psn_part1,
2746ceea22bSPhilippe De Muyter 				  lvip[lv_ix].pps_per_lv * pp_blocks_size);
2756ceea22bSPhilippe De Muyter 				snprintf(tmp, sizeof(tmp), " <%s>\n",
2766ceea22bSPhilippe De Muyter 					 n[lv_ix].name);
2776ceea22bSPhilippe De Muyter 				strlcat(state->pp_buf, tmp, PAGE_SIZE);
2786ceea22bSPhilippe De Muyter 				lvip[lv_ix].lv_is_contiguous = 1;
2796ceea22bSPhilippe De Muyter 				ret = 1;
2806ceea22bSPhilippe De Muyter 				next_lp_ix = 1;
2816ceea22bSPhilippe De Muyter 			} else
2826ceea22bSPhilippe De Muyter 				next_lp_ix += 1;
2836ceea22bSPhilippe De Muyter 		}
2846ceea22bSPhilippe De Muyter 		for (i = 0; i < state->limit; i += 1)
2856ceea22bSPhilippe De Muyter 			if (lvip[i].pps_found && !lvip[i].lv_is_contiguous)
2866ceea22bSPhilippe De Muyter 				pr_warn("partition %s (%u pp's found) is "
2876ceea22bSPhilippe De Muyter 					"not contiguous\n",
2886ceea22bSPhilippe De Muyter 					n[i].name, lvip[i].pps_found);
2896ceea22bSPhilippe De Muyter 		kfree(pvd);
2906ceea22bSPhilippe De Muyter 	}
2916ceea22bSPhilippe De Muyter 	kfree(n);
2926ceea22bSPhilippe De Muyter 	kfree(lvip);
2936ceea22bSPhilippe De Muyter 	return ret;
2946ceea22bSPhilippe De Muyter }
295