file.c 12.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
T
Tejun Heo 已提交
2 3 4 5 6 7 8 9 10
 * fs/sysfs/file.c - sysfs regular (text) file implementation
 *
 * Copyright (c) 2001-3 Patrick Mochel
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
 *
 * This file is released under the GPLv2.
 *
 * Please see Documentation/filesystems/sysfs.txt for more information.
L
Linus Torvalds 已提交
11 12 13 14
 */

#include <linux/module.h>
#include <linux/kobject.h>
15
#include <linux/kallsyms.h>
16
#include <linux/slab.h>
17
#include <linux/list.h>
18
#include <linux/mutex.h>
19
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
20 21

#include "sysfs.h"
22
#include "../kernfs/kernfs-internal.h"
T
Tejun Heo 已提交
23

T
Tejun Heo 已提交
24 25 26 27 28 29
/*
 * Determine ktype->sysfs_ops for the given sysfs_dirent.  This function
 * must be called while holding an active reference.
 */
static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
{
30
	struct kobject *kobj = sd->s_parent->priv;
T
Tejun Heo 已提交
31

32
	if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
33
		lockdep_assert_held(sd);
T
Tejun Heo 已提交
34 35 36
	return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
}

37 38 39 40
/*
 * Reads on sysfs are handled through seq_file, which takes care of hairy
 * details like buffering and seeking.  The following function pipes
 * sysfs_ops->show() result through seq_file.
L
Linus Torvalds 已提交
41
 */
42
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
L
Linus Torvalds 已提交
43
{
44
	struct sysfs_open_file *of = sf->private;
45
	struct kobject *kobj = of->sd->s_parent->priv;
46
	const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
L
Linus Torvalds 已提交
47
	ssize_t count;
48
	char *buf;
L
Linus Torvalds 已提交
49

50 51 52 53 54 55
	/* acquire buffer and ensure that it's >= PAGE_SIZE */
	count = seq_get_buf(sf, &buf);
	if (count < PAGE_SIZE) {
		seq_commit(sf, -1);
		return 0;
	}
L
Linus Torvalds 已提交
56

57
	/*
58 59
	 * Invoke show().  Control may reach here via seq file lseek even
	 * if @ops->show() isn't implemented.
60
	 */
61
	if (ops->show) {
62
		count = ops->show(kobj, of->sd->priv, buf);
63 64 65
		if (count < 0)
			return count;
	}
66

67 68 69 70
	/*
	 * The code works fine with PAGE_SIZE return but it's likely to
	 * indicate truncated result or overflow in normal use cases.
	 */
71 72 73 74 75 76
	if (count >= (ssize_t)PAGE_SIZE) {
		print_symbol("fill_read_buffer: %s returned bad count\n",
			(unsigned long)ops->show);
		/* Try to struggle along */
		count = PAGE_SIZE - 1;
	}
77 78
	seq_commit(sf, count);
	return 0;
L
Linus Torvalds 已提交
79 80
}

81 82
static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
				 size_t count, loff_t pos)
T
Tejun Heo 已提交
83
{
84 85
	struct bin_attribute *battr = of->sd->priv;
	struct kobject *kobj = of->sd->s_parent->priv;
86
	loff_t size = file_inode(of->file)->i_size;
T
Tejun Heo 已提交
87

88
	if (!count)
T
Tejun Heo 已提交
89 90 91
		return 0;

	if (size) {
92
		if (pos > size)
T
Tejun Heo 已提交
93
			return 0;
94 95
		if (pos + count > size)
			count = size - pos;
T
Tejun Heo 已提交
96 97
	}

98 99 100 101 102 103
	if (!battr->read)
		return -EIO;

	return battr->read(of->file, kobj, battr, buf, pos, count);
}

104 105 106
/* kernfs write callback for regular sysfs files */
static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
			      size_t count, loff_t pos)
L
Linus Torvalds 已提交
107
{
108
	const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
109
	struct kobject *kobj = of->sd->s_parent->priv;
110

111 112
	if (!count)
		return 0;
113

114 115
	return ops->store(kobj, of->sd->priv, buf, count);
}
116

