2 * device-process.c: detailed processing of device information sent
5 * Copyright (c) 2006 The Regents of the University of Michigan.
8 * Andy Adamson <andros@citi.umich.edu>
9 * Fred Isaman <iisaman@umich.edu>
11 * Copyright (c) 2010 EMC Corporation, Haiying Tang <Tang_Haiying@emc.com>
13 * Used codes in linux/fs/nfs/blocklayout/blocklayoutdev.c.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
39 #include <arpa/inet.h>
40 #include <linux/kdev_t.h>
50 #include "device-discovery.h"
52 uint32_t *blk_overflow(uint32_t * p, uint32_t * end, size_t nbytes)
54 uint32_t *q = p + ((nbytes + 3) >> 2);
61 static int decode_blk_signature(uint32_t **pp, uint32_t * end,
65 uint32_t siglen, *p = *pp;
67 BLK_READBUF(p, end, 4);
68 READ32(sig->si_num_comps);
69 if (sig->si_num_comps == 0) {
70 BL_LOG_ERR("0 components in sig\n");
73 if (sig->si_num_comps >= BLOCK_MAX_SIG_COMP) {
74 BL_LOG_ERR("number of sig comps %i >= BLOCK_MAX_SIG_COMP\n",
78 for (i = 0; i < sig->si_num_comps; i++) {
79 struct bl_sig_comp *comp = &sig->si_comps[i];
81 BLK_READBUF(p, end, 12);
82 READ64(comp->bs_offset);
84 comp->bs_length = siglen;
85 BLK_READBUF(p, end, siglen);
86 /* Note we rely here on fact that sig is used immediately
87 * for mapping, then thrown away.
89 comp->bs_string = (char *)p;
90 p += ((siglen + 3) >> 2);
99 * Read signature from device and compare to sig_comp
100 * return: 0=match, 1=no match, -1=error
103 read_cmp_blk_sig(struct bl_disk *disk, int fd, struct bl_sig_comp *comp)
105 const char *dev_name = disk->valid_path->full_path;
107 ssize_t siglen = comp->bs_length;
108 int64_t bs_offset = comp->bs_offset;
111 sig = (char *)malloc(siglen);
113 BL_LOG_ERR("%s: Out of memory\n", __func__);
118 bs_offset += (((int64_t) disk->size) << 9);
119 if (lseek64(fd, bs_offset, SEEK_SET) == -1) {
120 BL_LOG_ERR("File %s lseek error\n", dev_name);
124 if (read(fd, sig, siglen) != siglen) {
125 BL_LOG_ERR("File %s read error\n", dev_name);
129 ret = memcmp(sig, comp->bs_string, siglen);
138 * All signatures in sig must be found on disk for verification.
139 * Returns True if sig matches, False otherwise.
141 static int verify_sig(struct bl_disk *disk, struct bl_sig *sig)
143 const char *dev_name = disk->valid_path->full_path;
146 fd = open(dev_name, O_RDONLY | O_LARGEFILE);
148 BL_LOG_ERR("%s: %s could not be opened for read\n", __func__,
155 for (i = 0; i < sig->si_num_comps; i++) {
156 if (read_cmp_blk_sig(disk, fd, &sig->si_comps[i])) {
168 * map_sig_to_device()
169 * Given a signature, walk the list of visible disks searching for
170 * a match. Returns True if mapping was done, False otherwise.
172 * While we're at it, fill in the vol->bv_size.
174 static int map_sig_to_device(struct bl_sig *sig, struct bl_volume *vol)
177 struct bl_disk *disk;
179 /* scan disk list to find out match device */
180 for (disk = visible_disk_list; disk; disk = disk->next) {
181 /* FIXME: should we use better algorithm for disk scan? */
182 mapped = verify_sig(disk, sig);
184 vol->param.bv_dev = disk->dev;
185 vol->bv_size = disk->size;
192 /* We are given an array of XDR encoded array indices, each of which should
193 * refer to a previously decoded device. Translate into a list of pointers
194 * to the appropriate pnfs_blk_volume's.
196 static int set_vol_array(uint32_t **pp, uint32_t *end,
197 struct bl_volume *vols, int working)
201 struct bl_volume **array = vols[working].bv_vols;
203 for (i = 0; i < vols[working].bv_vol_n; i++) {
204 BLK_READBUF(p, end, 4);
206 if ((index < 0) || (index >= working)) {
207 BL_LOG_ERR("set_vol_array: Id %i out of range\n",
211 array[i] = &vols[index];
219 static uint64_t sum_subvolume_sizes(struct bl_volume *vol)
224 for (i = 0; i < vol->bv_vol_n; i++)
225 sum += vol->bv_vols[i]->bv_size;
230 decode_blk_volume(uint32_t **pp, uint32_t *end, struct bl_volume *vols, int voln,
236 struct bl_volume *vol = &vols[voln];
239 BLK_READBUF(p, end, 4);
240 READ32(vol->bv_type);
242 switch (vol->bv_type) {
243 case BLOCK_VOLUME_SIMPLE:
245 status = decode_blk_signature(&p, end, &sig);
248 status = map_sig_to_device(&sig, vol);
250 BL_LOG_ERR("Could not find disk for device\n");
253 BL_LOG_INFO("%s: simple %d\n", __func__, voln);
256 case BLOCK_VOLUME_SLICE:
257 BLK_READBUF(p, end, 16);
258 READ_SECTOR(vol->param.bv_offset);
259 READ_SECTOR(vol->bv_size);
260 *array_cnt = vol->bv_vol_n = 1;
261 BL_LOG_INFO("%s: slice %d\n", __func__, voln);
262 status = set_vol_array(&p, end, vols, voln);
264 case BLOCK_VOLUME_STRIPE:
265 BLK_READBUF(p, end, 8);
266 READ_SECTOR(vol->param.bv_stripe_unit);
267 off_t stripe_unit = vol->param.bv_stripe_unit;
268 /* Check limitations imposed by device-mapper */
269 if ((stripe_unit & (stripe_unit - 1)) != 0
270 || stripe_unit < (off_t) (sysconf(_SC_PAGE_SIZE) >> 9))
272 BLK_READBUF(p, end, 4);
273 READ32(vol->bv_vol_n);
276 *array_cnt = vol->bv_vol_n;
277 BL_LOG_INFO("%s: stripe %d nvols=%d unit=%ld\n", __func__, voln,
278 vol->bv_vol_n, (long)stripe_unit);
279 status = set_vol_array(&p, end, vols, voln);
282 for (j = 1; j < vol->bv_vol_n; j++) {
283 if (vol->bv_vols[j]->bv_size !=
284 vol->bv_vols[0]->bv_size) {
285 BL_LOG_ERR("varying subvol size\n");
289 vol->bv_size = vol->bv_vols[0]->bv_size * vol->bv_vol_n;
291 case BLOCK_VOLUME_CONCAT:
292 BLK_READBUF(p, end, 4);
293 READ32(vol->bv_vol_n);
296 *array_cnt = vol->bv_vol_n;
297 BL_LOG_INFO("%s: concat %d %d\n", __func__, voln,
299 status = set_vol_array(&p, end, vols, voln);
302 vol->bv_size = sum_subvolume_sizes(vol);
305 BL_LOG_ERR("Unknown volume type %i\n", vol->bv_type);
313 uint64_t process_deviceinfo(const char *dev_addr_buf,
314 unsigned int dev_addr_len,
315 uint32_t *major, uint32_t *minor)
317 int num_vols, i, status, count;
319 struct bl_volume *vols = NULL, **arrays = NULL, **arrays_ptr = NULL;
322 p = (uint32_t *) dev_addr_buf;
323 end = (uint32_t *) ((char *)p + dev_addr_len);
325 /* Decode block volume */
326 BLK_READBUF(p, end, 4);
328 BL_LOG_INFO("%s: %d vols\n", __func__, num_vols);
332 vols = (struct bl_volume *)malloc(num_vols * sizeof(struct bl_volume));
334 BL_LOG_ERR("%s: Out of memory\n", __func__);
338 /* Each volume in vols array needs its own array. Save time by
339 * allocating them all in one large hunk. Because each volume
340 * array can only reference previous volumes, and because once
341 * a concat or stripe references a volume, it may never be
342 * referenced again, the volume arrays are guaranteed to fit
343 * in the suprisingly small space allocated.
345 arrays_ptr = arrays =
346 (struct bl_volume **)malloc(num_vols * 2 *
347 sizeof(struct bl_volume *));
349 BL_LOG_ERR("%s: Out of memory\n", __func__);
353 for (i = 0; i < num_vols; i++) {
354 vols[i].bv_vols = arrays_ptr;
355 status = decode_blk_volume(&p, end, vols, i, &count);
362 BL_LOG_ERR("p is not equal to end!\n");
366 dev = dm_device_create(vols, num_vols);