group.c 1.8 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
#include <linux/err.h>
16
#include <linux/fs.h>
L
Linus Torvalds 已提交
17 18 19
#include "sysfs.h"


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

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

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

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


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

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

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

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

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

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

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


EXPORT_SYMBOL_GPL(sysfs_create_group);
EXPORT_SYMBOL_GPL(sysfs_remove_group);