Blame view

kernel/ksysfs.c 5.15 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
   * 		     are not related to any other subsystem
   *
   * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
   * 
   * This file is release under the GPLv2
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
  #include <linux/kobject.h>
  #include <linux/string.h>
  #include <linux/sysfs.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
13
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/init.h>
c330dda90   Jeff Moyer   [PATCH] Add a sys...
15
  #include <linux/kexec.h>
22b8ce947   Dave Hansen   profiling: dynami...
16
  #include <linux/profile.h>
1596425fd   Paul Gortmaker   kernel: ksysfs.c ...
17
  #include <linux/stat.h>
5cb350baf   Dhaval Giani   sched: group sche...
18
  #include <linux/sched.h>
088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
19
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  
  #define KERNEL_ATTR_RO(_name) \
386f275f5   Kay Sievers   Driver Core: swit...
22
  static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  
  #define KERNEL_ATTR_RW(_name) \
386f275f5   Kay Sievers   Driver Core: swit...
25
  static struct kobj_attribute _name##_attr = \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  	__ATTR(_name, 0644, _name##_show, _name##_store)
cd3772e68   Ming Lei   kernel/ksysfs.c:f...
27
  #if defined(CONFIG_HOTPLUG)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
28
  /* current uevent sequence number */
386f275f5   Kay Sievers   Driver Core: swit...
29
30
  static ssize_t uevent_seqnum_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  {
386f275f5   Kay Sievers   Driver Core: swit...
32
33
  	return sprintf(buf, "%llu
  ", (unsigned long long)uevent_seqnum);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  }
0f76e5acf   Kay Sievers   [PATCH] add ueven...
35
  KERNEL_ATTR_RO(uevent_seqnum);
af6658527   Thadeu Lima de Souza Cascardo   fix comment typo ...
36
  /* uevent helper program, used during early boot */
386f275f5   Kay Sievers   Driver Core: swit...
37
38
  static ssize_t uevent_helper_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
39
  {
386f275f5   Kay Sievers   Driver Core: swit...
40
41
  	return sprintf(buf, "%s
  ", uevent_helper);
0f76e5acf   Kay Sievers   [PATCH] add ueven...
42
  }
386f275f5   Kay Sievers   Driver Core: swit...
43
44
45
  static ssize_t uevent_helper_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
46
  {
312c004d3   Kay Sievers   [PATCH] driver co...
47
  	if (count+1 > UEVENT_HELPER_PATH_LEN)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
48
  		return -ENOENT;
386f275f5   Kay Sievers   Driver Core: swit...
49
  	memcpy(uevent_helper, buf, count);
312c004d3   Kay Sievers   [PATCH] driver co...
50
51
52
53
  	uevent_helper[count] = '\0';
  	if (count && uevent_helper[count-1] == '
  ')
  		uevent_helper[count-1] = '\0';
0f76e5acf   Kay Sievers   [PATCH] add ueven...
54
55
56
  	return count;
  }
  KERNEL_ATTR_RW(uevent_helper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  #endif
22b8ce947   Dave Hansen   profiling: dynami...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  #ifdef CONFIG_PROFILING
  static ssize_t profiling_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%d
  ", prof_on);
  }
  static ssize_t profiling_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
  {
  	int ret;
  
  	if (prof_on)
  		return -EEXIST;
  	/*
  	 * This eventually calls into get_option() which
  	 * has a ton of callers and is not const.  It is
  	 * easiest to cast it away here.
  	 */
  	profile_setup((char *)buf);
  	ret = profile_init();
  	if (ret)
  		return ret;
  	ret = create_proc_profile();
  	if (ret)
  		return ret;
  	return count;
  }
  KERNEL_ATTR_RW(profiling);
  #endif
c330dda90   Jeff Moyer   [PATCH] Add a sys...
89
  #ifdef CONFIG_KEXEC
386f275f5   Kay Sievers   Driver Core: swit...
90
91
  static ssize_t kexec_loaded_show(struct kobject *kobj,
  				 struct kobj_attribute *attr, char *buf)
c330dda90   Jeff Moyer   [PATCH] Add a sys...
92
  {
386f275f5   Kay Sievers   Driver Core: swit...
93
94
  	return sprintf(buf, "%d
  ", !!kexec_image);
c330dda90   Jeff Moyer   [PATCH] Add a sys...
95
96
  }
  KERNEL_ATTR_RO(kexec_loaded);
386f275f5   Kay Sievers   Driver Core: swit...
97
98
  static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  				       struct kobj_attribute *attr, char *buf)
c330dda90   Jeff Moyer   [PATCH] Add a sys...
99
  {
386f275f5   Kay Sievers   Driver Core: swit...
100
101
  	return sprintf(buf, "%d
  ", !!kexec_crash_image);
c330dda90   Jeff Moyer   [PATCH] Add a sys...
102
103
  }
  KERNEL_ATTR_RO(kexec_crash_loaded);
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
104

06a7f7112   Amerigo Wang   kexec: premit red...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  static ssize_t kexec_crash_size_show(struct kobject *kobj,
  				       struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%zu
  ", crash_get_memory_size());
  }
  static ssize_t kexec_crash_size_store(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   const char *buf, size_t count)
  {
  	unsigned long cnt;
  	int ret;
  
  	if (strict_strtoul(buf, 0, &cnt))
  		return -EINVAL;
  
  	ret = crash_shrink_memory(cnt);
  	return ret < 0 ? ret : count;
  }
  KERNEL_ATTR_RW(kexec_crash_size);
386f275f5   Kay Sievers   Driver Core: swit...
125
126
  static ssize_t vmcoreinfo_show(struct kobject *kobj,
  			       struct kobj_attribute *attr, char *buf)
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
127
  {
386f275f5   Kay Sievers   Driver Core: swit...
128
129
  	return sprintf(buf, "%lx %x
  ",
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
130
  		       paddr_vmcoreinfo_note(),
d768281e9   Ken'ichi Ohmichi   add-vmcore: clean...
131
  		       (unsigned int)vmcoreinfo_max_size);
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
132
133
  }
  KERNEL_ATTR_RO(vmcoreinfo);
c330dda90   Jeff Moyer   [PATCH] Add a sys...
134
  #endif /* CONFIG_KEXEC */
088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
135
136
137
138
139
140
141
142
  /* whether file capabilities are enabled */
  static ssize_t fscaps_show(struct kobject *kobj,
  				  struct kobj_attribute *attr, char *buf)
  {
  	return sprintf(buf, "%d
  ", file_caps_enabled);
  }
  KERNEL_ATTR_RO(fscaps);
da1a679cd   Roland McGrath   Add /sys/kernel/n...
143
144
145
  /*
   * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
   */
0b1937ac0   David Howells   FRV: Fix linkage ...
146
147
  extern const void __start_notes __attribute__((weak));
  extern const void __stop_notes __attribute__((weak));
da1a679cd   Roland McGrath   Add /sys/kernel/n...
148
  #define	notes_size (&__stop_notes - &__start_notes)
2c3c8bea6   Chris Wright   sysfs: add struct...
149
150
  static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  			  struct bin_attribute *bin_attr,
da1a679cd   Roland McGrath   Add /sys/kernel/n...
151
152
153
154
155
156
157
158
159
160
161
162
163
  			  char *buf, loff_t off, size_t count)
  {
  	memcpy(buf, &__start_notes + off, count);
  	return count;
  }
  
  static struct bin_attribute notes_attr = {
  	.attr = {
  		.name = "notes",
  		.mode = S_IRUGO,
  	},
  	.read = &notes_read,
  };
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
164
165
  struct kobject *kernel_kobj;
  EXPORT_SYMBOL_GPL(kernel_kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
  
  static struct attribute * kernel_attrs[] = {
088ab0b4d   Ludwig Nussel   kernel/ksysfs.c: ...
168
  	&fscaps_attr.attr,
cd3772e68   Ming Lei   kernel/ksysfs.c:f...
169
  #if defined(CONFIG_HOTPLUG)
0f76e5acf   Kay Sievers   [PATCH] add ueven...
170
171
  	&uevent_seqnum_attr.attr,
  	&uevent_helper_attr.attr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
  #endif
22b8ce947   Dave Hansen   profiling: dynami...
173
174
175
  #ifdef CONFIG_PROFILING
  	&profiling_attr.attr,
  #endif
c330dda90   Jeff Moyer   [PATCH] Add a sys...
176
177
178
  #ifdef CONFIG_KEXEC
  	&kexec_loaded_attr.attr,
  	&kexec_crash_loaded_attr.attr,
06a7f7112   Amerigo Wang   kexec: premit red...
179
  	&kexec_crash_size_attr.attr,
fd59d231f   Ken'ichi Ohmichi   Add vmcoreinfo
180
  	&vmcoreinfo_attr.attr,
c330dda90   Jeff Moyer   [PATCH] Add a sys...
181
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
  	NULL
  };
  
  static struct attribute_group kernel_attr_group = {
  	.attrs = kernel_attrs,
  };
  
  static int __init ksysfs_init(void)
  {
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
191
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192

0ff21e466   Greg Kroah-Hartman   kobject: convert ...
193
194
  	kernel_kobj = kobject_create_and_add("kernel", NULL);
  	if (!kernel_kobj) {
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
195
196
197
  		error = -ENOMEM;
  		goto exit;
  	}
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
198
  	error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
199
200
201
202
  	if (error)
  		goto kset_exit;
  
  	if (notes_size > 0) {
da1a679cd   Roland McGrath   Add /sys/kernel/n...
203
  		notes_attr.size = notes_size;
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
204
  		error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
205
206
  		if (error)
  			goto group_exit;
da1a679cd   Roland McGrath   Add /sys/kernel/n...
207
  	}
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
208
  	return 0;
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
209
  group_exit:
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
210
  	sysfs_remove_group(kernel_kobj, &kernel_attr_group);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
211
  kset_exit:
78a2d906b   Greg Kroah-Hartman   Kobject: convert ...
212
  	kobject_put(kernel_kobj);
bd35b93d8   Greg Kroah-Hartman   kset: convert ker...
213
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
217
  	return error;
  }
  
  core_initcall(ksysfs_init);