file.c 11.8 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
 * Determine ktype->sysfs_ops for the given kernfs_node.  This function
T
Tejun Heo 已提交
26 27
 * must be called while holding an active reference.
 */
28
static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
T
Tejun Heo 已提交
29
{
30
	struct kobject *kobj = kn->parent->priv;
T
Tejun Heo 已提交
31

T
Tejun Heo 已提交
32
	if (kn->flags & KERNFS_LOCKDEP)
33
		lockdep_assert_held(kn);
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 kernfs_open_file *of = sf->private;
45
	struct kobject *kobj = of->kn->parent->priv;
46
	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
L
Linus Torvalds 已提交
47
	ssize_t count;
48
	char *buf;
L
Linus Torvalds 已提交
49

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

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

68 69 70 71
	/*
	 * The code works fine with PAGE_SIZE return but it's likely to
	 * indicate truncated result or overflow in normal use cases.
	 */
72 73 74 75 76 77
	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;
	}
78 79
	seq_commit(sf, count);
	return 0;
L
Linus Torvalds 已提交
80 81
}

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

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

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

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

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

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* kernfs read callback for regular sysfs files with pre-alloc */
static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
			     size_t count, loff_t pos)
{
	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
	struct kobject *kobj = of->kn->parent->priv;

	/*
	 * If buf != of->prealloc_buf, we don't know how
	 * large it is, so cannot safely pass it to ->show
	 */
	if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
		return 0;
	return ops->show(kobj, of->kn->priv, buf);
}

121
/* kernfs write callback for regular sysfs files */
122
static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
123
			      size_t count, loff_t pos)
L
Linus Torvalds 已提交
124
{
125
	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
126
	struct kobject *kobj = of->kn->parent->priv;
127

128 129
	if (!count)
		return 0;
130

131
	return ops->store(kobj, of->kn->priv, buf, count);
132
}
133

134
/* kernfs write callback for bin sysfs files */
135
static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
136 137
				  size_t count, loff_t pos)
{
138
	struct bin_attribute *battr = of->kn->priv;
139
	struct kobject *kobj = of->kn->parent->priv;
140
	loff_t size = file_inode(of->file)->i_size;
141

142 143
	if (size) {
		if (size <= pos)
144
			return -EFBIG;
145
		count = min_t(ssize_t, count, size - pos);
146
	}
147 148
	if (!count)
		return 0;
149

150 151
	if (!battr->write)
		return -EIO;
L
Linus Torvalds 已提交
152

153
	return battr->write(of->file, kobj, battr, buf, pos, count);
L
Linus Torvalds 已提交
154 155
}

156
static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
157 158
			     struct vm_area_struct *vma)
{
159
	struct bin_attribute *battr = of->kn->priv;
160
	struct kobject *kobj = of->kn->parent->priv;
161 162 163 164

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

165
void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
166
{
167
	struct kernfs_node *kn = kobj->sd, *tmp;
168

169 170
	if (kn && dir)
		kn = kernfs_find_and_get(kn, dir);
171
	else
172
		kernfs_get(kn);
173

174 175 176 177
	if (kn && attr) {
		tmp = kernfs_find_and_get(kn, attr);
		kernfs_put(kn);
		kn = tmp;
178
	}
179

180 181 182
	if (kn) {
		kernfs_notify(kn);
		kernfs_put(kn);
183
	}
184 185 186
}
EXPORT_SYMBOL_GPL(sysfs_notify);

T
Tejun Heo 已提交
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,
};

203 204 205 206 207
static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
	.read		= sysfs_kf_read,
	.prealloc	= true,
};

208 209 210 211 212 213
static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
	.write		= sysfs_kf_write,
	.prealloc	= true,
};

static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
214
	.read		= sysfs_kf_read,
215 216 217 218
	.write		= sysfs_kf_write,
	.prealloc	= true,
};

T
Tejun Heo 已提交
219 220 221 222 223 224 225 226 227 228 229
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,
230 231 232 233 234
};

static const struct kernfs_ops sysfs_bin_kfops_mmap = {
	.read		= sysfs_kf_bin_read,
	.write		= sysfs_kf_bin_write,
T
Tejun Heo 已提交
235 236 237
	.mmap		= sysfs_kf_bin_mmap,
};

238
int sysfs_add_file_mode_ns(struct kernfs_node *parent,
239
			   const struct attribute *attr, bool is_bin,
240
			   umode_t mode, const void *ns)
