1SPDX-License-Identifier: GPL-2.0
2
3Chinese translated version of Documentation/filesystems/sysfs.rst
4
5If you have any comment or update to the content, please contact the
6original document maintainer directly.  However, if you have a problem
7communicating in English you can also ask the Chinese maintainer for
8help.  Contact the Chinese maintainer if this translation is outdated
9or if there is a problem with the translation.
10
11Maintainer: Patrick Mochel	<mochel@osdl.org>
12		Mike Murphy <mamurph@cs.clemson.edu>
13Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
14---------------------------------------------------------------------
15Documentation/filesystems/sysfs.rst ���������������
16
17������������������������������������������������������������������������������������������������������
18������������������������������������������������������������������������������������������������������
19������������������������������������������������
20��������������������� Patrick Mochel	<mochel@osdl.org>
21		Mike Murphy <mamurph@cs.clemson.edu>
22��������������������� ������ Fu Wei <tekkamanninja@gmail.com>
23��������������������� ������ Fu Wei <tekkamanninja@gmail.com>
24��������������������� ������ Fu Wei <tekkamanninja@gmail.com>
25������������������������������������ Hu Haowen <2023002089@link.tyut.edu.cn>
26
27
28���������������
29---------------------------------------------------------------------
30sysfs - ������������������������(kobject)���������������
31
32Patrick Mochel	<mochel@osdl.org>
33Mike Murphy <mamurph@cs.clemson.edu>
34
35������:    16 August 2011
36������������:   10 January 2003
37
38
39sysfs ������:
40~~~~~~~~~~
41
42sysfs ��������������������� ramfs ������������������������������������������������������
43���������������������������������������������������������������������������������
44
45sysfs ��������� kobject ���������������������������������������
46Documentation/core-api/kobject.rst ��������������������������� kobject ���������
47���������
48
49
50������ sysfs
51~~~~~~~~~~~
52
53������������������������������ CONFIG_SYSFS ���sysfs ���������������������������������
54���������������������������:
55
56    mount -t sysfs sysfs /sys
57
58
59������������
60~~~~~~~~
61
62������ kobject ��������������������������������������������� sysfs ���������������������
63������������������ kobject ������������������������������������������������������������������
64���������������������������������������sysfs ������������������������������������������������
65������������������������������������������������������������
66
67Sysfs ������������������������ kernfs_node ���������������������������������������
68��������� kobject ��������������������������� kobject ��������� sysfs ������������
69kobject ��������������������������������������������������� sysfs ������������kobject
70������������������������ sysfs_schedule_callback() ���������������������
71
72
73������
74~~~~
75
76kobject ���������������������������������������������������������������Sysfs ���������������
77��������������� I/O ������������������������������������������������������
78
79
80������������ ASCII ���������������������������������������������������������������������������
81���������������������������������������������������������������������������������������������������
82������������������������������
83
84������������������������������������������������������������������������������������������������������
85������������,������������������������������������������������������������
86
87
88���������������������������������������:
89
90struct attribute {
91        char                    * name;
92        struct module		*owner;
93        umode_t                 mode;
94};
95
96
97int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
98void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
99
100
101���������������������������������������������������������������������������������������������������
102���������������������������������������������������������������������
103
104������:��������������������������� device_attribute ���������������:
105
106struct device_attribute {
107	struct attribute	attr;
108	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
109			char *buf);
110	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
111			 const char *buf, size_t count);
112};
113
114int device_create_file(struct device *, const struct device_attribute *);
115void device_remove_file(struct device *, const struct device_attribute *);
116
117���������������������������������������������������������:
118
119#define DEVICE_ATTR(_name, _mode, _show, _store) \
120struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
121
122������:������
123
124static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
125
126������������������������
127
128static struct device_attribute dev_attr_foo = {
129       .attr	= {
130		.name = "foo",
131		.mode = S_IWUSR | S_IRUGO,
132		.show = show_foo,
133		.store = store_foo,
134	},
135};
136
137
138������������������������������
139~~~~~~~~~~~~~~~~~~~
140
141������������������������������������������������������������������������������ sysfs ���������
142���������������������������������������������������������������������
143
144struct sysfs_ops {
145        ssize_t (*show)(struct kobject *, struct attribute *, char *);
146        ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
147};
148
149[��������������������������������� struct kobj_type ������������������������������
150��������������������������� sysfs_ops ��������������������������������� kobject ���
151������]
152
153sysfs ���������������������������������������������������������������������������������������
154������������kobject ��� attribute ������������������������������������������������
155���������������������������
156
157
158������:
159
160#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
161
162static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
163                             char *buf)
164{
165        struct device_attribute *dev_attr = to_dev_attr(attr);
166        struct device *dev = kobj_to_dev(kobj);
167        ssize_t ret = -EIO;
168
169        if (dev_attr->show)
170                ret = dev_attr->show(dev, dev_attr, buf);
171        if (ret >= (ssize_t)PAGE_SIZE) {
172                printk("dev_attr_show: %pS returned bad count\n",
173                                dev_attr->show);
174        }
175        return ret;
176}
177
178
179
180������������������
181~~~~~~~~~~~~
182
183��������������������������������� show() ��� store() ���������������������������
184������������������������������������������������������������������������������������
185
186ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
187ssize_t (*store)(struct device *dev, struct device_attribute *attr,
188                 const char *buf, size_t count);
189
190������������,������������������������������������������������������������������������������������
191
192sysfs ������������������������ (PAGE_SIZE) ���������������������������������������
193Sysfs ���������������������������������������������������������������������������������������
194������������������������:
195
196- ���������������read(2)������show() ������������������������������������������������
197  ���������������������������������������������������������������������������������������������
198  ������������������
199
200  ������������������������������������������������������������������������������������������������
201  ������������������������������0���������������������pread(2)���������show()���������
202  ������������������������������������������
203
204- ���������������write(2)������sysfs ������������������������������������������������������
205  ������ Sysfs ������������������������ store() ���������
206
207  ��������� sysfs ������������������������������������������������������������������������
208  ���������������������������������������������
209
210  ���������������������������������������������������������������������������
211
212������:
213
214- ������������������ show() ���������������������������������������������
215
216- ������������������ PAGE_SIZE ���������������i386���������������4096���
217
218- show() ��������������������������������������������������������� scnprintf()���
219  ������������
220
221- show() ������������������������������������������������������������������������snprintf()���
222  ������������������������������������������������������������sprintf()���������������������
223  scnprintf()���
224
225- store() ������������������������������������������������������������������������������������
226  count ���������
227
228- show() ��� store() ���������������������������������������������������������������������
229  ������������
230
231- ������������������������������������������ sysfs ������������������������������������������
232  ������������������������������������������������������(���������)������������������������������������
233  ���������������������������������
234
235���������������(���������������������)���������������������������
236
237static ssize_t show_name(struct device *dev, struct device_attribute *attr,
238                         char *buf)
239{
240	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
241}
242
243static ssize_t store_name(struct device *dev, struct device_attribute *attr,
244                          const char *buf, size_t count)
245{
246        snprintf(dev->name, sizeof(dev->name), "%.*s",
247                 (int)min(count, sizeof(dev->name) - 1), buf);
248	return count;
249}
250
251static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
252
253
254���������������������������������������������������������������������
255
256������������������
257~~~~~~~~~~~~
258
259sysfs ������������������������������������������������������������
260
261������ sysfs ������������:
262
263block/
264bus/
265class/
266dev/
267devices/
268firmware/
269net/
270fs/
271
272devices/ ���������������������������������������������������������������������������������
273���������������������������������������������
274
275bus/ ������������������������������������������������������������������������������������������
276���������:
277
278	devices/
279	drivers/
280
281devices/ ��������������������������������������������������������������������� root/ ������
282���������������
283
284drivers/ ������������������������������������������������������������������������������(������
285������������������������������������������)���
286
287fs/ ���������������������������������������������������������������������������������������������������
288��� fs/ ������������������������������(������Documentation/filesystems/fuse.rst)���
289
290dev/ ������������������������ char/ ��� block/������������������������������������
291<major>:<minor> ������������������������������������������������������ sysfs ������
292���������������������/sys/dev ������������������������ stat(2) ���������������������
293������ sysfs ������������������������
294
295������������ driver-model ������������������������ Documentation/driver-api/driver-model/
296������������
297
298
299TODO: ������������������
300
301
302������������
303~~~~~~~~
304
305������������������������������������������sysfs���:
306
307- ������ (include/linux/device.h)
308----------------------------------
309���������:
310
311struct device_attribute {
312	struct attribute	attr;
313	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
314			char *buf);
315	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
316			 const char *buf, size_t count);
317};
318
319������:
320
321DEVICE_ATTR(_name, _mode, _show, _store);
322
323���/���������:
324
325int device_create_file(struct device *dev, const struct device_attribute * attr);
326void device_remove_file(struct device *dev, const struct device_attribute * attr);
327
328
329- ������������������ (include/linux/device.h)
330--------------------------------------
331���������:
332
333struct bus_attribute {
334        struct attribute        attr;
335        ssize_t (*show)(const struct bus_type *, char * buf);
336        ssize_t (*store)(const struct bus_type *, const char * buf, size_t count);
337};
338
339������:
340
341BUS_ATTR(_name, _mode, _show, _store)
342
343���/���������:
344
345int bus_create_file(struct bus_type *, struct bus_attribute *);
346void bus_remove_file(struct bus_type *, struct bus_attribute *);
347
348
349- ������������������ (include/linux/device.h)
350-----------------------------------------
351
352���������:
353
354struct driver_attribute {
355        struct attribute        attr;
356        ssize_t (*show)(struct device_driver *, char * buf);
357        ssize_t (*store)(struct device_driver *, const char * buf,
358                         size_t count);
359};
360
361������:
362
363DRIVER_ATTR(_name, _mode, _show, _store)
364
365���/������������
366
367int driver_create_file(struct device_driver *, const struct driver_attribute *);
368void driver_remove_file(struct device_driver *, const struct driver_attribute *);
369
370
371������
372~~~~
373
374sysfs ������������������������������������������������������������������������������������ ABI���
375������������ ABI������������������������������������������������������������������������ sysfs
376��������������� Documentation/ABI ��������������������� Documentation/ABI/README���
377
378