[PATCH] Make attributes names const char *

sysfs: make attributes and attribute_group's names const char *

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
Dmitry Torokhov 2005-04-29 00:58:46 -05:00 committed by Greg Kroah-Hartman
parent 8d790d7408
commit d48593bf20
3 changed files with 58 additions and 79 deletions

View File

@ -40,9 +40,7 @@ struct ib_port {
struct kobject kobj; struct kobject kobj;
struct ib_device *ibdev; struct ib_device *ibdev;
struct attribute_group gid_group; struct attribute_group gid_group;
struct attribute **gid_attr;
struct attribute_group pkey_group; struct attribute_group pkey_group;
struct attribute **pkey_attr;
u8 port_num; u8 port_num;
}; };
@ -60,8 +58,9 @@ struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
struct port_attribute port_attr_##_name = __ATTR_RO(_name) struct port_attribute port_attr_##_name = __ATTR_RO(_name)
struct port_table_attribute { struct port_table_attribute {
struct port_attribute attr; struct port_attribute attr;
int index; char name[8];
int index;
}; };
static ssize_t port_attr_show(struct kobject *kobj, static ssize_t port_attr_show(struct kobject *kobj,
@ -398,17 +397,16 @@ static void ib_port_release(struct kobject *kobj)
struct attribute *a; struct attribute *a;
int i; int i;
for (i = 0; (a = p->gid_attr[i]); ++i) { for (i = 0; (a = p->gid_group.attrs[i]); ++i)
kfree(a->name);
kfree(a); kfree(a);
}
for (i = 0; (a = p->pkey_attr[i]); ++i) { kfree(p->gid_group.attrs);
kfree(a->name);
for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
kfree(a); kfree(a);
}
kfree(p->gid_attr); kfree(p->pkey_group.attrs);
kfree(p); kfree(p);
} }
@ -449,58 +447,45 @@ static int ib_device_hotplug(struct class_device *cdev, char **envp,
return 0; return 0;
} }
static int alloc_group(struct attribute ***attr, static struct attribute **
ssize_t (*show)(struct ib_port *, alloc_group_attrs(ssize_t (*show)(struct ib_port *,
struct port_attribute *, char *buf), struct port_attribute *, char *buf),
int len) int len)
{ {
struct port_table_attribute ***tab_attr = struct attribute **tab_attr;
(struct port_table_attribute ***) attr; struct port_table_attribute *element;
int i; int i;
int ret;
*tab_attr = kmalloc((1 + len) * sizeof *tab_attr, GFP_KERNEL); tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
if (!*tab_attr) if (!tab_attr)
return -ENOMEM; return NULL;
memset(*tab_attr, 0, (1 + len) * sizeof *tab_attr); for (i = 0; i < len; i++) {
element = kcalloc(1, sizeof(struct port_table_attribute),
for (i = 0; i < len; ++i) { GFP_KERNEL);
(*tab_attr)[i] = kmalloc(sizeof *(*tab_attr)[i], GFP_KERNEL); if (!element)
if (!(*tab_attr)[i]) {
ret = -ENOMEM;
goto err; goto err;
}
memset((*tab_attr)[i], 0, sizeof *(*tab_attr)[i]);
(*tab_attr)[i]->attr.attr.name = kmalloc(8, GFP_KERNEL);
if (!(*tab_attr)[i]->attr.attr.name) {
ret = -ENOMEM;
goto err;
}
if (snprintf((*tab_attr)[i]->attr.attr.name, 8, "%d", i) >= 8) { if (snprintf(element->name, sizeof(element->name),
ret = -ENOMEM; "%d", i) >= sizeof(element->name))
goto err; goto err;
}
(*tab_attr)[i]->attr.attr.mode = S_IRUGO; element->attr.attr.name = element->name;
(*tab_attr)[i]->attr.attr.owner = THIS_MODULE; element->attr.attr.mode = S_IRUGO;
(*tab_attr)[i]->attr.show = show; element->attr.attr.owner = THIS_MODULE;
(*tab_attr)[i]->index = i; element->attr.show = show;
element->index = i;
tab_attr[i] = &element->attr.attr;
} }
return 0; return tab_attr;
err: err:
for (i = 0; i < len; ++i) { while (--i >= 0)
if ((*tab_attr)[i]) kfree(tab_attr[i]);
kfree((*tab_attr)[i]->attr.attr.name); kfree(tab_attr);
kfree((*tab_attr)[i]); return NULL;
}
kfree(*tab_attr);
return ret;
} }
static int add_port(struct ib_device *device, int port_num) static int add_port(struct ib_device *device, int port_num)
@ -541,23 +526,20 @@ static int add_port(struct ib_device *device, int port_num)
if (ret) if (ret)
goto err_put; goto err_put;
ret = alloc_group(&p->gid_attr, show_port_gid, attr.gid_tbl_len);
if (ret)
goto err_remove_pma;
p->gid_group.name = "gids"; p->gid_group.name = "gids";
p->gid_group.attrs = p->gid_attr; p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
if (!p->gid_group.attrs)
goto err_remove_pma;
ret = sysfs_create_group(&p->kobj, &p->gid_group); ret = sysfs_create_group(&p->kobj, &p->gid_group);
if (ret) if (ret)
goto err_free_gid; goto err_free_gid;
ret = alloc_group(&p->pkey_attr, show_port_pkey, attr.pkey_tbl_len);
if (ret)
goto err_remove_gid;
p->pkey_group.name = "pkeys"; p->pkey_group.name = "pkeys";
p->pkey_group.attrs = p->pkey_attr; p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
attr.pkey_tbl_len);
if (!p->pkey_group.attrs)
goto err_remove_gid;
ret = sysfs_create_group(&p->kobj, &p->pkey_group); ret = sysfs_create_group(&p->kobj, &p->pkey_group);
if (ret) if (ret)
@ -568,23 +550,19 @@ static int add_port(struct ib_device *device, int port_num)
return 0; return 0;
err_free_pkey: err_free_pkey:
for (i = 0; i < attr.pkey_tbl_len; ++i) { for (i = 0; i < attr.pkey_tbl_len; ++i)
kfree(p->pkey_attr[i]->name); kfree(p->pkey_group.attrs[i]);
kfree(p->pkey_attr[i]);
}
kfree(p->pkey_attr); kfree(p->pkey_group.attrs);
err_remove_gid: err_remove_gid:
sysfs_remove_group(&p->kobj, &p->gid_group); sysfs_remove_group(&p->kobj, &p->gid_group);
err_free_gid: err_free_gid:
for (i = 0; i < attr.gid_tbl_len; ++i) { for (i = 0; i < attr.gid_tbl_len; ++i)
kfree(p->gid_attr[i]->name); kfree(p->gid_group.attrs[i]);
kfree(p->gid_attr[i]);
}
kfree(p->gid_attr); kfree(p->gid_group.attrs);
err_remove_pma: err_remove_pma:
sysfs_remove_group(&p->kobj, &pma_group); sysfs_remove_group(&p->kobj, &pma_group);