117 118 119 120 121 122 123
/* kernfs write callback for bin sysfs files */
static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
				  size_t count, loff_t pos)
{
	struct bin_attribute *battr = of->sd->priv;
	struct kobject *kobj = of->sd->s_parent->priv;
	loff_t size = file_inode(of->file)->i_size;
124

125 126 127 128
	if (size) {
		if (size <= pos)
			return 0;
		count = min_t(ssize_t, count, size - pos);
129
	}
130 131
	if (!count)
		return 0;
132

133 134
	if (!battr->write)
		return -EIO;
L
Linus Torvalds 已提交
135

136
	return battr->write(of->file, kobj, battr, buf, pos, count);
L
Linus Torvalds 已提交
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150
static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
			     struct vm_area_struct *vma)
{
	struct bin_attribute *battr = of->sd->priv;
	struct kobject *kobj = of->sd->s_parent->priv;

	if (!battr->mmap)
		return -ENODEV;

	return battr->mmap(of->file, kobj, battr, vma);
}

151
void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
152
{
153
	struct sysfs_dirent *sd = k->sd, *tmp;
154 155

	if (sd && dir)
156
		sd = kernfs_find_and_get(sd, dir);
157
	else
158
		kernfs_get(sd);
159 160

	if (sd && attr) {
161 162
		tmp = kernfs_find_and_get(sd, attr);
		kernfs_put(sd);
163 164
		sd = tmp;
	}
165

166 167
	if (sd) {
		kernfs_notify(sd);
168
		kernfs_put(sd);
169
	}
170 171 172
}
EXPORT_SYMBOL_GPL(sysfs_notify);

T
Tejun Heo 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static const struct kernfs_ops sysfs_file_kfops_empty = {
};

static const struct kernfs_ops sysfs_file_kfops_ro = {
	.seq_show	= sysfs_kf_seq_show,
};

static const struct kernfs_ops sysfs_file_kfops_wo = {
	.write		= sysfs_kf_write,
};

static const struct kernfs_ops sysfs_file_kfops_rw = {
	.seq_show	= sysfs_kf_seq_show,
	.write		= sysfs_kf_write,
};

static const struct kernfs_ops sysfs_bin_kfops_ro = {
	.read		= sysfs_kf_bin_read,
};

static const struct kernfs_ops sysfs_bin_kfops_wo = {
	.write		= sysfs_kf_bin_write,
};

static const struct kernfs_ops sysfs_bin_kfops_rw = {
	.read		= sysfs_kf_bin_read,
	.write		= sysfs_kf_bin_write,
	.mmap		= sysfs_kf_bin_mmap,
};

203
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
204
			   const struct attribute *attr, bool is_bin,
205
			   umode_t mode, const void *ns)
L
Linus Torvalds 已提交
206
{
207
	struct lock_class_key *key = NULL;
T
Tejun Heo 已提交
208
	const struct kernfs_ops *ops;
209
	struct sysfs_dirent *sd;
210
	loff_t size;
L
Linus Torvalds 已提交
211

212
	if (!is_bin) {
T
Tejun Heo 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		struct kobject *kobj = dir_sd->priv;
		const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;

		/* every kobject with an attribute needs a ktype assigned */
		if (WARN(!sysfs_ops, KERN_ERR
			 "missing sysfs attribute operations for kobject: %s\n",
			 kobject_name(kobj)))
			return -EINVAL;

		if (sysfs_ops->show && sysfs_ops->store)
			ops = &sysfs_file_kfops_rw;
		else if (sysfs_ops->show)
			ops = &sysfs_file_kfops_ro;
		else if (sysfs_ops->store)
			ops = &sysfs_file_kfops_wo;
		else
			ops = &sysfs_file_kfops_empty;
230 231

		size = PAGE_SIZE;
T
Tejun Heo 已提交
232 233 234 235 236 237 238 239 240 241 242
	} else {
		struct bin_attribute *battr = (void *)attr;

		if ((battr->read && battr->write) || battr->mmap)
			ops = &sysfs_bin_kfops_rw;
		else if (battr->read)
			ops = &sysfs_bin_kfops_ro;
		else if (battr->write)
			ops = &sysfs_bin_kfops_wo;
		else
			ops = &sysfs_file_kfops_empty;
243 244

		size = battr->size;
T
Tejun Heo 已提交
245 246
	}

247 248 249 250 251 252
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	if (!attr->ignore_lockdep)
		key = attr->key ?: (struct lock_class_key *)&attr->skey;
#endif
	sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
				       ops, (void *)attr, ns, key);
253 254 255 256 257 258 259 260
	if (IS_ERR(sd)) {
		if (PTR_ERR(sd) == -EEXIST)
			sysfs_warn_dup(dir_sd, attr->name);
		return PTR_ERR(sd);
	}
	return 0;
}

261
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
262
		   bool is_bin)
263
{
264
	return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
265 266
}

