1 // SPDX-License-Identifier: GPL-2.0
2 /*
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  *
9  * Please see Documentation/filesystems/sysfs.txt for more information.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kobject.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/mutex.h>
17 #include <linux/seq_file.h>
18 #include <linux/mm.h>
19 
20 #include "sysfs.h"
21 #include "../kernfs/kernfs-internal.h"
22 
23 /*
24  * Determine ktype->sysfs_ops for the given kernfs_node.  This function
25  * must be called while holding an active reference.
26  */
sysfs_file_ops(struct kernfs_node * kn)27 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
28 {
29 	struct kobject *kobj = kn->parent->priv;
30 
31 	if (kn->flags & KERNFS_LOCKDEP)
32 		lockdep_assert_held(kn);
33 	return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
34 }
35 
36 /*
37  * Reads on sysfs are handled through seq_file, which takes care of hairy
38  * details like buffering and seeking.  The following function pipes
39  * sysfs_ops->show() result through seq_file.
40  */
sysfs_kf_seq_show(struct seq_file * sf,void * v)41 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
42 {
43 	struct kernfs_open_file *of = sf->private;
44 	struct kobject *kobj = of->kn->parent->priv;
45 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
46 	ssize_t count;
47 	char *buf;
48 
49 	/* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
50 	count = seq_get_buf(sf, &buf);
51 	if (count < PAGE_SIZE) {
52 		seq_commit(sf, -1);
53 		return 0;
54 	}
55 	memset(buf, 0, PAGE_SIZE);
56 
57 	/*
58 	 * Invoke show().  Control may reach here via seq file lseek even
59 	 * if @ops->show() isn't implemented.
60 	 */
61 	if (ops->show) {
62 		count = ops->show(kobj, of->kn->priv, buf);
63 		if (count < 0)
64 			return count;
65 	}
66 
67 	/*
68 	 * The code works fine with PAGE_SIZE return but it's likely to
69 	 * indicate truncated result or overflow in normal use cases.
70 	 */
71 	if (count >= (ssize_t)PAGE_SIZE) {
72 		printk("fill_read_buffer: %pS returned bad count\n",
73 				ops->show);
74 		/* Try to struggle along */
75 		count = PAGE_SIZE - 1;
76 	}
77 	seq_commit(sf, count);
78 	return 0;
79 }
80 
sysfs_kf_bin_read(struct kernfs_open_file * of,char * buf,size_t count,loff_t pos)81 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
82 				 size_t count, loff_t pos)
83 {
84 	struct bin_attribute *battr = of->kn->priv;
85 	struct kobject *kobj = of->kn->parent->priv;
86 	loff_t size = file_inode(of->file)->i_size;
87 
88 	if (!count)
89 		return 0;
90 
91 	if (size) {
92 		if (pos >= size)
93 			return 0;
94 		if (pos + count > size)
95 			count = size - pos;
96 	}
97 
98 	if (!battr->read)
99 		return -EIO;
100 
101 	return battr->read(of->file, kobj, battr, buf, pos, count);
102 }
103 
104 /* kernfs read callback for regular sysfs files with pre-alloc */
sysfs_kf_read(struct kernfs_open_file * of,char * buf,size_t count,loff_t pos)105 static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
106 			     size_t count, loff_t pos)
107 {
108 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
109 	struct kobject *kobj = of->kn->parent->priv;
110 	ssize_t len;
111 
112 	/*
113 	 * If buf != of->prealloc_buf, we don't know how
114 	 * large it is, so cannot safely pass it to ->show
115 	 */
116 	if (WARN_ON_ONCE(buf != of->prealloc_buf))
117 		return 0;
118 	len = ops->show(kobj, of->kn->priv, buf);
119 	if (len < 0)
120 		return len;
121 	if (pos) {
122 		if (len <= pos)
123 			return 0;
124 		len -= pos;
125 		memmove(buf, buf + pos, len);
126 	}
127 	return min_t(ssize_t, count, len);
128 }
129 
130 /* kernfs write callback for regular sysfs files */
sysfs_kf_write(struct kernfs_open_file * of,char * buf,size_t count,loff_t pos)131 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
132 			      size_t count, loff_t pos)
133 {
134 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
135 	struct kobject *kobj = of->kn->parent->priv;
136 
137 	if (!count)
138 		return 0;
139 
140 	return ops->store(kobj, of->kn->priv, buf, count);
141 }
142 
143 /* kernfs write callback for bin sysfs files */
sysfs_kf_bin_write(struct kernfs_open_file * of,char * buf,size_t count,loff_t pos)144 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
145 				  size_t count, loff_t pos)
146 {
147 	struct bin_attribute *battr = of->kn->priv;
148 	struct kobject *kobj = of->kn->parent->priv;
149 	loff_t size = file_inode(of->file)->i_size;
150 
151 	if (size) {
152 		if (size <= pos)
153 			return -EFBIG;
154 		count = min_t(ssize_t, count, size - pos);
155 	}
156 	if (!count)
157 		return 0;
158 
159 	if (!battr->write)
160 		return -EIO;
161 
162 	return battr->write(of->file, kobj, battr, buf, pos, count);
163 }
164 
sysfs_kf_bin_mmap(struct kernfs_open_file * of,struct vm_area_struct * vma)165 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
166 			     struct vm_area_struct *vma)
167 {
168 	struct bin_attribute *battr = of->kn->priv;
169 	struct kobject *kobj = of->kn->parent->priv;
170 
171 	return battr->mmap(of->file, kobj, battr, vma);
172 }
173 
sysfs_notify(struct kobject * kobj,const char * dir,const char * attr)174 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
175 {
176 	struct kernfs_node *kn = kobj->sd, *tmp;
177 
178 	if (kn && dir)
179 		kn = kernfs_find_and_get(kn, dir);
180 	else
181 		kernfs_get(kn);
182 
183 	if (kn && attr) {
184 		tmp = kernfs_find_and_get(kn, attr);
185 		kernfs_put(kn);
186 		kn = tmp;
187 	}
188 
189 	if (kn) {
190 		kernfs_notify(kn);
191 		kernfs_put(kn);
192 	}
193 }
194 EXPORT_SYMBOL_GPL(sysfs_notify);
195 
196 static const struct kernfs_ops sysfs_file_kfops_empty = {
197 };
198 
199 static const struct kernfs_ops sysfs_file_kfops_ro = {
200 	.seq_show	= sysfs_kf_seq_show,
201 };
202 
203 static const struct kernfs_ops sysfs_file_kfops_wo = {
204 	.write		= sysfs_kf_write,
205 };
206 
207 static const struct kernfs_ops sysfs_file_kfops_rw = {
208 	.seq_show	= sysfs_kf_seq_show,
209 	.write		= sysfs_kf_write,
210 };
211 
212 static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
213 	.read		= sysfs_kf_read,
214 	.prealloc	= true,
215 };
216 
217 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
218 	.write		= sysfs_kf_write,
219 	.prealloc	= true,
220 };
221 
222 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
223 	.read		= sysfs_kf_read,
224 	.write		= sysfs_kf_write,
225 	.prealloc	= true,
226 };
227 
228 static const struct kernfs_ops sysfs_bin_kfops_ro = {
229 	.read		= sysfs_kf_bin_read,
230 };
231 
232 static const struct kernfs_ops sysfs_bin_kfops_wo = {
233 	.write		= sysfs_kf_bin_write,
234 };
235 
236 static const struct kernfs_ops sysfs_bin_kfops_rw = {
237 	.read		= sysfs_kf_bin_read,
238 	.write		= sysfs_kf_bin_write,
239 };
240 
241 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
242 	.read		= sysfs_kf_bin_read,
243 	.write		= sysfs_kf_bin_write,
244 	.mmap		= sysfs_kf_bin_mmap,
245 };
246 
sysfs_add_file_mode_ns(struct kernfs_node * parent,const struct attribute * attr,bool is_bin,umode_t mode,kuid_t uid,kgid_t gid,const void * ns)247 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
248 			   const struct attribute *attr, bool is_bin,
249 			   umode_t mode, kuid_t uid, kgid_t gid, const void *ns)
250 {
251 	struct lock_class_key *key = NULL;
252 	const struct kernfs_ops *ops;
253 	struct kernfs_node *kn;
254 	loff_t size;
255 
256 	if (!is_bin) {
257 		struct kobject *kobj = parent->priv;
258 		const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
259 
260 		/* every kobject with an attribute needs a ktype assigned */
261 		if (WARN(!sysfs_ops, KERN_ERR
262 			 "missing sysfs attribute operations for kobject: %s\n",
263 			 kobject_name(kobj)))
264 			return -EINVAL;
265 
266 		if (sysfs_ops->show && sysfs_ops->store) {
267 			if (mode & SYSFS_PREALLOC)
268 				ops = &sysfs_prealloc_kfops_rw;
269 			else
270 				ops = &sysfs_file_kfops_rw;
271 		} else if (sysfs_ops->show) {
272 			if (mode & SYSFS_PREALLOC)
273 				ops = &sysfs_prealloc_kfops_ro;
274 			else
275 				ops = &sysfs_file_kfops_ro;
276 		} else if (sysfs_ops->store) {
277 			if (mode & SYSFS_PREALLOC)
278 				ops = &sysfs_prealloc_kfops_wo;
279 			else
280 				ops = &sysfs_file_kfops_wo;
281 		} else
282 			ops = &sysfs_file_kfops_empty;
283 
284 		size = PAGE_SIZE;
285 	} else {
286 		struct bin_attribute *battr = (void *)attr;
287 
288 		if (battr->mmap)
289 			ops = &sysfs_bin_kfops_mmap;
290 		else if (battr->read && battr->write)
291 			ops = &sysfs_bin_kfops_rw;
292 		else if (battr->read)
293 			ops = &sysfs_bin_kfops_ro;
294 		else if (battr->write)
295 			ops = &sysfs_bin_kfops_wo;
296 		else
297 			ops = &sysfs_file_kfops_empty;
298 
299 		size = battr->size;
300 	}
301 
302 #ifdef CONFIG_DEBUG_LOCK_ALLOC
303 	if (!attr->ignore_lockdep)
304 		key = attr->key ?: (struct lock_class_key *)&attr->skey;
305 #endif
306 
307 	kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
308 				  size, ops, (void *)attr, ns, key);
309 	if (IS_ERR(kn)) {
310 		if (PTR_ERR(kn) == -EEXIST)
311 			sysfs_warn_dup(parent, attr->name);
312 		return PTR_ERR(kn);
313 	}
314 	return 0;
315 }
316 
317 /**
318  * sysfs_create_file_ns - create an attribute file for an object with custom ns
319  * @kobj: object we're creating for
320  * @attr: attribute descriptor
321  * @ns: namespace the new file should belong to
322  */
sysfs_create_file_ns(struct kobject * kobj,const struct attribute * attr,const void * ns)323 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
324 			 const void *ns)
325 {
326 	kuid_t uid;
327 	kgid_t gid;
328 
329 	BUG_ON(!kobj || !kobj->sd || !attr);
330 
331 	kobject_get_ownership(kobj, &uid, &gid);
332 	return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
333 				      uid, gid, ns);
334 
335 }
336 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
337 
sysfs_create_files(struct kobject * kobj,const struct attribute ** ptr)338 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
339 {
340 	int err = 0;
341 	int i;
342 
343 	for (i = 0; ptr[i] && !err; i++)
344 		err = sysfs_create_file(kobj, ptr[i]);
345 	if (err)
346 		while (--i >= 0)
347 			sysfs_remove_file(kobj, ptr[i]);
348 	return err;
349 }
350 EXPORT_SYMBOL_GPL(sysfs_create_files);
351 
352 /**
353  * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
354  * @kobj: object we're acting for.
355  * @attr: attribute descriptor.
356  * @group: group name.
357  */
sysfs_add_file_to_group(struct kobject * kobj,const struct attribute * attr,const char * group)358 int sysfs_add_file_to_group(struct kobject *kobj,
359 		const struct attribute *attr, const char *group)
360 {
361 	struct kernfs_node *parent;
362 	kuid_t uid;
363 	kgid_t gid;
364 	int error;
365 
366 	if (group) {
367 		parent = kernfs_find_and_get(kobj->sd, group);
368 	} else {
369 		parent = kobj->sd;
370 		kernfs_get(parent);
371 	}
372 
373 	if (!parent)
374 		return -ENOENT;
375 
376 	kobject_get_ownership(kobj, &uid, &gid);
377 	error = sysfs_add_file_mode_ns(parent, attr, false,
378 				       attr->mode, uid, gid, NULL);
379 	kernfs_put(parent);
380 
381 	return error;
382 }
383 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
384 
385 /**
386  * sysfs_chmod_file - update the modified mode value on an object attribute.
387  * @kobj: object we're acting for.
388  * @attr: attribute descriptor.
389  * @mode: file permissions.
390  *
391  */
sysfs_chmod_file(struct kobject * kobj,const struct attribute * attr,umode_t mode)392 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
393 		     umode_t mode)
394 {
395 	struct kernfs_node *kn;
396 	struct iattr newattrs;
397 	int rc;
398 
399 	kn = kernfs_find_and_get(kobj->sd, attr->name);
400 	if (!kn)
401 		return -ENOENT;
402 
403 	newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
404 	newattrs.ia_valid = ATTR_MODE;
405 
406 	rc = kernfs_setattr(kn, &newattrs);
407 
408 	kernfs_put(kn);
409 	return rc;
410 }
411 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
412 
413 /**
414  * sysfs_break_active_protection - break "active" protection
415  * @kobj: The kernel object @attr is associated with.
416  * @attr: The attribute to break the "active" protection for.
417  *
418  * With sysfs, just like kernfs, deletion of an attribute is postponed until
419  * all active .show() and .store() callbacks have finished unless this function
420  * is called. Hence this function is useful in methods that implement self
421  * deletion.
422  */
sysfs_break_active_protection(struct kobject * kobj,const struct attribute * attr)423 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
424 						  const struct attribute *attr)
425 {
426 	struct kernfs_node *kn;
427 
428 	kobject_get(kobj);
429 	kn = kernfs_find_and_get(kobj->sd, attr->name);
430 	if (kn)
431 		kernfs_break_active_protection(kn);
432 	return kn;
433 }
434 EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
435 
436 /**
437  * sysfs_unbreak_active_protection - restore "active" protection
438  * @kn: Pointer returned by sysfs_break_active_protection().
439  *
440  * Undo the effects of sysfs_break_active_protection(). Since this function
441  * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
442  * argument passed to sysfs_break_active_protection() that attribute may have
443  * been removed between the sysfs_break_active_protection() and
444  * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
445  * this function has returned.
446  */
sysfs_unbreak_active_protection(struct kernfs_node * kn)447 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
448 {
449 	struct kobject *kobj = kn->parent->priv;
450 
451 	kernfs_unbreak_active_protection(kn);
452 	kernfs_put(kn);
453 	kobject_put(kobj);
454 }
455 EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
456 
457 /**
458  * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
459  * @kobj: object we're acting for
460  * @attr: attribute descriptor
461  * @ns: namespace tag of the file to remove
462  *
463  * Hash the attribute name and namespace tag and kill the victim.
464  */
sysfs_remove_file_ns(struct kobject * kobj,const struct attribute * attr,const void * ns)465 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
466 			  const void *ns)
467 {
468 	struct kernfs_node *parent = kobj->sd;
469 
470 	kernfs_remove_by_name_ns(parent, attr->name, ns);
471 }
472 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
473 
474 /**
475  * sysfs_remove_file_self - remove an object attribute from its own method
476  * @kobj: object we're acting for
477  * @attr: attribute descriptor
478  *
479  * See kernfs_remove_self() for details.
480  */
sysfs_remove_file_self(struct kobject * kobj,const struct attribute * attr)481 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
482 {
483 	struct kernfs_node *parent = kobj->sd;
484 	struct kernfs_node *kn;
485 	bool ret;
486 
487 	kn = kernfs_find_and_get(parent, attr->name);
488 	if (WARN_ON_ONCE(!kn))
489 		return false;
490 
491 	ret = kernfs_remove_self(kn);
492 
493 	kernfs_put(kn);
494 	return ret;
495 }
496 
sysfs_remove_files(struct kobject * kobj,const struct attribute ** ptr)497 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
498 {
499 	int i;
500 	for (i = 0; ptr[i]; i++)
501 		sysfs_remove_file(kobj, ptr[i]);
502 }
503 EXPORT_SYMBOL_GPL(sysfs_remove_files);
504 
505 /**
506  * sysfs_remove_file_from_group - remove an attribute file from a group.
507  * @kobj: object we're acting for.
508  * @attr: attribute descriptor.
509  * @group: group name.
510  */
sysfs_remove_file_from_group(struct kobject * kobj,const struct attribute * attr,const char * group)511 void sysfs_remove_file_from_group(struct kobject *kobj,
512 		const struct attribute *attr, const char *group)
513 {
514 	struct kernfs_node *parent;
515 
516 	if (group) {
517 		parent = kernfs_find_and_get(kobj->sd, group);
518 	} else {
519 		parent = kobj->sd;
520 		kernfs_get(parent);
521 	}
522 
523 	if (parent) {
524 		kernfs_remove_by_name(parent, attr->name);
525 		kernfs_put(parent);
526 	}
527 }
528 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
529 
530 /**
531  *	sysfs_create_bin_file - create binary file for object.
532  *	@kobj:	object.
533  *	@attr:	attribute descriptor.
534  */
sysfs_create_bin_file(struct kobject * kobj,const struct bin_attribute * attr)535 int sysfs_create_bin_file(struct kobject *kobj,
536 			  const struct bin_attribute *attr)
537 {
538 	kuid_t uid;
539 	kgid_t gid;
540 
541 	BUG_ON(!kobj || !kobj->sd || !attr);
542 
543 	kobject_get_ownership(kobj, &uid, &gid);
544 	return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true,
545 				      attr->attr.mode, uid, gid, NULL);
546 }
547 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
548 
549 /**
550  *	sysfs_remove_bin_file - remove binary file for object.
551  *	@kobj:	object.
552  *	@attr:	attribute descriptor.
553  */
sysfs_remove_bin_file(struct kobject * kobj,const struct bin_attribute * attr)554 void sysfs_remove_bin_file(struct kobject *kobj,
555 			   const struct bin_attribute *attr)
556 {
557 	kernfs_remove_by_name(kobj->sd, attr->attr.name);
558 }
559 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
560 
561 /**
562  *	sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
563  *	@buf:	start of PAGE_SIZE buffer.
564  *	@fmt:	format
565  *	@...:	optional arguments to @format
566  *
567  *
568  * Returns number of characters written to @buf.
569  */
sysfs_emit(char * buf,const char * fmt,...)570 int sysfs_emit(char *buf, const char *fmt, ...)
571 {
572 	va_list args;
573 	int len;
574 
575 	if (WARN(!buf, "invalid sysfs_emit: buf:%p\n", buf))
576 		return 0;
577 
578 	va_start(args, fmt);
579 	len = vscnprintf(buf, PAGE_SIZE, fmt, args);
580 	va_end(args);
581 
582 	return len;
583 }
584 EXPORT_SYMBOL_GPL(sysfs_emit);
585 
586 /**
587  *	sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
588  *	@buf:	start of PAGE_SIZE buffer.
589  *	@at:	offset in @buf to start write in bytes
590  *		@at must be >= 0 && < PAGE_SIZE
591  *	@fmt:	format
592  *	@...:	optional arguments to @fmt
593  *
594  *
595  * Returns number of characters written starting at &@buf[@at].
596  */
sysfs_emit_at(char * buf,int at,const char * fmt,...)597 int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
598 {
599 	va_list args;
600 	int len;
601 
602 	if (WARN(!buf || at < 0 || at >= PAGE_SIZE,
603 		 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
604 		return 0;
605 
606 	va_start(args, fmt);
607 	len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
608 	va_end(args);
609 
610 	return len;
611 }
612 EXPORT_SYMBOL_GPL(sysfs_emit_at);
613