View File

@ -339,16 +339,17 @@ pci_create_resource_files(struct pci_dev *pdev)
if (!pci_resource_len(pdev, i)) if (!pci_resource_len(pdev, i))
continue; continue;
res_attr = kmalloc(sizeof(*res_attr) + 10, GFP_ATOMIC); /* allocate attribute structure, piggyback attribute name */
res_attr = kcalloc(1, sizeof(*res_attr) + 10, GFP_ATOMIC);
if (res_attr) { if (res_attr) {
memset(res_attr, 0, sizeof(*res_attr) + 10); char *res_attr_name = (char *)(res_attr + 1);
pdev->res_attr[i] = res_attr; pdev->res_attr[i] = res_attr;
/* Allocated above after the res_attr struct */ sprintf(res_attr_name, "resource%d", i);
res_attr->attr.name = (char *)(res_attr + 1); res_attr->attr.name = res_attr_name;
sprintf(res_attr->attr.name, "resource%d", i);
res_attr->size = pci_resource_len(pdev, i);
res_attr->attr.mode = S_IRUSR | S_IWUSR; res_attr->attr.mode = S_IRUSR | S_IWUSR;
res_attr->attr.owner = THIS_MODULE; res_attr->attr.owner = THIS_MODULE;
res_attr->size = pci_resource_len(pdev, i);
res_attr->mmap = pci_mmap_resource; res_attr->mmap = pci_mmap_resource;
res_attr->private = &pdev->resource[i]; res_attr->private = &pdev->resource[i];
sysfs_create_bin_file(&pdev->dev.kobj, res_attr); sysfs_create_bin_file(&pdev->dev.kobj, res_attr);

View File

@ -16,13 +16,13 @@ struct kobject;
struct module; struct module;
struct attribute { struct attribute {
char * name; const char * name;
struct module * owner; struct module * owner;
mode_t mode; mode_t mode;
}; };
struct attribute_group { struct attribute_group {
char * name; const char * name;
struct attribute ** attrs; struct attribute ** attrs;
}; };