L
Linus Torvalds 已提交
267
/**
268 269 270 271
 * sysfs_create_file_ns - create an attribute file for an object with custom ns
 * @kobj: object we're creating for
 * @attr: attribute descriptor
 * @ns: namespace the new file should belong to
L
Linus Torvalds 已提交
272
 */
273 274
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
			 const void *ns)
L
Linus Torvalds 已提交
275
{
276
	BUG_ON(!kobj || !kobj->sd || !attr);
L
Linus Torvalds 已提交
277

278
	return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
L
Linus Torvalds 已提交
279 280

}
281
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
L
Linus Torvalds 已提交
282

283 284 285 286 287 288 289 290 291 292 293 294
int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
{
	int err = 0;
	int i;

	for (i = 0; ptr[i] && !err; i++)
		err = sysfs_create_file(kobj, ptr[i]);
	if (err)
		while (--i >= 0)
			sysfs_remove_file(kobj, ptr[i]);
	return err;
}
295
EXPORT_SYMBOL_GPL(sysfs_create_files);
L
Linus Torvalds 已提交
296

297 298 299 300 301 302 303 304 305
/**
 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
 * @kobj: object we're acting for.
 * @attr: attribute descriptor.
 * @group: group name.
 */
int sysfs_add_file_to_group(struct kobject *kobj,
		const struct attribute *attr, const char *group)
{
306
	struct sysfs_dirent *dir_sd;
307 308
	int error;

309 310 311 312 313 314
	if (group) {
		dir_sd = kernfs_find_and_get(kobj->sd, group);
	} else {
		dir_sd = kobj->sd;
		kernfs_get(dir_sd);
	}
315

316 317 318
	if (!dir_sd)
		return -ENOENT;

319
	error = sysfs_add_file(dir_sd, attr, false);
320
	kernfs_put(dir_sd);
321

322 323 324 325
	return error;
}
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);

326 327 328 329 330 331 332
/**
 * sysfs_chmod_file - update the modified mode value on an object attribute.
 * @kobj: object we're acting for.
 * @attr: attribute descriptor.
 * @mode: file permissions.
 *
 */
333
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
A
Al Viro 已提交
334
		     umode_t mode)
335
{
336
	struct sysfs_dirent *sd;
337
	struct iattr newattrs;
338 339
	int rc;

340
	sd = kernfs_find_and_get(kobj->sd, attr->name);
341
	if (!sd)
342
		return -ENOENT;
343

344
	newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
345
	newattrs.ia_valid = ATTR_MODE;
346

347 348
	rc = kernfs_setattr(sd, &newattrs);

349
	kernfs_put(sd);
350
	return rc;
351 352 353
}
EXPORT_SYMBOL_GPL(sysfs_chmod_file);

L
Linus Torvalds 已提交
354
/**
355 356 357 358
 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
 * @kobj: object we're acting for
 * @attr: attribute descriptor
 * @ns: namespace tag of the file to remove
L
Linus Torvalds 已提交
359
 *
360
 * Hash the attribute name and namespace tag and kill the victim.
L
Linus Torvalds 已提交
361
 */
362 363
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
			  const void *ns)
L
Linus Torvalds 已提交
364
{
365
	struct sysfs_dirent *dir_sd = kobj->sd;
366

367
	kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
L
Linus Torvalds 已提交
368
}
369
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
L
Linus Torvalds 已提交
370

371
void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
372 373 374 375 376
{
	int i;
	for (i = 0; ptr[i]; i++)
		sysfs_remove_file(kobj, ptr[i]);
}
377
EXPORT_SYMBOL_GPL(sysfs_remove_files);
L
Linus Torvalds 已提交
378

379 380 381 382 383 384 385 386 387
/**
 * sysfs_remove_file_from_group - remove an attribute file from a group.
 * @kobj: object we're acting for.
 * @attr: attribute descriptor.
 * @group: group name.
 */
void sysfs_remove_file_from_group(struct kobject *kobj,
		const struct attribute *attr, const char *group)
{
388
	struct sysfs_dirent *dir_sd;
389

390 391 392 393 394 395 396
	if (group) {
		dir_sd = kernfs_find_and_get(kobj->sd, group);
	} else {
		dir_sd = kobj->sd;
		kernfs_get(dir_sd);
	}

397
	if (dir_sd) {
398
		kernfs_remove_by_name(dir_sd, attr->name);
399
		kernfs_put(dir_sd);
400 401 402 403
	}
}
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);

