bitmap_done = 0;
#endif
/* Ok, no bad inconsistancy, we can try updating etc */
- devices = malloc(num_devs * sizeof(*devices));
- devmap = calloc(num_devs * content->array.raid_disks, 1);
+ devices = xcalloc(num_devs, sizeof(*devices));
+ devmap = xcalloc(num_devs, content->array.raid_disks);
for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
char *devname = tmpdev->devname;
struct stat stb;
if (i < 10000) {
if (i >= bestcnt) {
int newbestcnt = i+10;
- int *newbest = malloc(sizeof(int)*newbestcnt);
+ int *newbest = xmalloc(sizeof(int)*newbestcnt);
int c;
for (c=0; c < newbestcnt; c++)
if (c < bestcnt)
/* now we have some devices that might be suitable.
* I wonder how many
*/
- avail = malloc(content->array.raid_disks);
- memset(avail, 0, content->array.raid_disks);
+ avail = xcalloc(content->array.raid_disks, 1);
okcnt = 0;
sparecnt=0;
rebuilding_cnt=0;
#ifndef MDASSEMBLE
if (content->reshape_active) {
int err = 0;
- int *fdlist = malloc(sizeof(int)* bestcnt);
+ int *fdlist = xmalloc(sizeof(int)* bestcnt);
if (verbose > 0)
pr_err(":%s has an active reshape - checking "
"if critical section needs to be restored\n",
}
}
- infos = malloc(sizeof(*infos) * total_slots);
- if (!infos) {
- pr_err("Unable to allocate memory\n");
- goto abort;
- }
+ infos = xmalloc(sizeof(*infos) * total_slots);
for (pass=1; pass <=2 ; pass++) {
struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */
/* update parent container uuid */
if (me) {
- char *path = strdup(me->path);
+ char *path = xstrdup(me->path);
st->ss->getinfo_super(st, &info_new, NULL);
map_update(&map, st->container_dev,
if (subarray)
info = st->ss->container_content(st, subarray);
else {
- info = malloc(sizeof(*info));
+ info = xmalloc(sizeof(*info));
st->ss->getinfo_super(st, info, NULL);
}
if (!info)
goto out;
}
- disks = malloc(max_disks * sizeof(mdu_disk_info_t));
+ disks = xmalloc(max_disks * sizeof(mdu_disk_info_t));
for (d=0; d<max_disks; d++) {
disks[d].state = (1<<MD_DISK_REMOVED);
disks[d].major = disks[d].minor = 0;
disks[next++] = disk;
}
- avail = calloc(array.raid_disks, 1);
+ avail = xcalloc(array.raid_disks, 1);
for (d= 0; d < array.raid_disks; d++) {
mdu_disk_info_t disk = disks[d];
if ((dv=map_dev_preferred(disk.major, disk.minor, 0, prefer))) {
if (brief) {
if (devices) {
- devices = realloc(devices,
+ devices = xrealloc(devices,
strlen(devices)+1+strlen(dv)+1);
strcat(strcat(devices,","),dv);
} else
- devices = strdup(dv);
+ devices = xstrdup(dv);
} else
printf(" %s", dv);
}
break;
}
if (!ap) {
- ap = malloc(sizeof(*ap));
+ ap = xmalloc(sizeof(*ap));
ap->devs = dl_head();
ap->next = arrays;
ap->spares = 0;
int disk_count = next_spare + working_disks;
dprintf("Called restore_backup()\n");
- fdlist = malloc(sizeof(int) * disk_count);
- if (fdlist == NULL) {
- pr_err("cannot allocate memory for disk list\n");
- return 1;
- }
+ fdlist = xmalloc(sizeof(int) * disk_count);
+
for (i = 0; i < next_spare; i++)
fdlist[i] = -1;
for (dev = content->devs; dev; dev = dev->next) {
nrdisks = max(reshape.before.data_disks,
reshape.after.data_disks) + reshape.parity
+ sra->array.spare_disks;
- fdlist = malloc((1+nrdisks) * sizeof(int));
- offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
- if (!fdlist || !offsets) {
- pr_err("malloc failed: grow aborted\n");
- goto release;
- }
+ fdlist = xcalloc((1+nrdisks), sizeof(int));
+ offsets = xcalloc((1+nrdisks), sizeof(offsets[0]));
odisks = reshape.before.data_disks + reshape.parity;
d = reshape_prepare_fdlist(devname, sra, odisks,
free(abuf);
free(bbuf);
abuflen = len;
- abuf = malloc(abuflen);
- bbuf = malloc(abuflen);
+ abuf = xmalloc(abuflen);
+ bbuf = xmalloc(abuflen);
}
lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
goto second_fail; /* Cannot find leading superblock */
/* Now need the data offsets for all devices. */
- offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
+ offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks);
for(j=0; j<info->array.raid_disks; j++) {
if (fdlist[j] < 0)
continue;
st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
if (!avail) {
raid_disks = info.array.raid_disks;
- avail = calloc(raid_disks, 1);
- if (!avail) {
- pr_err("out of memory.\n");
- exit(1);
- }
+ avail = xcalloc(raid_disks, 1);
*availp = avail;
- best = calloc(raid_disks, sizeof(int));
- devmap = calloc(raid_disks * numdevs, 1);
+ best = xcalloc(raid_disks, sizeof(int));
+ devmap = xcalloc(raid_disks, numdevs);
st->ss->getinfo_super(st, &info, devmap);
}
sprintf(base, "/dev/md_d%d", -1-devnum);
be = base + strlen(base);
- path2 = malloc(strlen(path)+20);
+ path2 = xmalloc(strlen(path)+20);
strcpy(path2, path);
pe = path2 + strlen(path2);
goto abort;
}
if (array.active_disks < array.raid_disks) {
- char *avail = calloc(array.raid_disks, 1);
+ char *avail = xcalloc(array.raid_disks, 1);
int d;
int found = 0;
* As we are "--re-add"ing we must find a spare slot
* to fill.
*/
- char *used = malloc(array.raid_disks);
- memset(used, 0, array.raid_disks);
+ char *used = xcalloc(array.raid_disks, 1);
for (j=0; j< tst->max_devs; j++) {
mdu_disk_info_t disc2;
disc2.number = j;
continue;
if (strcasecmp(mdlist->devname, "<ignore>") == 0)
continue;
- st = calloc(1, sizeof *st);
- if (st == NULL)
- continue;
+ st = xcalloc(1, sizeof *st);
if (mdlist->devname[0] == '/')
- st->devname = strdup(mdlist->devname);
+ st->devname = xstrdup(mdlist->devname);
else {
- st->devname = malloc(8+strlen(mdlist->devname)+1);
+ st->devname = xmalloc(8+strlen(mdlist->devname)+1);
strcpy(strcpy(st->devname, "/dev/md/"),
mdlist->devname);
}
st->percent = RESYNC_UNKNOWN;
st->expected_spares = mdlist->spare_disks;
if (mdlist->spare_group)
- st->spare_group = strdup(mdlist->spare_group);
+ st->spare_group = xstrdup(mdlist->spare_group);
statelist = st;
}
} else {
struct mddev_dev *dv;
for (dv=devlist ; dv; dv=dv->next) {
struct mddev_ident *mdlist = conf_get_ident(dv->devname);
- struct state *st = calloc(1, sizeof *st);
- if (st == NULL)
- continue;
- st->devname = strdup(dv->devname);
+ struct state *st = xcalloc(1, sizeof *st);
+ st->devname = xstrdup(dv->devname);
st->next = statelist;
st->devnum = INT_MAX;
st->percent = RESYNC_UNKNOWN;
if (mdlist) {
st->expected_spares = mdlist->spare_disks;
if (mdlist->spare_group)
- st->spare_group = strdup(mdlist->spare_group);
+ st->spare_group = xstrdup(mdlist->spare_group);
}
statelist = st;
}
(strcmp(mse->level, "raid0") != 0 &&
strcmp(mse->level, "linear") != 0))
) {
- struct state *st = calloc(1, sizeof *st);
+ struct state *st = xcalloc(1, sizeof *st);
mdu_array_info_t array;
int fd;
- if (st == NULL)
- continue;
- st->devname = strdup(get_md_name(mse->devnum));
+ st->devname = xstrdup(get_md_name(mse->devnum));
if ((fd = open(st->devname, O_RDONLY)) < 0 ||
ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
/* no such array */
}
n = read(fd, buf, 8192);
- info = malloc(sizeof(*info));
- if (info == NULL) {
-#if __GNUC__ < 3
- pr_err("failed to allocate %d bytes\n",
- (int)sizeof(*info));
-#else
- pr_err("failed to allocate %zd bytes\n",
- sizeof(*info));
-#endif
- free(buf);
- return NULL;
- }
+ info = xmalloc(sizeof(*info));
if (n < sizeof(info->sb)) {
pr_err("failed to read superblock of bitmap "
name = map_dev(major, minor, 1);
if (!name)
continue;
- d = malloc(sizeof(*d));
- d->devname = strdup(name);
+ d = xmalloc(sizeof(*d));
+ d->devname = xstrdup(name);
d->next = rv;
d->used = 0;
rv = d;
if (ent->metadata_version &&
strncmp(ent->metadata_version, "external:", 9) == 0 &&
!is_subarray(&ent->metadata_version[9])) {
- d = malloc(sizeof(*d));
- if (!d)
- continue;
+ d = xmalloc(sizeof(*d));
if (asprintf(&d->devname, "/dev/%s", ent->dev) < 0) {
free(d);
continue;
for (w=dl_next(line); w != line; w=dl_next(w)) {
if (w[0] == '/' || strcasecmp(w, "partitions") == 0 ||
strcasecmp(w, "containers") == 0) {
- cd = malloc(sizeof(*cd));
- cd->name = strdup(w);
+ cd = xmalloc(sizeof(*cd));
+ cd->name = xstrdup(w);
cd->next = cdevlist;
cdevlist = cd;
} else {
pr_err("only specify bitmap file once. %s ignored\n",
w);
else
- mis.bitmap_file = strdup(w+7);
+ mis.bitmap_file = xstrdup(w+7);
} else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
if (mis.devices)
pr_err("only specify devices once (use a comma separated list). %s ignored\n",
w);
else
- mis.devices = strdup(w+8);
+ mis.devices = xstrdup(w+8);
} else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
if (mis.spare_group)
pr_err("only specify one spare group per array. %s ignored.\n",
w);
else
- mis.spare_group = strdup(w+12);
+ mis.spare_group = xstrdup(w+12);
} else if (strncasecmp(w, "level=", 6) == 0 ) {
/* this is mainly for compatability with --brief output */
mis.level = map_name(pers, w+6);
mis.autof = parse_auto(w+5, "auto type", 0);
} else if (strncasecmp(w, "member=", 7) == 0) {
/* subarray within a container */
- mis.member = strdup(w+7);
+ mis.member = xstrdup(w+7);
} else if (strncasecmp(w, "container=", 10) == 0) {
/* the container holding this subarray. Either a device name
* or a uuid */
- mis.container = strdup(w+10);
+ mis.container = xstrdup(w+10);
} else {
pr_err("unrecognised word on ARRAY line: %s\n",
w);
(mis.container == NULL || mis.member == NULL))
pr_err("ARRAY line %s has no identity information.\n", mis.devname);
else {
- mi = malloc(sizeof(*mi));
+ mi = xmalloc(sizeof(*mi));
*mi = mis;
- mi->devname = mis.devname ? strdup(mis.devname) : NULL;
+ mi->devname = mis.devname ? xstrdup(mis.devname) : NULL;
mi->next = NULL;
*mddevlp = mi;
mddevlp = &mi->next;
for (w=dl_next(line); w != line ; w=dl_next(w)) {
if (alert_email == NULL)
- alert_email = strdup(w);
+ alert_email = xstrdup(w);
else
pr_err("excess address on MAIL line: %s - ignored\n",
w);
for (w=dl_next(line); w != line ; w=dl_next(w)) {
if (alert_mail_from == NULL)
- alert_mail_from = strdup(w);
+ alert_mail_from = xstrdup(w);
else {
char *t = NULL;
for (w=dl_next(line); w != line ; w=dl_next(w)) {
if (alert_program == NULL)
- alert_program = strdup(w);
+ alert_program = xstrdup(w);
else
pr_err("excess program on PROGRAM line: %s - ignored\n",
w);
require_homehost = 0;
else if (home_host == NULL) {
if (strcasecmp(w, "<none>")==0)
- home_host = strdup("");
+ home_host = xstrdup("");
else
- home_host = strdup(w);
+ home_host = xstrdup(w);
}else
pr_err("excess host name on HOMEHOST line: %s - ignored\n",
w);
for (super_cnt = 0; superlist[super_cnt]; super_cnt++)
;
- seen = calloc(super_cnt, 1);
+ seen = xcalloc(super_cnt, 1);
for (w = dl_next(line); w != line ; w = dl_next(w)) {
char *val;
}
if (flags & GLOB_APPEND) {
for (i=0; i<globbuf.gl_pathc; i++) {
- struct mddev_dev *t = malloc(sizeof(*t));
- t->devname = strdup(globbuf.gl_pathv[i]);
+ struct mddev_dev *t = xmalloc(sizeof(*t));
+ t->devname = xstrdup(globbuf.gl_pathv[i]);
t->next = dlist;
t->used = 0;
dlist = t;
#ifdef __dietlibc__
char *strncpy(char *dest, const char *src, size_t n) __THROW;
#endif
+void *xcalloc(size_t num, size_t size);
#include "dlink.h"
if (s == NULL)
return NULL;
n = dl_newv(char, l+1);
- if (n == NULL)
- return NULL;
- else
- {
- strncpy(n, s, l);
- n[l] = 0;
- return n;
- }
+ strncpy(n, s, l);
+ n[l] = 0;
+ return n;
}
char *dl_strdup(char *s)
void * dh_next;
};
-#define dl_alloc(size) ((void*)(((char*)calloc(1,(size)+sizeof(struct __dl_head)))+sizeof(struct __dl_head)))
+#define dl_alloc(size) ((void*)(((char*)xcalloc(1,(size)+sizeof(struct __dl_head)))+sizeof(struct __dl_head)))
#define dl_new(t) ((t*)dl_alloc(sizeof(t)))
#define dl_newv(t,n) ((t*)dl_alloc(sizeof(t)*n))
{
char name[100];
fmt_devname(name,num);
- return strdup(name);
+ return xstrdup(name);
}
int devname2devnum(char *name)
}
if ((stb->st_mode&S_IFMT)== S_IFBLK) {
- char *n = strdup(name);
- struct devmap *dm = malloc(sizeof(*dm));
+ char *n = xstrdup(name);
+ struct devmap *dm = xmalloc(sizeof(*dm));
if (strncmp(n, "/dev/./", 7)==0)
strcpy(n+4, name+6);
if (dm) {
int c;
int quote;
int wordfound = 0;
- char *word = malloc(wsize);
-
- if (!word) abort();
+ char *word = xmalloc(wsize);
while (wordfound==0) {
/* at the end of a word.. */
else {
if (len == wsize-1) {
wsize += 100;
- word = realloc(word, wsize);
- if (!word) abort();
+ word = xrealloc(word, wsize);
}
word[len++] = c;
}
static struct active_array *duplicate_aa(struct active_array *aa)
{
- struct active_array *newa = malloc(sizeof(*newa));
+ struct active_array *newa = xmalloc(sizeof(*newa));
struct mdinfo **dp1, **dp2;
*newa = *aa;
if ((*dp1)->state_fd < 0)
continue;
- d = malloc(sizeof(*d));
+ d = xmalloc(sizeof(*d));
*d = **dp1;
*dp2 = d;
dp2 = & d->next;
di->disk.minor == cd->disk.minor)
break;
if (!cd) {
- struct mdinfo *newd = malloc(sizeof(*newd));
+ struct mdinfo *newd = xmalloc(sizeof(*newd));
- if (!newd) {
- container->devcnt = -1;
- continue;
- }
*newd = *di;
add_disk_to_container(container, newd);
}
for (d = newdev; d ; d = d->next) {
struct mdinfo *newd;
- newd = malloc(sizeof(*newd));
- if (!newd)
- continue;
+ newd = xmalloc(sizeof(*newd));
if (sysfs_add_disk(&newa->info, d, 0) < 0) {
free(newd);
continue;
if (!newa)
break;
}
- newd = malloc(sizeof(*newd));
- if (!newd)
- continue;
+ newd = xmalloc(sizeof(*newd));
disk_init_and_add(newd, d, newa);
}
if (sysfs_get_ll(info, NULL, "array_size", &array_size) == 0
GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT|
GET_DEGRADED|GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
- new = malloc(sizeof(*new));
- if (!new || !mdi) {
- if (mdi)
- sysfs_free(mdi);
- if (new)
- free(new);
+ if (!mdi)
return;
- }
- memset(new, 0, sizeof(*new));
+ new = xcalloc(1, sizeof(*new));
new->devnum = mdstat->devnum;
strcpy(new->info.sys_name, devnum2devname(new->devnum));
new->info.component_size = mdi->component_size;
for (i = 0; i < new->info.array.raid_disks; i++) {
- struct mdinfo *newd = malloc(sizeof(*newd));
+ struct mdinfo *newd = xmalloc(sizeof(*newd));
for (di = mdi->devs; di; di = di->next)
if (i == di->disk.raid_disk)
manage(mdstat, container);
free_mdstat(mdstat);
} else if (!sigterm) {
- mu = malloc(sizeof(*mu));
+ mu = xmalloc(sizeof(*mu));
mu->len = msg->len;
mu->buf = msg->buf;
msg->buf = NULL;
void map_add(struct map_ent **melp,
int devnum, char *metadata, int uuid[4], char *path)
{
- struct map_ent *me = malloc(sizeof(*me));
+ struct map_ent *me = xmalloc(sizeof(*me));
me->devnum = devnum;
strcpy(me->metadata, metadata);
memcpy(me->uuid, uuid, 16);
- me->path = path ? strdup(path) : NULL;
+ me->path = path ? xstrdup(path) : NULL;
me->next = *melp;
me->bad = 0;
*melp = me;
strcpy(mp->metadata, metadata);
memcpy(mp->uuid, uuid, 16);
free(mp->path);
- mp->path = path ? strdup(path) : NULL;
+ mp->path = path ? xstrdup(path) : NULL;
mp->bad = 0;
break;
}
/* If first option is a device, don't force the mode yet */
if (opt == 1) {
if (devs_found == 0) {
- dv = malloc(sizeof(*dv));
- if (dv == NULL) {
- pr_err("malloc failed\n");
- exit(3);
- }
+ dv = xmalloc(sizeof(*dv));
dv->devname = optarg;
dv->disposition = devmode;
dv->writemostly = writemostly;
" devices to add: %s\n", optarg);
exit(2);
}
- dv = malloc(sizeof(*dv));
- if (dv == NULL) {
- pr_err("malloc failed\n");
- exit(3);
- }
+ dv = xmalloc(sizeof(*dv));
dv->devname = optarg;
dv->disposition = devmode;
dv->writemostly = writemostly;
#define pr_err(fmt ...) fprintf(stderr, Name ": " fmt)
#define cont_err(fmt ...) fprintf(stderr, " " fmt)
+void *xmalloc(size_t len);
+void *xrealloc(void *ptr, size_t len);
+void *xcalloc(size_t num, size_t size);
+char *xstrdup(const char *str);
+
#define LEVEL_MULTIPATH (-4)
#define LEVEL_LINEAR (-1)
#define LEVEL_FAULTY (-5)
} else
pfd[0] = pfd[1] = -1;
- container = calloc(1, sizeof(*container));
+ container = xcalloc(1, sizeof(*container));
container->devnum = devnum;
container->devname = devname;
container->arrays = NULL;
container->devs = NULL;
for (di = mdi->devs; di; di = di->next) {
- struct mdinfo *cd = malloc(sizeof(*cd));
+ struct mdinfo *cd = xmalloc(sizeof(*cd));
*cd = *di;
cd->next = container->devs;
container->devs = cd;
minor_num = -1;
} else
return;
- name = malloc(nlen);
+ name = xmalloc(nlen);
for (i=1; i <= cnt ; i++) {
struct stat stb2;
snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
/* not a device */
return 0;
- new = malloc(sizeof(*new));
+ new = xmalloc(sizeof(*new));
new->name = strndup(name, t - name);
new->next = *m;
*m = new;
continue;
}
- ent = malloc(sizeof(*ent));
- if (!ent) {
- pr_err("malloc failed reading /proc/mdstat.\n");
- free_line(line);
- break;
- }
+ ent = xmalloc(sizeof(*ent));
ent->dev = ent->level = ent->pattern= NULL;
ent->next = NULL;
ent->percent = RESYNC_NONE;
ent->devcnt = 0;
ent->members = NULL;
- ent->dev = strdup(line);
+ ent->dev = xstrdup(line);
ent->devnum = devnum;
for (w=dl_next(line); w!= line ; w=dl_next(w)) {
} else if (ent->active > 0 &&
ent->level == NULL &&
w[0] != '(' /*readonly*/) {
- ent->level = strdup(w);
+ ent->level = xstrdup(w);
in_devs = 1;
} else if (in_devs && strcmp(w, "blocks")==0)
in_devs = 0;
} else if (strcmp(w, "super") == 0 &&
dl_next(w) != line) {
w = dl_next(w);
- ent->metadata_version = strdup(w);
+ ent->metadata_version = xstrdup(w);
} else if (w[0] == '[' && isdigit(w[1])) {
ent->raid_disks = atoi(w+1);
} else if (!ent->pattern &&
w[0] == '[' &&
(w[1] == 'U' || w[1] == '_')) {
- ent->pattern = strdup(w+1);
+ ent->pattern = xstrdup(w+1);
if (ent->pattern[l-2]==']')
ent->pattern[l-2] = '\0';
} else if (ent->percent == RESYNC_NONE &&
if (rv < 0 || len > MSG_MAX_LEN)
return -1;
if (len > 0) {
- msg->buf = malloc(len);
- if (msg->buf == NULL)
- return -1;
+ msg->buf = xmalloc(len);
rv = recv_buf(fd, msg->buf, len, tmo);
if (rv < 0) {
free(msg->buf);
/* start / add list entry */
if (!head) {
- head = malloc(sizeof(*head));
+ head = xmalloc(sizeof(*head));
list = head;
} else {
- list->next = malloc(sizeof(*head));
+ list->next = xmalloc(sizeof(*head));
list = list->next;
}
static void pol_new(struct dev_policy **pol, char *name, const char *val,
const char *metadata)
{
- struct dev_policy *n = malloc(sizeof(*n));
+ struct dev_policy *n = xmalloc(sizeof(*n));
const char *real_metadata = NULL;
int i;
if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor))
continue;
closedir(by_path);
- return strdup(ent->d_name);
+ return xstrdup(ent->d_name);
}
closedir(by_path);
/* A NULL path isn't really acceptable - use the devname.. */
nm[rv] = 0;
dname = strrchr(nm, '/');
if (dname)
- return strdup(dname + 1);
+ return xstrdup(dname + 1);
}
- return strdup("unknown");
+ return xstrdup("unknown");
}
char type_part[] = "part";
if (strncmp(w, name, len) != 0 ||
w[len] != '=')
return 0;
- r = malloc(sizeof(*r));
+ r = xmalloc(sizeof(*r));
r->next = *rp;
r->name = name;
- r->value = strdup(w+len+1);
+ r->value = xstrdup(w+len+1);
r->dups = NULL;
*rp = r;
return 1;
if (config_rules_end == NULL)
config_rules_end = &config_rules;
- pr = malloc(sizeof(*pr));
+ pr = xmalloc(sizeof(*pr));
pr->type = type;
pr->rule = NULL;
for (w = dl_next(line); w != line ; w = dl_next(w)) {
struct pol_rule *pr;
char *name, *val;
- pr = malloc(sizeof(*pr));
+ pr = xmalloc(sizeof(*pr));
pr->type = type;
pr->rule = NULL;
struct rule *r;
val = va_arg(ap, char*);
- r = malloc(sizeof(*r));
+ r = xmalloc(sizeof(*r));
r->next = pr->rule;
r->name = name;
- r->value = strdup(val);
+ r->value = xstrdup(val);
r->dups = NULL;
pr->rule = r;
}
dom = *domp;
}
if (dom == NULL || strcmp(dom->dom, domain) != 0) {
- dom = malloc(sizeof(*dom));
+ dom = xmalloc(sizeof(*dom));
dom->next = *domp;
dom->dom = domain;
*domp = dom;
unsigned long long start, unsigned long long length, char *name[])
{
/* read the data and p and q blocks, and check we got them right */
- char *stripe_buf = malloc(raid_disks * chunk_size);
- char **stripes = malloc(raid_disks * sizeof(char*));
- char **blocks = malloc(raid_disks * sizeof(char*));
- uint8_t *p = malloc(chunk_size);
- uint8_t *q = malloc(chunk_size);
- int *results = malloc(chunk_size * sizeof(int));
+ char *stripe_buf = xmalloc(raid_disks * chunk_size);
+ char **stripes = xmalloc(raid_disks * sizeof(char*));
+ char **blocks = xmalloc(raid_disks * sizeof(char*));
+ uint8_t *p = xmalloc(chunk_size);
+ uint8_t *q = xmalloc(chunk_size);
+ int *results = xmalloc(chunk_size * sizeof(int));
int i;
int diskP, diskQ;
extern int tables_ready;
- if((stripe_buf == NULL) ||
- (stripes == NULL) ||
- (blocks == NULL) ||
- (p == NULL) ||
- (q == NULL) ||
- (results == NULL)) {
- err = 1;
- goto exitCheck;
- }
-
if (!tables_ready)
make_tables();
length = (info->component_size * 512) / chunk_size - start;
}
- disk_name = malloc(raid_disks * sizeof(*disk_name));
- fds = malloc(raid_disks * sizeof(*fds));
- offsets = malloc(raid_disks * sizeof(*offsets));
- buf = malloc(raid_disks * chunk_size);
-
- if((disk_name == NULL) ||
- (fds == NULL) ||
- (offsets == NULL) ||
- (buf == NULL)) {
- fprintf(stderr, "%s: allocation fail\n", prg);
- exit_err = 5;
- goto exitHere;
- }
+ disk_name = xmalloc(raid_disks * sizeof(*disk_name));
+ fds = xmalloc(raid_disks * sizeof(*fds));
+ offsets = xcalloc(raid_disks, sizeof(*offsets));
+ buf = xmalloc(raid_disks * chunk_size);
- memset(offsets, 0, raid_disks * sizeof(*offsets));
for(i=0; i<raid_disks; i++) {
fds[i] = -1;
}
if (zero == NULL || chunk_size > zero_size) {
if (zero)
free(zero);
- zero = malloc(chunk_size);
- if (zero)
- memset(zero, 0, chunk_size);
+ zero = xcalloc(1, chunk_size);
zero_size = chunk_size;
}
char *src_buf)
{
char *stripe_buf;
- char **stripes = malloc(raid_disks * sizeof(char*));
- char **blocks = malloc(raid_disks * sizeof(char*));
+ char **stripes = xmalloc(raid_disks * sizeof(char*));
+ char **blocks = xmalloc(raid_disks * sizeof(char*));
int i;
int rv;
if (zero == NULL || chunk_size > zero_size) {
if (zero)
free(zero);
- zero = malloc(chunk_size);
- if (zero)
- memset(zero, 0, chunk_size);
+ zero = xcalloc(1, chunk_size);
zero_size = chunk_size;
}
unsigned long long start, unsigned long long length)
{
/* ready the data and p (and q) blocks, and check we got them right */
- char *stripe_buf = malloc(raid_disks * chunk_size);
- char **stripes = malloc(raid_disks * sizeof(char*));
- char **blocks = malloc(raid_disks * sizeof(char*));
- char *p = malloc(chunk_size);
- char *q = malloc(chunk_size);
+ char *stripe_buf = xmalloc(raid_disks * chunk_size);
+ char **stripes = xmalloc(raid_disks * sizeof(char*));
+ char **blocks = xmalloc(raid_disks * sizeof(char*));
+ char *p = xmalloc(chunk_size);
+ char *q = xmalloc(chunk_size);
int i;
int diskP, diskQ;
raid_disks, argc-9);
exit(2);
}
- fds = malloc(raid_disks * sizeof(*fds));
- offsets = malloc(raid_disks * sizeof(*offsets));
- memset(offsets, 0, raid_disks * sizeof(*offsets));
+ fds = xmalloc(raid_disks * sizeof(*fds));
+ offsets = xcalloc(raid_disks, sizeof(*offsets));
storefd = open(file, O_RDWR);
if (storefd < 0) {
}
}
- buf = malloc(raid_disks * chunk_size);
+ buf = xmalloc(raid_disks * chunk_size);
if (save == 1) {
int rv = save_stripes(fds, offsets,
exit(0);
}
+
+void *xmalloc(size_t len)
+{
+ void *rv = malloc(len);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ write(2, msg, strlen(msg));
+ exit(4);
+}
+
+void *xcalloc(size_t num, size_t size)
+{
+ void *rv = calloc(num, size);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ write(2, msg, strlen(msg));
+ exit(4);
+}
#endif /* MAIN */
super->active->data_section_offset,
super->active->data_section_length,
0);
- dl->devname = devname ? strdup(devname) : NULL;
+ dl->devname = devname ? xstrdup(devname) : NULL;
fstat(fd, &stb);
dl->major = major(stb.st_rdev);
)
return NULL;
- st = malloc(sizeof(*st));
- memset(st, 0, sizeof(*st));
+ st = xcalloc(1, sizeof(*st));
st->container_dev = NoMdDev;
st->ss = &super_ddf;
st->max_devs = 512;
int n = 0;
unsigned int i, j;
- rv = malloc(sizeof(struct extent) * (ddf->max_part + 2));
- if (!rv)
- return NULL;
+ rv = xmalloc(sizeof(struct extent) * (ddf->max_part + 2));
for (i = 0; i < ddf->max_part; i++) {
struct vcl *v = dl->vlist[i];
sizeof(struct phys_disk_entry));
struct phys_disk *pd;
- pd = malloc(len);
+ pd = xmalloc(len);
pd->magic = DDF_PHYS_RECORDS_MAGIC;
pd->used_pdes = __cpu_to_be16(n);
pde = &pd->entries[0];
sizeof(struct phys_disk_entry));
struct phys_disk *pd;
- pd = malloc(len);
+ pd = xmalloc(len);
pd->magic = DDF_PHYS_RECORDS_MAGIC;
pd->used_pdes = __cpu_to_be16(dl->pdnum);
pd->entries[0].state = __cpu_to_be16(DDF_Missing);
/* First the virtual disk. We have a slightly fake header */
len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry);
- vd = malloc(len);
+ vd = xmalloc(len);
*vd = *ddf->virt;
vd->entries[0] = ddf->virt->entries[currentconf->vcnum];
vd->populated_vdes = __cpu_to_be16(currentconf->vcnum);
/* Then the vd_config */
len = ddf->conf_rec_len * 512;
- vc = malloc(len);
+ vc = xmalloc(len);
memcpy(vc, ¤tconf->conf, len);
append_metadata_update(st, vc, len);
*ep != '\0'))
continue;
- this = malloc(sizeof(*this));
- memset(this, 0, sizeof(*this));
+ this = xcalloc(1, sizeof(*this));
this->next = rest;
rest = this;
/* Haven't found that one yet, maybe there are others */
continue;
- dev = malloc(sizeof(*dev));
- memset(dev, 0, sizeof(*dev));
+ dev = xcalloc(1, sizeof(*dev));
dev->next = this->devs;
this->devs = dev;
}
/* Cool, we have a device with some space at pos */
- di = malloc(sizeof(*di));
- if (!di)
- continue;
- memset(di, 0, sizeof(*di));
+ di = xcalloc(1, sizeof(*di));
di->disk.number = i;
di->disk.raid_disk = i;
di->disk.major = dl->major;
* Create a metadata_update record to update the
* phys_refnum and lba_offset values
*/
- mu = malloc(sizeof(*mu));
- if (mu && posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
+ mu = xmalloc(sizeof(*mu));
+ if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
free(mu);
mu = NULL;
}
- if (!mu) {
- while (rv) {
- struct mdinfo *n = rv->next;
-
- free(rv);
- rv = n;
- }
- return NULL;
- }
-
- mu->buf = malloc(ddf->conf_rec_len * 512);
+ mu->buf = xmalloc(ddf->conf_rec_len * 512);
mu->len = ddf->conf_rec_len * 512;
mu->space = NULL;
mu->space_list = NULL;
static struct supertype *match_metadata_desc(char *arg)
{
- struct supertype *st = malloc(sizeof(*st));
+ struct supertype *st = xmalloc(sizeof(*st));
if (!st)
return st;
static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
{
- struct intel_hba *result = malloc(sizeof(*result));
- if (result) {
- result->type = device->type;
- result->path = strdup(device->path);
- result->next = NULL;
- if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
- result->pci_id++;
- }
+ struct intel_hba *result = xmalloc(sizeof(*result));
+
+ result->type = device->type;
+ result->path = xstrdup(device->path);
+ result->next = NULL;
+ if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
+ result->pci_id++;
+
return result;
}
)
return NULL;
- st = malloc(sizeof(*st));
- if (!st)
- return NULL;
- memset(st, 0, sizeof(*st));
+ st = xcalloc(1, sizeof(*st));
st->container_dev = NoMdDev;
st->ss = &super_imsm;
st->max_devs = IMSM_MAX_DEVICES;
else
reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
- rv = malloc(sizeof(struct extent) * (memberships + 1));
- if (!rv)
- return NULL;
+ rv = xcalloc(sizeof(struct extent), (memberships + 1));
e = rv;
for (i = 0; i < super->anchor->num_raid_devs; i++) {
update_memory_size =
sizeof(struct imsm_update_general_migration_checkpoint);
- *u = calloc(1, update_memory_size);
+ *u = xcalloc(1, update_memory_size);
if (*u == NULL) {
dprintf("error: cannot get memory for "
"imsm_create_metadata_checkpoint_update update\n");
if (!super || !super->disks)
return NULL;
dl = super->disks;
- mddev = malloc(sizeof(*mddev));
- if (!mddev) {
- pr_err("Failed to allocate memory.\n");
- return NULL;
- }
- memset(mddev, 0, sizeof(*mddev));
+ mddev = xcalloc(1, sizeof(*mddev));
while (dl) {
struct mdinfo *tmp;
disk = &dl->disk;
- tmp = malloc(sizeof(*tmp));
- if (!tmp) {
- pr_err("Failed to allocate memory.\n");
- if (mddev)
- sysfs_free(mddev);
- return NULL;
- }
- memset(tmp, 0, sizeof(*tmp));
+ tmp = xcalloc(1, sizeof(*tmp));
if (mddev->devs)
tmp->next = mddev->devs;
mddev->devs = tmp;
* fails here we don't associate the spare
*/
for (i = 0; i < sec->anchor->num_raid_devs; i++) {
- dv = malloc(sizeof(*dv));
- if (!dv)
- break;
- dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
- if (!dev) {
- free(dv);
- break;
- }
+ dv = xmalloc(sizeof(*dv));
+ dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
dv->dev = dev;
dv->index = i;
dv->next = first->devlist;
if (rv != 0)
return 2;
- dl = calloc(1, sizeof(*dl));
- if (!dl) {
- if (devname)
- pr_err("failed to allocate disk buffer for %s\n",
- devname);
- return 2;
- }
+ dl = xcalloc(1, sizeof(*dl));
fstat(fd, &stb);
dl->major = major(stb.st_rdev);
dl->e = NULL;
fd2devname(fd, name);
if (devname)
- dl->devname = strdup(devname);
+ dl->devname = xstrdup(devname);
else
- dl->devname = strdup(name);
+ dl->devname = xstrdup(name);
/* look up this disk's index in the current anchor */
disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
if (len_migr > len)
space_needed += len_migr - len;
- dv = malloc(sizeof(*dv));
- if (!dv)
- return 1;
+ dv = xmalloc(sizeof(*dv));
if (max_len < len_migr)
max_len = len_migr;
if (max_len > len_migr)
space_needed += max_len - len_migr;
- dev_new = malloc(max_len);
- if (!dev_new) {
- free(dv);
- return 1;
- }
+ dev_new = xmalloc(max_len);
imsm_copy_dev(dev_new, dev_iter);
dv->dev = dev_new;
dv->index = i;
static struct intel_super *alloc_super(void)
{
- struct intel_super *super = malloc(sizeof(*super));
+ struct intel_super *super = xcalloc(1, sizeof(*super));
- if (super) {
- memset(super, 0, sizeof(*super));
- super->current_vol = -1;
- super->create_offset = ~((unsigned long long) 0);
- }
+ super->current_vol = -1;
+ super->create_offset = ~((unsigned long long) 0);
return super;
}
if (dl)
continue;
- dl = malloc(sizeof(*dl));
- if (!dl)
- return 1;
+ dl = xmalloc(sizeof(*dl));
dl->major = 0;
dl->minor = 0;
dl->fd = -1;
- dl->devname = strdup("missing");
+ dl->devname = xstrdup("missing");
dl->index = i;
serialcpy(dl->serial, disk->serial);
dl->disk = *disk;
is_failed(&idisk->disk))
idisk->disk.status &= ~(SPARE_DISK);
} else {
- idisk = calloc(1, sizeof(*idisk));
- if (!idisk)
- return -1;
+ idisk = xcalloc(1, sizeof(*idisk));
idisk->owner = IMSM_UNKNOWN_OWNER;
idisk->disk = *disk;
idisk->next = *disk_list;
free_super_imsm(st);
super = alloc_super();
- if (!super) {
- pr_err("malloc of %zu failed.\n",
- sizeof(*super));
- return 1;
- }
/* Load hba and capabilities if they exist.
* But do not preclude loading metadata in case capabilities or hba are
* non-compliant and ignore_hw_compat is set.
if (!check_name(super, name, 0))
return 0;
- dv = malloc(sizeof(*dv));
- if (!dv) {
- pr_err("failed to allocate device list entry\n");
- return 0;
- }
- dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
- if (!dev) {
- free(dv);
- pr_err("could not allocate raid device\n");
- return 0;
- }
-
+ dv = xmalloc(sizeof(*dv));
+ dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
array_blocks = calc_array_size(info->level, info->raid_disks,
info->layout, info->chunk_size,
return add_to_super_imsm_volume(st, dk, fd, devname);
fstat(fd, &stb);
- dd = malloc(sizeof(*dd));
- if (!dd) {
- pr_err("malloc failed %s:%d.\n", __func__, __LINE__);
- return 1;
- }
- memset(dd, 0, sizeof(*dd));
+ dd = xcalloc(sizeof(*dd), 1);
dd->major = major(stb.st_rdev);
dd->minor = minor(stb.st_rdev);
- dd->devname = devname ? strdup(devname) : NULL;
+ dd->devname = devname ? xstrdup(devname) : NULL;
dd->fd = fd;
dd->e = NULL;
dd->action = DISK_ADD;
"(line %d).\n", __func__, __LINE__);
return 1;
}
- dd = malloc(sizeof(*dd));
- if (!dd) {
- pr_err("malloc failed %s:%d.\n", __func__, __LINE__);
- return 1;
- }
- memset(dd, 0, sizeof(*dd));
+ dd = xcalloc(1, sizeof(*dd));
dd->major = dk->major;
dd->minor = dk->minor;
dd->fd = -1;
len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
sizeof(*inf) * map->num_members;
- u = malloc(len);
- if (!u) {
- fprintf(stderr, "%s: failed to allocate update buffer\n",
- __func__);
- return 1;
- }
-
+ u = xmalloc(len);
u->type = update_create_array;
u->dev_idx = dev_idx;
imsm_copy_dev(&u->dev, dev);
return 0;
len = sizeof(*u);
- u = malloc(len);
- if (!u) {
- fprintf(stderr, "%s: failed to allocate update buffer\n",
- __func__);
- return 1;
- }
-
+ u = xmalloc(len);
u->type = update_add_remove_disk;
append_metadata_update(st, u, len);
* note that there is no fd for the disks in array.
*/
super = alloc_super();
- if (!super) {
- pr_err("malloc of %zu failed.\n",
- sizeof(*super));
- close(fd);
- return 0;
- }
-
rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL);
if (rv != 0) {
#if DEBUG
* 'maxsize' given the "all disks in an array must share a common start
* offset" constraint
*/
- struct extent *e = calloc(sum_extents, sizeof(*e));
+ struct extent *e = xcalloc(sum_extents, sizeof(*e));
struct dl *dl;
int i, j;
int start_extent;
unsigned long long maxsize;
unsigned long reserve;
- if (!e)
- return 0;
-
/* coalesce and sort all extents. also, check to see if we need to
* reserve space between member arrays
*/
struct dev_member *dev = memb->members;
int fd = -1;
while(dev && (fd < 0)) {
- char *path = malloc(strlen(dev->name) + strlen("/dev/") + 1);
- if (path) {
- num = sprintf(path, "%s%s", "/dev/", dev->name);
- if (num > 0)
- fd = open(path, O_RDONLY, 0);
- if ((num <= 0) || (fd < 0)) {
- pr_vrb(": Cannot open %s: %s\n",
- dev->name, strerror(errno));
- }
- free(path);
+ char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
+ num = sprintf(path, "%s%s", "/dev/", dev->name);
+ if (num > 0)
+ fd = open(path, O_RDONLY, 0);
+ if ((num <= 0) || (fd < 0)) {
+ pr_vrb(": Cannot open %s: %s\n",
+ dev->name, strerror(errno));
}
+ free(path);
dev = dev->next;
}
found = 0;
}
}
if (*devlist && (found < dpa)) {
- dv = calloc(1, sizeof(*dv));
- if (dv == NULL)
- pr_err("calloc failed\n");
- else {
- dv->devname = malloc(strlen(memb->dev) + strlen("/dev/") + 1);
- if (dv->devname != NULL) {
- sprintf(dv->devname, "%s%s", "/dev/", memb->dev);
- dv->found = found;
- dv->used = 0;
- dv->next = *devlist;
- *devlist = dv;
- } else
- free(dv);
- }
+ dv = xcalloc(1, sizeof(*dv));
+ dv->devname = xmalloc(strlen(memb->dev) + strlen("/dev/") + 1);
+ sprintf(dv->devname, "%s%s", "/dev/", memb->dev);
+ dv->found = found;
+ dv->used = 0;
+ dv->next = *devlist;
+ *devlist = dv;
}
}
if (fd >= 0)
struct md_list *dv = NULL;
for(i = 0; i < 12; i++) {
- dv = calloc(1, sizeof(*dv));
- if (dv == NULL) {
- pr_err("calloc failed\n");
- break;
- }
- dv->devname = malloc(40);
- if (dv->devname == NULL) {
- pr_err("malloc failed\n");
- free(dv);
- break;
- }
+ dv = xcalloc(1, sizeof(*dv));
+ dv->devname = xmalloc(40);
sprintf(dv->devname, "/dev/loop%d", i);
dv->next = devlist;
devlist = dv;
}
- dv = calloc(1, sizeof(*dv));
- if (dv == NULL) {
- pr_err("malloc failed\n");
- err = 1;
- break;
- }
- dv->devname = strdup(buf);
- if (dv->devname == NULL) {
- pr_err("malloc failed\n");
- err = 1;
- free(dv);
- break;
- }
+ dv = xcalloc(1, sizeof(*dv));
+ dv->devname = xstrdup(buf);
dv->next = devlist;
devlist = dv;
}
}
if (st->update_tail) {
- struct imsm_update_kill_array *u = malloc(sizeof(*u));
+ struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
- if (!u)
- return 2;
u->type = update_kill_array;
u->dev_idx = current_vol;
append_metadata_update(st, u, sizeof(*u));
return 2;
if (st->update_tail) {
- struct imsm_update_rename_array *u = malloc(sizeof(*u));
+ struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
- if (!u)
- return 2;
u->type = update_rename_array;
u->dev_idx = vol;
snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
* OROM/EFI
*/
- this = malloc(sizeof(*this));
- if (!this) {
- pr_err("failed to allocate %zu bytes\n",
- sizeof(*this));
- break;
- }
+ this = xmalloc(sizeof(*this));
super->current_vol = i;
getinfo_super_imsm_volume(st, this, NULL);
if (skip)
continue;
- info_d = calloc(1, sizeof(*info_d));
- if (!info_d) {
- pr_err("failed to allocate disk"
- " for volume %.16s\n", dev->volume);
- info_d = this->devs;
- while (info_d) {
- struct mdinfo *d = info_d->next;
-
- free(info_d);
- info_d = d;
- }
- free(this);
- this = rest;
- break;
- }
+ info_d = xcalloc(1, sizeof(*info_d));
info_d->next = this->devs;
this->devs = info_d;
continue;
/* found a usable disk with enough space */
- di = malloc(sizeof(*di));
- if (!di)
- continue;
- memset(di, 0, sizeof(*di));
+ di = xcalloc(1, sizeof(*di));
/* dl->index will be -1 in the case we are activating a
* pristine spare. imsm_process_update() will create a
* Create a metadata_update record to update the
* disk_ord_tbl for the array
*/
- mu = malloc(sizeof(*mu));
- if (mu) {
- mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
- if (mu->buf == NULL) {
- free(mu);
- mu = NULL;
- }
- }
- if (!mu) {
- while (rv) {
- struct mdinfo *n = rv->next;
-
- free(rv);
- rv = n;
- }
- return NULL;
- }
-
+ mu = xmalloc(sizeof(*mu));
+ mu->buf = xcalloc(num_spares,
+ sizeof(struct imsm_update_activate_spare));
mu->space = NULL;
mu->space_list = NULL;
mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
int num_members = map->num_members;
void *space;
int size, i;
- int err = 0;
/* allocate memory for added disks */
for (i = 0; i < num_members; i++) {
size = sizeof(struct dl);
- space = malloc(size);
- if (!space) {
- err++;
- break;
- }
+ space = xmalloc(size);
*tail = space;
tail = space;
*tail = NULL;
/* allocate memory for new device */
size = sizeof_imsm_dev(super->devlist->dev, 0) +
(num_members * sizeof(__u32));
- space = malloc(size);
- if (!space)
- err++;
- else {
- *tail = space;
- tail = space;
- *tail = NULL;
- }
- if (!err) {
- len = disks_to_mpb_size(num_members * 2);
- } else {
- /* if allocation didn't success, free buffer */
- while (update->space_list) {
- void **sp = update->space_list;
- update->space_list = *sp;
- free(sp);
- }
- }
+ space = xmalloc(size);
+ *tail = space;
+ tail = space;
+ *tail = NULL;
+ len = disks_to_mpb_size(num_members * 2);
}
break;
if (u->new_raid_disks > u->old_raid_disks)
size += sizeof(__u32)*2*
(u->new_raid_disks - u->old_raid_disks);
- s = malloc(size);
- if (!s)
- break;
+ s = xmalloc(size);
*space_tail = s;
space_tail = s;
*space_tail = NULL;
if (u->new_raid_disks > u->old_raid_disks)
size += sizeof(__u32)*2*
(u->new_raid_disks - u->old_raid_disks);
- s = malloc(size);
- if (!s)
- break;
+ s = xmalloc(size);
*space_tail = s;
space_tail = s;
*space_tail = NULL;
/* add space for disk in update
*/
size = sizeof(struct dl);
- s = malloc(size);
- if (!s) {
- free(update->space_list);
- update->space_list = NULL;
- break;
- }
+ s = xmalloc(size);
*space_tail = s;
space_tail = s;
*space_tail = NULL;
inf = get_disk_info(u);
len = sizeof_imsm_dev(dev, 1);
/* allocate a new super->devlist entry */
- dv = malloc(sizeof(*dv));
- if (dv) {
- dv->dev = malloc(len);
- if (dv->dev)
- update->space = dv;
- else {
- free(dv);
- update->space = NULL;
- }
- }
+ dv = xmalloc(sizeof(*dv));
+ dv->dev = xmalloc(len);
+ update->space = dv;
/* count how many spares will be converted to members */
for (i = 0; i < map->num_members; i++) {
unsigned long long start;
int data_disks = imsm_num_data_members(dev, MAP_0);
- targets = malloc(new_disks * sizeof(int));
- if (!targets)
- goto abort;
+ targets = xmalloc(new_disks * sizeof(int));
for (i = 0; i < new_disks; i++)
targets[i] = -1;
- target_offsets = malloc(new_disks * sizeof(unsigned long long));
- if (!target_offsets)
- goto abort;
+ target_offsets = xcalloc(new_disks, sizeof(unsigned long long));
start = info->reshape_progress * 512;
for (i = 0; i < new_disks; i++) {
unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
if (posix_memalign((void **)&buf, 512, unit_len) != 0)
goto abort;
- targets = malloc(new_disks * sizeof(int));
- if (!targets)
- goto abort;
+ targets = xcalloc(new_disks, sizeof(int));
if (open_backup_targets(info, new_disks, targets, super, id->dev)) {
pr_err("Cannot open some devices belonging to array.\n");
/* now add space for spare disks that we need to add. */
update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
- u = calloc(1, update_memory_size);
- if (u == NULL) {
- dprintf("error: "
- "cannot get memory for imsm_update_reshape update\n");
- return 0;
- }
+ u = xcalloc(1, update_memory_size);
u->type = update_reshape_container_disks;
u->old_raid_disks = old_raid_disks;
u->new_raid_disks = geo->raid_disks;
/* size of all update data without anchor */
update_memory_size = sizeof(struct imsm_update_size_change);
- u = calloc(1, update_memory_size);
- if (u == NULL) {
- dprintf("error: cannot get memory for "
- "imsm_create_metadata_update_for_size_change\n");
- return 0;
- }
+ u = xcalloc(1, update_memory_size);
u->type = update_size_change;
u->subdev = super->current_vol;
u->new_size = geo->size;
/* size of all update data without anchor */
update_memory_size = sizeof(struct imsm_update_reshape_migration);
- u = calloc(1, update_memory_size);
- if (u == NULL) {
- dprintf("error: cannot get memory for "
- "imsm_create_metadata_update_for_migration\n");
- return 0;
- }
+ u = xcalloc(1, update_memory_size);
u->type = update_reshape_migration;
u->subdev = super->current_vol;
u->new_level = geo->level;
struct intel_super *super = st->sb;
struct imsm_update_takeover *u;
- u = malloc(sizeof(struct imsm_update_takeover));
- if (u == NULL)
- return 1;
+ u = xmalloc(sizeof(struct imsm_update_takeover));
u->type = update_takeover;
u->subarray = super->current_vol;
if (strcmp(arg, "mbr") != 0)
return NULL;
- st = malloc(sizeof(*st));
- if (!st)
- return st;
+ st = xmalloc(sizeof(*st));
st->ss = &mbr;
st->info = NULL;
st->minor_version = 0;
if (subarray)
return NULL;
- info = malloc(sizeof(*info));
+ info = xmalloc(sizeof(*info));
getinfo_super0(st, info, NULL);
return info;
}
dip = (struct devinfo **)&st->info;
while (*dip)
dip = &(*dip)->next;
- di = malloc(sizeof(struct devinfo));
+ di = xmalloc(sizeof(struct devinfo));
di->fd = fd;
di->devname = devname;
di->disk = *dinfo;
static struct supertype *match_metadata_desc0(char *arg)
{
- struct supertype *st = calloc(1, sizeof(*st));
- if (!st)
- return st;
+ struct supertype *st = xcalloc(1, sizeof(*st));
st->container_dev = NoMdDev;
st->ss = &super0;
if (subarray)
return NULL;
- info = malloc(sizeof(*info));
+ info = xmalloc(sizeof(*info));
getinfo_super1(st, info, NULL);
return info;
}
dip = (struct devinfo **)&st->info;
while (*dip)
dip = &(*dip)->next;
- di = malloc(sizeof(struct devinfo));
+ di = xmalloc(sizeof(struct devinfo));
di->fd = fd;
di->devname = devname;
di->disk = *dk;
static struct supertype *match_metadata_desc1(char *arg)
{
- struct supertype *st = calloc(1, sizeof(*st));
- if (!st)
- return st;
+ struct supertype *st = xcalloc(1, sizeof(*st));
st->container_dev = NoMdDev;
st->ss = &super1;
DIR *dir = NULL;
struct dirent *de;
- sra = malloc(sizeof(*sra));
- if (sra == NULL)
- return sra;
- memset(sra, 0, sizeof(*sra));
+ sra = xcalloc(1, sizeof(*sra));
sysfs_init(sra, fd, devnum);
if (sra->sys_name[0] == 0) {
free(sra);
dbase = base + strlen(base);
*dbase++ = '/';
- dev = malloc(sizeof(*dev));
- if (!dev)
- goto abort;
+ dev = xmalloc(sizeof(*dev));
/* Always get slot, major, minor */
strcpy(dbase, "slot");
{
/* Parse the layout string for 'faulty' */
int ln = strcspn(layout, "0123456789");
- char *m = strdup(layout);
+ char *m = xstrdup(layout);
int mode;
m[ln] = 0;
mode = map_name(faultylayout, m);
if (ioctl(fd, GET_ARRAY_INFO, &array) != 0 ||
array.raid_disks <= 0)
return 0;
- avail = calloc(array.raid_disks, 1);
+ avail = xcalloc(array.raid_disks, 1);
for (i=0; i < MAX_DISKS && array.nr_disks > 0; i++) {
disk.number = i;
if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
subarray = strchr(dev, '/');
if (subarray) {
*subarray++ = '\0';
- subarray = strdup(subarray);
+ subarray = xstrdup(subarray);
}
container = devname2devnum(dev);
if (sra)
if (!orig)
return orig;
- st = malloc(sizeof(*st));
- if (!st)
- return st;
- memset(st, 0, sizeof(*st));
+ st = xcalloc(1, sizeof(*st));
st->ss = orig->ss;
st->max_devs = orig->max_devs;
st->minor_version = orig->minor_version;
int bestsuper = -1;
int i;
- st = malloc(sizeof(*st));
- memset(st, 0, sizeof(*st));
+ st = xcalloc(1, sizeof(*st));
st->container_dev = NoMdDev;
for (i=0 ; superlist[i]; i++) {
if (sd2 == info)
break;
if (sd2 == NULL) {
- sd2 = malloc(sizeof(*sd2));
+ sd2 = xmalloc(sizeof(*sd2));
*sd2 = *info;
sd2->next = sra->devs;
sra->devs = sd2;
void append_metadata_update(struct supertype *st, void *buf, int len)
{
- struct metadata_update *mu = malloc(sizeof(*mu));
+ struct metadata_update *mu = xmalloc(sizeof(*mu));
mu->buf = buf;
mu->len = len;
}
return disks;
}
+
+void *xmalloc(size_t len)
+{
+ void *rv = malloc(len);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ exit(4+!!write(2, msg, strlen(msg)));
+}
+
+void *xrealloc(void *ptr, size_t len)
+{
+ void *rv = realloc(ptr, len);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ exit(4+!!write(2, msg, strlen(msg)));
+}
+
+void *xcalloc(size_t num, size_t size)
+{
+ void *rv = calloc(num, size);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ exit(4+!!write(2, msg, strlen(msg)));
+}
+
+char *xstrdup(const char *str)
+{
+ char *rv = strdup(str);
+ char *msg;
+ if (rv)
+ return rv;
+ msg = Name ": memory allocation failure - aborting\n";
+ exit(4+!!write(2, msg, strlen(msg)));
+}