L
Linus Torvalds 已提交
241
{
242
	struct lock_class_key *key = NULL;
T
Tejun Heo 已提交
243
	const struct kernfs_ops *ops;
244
	struct kernfs_node *kn;
245
	loff_t size;
L
Linus Torvalds 已提交
246

247
	if (!is_bin) {
248
		struct kobject *kobj = parent->priv;
T
Tejun Heo 已提交
249 250 251 252 253 254 255 256
		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;

257 258 259 260 261
		if (sysfs_ops->show && sysfs_ops->store) {
			if (mode & SYSFS_PREALLOC)
				ops = &sysfs_prealloc_kfops_rw;
			else
				ops = &sysfs_file_kfops_rw;
262 263 264 265 266 267
		} else if (sysfs_ops->show) {
			if (mode & SYSFS_PREALLOC)
				ops = &sysfs_prealloc_kfops_ro;
			else
				ops = &sysfs_file_kfops_ro;
		} else if (sysfs_ops->store) {
268 269 270 271 272
			if (mode & SYSFS_PREALLOC)
				ops = &sysfs_prealloc_kfops_wo;
			else
				ops = &sysfs_file_kfops_wo;
		} else
T
Tejun Heo 已提交
273
			ops = &sysfs_file_kfops_empty;
274 275

		size = PAGE_SIZE;
T
Tejun Heo 已提交
276 277 278
	} else {
		struct bin_attribute *battr = (void *)attr;

279 280 281
		if (battr->mmap)
			ops = &sysfs_bin_kfops_mmap;
		else if (battr->read && battr->write)
T
Tejun Heo 已提交
282 283 284 285 286 287 288
			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;
289 290

		size = battr->size;
T
Tejun Heo 已提交
291 292
	}

293 294 295 296
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	if (!attr->ignore_lockdep)
		key = attr->key ?: (struct lock_class_key *)&attr->skey;
#endif
297
	kn = __kernfs_create_file(parent, attr->name, mode & 0777, size, ops,
T
Tejun Heo 已提交
298
				  (void *)attr, ns, key);
299 300 301 302
	if (IS_ERR(kn)) {
		if (PTR_ERR(kn) == -EEXIST)
			sysfs_warn_dup(parent, attr->name);
		return PTR_ERR(kn);
303 304 305 306
	}
	return 0;
}

307
int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
308
		   bool is_bin)
309
{
310
	return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
311 312
}

L
Linus Torvalds 已提交
313
/**
314 315 316 317
 * 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 已提交
318
 */
319 320
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
			 const void *ns)
L
Linus Torvalds 已提交
321
{
322
	BUG_ON(!kobj || !kobj->sd || !attr);
L
Linus Torvalds 已提交
323

324
	return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
L
Linus Torvalds 已提交
325 326

}
327
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
L
Linus Torvalds 已提交
328

329 330 331 332 333 334 335 336 337 338 339 340
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;
}
341
EXPORT_SYMBOL_GPL(sysfs_create_files);
L
Linus Torvalds 已提交
342

343 344 345 346 347 348 349 350 351
/**
 * 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)
{
352
	struct kernfs_node *parent;
353 354
	int error;

355
	if (group) {
356
		parent = kernfs_find_and_get(kobj->sd, group);
357
	} else {
358 359
		parent = kobj->sd;
		kernfs_get(parent);
360
	}
361

362
	if (!parent)
363 364
		return -ENOENT;

365 366
	error = sysfs_add_file(parent, attr, false);
	kernfs_put(parent);
367

368 369 370 371
	return error;
}
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);

372 373 374 375 376 377 378
/**
 * sysfs_chmod_file - update the modified mode value on an object attribute.
 * @kobj: object we're acting for.
 * @attr: attribute descriptor.
 * @mode: file permissions.
 *
 */
379
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
A
Al Viro 已提交
380
		     umode_t mode)
381
{
382
	struct kernfs_node *kn;
383
	struct iattr newattrs;
384 385
	int rc;

386 387
	kn = kernfs_find_and_get(kobj->sd, attr->name);
	if (!kn)
388
		return -ENOENT;
389

390
	newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
391
	newattrs.ia_valid = ATTR_MODE;
392

393
	rc = kernfs_setattr(kn, &newattrs);
394

395
	kernfs_put(kn);
396
	return rc;
397 398 399
}
EXPORT_SYMBOL_GPL(sysfs_chmod_file);

L
Linus Torvalds 已提交
400
/**
401 402 403 404
 * 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 已提交
405
 *
406
 * Hash the attribute name and namespace tag and kill the victim.
L
Linus Torvalds 已提交
407
 */
408 409
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
			  const void *ns)
L
Linus Torvalds 已提交
410
{
411
	struct kernfs_node *parent = kobj->sd;
412

413
	kernfs_remove_by_name_ns(parent, attr->name, ns);
L
Linus Torvalds 已提交
414
}
415
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
L
Linus Torvalds 已提交
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/**
 * sysfs_remove_file_self - remove an object attribute from its own method
 * @kobj: object we're acting for
 * @attr: attribute descriptor
 *
 * See kernfs_remove_self() for details.
 */
bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
{
	struct kernfs_node *parent = kobj->sd;
	struct kernfs_node *kn;
	bool ret;

	kn = kernfs_find_and_get(parent, attr->name);
	if (WARN_ON_ONCE(!kn))
		return false;

	ret = kernfs_remove_self(kn);

	kernfs_put(kn);
	return ret;
}

440
void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
441 442 443 444 445
{
	int i;
	for (i = 0; ptr[i]; i++)
		sysfs_remove_file(kobj, ptr[i]);
}
446
EXPORT_SYMBOL_GPL(sysfs_remove_files);
L
Linus Torvalds 已提交
447

448 449 450 451 452 453 454 455 456
/**
 * 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)
{
457
	struct kernfs_node *parent;
458

459
	if (group) {
460
		parent = kernfs_find_and_get(kobj->sd, group);
461
	} else {
462 463
		parent = kobj->sd;
		kernfs_get(parent);
464 465
	}

466 467 468
	if (parent) {
		kernfs_remove_by_name(parent, attr->name);
		kernfs_put(parent);
469 470 471 472
	}
}
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);

473 474 475 476 477 478 479 480 481 482
/**
 *	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);

483
	return sysfs_add_file(kobj->sd, &attr->attr, true);
484 485 486 487 488 489 490 491 492 493 494
}
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)
{
495
	kernfs_remove_by_name(kobj->sd, attr->attr.name);
496 497
}
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);