404 405 406 407 408 409 410 411 412 413
/**
 *	sysfs_create_bin_file - create binary file for object.
 *	@kobj:	object.
 *	@attr:	attribute descriptor.
 */
int sysfs_create_bin_file(struct kobject *kobj,
			  const struct bin_attribute *attr)
{
	BUG_ON(!kobj || !kobj->sd || !attr);

414
	return sysfs_add_file(kobj->sd, &attr->attr, true);
415 416 417 418 419 420 421 422 423 424 425
}
EXPORT_SYMBOL_GPL(sysfs_create_bin_file);

/**
 *	sysfs_remove_bin_file - remove binary file for object.
 *	@kobj:	object.
 *	@attr:	attribute descriptor.
 */
void sysfs_remove_bin_file(struct kobject *kobj,
			   const struct bin_attribute *attr)
{
426
	kernfs_remove_by_name(kobj->sd, attr->attr.name);
427 428 429
}
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);

430
struct sysfs_schedule_callback_struct {
431 432
	struct list_head	workq_list;
	struct kobject		*kobj;
433 434
	void			(*func)(void *);
	void			*data;
435
	struct module		*owner;
436 437 438
	struct work_struct	work;
};

439
static struct workqueue_struct *sysfs_workqueue;
440 441
static DEFINE_MUTEX(sysfs_workq_mutex);
static LIST_HEAD(sysfs_workq);
442 443 444 445 446 447 448
static void sysfs_schedule_callback_work(struct work_struct *work)
{
	struct sysfs_schedule_callback_struct *ss = container_of(work,
			struct sysfs_schedule_callback_struct, work);

	(ss->func)(ss->data);
	kobject_put(ss->kobj);
449
	module_put(ss->owner);
450 451 452
	mutex_lock(&sysfs_workq_mutex);
	list_del(&ss->workq_list);
	mutex_unlock(&sysfs_workq_mutex);
453 454 455 456 457 458 459 460
	kfree(ss);
}

/**
 * sysfs_schedule_callback - helper to schedule a callback for a kobject
 * @kobj: object we're acting for.
 * @func: callback function to invoke later.
 * @data: argument to pass to @func.
461
 * @owner: module owning the callback code
462 463 464 465 466 467 468 469 470 471 472 473
 *
 * sysfs attribute methods must not unregister themselves or their parent
 * kobject (which would amount to the same thing).  Attempts to do so will
 * deadlock, since unregistration is mutually exclusive with driver
 * callbacks.
 *
 * Instead methods can call this routine, which will attempt to allocate
 * and schedule a workqueue request to call back @func with @data as its
 * argument in the workqueue's process context.  @kobj will be pinned
 * until @func returns.
 *
 * Returns 0 if the request was submitted, -ENOMEM if storage could not
474 475
 * be allocated, -ENODEV if a reference to @owner isn't available,
 * -EAGAIN if a callback has already been scheduled for @kobj.
476 477
 */
int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
478
		void *data, struct module *owner)
479
{
480
	struct sysfs_schedule_callback_struct *ss, *tmp;
481

482 483
	if (!try_module_get(owner))
		return -ENODEV;
484 485 486 487

	mutex_lock(&sysfs_workq_mutex);
	list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
		if (ss->kobj == kobj) {
488
			module_put(owner);
489 490 491 492 493
			mutex_unlock(&sysfs_workq_mutex);
			return -EAGAIN;
		}
	mutex_unlock(&sysfs_workq_mutex);

494
	if (sysfs_workqueue == NULL) {
495
		sysfs_workqueue = create_singlethread_workqueue("sysfsd");
496 497 498 499 500 501
		if (sysfs_workqueue == NULL) {
			module_put(owner);
			return -ENOMEM;
		}
	}

502
	ss = kmalloc(sizeof(*ss), GFP_KERNEL);
503 504
	if (!ss) {
		module_put(owner);
505
		return -ENOMEM;
506
	}
507 508 509 510
	kobject_get(kobj);
	ss->kobj = kobj;
	ss->func = func;
	ss->data = data;
511
	ss->owner = owner;
512
	INIT_WORK(&ss->work, sysfs_schedule_callback_work);
513 514 515 516
	INIT_LIST_HEAD(&ss->workq_list);
	mutex_lock(&sysfs_workq_mutex);
	list_add_tail(&ss->workq_list, &sysfs_workq);
	mutex_unlock(&sysfs_workq_mutex);
517
	queue_work(sysfs_workqueue, &ss->work);
518 519 520
	return 0;
}
EXPORT_SYMBOL_GPL(sysfs_schedule_callback);