1 /*- 2 * Copyright (c) 2002 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #ifdef __FBSDID 29 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); 30 #endif 31 #ifdef __RCSID 32 __RCSID("$NetBSD: resize.c,v 1.8 2013/12/10 01:05:00 jnemeth Exp $"); 33 #endif 34 35 #include <sys/types.h> 36 37 #include <err.h> 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <inttypes.h> 44 45 #include "map.h" 46 #include "gpt.h" 47 48 static off_t alignment, sectors, size; 49 static unsigned int entry; 50 51 const char resizemsg[] = "resize -i index [-a alignment] [-s size] device ..."; 52 53 __dead static void 54 usage_resize(void) 55 { 56 57 fprintf(stderr, 58 "usage: %s %s\n", getprogname(), resizemsg); 59 exit(1); 60 } 61 62 static void 63 resize(int fd) 64 { 65 uuid_t uuid; 66 map_t *gpt, *tpg; 67 map_t *tbl, *lbt; 68 map_t *map; 69 struct gpt_hdr *hdr; 70 struct gpt_ent *ent; 71 unsigned int i; 72 off_t alignsecs, newsize; 73 74 75 gpt = map_find(MAP_TYPE_PRI_GPT_HDR); 76 ent = NULL; 77 if (gpt == NULL) { 78 warnx("%s: error: no primary GPT header; run create or recover", 79 device_name); 80 return; 81 } 82 83 tpg = map_find(MAP_TYPE_SEC_GPT_HDR); 84 if (tpg == NULL) { 85 warnx("%s: error: no secondary GPT header; run recover", 86 device_name); 87 return; 88 } 89 90 tbl = map_find(MAP_TYPE_PRI_GPT_TBL); 91 lbt = map_find(MAP_TYPE_SEC_GPT_TBL); 92 if (tbl == NULL || lbt == NULL) { 93 warnx("%s: error: run recover -- trust me", device_name); 94 return; 95 } 96 97 hdr = gpt->map_data; 98 if (entry > le32toh(hdr->hdr_entries)) { 99 warnx("%s: error: index %u out of range (%u max)", device_name, 100 entry, le32toh(hdr->hdr_entries)); 101 return; 102 } 103 104 i = entry - 1; 105 ent = (void*)((char*)tbl->map_data + i * 106 le32toh(hdr->hdr_entsz)); 107 le_uuid_dec(ent->ent_type, &uuid); 108 if (uuid_is_nil(&uuid, NULL)) { 109 warnx("%s: error: entry at index %u is unused", 110 device_name, entry); 111 return; 112 } 113 114 alignsecs = alignment / secsz; 115 116 for (map = map_first(); map != NULL; map = map->map_next) { 117 if (entry == map->map_index) 118 break; 119 } 120 if (map == NULL) { 121 warnx("%s: error: could not find map entry corresponding " 122 "to index", device_name); 123 return; 124 } 125 126 if (sectors > 0 && sectors == map->map_size) 127 if (alignment == 0 || 128 (alignment > 0 && sectors % alignsecs == 0)) { 129 /* nothing to do */ 130 warnx("%s: partition does not need resizing", 131 device_name); 132 return; 133 } 134 135 newsize = map_resize(map, sectors, alignsecs); 136 if (newsize == 0 && alignment > 0) { 137 warnx("%s: could not resize partition with alignment " 138 "constraint", device_name); 139 return; 140 } else if (newsize == 0) { 141 warnx("%s: could not resize partition", device_name); 142 return; 143 } 144 145 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL); 146 147 hdr->hdr_crc_table = htole32(crc32(tbl->map_data, 148 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 149 hdr->hdr_crc_self = 0; 150 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 151 152 gpt_write(fd, gpt); 153 gpt_write(fd, tbl); 154 155 hdr = tpg->map_data; 156 ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz)); 157 158 ent->ent_lba_end = htole64(map->map_start + newsize - 1LL); 159 160 hdr->hdr_crc_table = htole32(crc32(lbt->map_data, 161 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 162 hdr->hdr_crc_self = 0; 163 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 164 165 gpt_write(fd, lbt); 166 gpt_write(fd, tpg); 167 168 printf("Partition %d resized, use:\n", entry); 169 printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64 170 " <type>\n", device_arg, map->map_start, newsize); 171 printf("to create a wedge for it\n"); 172 } 173 174 int 175 cmd_resize(int argc, char *argv[]) 176 { 177 char *p; 178 int ch, fd; 179 int64_t human_num; 180 181 while ((ch = getopt(argc, argv, "a:i:s:")) != -1) { 182 switch(ch) { 183 case 'a': 184 if (alignment > 0) 185 usage_resize(); 186 if (dehumanize_number(optarg, &human_num) < 0) 187 usage_resize(); 188 alignment = human_num; 189 if (alignment < 1) 190 usage_resize(); 191 break; 192 case 'i': 193 if (entry > 0) 194 usage_resize(); 195 entry = strtoul(optarg, &p, 10); 196 if (*p != 0 || entry < 1) 197 usage_resize(); 198 break; 199 case 's': 200 if (sectors > 0 || size > 0) 201 usage_resize(); 202 sectors = strtoll(optarg, &p, 10); 203 if (sectors < 1) 204 usage_resize(); 205 if (*p == '\0') 206 break; 207 if (*p == 's' || *p == 'S') { 208 if (*(p + 1) == '\0') 209 break; 210 else 211 usage_resize(); 212 } 213 if (*p == 'b' || *p == 'B') { 214 if (*(p + 1) == '\0') { 215 size = sectors; 216 sectors = 0; 217 break; 218 } else 219 usage_resize(); 220 } 221 if (dehumanize_number(optarg, &human_num) < 0) 222 usage_resize(); 223 size = human_num; 224 sectors = 0; 225 break; 226 default: 227 usage_resize(); 228 } 229 } 230 231 if (argc == optind) 232 usage_resize(); 233 234 if (entry == 0) 235 usage_resize(); 236 237 while (optind < argc) { 238 fd = gpt_open(argv[optind++]); 239 if (fd == -1) { 240 warn("unable to open device '%s'", device_name); 241 continue; 242 } 243 244 if (alignment % secsz != 0) { 245 warnx("Alignment must be a multiple of sector size;"); 246 warnx("the sector size for %s is %d bytes.", 247 device_name, secsz); 248 continue; 249 } 250 251 if (size % secsz != 0) { 252 warnx("Size in bytes must be a multiple of sector " 253 "size;"); 254 warnx("the sector size for %s is %d bytes.", 255 device_name, secsz); 256 continue; 257 } 258 if (size > 0) 259 sectors = size / secsz; 260 261 resize(fd); 262 263 gpt_close(fd); 264 } 265 266 return 0; 267 } 268