]>
Commit | Line | Data |
---|---|---|
619daeee | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
6d66f5cd TH |
3 | * fs/sysfs/file.c - sysfs regular (text) file implementation |
4 | * | |
5 | * Copyright (c) 2001-3 Patrick Mochel | |
6 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
7 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> | |
8 | * | |
0c1bc6b8 | 9 | * Please see Documentation/filesystems/sysfs.rst for more information. |
1da177e4 LT |
10 | */ |
11 | ||
12 | #include <linux/module.h> | |
1da177e4 | 13 | #include <linux/kobject.h> |
c6f87733 | 14 | #include <linux/slab.h> |
94bebf4d | 15 | #include <linux/list.h> |
52e8c209 | 16 | #include <linux/mutex.h> |
13c589d5 | 17 | #include <linux/seq_file.h> |
2efc459d | 18 | #include <linux/mm.h> |
1da177e4 LT |
19 | |
20 | #include "sysfs.h" | |
f6acf8bb | 21 | |
375b611e | 22 | /* |
324a56e1 | 23 | * Determine ktype->sysfs_ops for the given kernfs_node. This function |
375b611e TH |
24 | * must be called while holding an active reference. |
25 | */ | |
324a56e1 | 26 | static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn) |
375b611e | 27 | { |
adc5e8b5 | 28 | struct kobject *kobj = kn->parent->priv; |
375b611e | 29 | |
df23fc39 | 30 | if (kn->flags & KERNFS_LOCKDEP) |
324a56e1 | 31 | lockdep_assert_held(kn); |
375b611e TH |
32 | return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; |
33 | } | |
34 | ||
13c589d5 TH |
35 | /* |
36 | * Reads on sysfs are handled through seq_file, which takes care of hairy | |
37 | * details like buffering and seeking. The following function pipes | |
38 | * sysfs_ops->show() result through seq_file. | |
1da177e4 | 39 | */ |
c2b19daf | 40 | static int sysfs_kf_seq_show(struct seq_file *sf, void *v) |
1da177e4 | 41 | { |
c525aadd | 42 | struct kernfs_open_file *of = sf->private; |
adc5e8b5 | 43 | struct kobject *kobj = of->kn->parent->priv; |
324a56e1 | 44 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); |
1da177e4 | 45 | ssize_t count; |
c2b19daf | 46 | char *buf; |
1da177e4 | 47 | |
f5c16f29 | 48 | /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */ |
13c589d5 TH |
49 | count = seq_get_buf(sf, &buf); |
50 | if (count < PAGE_SIZE) { | |
51 | seq_commit(sf, -1); | |
52 | return 0; | |
53 | } | |
f5c16f29 | 54 | memset(buf, 0, PAGE_SIZE); |
1da177e4 | 55 | |
13c589d5 | 56 | /* |
c2b19daf TH |
57 | * Invoke show(). Control may reach here via seq file lseek even |
58 | * if @ops->show() isn't implemented. | |
13c589d5 | 59 | */ |
c2b19daf | 60 | if (ops->show) { |
324a56e1 | 61 | count = ops->show(kobj, of->kn->priv, buf); |
c2b19daf TH |
62 | if (count < 0) |
63 | return count; | |
64 | } | |
0ab66088 | 65 | |
8118a859 MX |
66 | /* |
67 | * The code works fine with PAGE_SIZE return but it's likely to | |
68 | * indicate truncated result or overflow in normal use cases. | |
69 | */ | |
815d2d50 | 70 | if (count >= (ssize_t)PAGE_SIZE) { |
9e6d35ff SS |
71 | printk("fill_read_buffer: %pS returned bad count\n", |
72 | ops->show); | |
815d2d50 AM |
73 | /* Try to struggle along */ |
74 | count = PAGE_SIZE - 1; | |
75 | } | |
13c589d5 TH |
76 | seq_commit(sf, count); |
77 | return 0; | |
1da177e4 LT |
78 | } |
79 | ||
c525aadd | 80 | static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, |
c2b19daf | 81 | size_t count, loff_t pos) |
2f0c6b75 | 82 | { |
324a56e1 | 83 | struct bin_attribute *battr = of->kn->priv; |
adc5e8b5 | 84 | struct kobject *kobj = of->kn->parent->priv; |
c2b19daf | 85 | loff_t size = file_inode(of->file)->i_size; |
2f0c6b75 | 86 | |
c2b19daf | 87 | if (!count) |
2f0c6b75 TH |
88 | return 0; |
89 | ||
90 | if (size) { | |
eaa5cd92 | 91 | if (pos >= size) |
2f0c6b75 | 92 | return 0; |
c2b19daf TH |
93 | if (pos + count > size) |
94 | count = size - pos; | |
2f0c6b75 TH |
95 | } |
96 | ||
c2b19daf TH |
97 | if (!battr->read) |
98 | return -EIO; | |
99 | ||
100 | return battr->read(of->file, kobj, battr, buf, pos, count); | |
101 | } | |
102 | ||
4ef67a8c N |
103 | /* kernfs read callback for regular sysfs files with pre-alloc */ |
104 | static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, | |
105 | size_t count, loff_t pos) | |
106 | { | |
107 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); | |
108 | struct kobject *kobj = of->kn->parent->priv; | |
c8a139d0 | 109 | ssize_t len; |
4ef67a8c N |
110 | |
111 | /* | |
112 | * If buf != of->prealloc_buf, we don't know how | |
113 | * large it is, so cannot safely pass it to ->show | |
114 | */ | |
17d0774f | 115 | if (WARN_ON_ONCE(buf != of->prealloc_buf)) |
4ef67a8c | 116 | return 0; |
65da3484 | 117 | len = ops->show(kobj, of->kn->priv, buf); |
c8a139d0 N |
118 | if (len < 0) |
119 | return len; | |
17d0774f KK |
120 | if (pos) { |
121 | if (len <= pos) | |
122 | return 0; | |
123 | len -= pos; | |
124 | memmove(buf, buf + pos, len); | |
125 | } | |
c8a139d0 | 126 | return min_t(ssize_t, count, len); |
4ef67a8c N |
127 | } |
128 | ||
50b38ca0 | 129 | /* kernfs write callback for regular sysfs files */ |
c525aadd | 130 | static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf, |
50b38ca0 | 131 | size_t count, loff_t pos) |
1da177e4 | 132 | { |
324a56e1 | 133 | const struct sysfs_ops *ops = sysfs_file_ops(of->kn); |
adc5e8b5 | 134 | struct kobject *kobj = of->kn->parent->priv; |
0ab66088 | 135 | |
50b38ca0 TH |
136 | if (!count) |
137 | return 0; | |
0ab66088 | 138 | |
324a56e1 | 139 | return ops->store(kobj, of->kn->priv, buf, count); |
50b38ca0 | 140 | } |
f9b9a621 | 141 | |
50b38ca0 | 142 | /* kernfs write callback for bin sysfs files */ |
c525aadd | 143 | static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf, |
50b38ca0 TH |
144 | size_t count, loff_t pos) |
145 | { | |
324a56e1 | 146 | struct bin_attribute *battr = of->kn->priv; |
adc5e8b5 | 147 | struct kobject *kobj = of->kn->parent->priv; |
50b38ca0 | 148 | loff_t size = file_inode(of->file)->i_size; |
f9b9a621 | 149 | |
50b38ca0 TH |
150 | if (size) { |
151 | if (size <= pos) | |
09368960 | 152 | return -EFBIG; |
50b38ca0 | 153 | count = min_t(ssize_t, count, size - pos); |
f9b9a621 | 154 | } |
50b38ca0 TH |
155 | if (!count) |
156 | return 0; | |
0ab66088 | 157 | |
50b38ca0 TH |
158 | if (!battr->write) |
159 | return -EIO; | |
1da177e4 | 160 | |
50b38ca0 | 161 | return battr->write(of->file, kobj, battr, buf, pos, count); |
1da177e4 LT |
162 | } |
163 | ||
c525aadd | 164 | static int sysfs_kf_bin_mmap(struct kernfs_open_file *of, |
fdbffaa4 TH |
165 | struct vm_area_struct *vma) |
166 | { | |
324a56e1 | 167 | struct bin_attribute *battr = of->kn->priv; |
adc5e8b5 | 168 | struct kobject *kobj = of->kn->parent->priv; |
fdbffaa4 | 169 | |
fdbffaa4 TH |
170 | return battr->mmap(of->file, kobj, battr, vma); |
171 | } | |
172 | ||
74b30195 DV |
173 | static int sysfs_kf_bin_open(struct kernfs_open_file *of) |
174 | { | |
175 | struct bin_attribute *battr = of->kn->priv; | |
176 | ||
f06aff92 KW |
177 | if (battr->f_mapping) |
178 | of->file->f_mapping = battr->f_mapping(); | |
74b30195 DV |
179 | |
180 | return 0; | |
181 | } | |
182 | ||
324a56e1 | 183 | void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr) |
4508a7a7 | 184 | { |
324a56e1 | 185 | struct kernfs_node *kn = kobj->sd, *tmp; |
51225039 | 186 | |
324a56e1 TH |
187 | if (kn && dir) |
188 | kn = kernfs_find_and_get(kn, dir); | |
024f6471 | 189 | else |
324a56e1 | 190 | kernfs_get(kn); |
024f6471 | 191 | |
324a56e1 TH |
192 | if (kn && attr) { |
193 | tmp = kernfs_find_and_get(kn, attr); | |
194 | kernfs_put(kn); | |
195 | kn = tmp; | |
024f6471 | 196 | } |
51225039 | 197 | |
324a56e1 TH |
198 | if (kn) { |
199 | kernfs_notify(kn); | |
200 | kernfs_put(kn); | |
024f6471 | 201 | } |
4508a7a7 N |
202 | } |
203 | EXPORT_SYMBOL_GPL(sysfs_notify); | |
204 | ||
f6acf8bb TH |
205 | static const struct kernfs_ops sysfs_file_kfops_empty = { |
206 | }; | |
207 | ||
208 | static const struct kernfs_ops sysfs_file_kfops_ro = { | |
209 | .seq_show = sysfs_kf_seq_show, | |
210 | }; | |
211 | ||
212 | static const struct kernfs_ops sysfs_file_kfops_wo = { | |
213 | .write = sysfs_kf_write, | |
214 | }; | |
215 | ||
216 | static const struct kernfs_ops sysfs_file_kfops_rw = { | |
217 | .seq_show = sysfs_kf_seq_show, | |
218 | .write = sysfs_kf_write, | |
219 | }; | |
220 | ||
4ef67a8c N |
221 | static const struct kernfs_ops sysfs_prealloc_kfops_ro = { |
222 | .read = sysfs_kf_read, | |
223 | .prealloc = true, | |
224 | }; | |
225 | ||
2b75869b N |
226 | static const struct kernfs_ops sysfs_prealloc_kfops_wo = { |
227 | .write = sysfs_kf_write, | |
228 | .prealloc = true, | |
229 | }; | |
230 | ||
231 | static const struct kernfs_ops sysfs_prealloc_kfops_rw = { | |
4ef67a8c | 232 | .read = sysfs_kf_read, |
2b75869b N |
233 | .write = sysfs_kf_write, |
234 | .prealloc = true, | |
235 | }; | |
236 | ||
f6acf8bb TH |
237 | static const struct kernfs_ops sysfs_bin_kfops_ro = { |
238 | .read = sysfs_kf_bin_read, | |
239 | }; | |
240 | ||
241 | static const struct kernfs_ops sysfs_bin_kfops_wo = { | |
242 | .write = sysfs_kf_bin_write, | |
243 | }; | |
244 | ||
245 | static const struct kernfs_ops sysfs_bin_kfops_rw = { | |
246 | .read = sysfs_kf_bin_read, | |
247 | .write = sysfs_kf_bin_write, | |
9b2db6e1 TH |
248 | }; |
249 | ||
250 | static const struct kernfs_ops sysfs_bin_kfops_mmap = { | |
251 | .read = sysfs_kf_bin_read, | |
252 | .write = sysfs_kf_bin_write, | |
f6acf8bb | 253 | .mmap = sysfs_kf_bin_mmap, |
74b30195 | 254 | .open = sysfs_kf_bin_open, |
f6acf8bb TH |
255 | }; |
256 | ||
324a56e1 | 257 | int sysfs_add_file_mode_ns(struct kernfs_node *parent, |
a7dc66df | 258 | const struct attribute *attr, bool is_bin, |
5f81880d | 259 | umode_t mode, kuid_t uid, kgid_t gid, const void *ns) |
1da177e4 | 260 | { |
517e64f5 | 261 | struct lock_class_key *key = NULL; |
f6acf8bb | 262 | const struct kernfs_ops *ops; |
324a56e1 | 263 | struct kernfs_node *kn; |
471bd7b7 | 264 | loff_t size; |
1da177e4 | 265 | |
a7dc66df | 266 | if (!is_bin) { |
324a56e1 | 267 | struct kobject *kobj = parent->priv; |
f6acf8bb TH |
268 | const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops; |
269 | ||
270 | /* every kobject with an attribute needs a ktype assigned */ | |
271 | if (WARN(!sysfs_ops, KERN_ERR | |
272 | "missing sysfs attribute operations for kobject: %s\n", | |
273 | kobject_name(kobj))) | |
274 | return -EINVAL; | |
275 | ||
2b75869b N |
276 | if (sysfs_ops->show && sysfs_ops->store) { |
277 | if (mode & SYSFS_PREALLOC) | |
278 | ops = &sysfs_prealloc_kfops_rw; | |
279 | else | |
280 | ops = &sysfs_file_kfops_rw; | |
4ef67a8c N |
281 | } else if (sysfs_ops->show) { |
282 | if (mode & SYSFS_PREALLOC) | |
283 | ops = &sysfs_prealloc_kfops_ro; | |
284 | else | |
285 | ops = &sysfs_file_kfops_ro; | |
286 | } else if (sysfs_ops->store) { | |
2b75869b N |
287 | if (mode & SYSFS_PREALLOC) |
288 | ops = &sysfs_prealloc_kfops_wo; | |
289 | else | |
290 | ops = &sysfs_file_kfops_wo; | |
291 | } else | |
f6acf8bb | 292 | ops = &sysfs_file_kfops_empty; |
471bd7b7 TH |
293 | |
294 | size = PAGE_SIZE; | |
f6acf8bb TH |
295 | } else { |
296 | struct bin_attribute *battr = (void *)attr; | |
297 | ||
9b2db6e1 TH |
298 | if (battr->mmap) |
299 | ops = &sysfs_bin_kfops_mmap; | |
300 | else if (battr->read && battr->write) | |
f6acf8bb TH |
301 | ops = &sysfs_bin_kfops_rw; |
302 | else if (battr->read) | |
303 | ops = &sysfs_bin_kfops_ro; | |
304 | else if (battr->write) | |
305 | ops = &sysfs_bin_kfops_wo; | |
306 | else | |
307 | ops = &sysfs_file_kfops_empty; | |
471bd7b7 TH |
308 | |
309 | size = battr->size; | |
f6acf8bb TH |
310 | } |
311 | ||
517e64f5 TH |
312 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
313 | if (!attr->ignore_lockdep) | |
314 | key = attr->key ?: (struct lock_class_key *)&attr->skey; | |
315 | #endif | |
5f81880d DT |
316 | |
317 | kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid, | |
488dee96 | 318 | size, ops, (void *)attr, ns, key); |
324a56e1 TH |
319 | if (IS_ERR(kn)) { |
320 | if (PTR_ERR(kn) == -EEXIST) | |
321 | sysfs_warn_dup(parent, attr->name); | |
322 | return PTR_ERR(kn); | |
496f7394 TH |
323 | } |
324 | return 0; | |
325 | } | |
326 | ||
1da177e4 | 327 | /** |
58292cbe TH |
328 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
329 | * @kobj: object we're creating for | |
330 | * @attr: attribute descriptor | |
331 | * @ns: namespace the new file should belong to | |
1da177e4 | 332 | */ |
58292cbe TH |
333 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
334 | const void *ns) | |
1da177e4 | 335 | { |
5f81880d DT |
336 | kuid_t uid; |
337 | kgid_t gid; | |
338 | ||
de96e9fe GKH |
339 | if (WARN_ON(!kobj || !kobj->sd || !attr)) |
340 | return -EINVAL; | |
1da177e4 | 341 | |
5f81880d DT |
342 | kobject_get_ownership(kobj, &uid, &gid); |
343 | return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, | |
344 | uid, gid, ns); | |
1da177e4 LT |
345 | |
346 | } | |
58292cbe | 347 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
1da177e4 | 348 | |
9ee4685c | 349 | int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr) |
1c205ae1 AK |
350 | { |
351 | int err = 0; | |
352 | int i; | |
353 | ||
354 | for (i = 0; ptr[i] && !err; i++) | |
355 | err = sysfs_create_file(kobj, ptr[i]); | |
356 | if (err) | |
357 | while (--i >= 0) | |
358 | sysfs_remove_file(kobj, ptr[i]); | |
359 | return err; | |
360 | } | |
1b866757 | 361 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
1da177e4 | 362 | |
dfa87c82 AS |
363 | /** |
364 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. | |
365 | * @kobj: object we're acting for. | |
366 | * @attr: attribute descriptor. | |
367 | * @group: group name. | |
368 | */ | |
369 | int sysfs_add_file_to_group(struct kobject *kobj, | |
370 | const struct attribute *attr, const char *group) | |
371 | { | |
324a56e1 | 372 | struct kernfs_node *parent; |
5f81880d DT |
373 | kuid_t uid; |
374 | kgid_t gid; | |
dfa87c82 AS |
375 | int error; |
376 | ||
ccf73cf3 | 377 | if (group) { |
324a56e1 | 378 | parent = kernfs_find_and_get(kobj->sd, group); |
ccf73cf3 | 379 | } else { |
324a56e1 TH |
380 | parent = kobj->sd; |
381 | kernfs_get(parent); | |
ccf73cf3 | 382 | } |
11f24fbd | 383 | |
324a56e1 | 384 | if (!parent) |
608e266a TH |
385 | return -ENOENT; |
386 | ||
5f81880d | 387 | kobject_get_ownership(kobj, &uid, &gid); |
d1753390 | 388 | error = sysfs_add_file_mode_ns(parent, attr, false, |
5f81880d | 389 | attr->mode, uid, gid, NULL); |
324a56e1 | 390 | kernfs_put(parent); |
608e266a | 391 | |
dfa87c82 AS |
392 | return error; |
393 | } | |
394 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); | |
395 | ||
31e5abe9 KS |
396 | /** |
397 | * sysfs_chmod_file - update the modified mode value on an object attribute. | |
398 | * @kobj: object we're acting for. | |
399 | * @attr: attribute descriptor. | |
400 | * @mode: file permissions. | |
401 | * | |
402 | */ | |
49c19400 | 403 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
48176a97 | 404 | umode_t mode) |
31e5abe9 | 405 | { |
324a56e1 | 406 | struct kernfs_node *kn; |
bc062b1b | 407 | struct iattr newattrs; |
51225039 TH |
408 | int rc; |
409 | ||
324a56e1 TH |
410 | kn = kernfs_find_and_get(kobj->sd, attr->name); |
411 | if (!kn) | |
5d60418e | 412 | return -ENOENT; |
f88123ea | 413 | |
adc5e8b5 | 414 | newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO); |
4c6974f5 | 415 | newattrs.ia_valid = ATTR_MODE; |
f88123ea | 416 | |
324a56e1 | 417 | rc = kernfs_setattr(kn, &newattrs); |
5d60418e | 418 | |
324a56e1 | 419 | kernfs_put(kn); |
51225039 | 420 | return rc; |
31e5abe9 KS |
421 | } |
422 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); | |
423 | ||
2afc9166 BVA |
424 | /** |
425 | * sysfs_break_active_protection - break "active" protection | |
426 | * @kobj: The kernel object @attr is associated with. | |
427 | * @attr: The attribute to break the "active" protection for. | |
428 | * | |
429 | * With sysfs, just like kernfs, deletion of an attribute is postponed until | |
430 | * all active .show() and .store() callbacks have finished unless this function | |
431 | * is called. Hence this function is useful in methods that implement self | |
432 | * deletion. | |
433 | */ | |
434 | struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj, | |
435 | const struct attribute *attr) | |
436 | { | |
437 | struct kernfs_node *kn; | |
438 | ||
439 | kobject_get(kobj); | |
440 | kn = kernfs_find_and_get(kobj->sd, attr->name); | |
441 | if (kn) | |
442 | kernfs_break_active_protection(kn); | |
443 | return kn; | |
444 | } | |
445 | EXPORT_SYMBOL_GPL(sysfs_break_active_protection); | |
446 | ||
447 | /** | |
448 | * sysfs_unbreak_active_protection - restore "active" protection | |
449 | * @kn: Pointer returned by sysfs_break_active_protection(). | |
450 | * | |
451 | * Undo the effects of sysfs_break_active_protection(). Since this function | |
452 | * calls kernfs_put() on the kernfs node that corresponds to the 'attr' | |
453 | * argument passed to sysfs_break_active_protection() that attribute may have | |
454 | * been removed between the sysfs_break_active_protection() and | |
455 | * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after | |
456 | * this function has returned. | |
457 | */ | |
458 | void sysfs_unbreak_active_protection(struct kernfs_node *kn) | |
459 | { | |
460 | struct kobject *kobj = kn->parent->priv; | |
461 | ||
462 | kernfs_unbreak_active_protection(kn); | |
463 | kernfs_put(kn); | |
464 | kobject_put(kobj); | |
465 | } | |
466 | EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection); | |
467 | ||
1da177e4 | 468 | /** |
58292cbe TH |
469 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
470 | * @kobj: object we're acting for | |
471 | * @attr: attribute descriptor | |
472 | * @ns: namespace tag of the file to remove | |
1da177e4 | 473 | * |
58292cbe | 474 | * Hash the attribute name and namespace tag and kill the victim. |
1da177e4 | 475 | */ |
58292cbe TH |
476 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
477 | const void *ns) | |
1da177e4 | 478 | { |
324a56e1 | 479 | struct kernfs_node *parent = kobj->sd; |
487505c2 | 480 | |
324a56e1 | 481 | kernfs_remove_by_name_ns(parent, attr->name, ns); |
1da177e4 | 482 | } |
58292cbe | 483 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
1da177e4 | 484 | |
6b0afc2a TH |
485 | /** |
486 | * sysfs_remove_file_self - remove an object attribute from its own method | |
487 | * @kobj: object we're acting for | |
488 | * @attr: attribute descriptor | |
489 | * | |
490 | * See kernfs_remove_self() for details. | |
491 | */ | |
492 | bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr) | |
493 | { | |
494 | struct kernfs_node *parent = kobj->sd; | |
495 | struct kernfs_node *kn; | |
496 | bool ret; | |
497 | ||
498 | kn = kernfs_find_and_get(parent, attr->name); | |
499 | if (WARN_ON_ONCE(!kn)) | |
500 | return false; | |
501 | ||
502 | ret = kernfs_remove_self(kn); | |
503 | ||
504 | kernfs_put(kn); | |
505 | return ret; | |
506 | } | |
9ddacff1 | 507 | EXPORT_SYMBOL_GPL(sysfs_remove_file_self); |
6b0afc2a | 508 | |
9ee4685c | 509 | void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr) |
1c205ae1 AK |
510 | { |
511 | int i; | |
4bd4e92c | 512 | |
1c205ae1 AK |
513 | for (i = 0; ptr[i]; i++) |
514 | sysfs_remove_file(kobj, ptr[i]); | |
515 | } | |
1b866757 | 516 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
1da177e4 | 517 | |
dfa87c82 AS |
518 | /** |
519 | * sysfs_remove_file_from_group - remove an attribute file from a group. | |
520 | * @kobj: object we're acting for. | |
521 | * @attr: attribute descriptor. | |
522 | * @group: group name. | |
523 | */ | |
524 | void sysfs_remove_file_from_group(struct kobject *kobj, | |
525 | const struct attribute *attr, const char *group) | |
526 | { | |
324a56e1 | 527 | struct kernfs_node *parent; |
dfa87c82 | 528 | |
ccf73cf3 | 529 | if (group) { |
324a56e1 | 530 | parent = kernfs_find_and_get(kobj->sd, group); |
ccf73cf3 | 531 | } else { |
324a56e1 TH |
532 | parent = kobj->sd; |
533 | kernfs_get(parent); | |
ccf73cf3 TH |
534 | } |
535 | ||
324a56e1 TH |
536 | if (parent) { |
537 | kernfs_remove_by_name(parent, attr->name); | |
538 | kernfs_put(parent); | |
dfa87c82 AS |
539 | } |
540 | } | |
541 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); | |
542 | ||
3124eb16 TH |
543 | /** |
544 | * sysfs_create_bin_file - create binary file for object. | |
545 | * @kobj: object. | |
546 | * @attr: attribute descriptor. | |
547 | */ | |
548 | int sysfs_create_bin_file(struct kobject *kobj, | |
549 | const struct bin_attribute *attr) | |
550 | { | |
5f81880d DT |
551 | kuid_t uid; |
552 | kgid_t gid; | |
553 | ||
de96e9fe GKH |
554 | if (WARN_ON(!kobj || !kobj->sd || !attr)) |
555 | return -EINVAL; | |
3124eb16 | 556 | |
5f81880d DT |
557 | kobject_get_ownership(kobj, &uid, &gid); |
558 | return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true, | |
559 | attr->attr.mode, uid, gid, NULL); | |
3124eb16 TH |
560 | } |
561 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); | |
562 | ||
563 | /** | |
564 | * sysfs_remove_bin_file - remove binary file for object. | |
565 | * @kobj: object. | |
566 | * @attr: attribute descriptor. | |
567 | */ | |
568 | void sysfs_remove_bin_file(struct kobject *kobj, | |
569 | const struct bin_attribute *attr) | |
570 | { | |
879f40d1 | 571 | kernfs_remove_by_name(kobj->sd, attr->attr.name); |
3124eb16 TH |
572 | } |
573 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); | |
f70ce185 CB |
574 | |
575 | static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid, | |
576 | kgid_t kgid) | |
577 | { | |
578 | struct iattr newattrs = { | |
579 | .ia_valid = ATTR_UID | ATTR_GID, | |
580 | .ia_uid = kuid, | |
581 | .ia_gid = kgid, | |
582 | }; | |
583 | return kernfs_setattr(kn, &newattrs); | |
584 | } | |
585 | ||
0666a3ae CB |
586 | /** |
587 | * sysfs_link_change_owner - change owner of a sysfs file. | |
588 | * @kobj: object of the kernfs_node the symlink is located in. | |
589 | * @targ: object of the kernfs_node the symlink points to. | |
590 | * @name: name of the link. | |
591 | * @kuid: new owner's kuid | |
592 | * @kgid: new owner's kgid | |
593 | * | |
594 | * This function looks up the sysfs symlink entry @name under @kobj and changes | |
595 | * the ownership to @kuid/@kgid. The symlink is looked up in the namespace of | |
596 | * @targ. | |
597 | * | |
598 | * Returns 0 on success or error code on failure. | |
599 | */ | |
600 | int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ, | |
601 | const char *name, kuid_t kuid, kgid_t kgid) | |
602 | { | |
603 | struct kernfs_node *kn = NULL; | |
604 | int error; | |
605 | ||
606 | if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs) | |
607 | return -EINVAL; | |
608 | ||
609 | error = -ENOENT; | |
610 | kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns); | |
611 | if (!kn) | |
612 | goto out; | |
613 | ||
614 | error = -EINVAL; | |
615 | if (kernfs_type(kn) != KERNFS_LINK) | |
616 | goto out; | |
617 | if (kn->symlink.target_kn->priv != targ) | |
618 | goto out; | |
619 | ||
620 | error = internal_change_owner(kn, kuid, kgid); | |
621 | ||
622 | out: | |
623 | kernfs_put(kn); | |
624 | return error; | |
625 | } | |
626 | ||
f70ce185 CB |
627 | /** |
628 | * sysfs_file_change_owner - change owner of a sysfs file. | |
629 | * @kobj: object. | |
630 | * @name: name of the file to change. | |
631 | * @kuid: new owner's kuid | |
632 | * @kgid: new owner's kgid | |
633 | * | |
634 | * This function looks up the sysfs entry @name under @kobj and changes the | |
635 | * ownership to @kuid/@kgid. | |
636 | * | |
637 | * Returns 0 on success or error code on failure. | |
638 | */ | |
639 | int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid, | |
640 | kgid_t kgid) | |
641 | { | |
642 | struct kernfs_node *kn; | |
643 | int error; | |
644 | ||
645 | if (!name) | |
646 | return -EINVAL; | |
647 | ||
648 | if (!kobj->state_in_sysfs) | |
649 | return -EINVAL; | |
650 | ||
651 | kn = kernfs_find_and_get(kobj->sd, name); | |
652 | if (!kn) | |
653 | return -ENOENT; | |
654 | ||
655 | error = internal_change_owner(kn, kuid, kgid); | |
656 | ||
657 | kernfs_put(kn); | |
658 | ||
659 | return error; | |
660 | } | |
661 | EXPORT_SYMBOL_GPL(sysfs_file_change_owner); | |
2c4f9401 CB |
662 | |
663 | /** | |
664 | * sysfs_change_owner - change owner of the given object. | |
665 | * @kobj: object. | |
666 | * @kuid: new owner's kuid | |
667 | * @kgid: new owner's kgid | |
668 | * | |
669 | * Change the owner of the default directory, files, groups, and attributes of | |
670 | * @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs | |
671 | * entries for a kobject are added by driver core. In summary, | |
672 | * sysfs_change_owner() takes care of the default directory entry for @kobj, | |
673 | * the default attributes associated with the ktype of @kobj and the default | |
674 | * attributes associated with the ktype of @kobj. | |
675 | * Additional properties not added by driver core have to be changed by the | |
676 | * driver or subsystem which created them. This is similar to how | |
677 | * driver/subsystem specific entries are removed. | |
678 | * | |
679 | * Returns 0 on success or error code on failure. | |
680 | */ | |
681 | int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid) | |
682 | { | |
683 | int error; | |
684 | const struct kobj_type *ktype; | |
685 | ||
686 | if (!kobj->state_in_sysfs) | |
687 | return -EINVAL; | |
688 | ||
689 | /* Change the owner of the kobject itself. */ | |
690 | error = internal_change_owner(kobj->sd, kuid, kgid); | |
691 | if (error) | |
692 | return error; | |
693 | ||
694 | ktype = get_ktype(kobj); | |
695 | if (ktype) { | |
696 | struct attribute **kattr; | |
697 | ||
698 | /* | |
699 | * Change owner of the default attributes associated with the | |
700 | * ktype of @kobj. | |
701 | */ | |
702 | for (kattr = ktype->default_attrs; kattr && *kattr; kattr++) { | |
703 | error = sysfs_file_change_owner(kobj, (*kattr)->name, | |
704 | kuid, kgid); | |
705 | if (error) | |
706 | return error; | |
707 | } | |
708 | ||
709 | /* | |
710 | * Change owner of the default groups associated with the | |
711 | * ktype of @kobj. | |
712 | */ | |
713 | error = sysfs_groups_change_owner(kobj, ktype->default_groups, | |
714 | kuid, kgid); | |
715 | if (error) | |
716 | return error; | |
717 | } | |
718 | ||
719 | return 0; | |
720 | } | |
721 | EXPORT_SYMBOL_GPL(sysfs_change_owner); | |
2efc459d JP |
722 | |
723 | /** | |
724 | * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer. | |
725 | * @buf: start of PAGE_SIZE buffer. | |
726 | * @fmt: format | |
727 | * @...: optional arguments to @format | |
728 | * | |
729 | * | |
730 | * Returns number of characters written to @buf. | |
731 | */ | |
732 | int sysfs_emit(char *buf, const char *fmt, ...) | |
733 | { | |
734 | va_list args; | |
735 | int len; | |
736 | ||
737 | if (WARN(!buf || offset_in_page(buf), | |
738 | "invalid sysfs_emit: buf:%p\n", buf)) | |
739 | return 0; | |
740 | ||
741 | va_start(args, fmt); | |
742 | len = vscnprintf(buf, PAGE_SIZE, fmt, args); | |
743 | va_end(args); | |
744 | ||
745 | return len; | |
746 | } | |
747 | EXPORT_SYMBOL_GPL(sysfs_emit); | |
748 | ||
749 | /** | |
750 | * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer. | |
751 | * @buf: start of PAGE_SIZE buffer. | |
752 | * @at: offset in @buf to start write in bytes | |
753 | * @at must be >= 0 && < PAGE_SIZE | |
754 | * @fmt: format | |
755 | * @...: optional arguments to @fmt | |
756 | * | |
757 | * | |
758 | * Returns number of characters written starting at &@buf[@at]. | |
759 | */ | |
760 | int sysfs_emit_at(char *buf, int at, const char *fmt, ...) | |
761 | { | |
762 | va_list args; | |
763 | int len; | |
764 | ||
765 | if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE, | |
766 | "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at)) | |
767 | return 0; | |
768 | ||
769 | va_start(args, fmt); | |
770 | len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args); | |
771 | va_end(args); | |
772 | ||
773 | return len; | |
774 | } | |
775 | EXPORT_SYMBOL_GPL(sysfs_emit_at); |