disk.c (55b1c6e7e4a6909004e13c6d2f328f911a8e7b83) disk.c (de04d704a98a7d2e9e57ebd83d2dd7a19fc11dab)
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 72 unchanged lines hidden (view full) ---

81
82int
83ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84{
85 struct disk_devdesc *dev;
86 struct open_disk *od;
87
88 dev = (struct disk_devdesc *)d;
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 72 unchanged lines hidden (view full) ---

81
82int
83ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84{
85 struct disk_devdesc *dev;
86 struct open_disk *od;
87
88 dev = (struct disk_devdesc *)d;
89 od = (struct open_disk *)dev->d_opendata;
89 od = (struct open_disk *)dev->dd.d_opendata;
90
91 /*
92 * The strategy function assumes the offset is in units of 512 byte
93 * sectors. For larger sector sizes, we need to adjust the offset to
94 * match the actual sector size.
95 */
96 offset *= (od->sectorsize / 512);
97 /*
98 * As the GPT backup partition is located at the end of the disk,
99 * to avoid reading past disk end, flag bcache not to use RA.
100 */
90
91 /*
92 * The strategy function assumes the offset is in units of 512 byte
93 * sectors. For larger sector sizes, we need to adjust the offset to
94 * match the actual sector size.
95 */
96 offset *= (od->sectorsize / 512);
97 /*
98 * As the GPT backup partition is located at the end of the disk,
99 * to avoid reading past disk end, flag bcache not to use RA.
100 */
101 return (dev->d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
101 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
102 blocks * od->sectorsize, (char *)buf, NULL));
103}
104
105#define PWIDTH 35
106static int
107ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
108{
109 struct disk_devdesc dev;
110 struct print_args *pa, bsd;
111 struct open_disk *od;
112 struct ptable *table;
113 char line[80];
114 int res;
115
116 pa = (struct print_args *)arg;
102 blocks * od->sectorsize, (char *)buf, NULL));
103}
104
105#define PWIDTH 35
106static int
107ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
108{
109 struct disk_devdesc dev;
110 struct print_args *pa, bsd;
111 struct open_disk *od;
112 struct ptable *table;
113 char line[80];
114 int res;
115
116 pa = (struct print_args *)arg;
117 od = (struct open_disk *)pa->dev->d_opendata;
117 od = (struct open_disk *)pa->dev->dd.d_opendata;
118 sprintf(line, " %s%s: %s", pa->prefix, pname,
119 parttype2str(part->type));
120 if (pa->verbose)
121 sprintf(line, "%-*s%s", PWIDTH, line,
122 display_size(part->end - part->start + 1,
123 od->sectorsize));
124 strcat(line, "\n");
125 if (pager_output(line))
126 return 1;
127 res = 0;
128 if (part->type == PART_FREEBSD) {
129 /* Open slice with BSD label */
118 sprintf(line, " %s%s: %s", pa->prefix, pname,
119 parttype2str(part->type));
120 if (pa->verbose)
121 sprintf(line, "%-*s%s", PWIDTH, line,
122 display_size(part->end - part->start + 1,
123 od->sectorsize));
124 strcat(line, "\n");
125 if (pager_output(line))
126 return 1;
127 res = 0;
128 if (part->type == PART_FREEBSD) {
129 /* Open slice with BSD label */
130 dev.d_dev = pa->dev->d_dev;
131 dev.d_unit = pa->dev->d_unit;
130 dev.dd.d_dev = pa->dev->dd.d_dev;
131 dev.dd.d_unit = pa->dev->dd.d_unit;
132 dev.d_slice = part->index;
133 dev.d_partition = -1;
134 if (disk_open(&dev, part->end - part->start + 1,
135 od->sectorsize) == 0) {
136 table = ptable_open(&dev, part->end - part->start + 1,
137 od->sectorsize, ptblread);
138 if (table != NULL) {
139 sprintf(line, " %s%s", pa->prefix, pname);

--- 13 unchanged lines hidden (view full) ---

153
154int
155disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
156{
157 struct open_disk *od;
158 struct print_args pa;
159
160 /* Disk should be opened */
132 dev.d_slice = part->index;
133 dev.d_partition = -1;
134 if (disk_open(&dev, part->end - part->start + 1,
135 od->sectorsize) == 0) {
136 table = ptable_open(&dev, part->end - part->start + 1,
137 od->sectorsize, ptblread);
138 if (table != NULL) {
139 sprintf(line, " %s%s", pa->prefix, pname);

--- 13 unchanged lines hidden (view full) ---

153
154int
155disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
156{
157 struct open_disk *od;
158 struct print_args pa;
159
160 /* Disk should be opened */
161 od = (struct open_disk *)dev->d_opendata;
161 od = (struct open_disk *)dev->dd.d_opendata;
162 pa.dev = dev;
163 pa.prefix = prefix;
164 pa.verbose = verbose;
165 return (ptable_iterate(od->table, &pa, ptable_print));
166}
167
168int
169disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
170{
171 struct open_disk *od;
172 int ret;
173
162 pa.dev = dev;
163 pa.prefix = prefix;
164 pa.verbose = verbose;
165 return (ptable_iterate(od->table, &pa, ptable_print));
166}
167
168int
169disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
170{
171 struct open_disk *od;
172 int ret;
173
174 od = (struct open_disk *)dev->d_opendata;
175 ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
174 od = (struct open_disk *)dev->dd.d_opendata;
175 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
176 blocks * od->sectorsize, buf, NULL);
177
178 return (ret);
179}
180
181int
182disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
183{
184 struct open_disk *od;
185 int ret;
186
176 blocks * od->sectorsize, buf, NULL);
177
178 return (ret);
179}
180
181int
182disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
183{
184 struct open_disk *od;
185 int ret;
186
187 od = (struct open_disk *)dev->d_opendata;
188 ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
187 od = (struct open_disk *)dev->dd.d_opendata;
188 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
189 blocks * od->sectorsize, buf, NULL);
190
191 return (ret);
192}
193
194int
195disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
196{
189 blocks * od->sectorsize, buf, NULL);
190
191 return (ret);
192}
193
194int
195disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
196{
197 struct open_disk *od = dev->d_opendata;
197 struct open_disk *od = dev->dd.d_opendata;
198
199 if (od == NULL)
200 return (ENOTTY);
201
202 switch (cmd) {
203 case DIOCGSECTORSIZE:
204 *(u_int *)data = od->sectorsize;
205 break;

--- 27 unchanged lines hidden (view full) ---

233 table = NULL;
234 slice = dev->d_slice;
235 partition = dev->d_partition;
236 od = (struct open_disk *)malloc(sizeof(struct open_disk));
237 if (od == NULL) {
238 DEBUG("no memory");
239 return (ENOMEM);
240 }
198
199 if (od == NULL)
200 return (ENOTTY);
201
202 switch (cmd) {
203 case DIOCGSECTORSIZE:
204 *(u_int *)data = od->sectorsize;
205 break;

--- 27 unchanged lines hidden (view full) ---

233 table = NULL;
234 slice = dev->d_slice;
235 partition = dev->d_partition;
236 od = (struct open_disk *)malloc(sizeof(struct open_disk));
237 if (od == NULL) {
238 DEBUG("no memory");
239 return (ENOMEM);
240 }
241 dev->d_opendata = od;
241 dev->dd.d_opendata = od;
242 od->entrysize = 0;
243 od->mediasize = mediasize;
244 od->sectorsize = sectorsize;
245 DEBUG("%s unit %d, slice %d, partition %d => %p",
246 disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
247
248 /* Determine disk layout. */
249 od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,

--- 93 unchanged lines hidden (view full) ---

343 return (rc);
344}
345
346int
347disk_close(struct disk_devdesc *dev)
348{
349 struct open_disk *od;
350
242 od->entrysize = 0;
243 od->mediasize = mediasize;
244 od->sectorsize = sectorsize;
245 DEBUG("%s unit %d, slice %d, partition %d => %p",
246 disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
247
248 /* Determine disk layout. */
249 od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,

--- 93 unchanged lines hidden (view full) ---

343 return (rc);
344}
345
346int
347disk_close(struct disk_devdesc *dev)
348{
349 struct open_disk *od;
350
351 od = (struct open_disk *)dev->d_opendata;
351 od = (struct open_disk *)dev->dd.d_opendata;
352 DEBUG("%s closed => %p", disk_fmtdev(dev), od);
353 ptable_close(od->table);
354 free(od);
355 return (0);
356}
357
358char*
359disk_fmtdev(struct disk_devdesc *dev)
360{
361 static char buf[128];
362 char *cp;
363
352 DEBUG("%s closed => %p", disk_fmtdev(dev), od);
353 ptable_close(od->table);
354 free(od);
355 return (0);
356}
357
358char*
359disk_fmtdev(struct disk_devdesc *dev)
360{
361 static char buf[128];
362 char *cp;
363
364 cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
364 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
365 if (dev->d_slice >= 0) {
366#ifdef LOADER_GPT_SUPPORT
367 if (dev->d_partition == 255) {
368 sprintf(cp, "p%d:", dev->d_slice);
369 return (buf);
370 } else
371#endif
372#ifdef LOADER_MBR_SUPPORT

--- 45 unchanged lines hidden (view full) ---

418 return (EPART);
419 cp++;
420 }
421 } else
422 return (EINVAL);
423
424 if (*cp != '\0' && *cp != ':')
425 return (EINVAL);
365 if (dev->d_slice >= 0) {
366#ifdef LOADER_GPT_SUPPORT
367 if (dev->d_partition == 255) {
368 sprintf(cp, "p%d:", dev->d_slice);
369 return (buf);
370 } else
371#endif
372#ifdef LOADER_MBR_SUPPORT

--- 45 unchanged lines hidden (view full) ---

418 return (EPART);
419 cp++;
420 }
421 } else
422 return (EINVAL);
423
424 if (*cp != '\0' && *cp != ':')
425 return (EINVAL);
426 dev->d_unit = unit;
426 dev->dd.d_unit = unit;
427 dev->d_slice = slice;
428 dev->d_partition = partition;
429 if (path != NULL)
430 *path = (*cp == '\0') ? cp: cp + 1;
431 return (0);
432}
427 dev->d_slice = slice;
428 dev->d_partition = partition;
429 if (path != NULL)
430 *path = (*cp == '\0') ? cp: cp + 1;
431 return (0);
432}