group.c 1.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 *
 * This file is released undert the GPL v2. 
 *
 */

#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/dcache.h>
14
#include <linux/namei.h>
L
Linus Torvalds 已提交
15 16 17 18
#include <linux/err.h>
#include "sysfs.h"


19 20
static void remove_files(struct sysfs_dirent *dir_sd,
			 const struct attribute_group *grp)
L
Linus Torvalds 已提交
21 22 23 24
{
	struct attribute *const* attr;

	for (attr = grp->attrs; *attr; attr++)
25
		sysfs_hash_and_remove(dir_sd, (*attr)->name);
L
Linus Torvalds 已提交
26 27
}

28 29
static int create_files(struct sysfs_dirent *dir_sd,
			const struct attribute_group *grp)
L
Linus Torvalds 已提交
30 31 32 33
{
	struct attribute *const* attr;
	int error = 0;

34 35
	for (attr = grp->attrs; *attr && !error; attr++)
		error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
L
Linus Torvalds 已提交
36
	if (error)
37
		remove_files(dir_sd, grp);
L
Linus Torvalds 已提交
38 39 40 41 42 43 44
	return error;
}


int sysfs_create_group(struct kobject * kobj, 
		       const struct attribute_group * grp)
{
45
	struct sysfs_dirent *sd;
L
Linus Torvalds 已提交
46 47
	int error;

48
	BUG_ON(!kobj || !kobj->sd);
L
Linus Torvalds 已提交
49 50

	if (grp->name) {
51
		error = sysfs_create_subdir(kobj, grp->name, &sd);
L
Linus Torvalds 已提交
52 53 54
		if (error)
			return error;
	} else
55 56 57 58
		sd = kobj->sd;
	sysfs_get(sd);
	error = create_files(sd, grp);
	if (error) {
L
Linus Torvalds 已提交
59
		if (grp->name)
60
			sysfs_remove_subdir(sd);
L
Linus Torvalds 已提交
61
	}
62
	sysfs_put(sd);
L
Linus Torvalds 已提交
63 64 65 66 67 68
	return error;
}

void sysfs_remove_group(struct kobject * kobj, 
			const struct attribute_group * grp)
{
69 70
	struct sysfs_dirent *dir_sd = kobj->sd;
	struct sysfs_dirent *sd;
L
Linus Torvalds 已提交
71

72
	if (grp->name) {
73 74 75 76
		sd = sysfs_get_dirent(dir_sd, grp->name);
		BUG_ON(!sd);
	} else
		sd = sysfs_get(dir_sd);
L
Linus Torvalds 已提交
77

78
	remove_files(sd, grp);
L
Linus Torvalds 已提交
79
	if (grp->name)
80 81 82
		sysfs_remove_subdir(sd);

	sysfs_put(sd);
L
Linus Torvalds 已提交
83 84 85 86 87
}


EXPORT_SYMBOL_GPL(sysfs_create_group);
EXPORT_SYMBOL_GPL(sysfs_remove_group);