Blame view
fs/sysfs/file.c
19.3 KB
1da177e4c
|
1 |
/* |
6d66f5cd2
|
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. |
1da177e4c
|
11 12 13 |
*/ #include <linux/module.h> |
1da177e4c
|
14 |
#include <linux/kobject.h> |
815d2d50d
|
15 |
#include <linux/kallsyms.h> |
c6f877338
|
16 |
#include <linux/slab.h> |
93265d13e
|
17 |
#include <linux/fsnotify.h> |
5f45f1a78
|
18 |
#include <linux/namei.h> |
4508a7a73
|
19 |
#include <linux/poll.h> |
94bebf4d1
|
20 |
#include <linux/list.h> |
52e8c209d
|
21 |
#include <linux/mutex.h> |
ae87221d3
|
22 |
#include <linux/limits.h> |
1da177e4c
|
23 |
#include <asm/uaccess.h> |
1da177e4c
|
24 25 |
#include "sysfs.h" |
ae87221d3
|
26 27 28 29 30 31 32 |
/* used in crash dumps to help with debugging */ static char last_sysfs_file[PATH_MAX]; void sysfs_printk_last_file(void) { printk(KERN_EMERG "last sysfs file: %s ", last_sysfs_file); } |
85a4ffad3
|
33 34 35 36 37 38 39 40 41 |
/* * There's one sysfs_buffer for each open file and one * sysfs_open_dirent for each sysfs_dirent with one or more open * files. * * filp->private_data points to sysfs_buffer and * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open * is protected by sysfs_open_dirent_lock. */ |
d7b378896
|
42 |
static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
85a4ffad3
|
43 44 45 |
struct sysfs_open_dirent { atomic_t refcnt; |
a4e8b9125
|
46 47 |
atomic_t event; wait_queue_head_t poll; |
85a4ffad3
|
48 49 |
struct list_head buffers; /* goes through sysfs_buffer.list */ }; |
73107cb3a
|
50 51 52 53 |
struct sysfs_buffer { size_t count; loff_t pos; char * page; |
52cf25d0a
|
54 |
const struct sysfs_ops * ops; |
52e8c209d
|
55 |
struct mutex mutex; |
73107cb3a
|
56 57 |
int needs_read_fill; int event; |
85a4ffad3
|
58 |
struct list_head list; |
73107cb3a
|
59 |
}; |
1da177e4c
|
60 61 62 63 64 65 66 67 68 |
/** * fill_read_buffer - allocate and fill buffer from object. * @dentry: dentry pointer. * @buffer: data buffer for file. * * Allocate @buffer->page, if it hasn't been already, then call the * kobject's show() method to fill the buffer with this attribute's * data. |
82244b169
|
69 70 |
* This is called only once, on the file's first read unless an error * is returned. |
1da177e4c
|
71 72 73 |
*/ static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer) { |
0ab66088c
|
74 |
struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d614
|
75 |
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
52cf25d0a
|
76 |
const struct sysfs_ops * ops = buffer->ops; |
1da177e4c
|
77 78 79 80 81 82 83 |
int ret = 0; ssize_t count; if (!buffer->page) buffer->page = (char *) get_zeroed_page(GFP_KERNEL); if (!buffer->page) return -ENOMEM; |
0ab66088c
|
84 |
/* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8cc
|
85 |
if (!sysfs_get_active(attr_sd)) |
0ab66088c
|
86 |
return -ENODEV; |
a4e8b9125
|
87 |
buffer->event = atomic_read(&attr_sd->s_attr.open->event); |
b1fc3d614
|
88 |
count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page); |
0ab66088c
|
89 |
|
e72ceb8cc
|
90 |
sysfs_put_active(attr_sd); |
0ab66088c
|
91 |
|
8118a859d
|
92 93 94 95 |
/* * The code works fine with PAGE_SIZE return but it's likely to * indicate truncated result or overflow in normal use cases. */ |
815d2d50d
|
96 97 98 99 100 101 102 |
if (count >= (ssize_t)PAGE_SIZE) { print_symbol("fill_read_buffer: %s returned bad count ", (unsigned long)ops->show); /* Try to struggle along */ count = PAGE_SIZE - 1; } |
82244b169
|
103 104 |
if (count >= 0) { buffer->needs_read_fill = 0; |
1da177e4c
|
105 |
buffer->count = count; |
82244b169
|
106 |
} else { |
1da177e4c
|
107 |
ret = count; |
82244b169
|
108 |
} |
1da177e4c
|
109 110 |
return ret; } |
1da177e4c
|
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/** * sysfs_read_file - read an attribute. * @file: file pointer. * @buf: buffer to fill. * @count: number of bytes to read. * @ppos: starting offset in file. * * Userspace wants to read an attribute file. The attribute descriptor * is in the file's ->d_fsdata. The target object is in the directory's * ->d_fsdata. * * We call fill_read_buffer() to allocate and fill the buffer from the * object's show() method exactly once (if the read is happening from * the beginning of the file). That should fill the entire buffer with * all the data the object has to offer for that attribute. * We then call flush_read_buffer() to copy the buffer to userspace * in the increments specified. */ static ssize_t sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct sysfs_buffer * buffer = file->private_data; ssize_t retval = 0; |
52e8c209d
|
135 |
mutex_lock(&buffer->mutex); |
2424b5dd0
|
136 |
if (buffer->needs_read_fill || *ppos == 0) { |
73107cb3a
|
137 |
retval = fill_read_buffer(file->f_path.dentry,buffer); |
e7b0d26a8
|
138 |
if (retval) |
1da177e4c
|
139 140 |
goto out; } |
5c1fdf415
|
141 142 |
pr_debug("%s: count = %zd, ppos = %lld, buf = %s ", |
8e24eea72
|
143 |
__func__, count, *ppos, buffer->page); |
92f4c701a
|
144 145 |
retval = simple_read_from_buffer(buf, count, ppos, buffer->page, buffer->count); |
1da177e4c
|
146 |
out: |
52e8c209d
|
147 |
mutex_unlock(&buffer->mutex); |
1da177e4c
|
148 149 |
return retval; } |
1da177e4c
|
150 151 152 |
/** * fill_write_buffer - copy buffer from userspace. * @buffer: data buffer for file. |
67be2dd1b
|
153 |
* @buf: data from user. |
1da177e4c
|
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
* @count: number of bytes in @userbuf. * * Allocate @buffer->page if it hasn't been already, then * copy the user-supplied buffer into it. */ static int fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) { int error; if (!buffer->page) buffer->page = (char *)get_zeroed_page(GFP_KERNEL); if (!buffer->page) return -ENOMEM; if (count >= PAGE_SIZE) |
6e0dd741a
|
171 |
count = PAGE_SIZE - 1; |
1da177e4c
|
172 173 |
error = copy_from_user(buffer->page,buf,count); buffer->needs_read_fill = 1; |
035ed7a49
|
174 175 176 |
/* if buf is assumed to contain a string, terminate it by \0, so e.g. sscanf() can scan the string easily */ buffer->page[count] = 0; |
1da177e4c
|
177 178 179 180 181 182 |
return error ? -EFAULT : count; } /** * flush_write_buffer - push buffer to kobject. |
3d41088fa
|
183 |
* @dentry: dentry to the attribute |
1da177e4c
|
184 |
* @buffer: data buffer for file. |
3d41088fa
|
185 |
* @count: number of bytes |
1da177e4c
|
186 187 188 189 190 |
* * Get the correct pointers for the kobject and the attribute we're * dealing with, then call the store() method for the attribute, * passing the buffer that we acquired in fill_write_buffer(). */ |
0ab66088c
|
191 |
static int |
1da177e4c
|
192 193 |
flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count) { |
3e5190380
|
194 |
struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d614
|
195 |
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
52cf25d0a
|
196 |
const struct sysfs_ops * ops = buffer->ops; |
0ab66088c
|
197 198 199 |
int rc; /* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8cc
|
200 |
if (!sysfs_get_active(attr_sd)) |
0ab66088c
|
201 |
return -ENODEV; |
b1fc3d614
|
202 |
rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count); |
0ab66088c
|
203 |
|
e72ceb8cc
|
204 |
sysfs_put_active(attr_sd); |
1da177e4c
|
205 |
|
0ab66088c
|
206 |
return rc; |
1da177e4c
|
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
} /** * sysfs_write_file - write an attribute. * @file: file pointer * @buf: data to write * @count: number of bytes * @ppos: starting offset * * Similar to sysfs_read_file(), though working in the opposite direction. * We allocate and fill the data from the user in fill_write_buffer(), * then push it to the kobject in flush_write_buffer(). * There is no easy way for us to know if userspace is only doing a partial * write, so we don't support them. We expect the entire buffer to come * on the first write. * Hint: if you're writing a value, first read the file, modify only the * the value you're changing, then write entire buffer back. */ static ssize_t sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct sysfs_buffer * buffer = file->private_data; ssize_t len; |
52e8c209d
|
232 |
mutex_lock(&buffer->mutex); |
1da177e4c
|
233 234 |
len = fill_write_buffer(buffer, buf, count); if (len > 0) |
f427f5d5d
|
235 |
len = flush_write_buffer(file->f_path.dentry, buffer, len); |
1da177e4c
|
236 237 |
if (len > 0) *ppos += len; |
52e8c209d
|
238 |
mutex_unlock(&buffer->mutex); |
1da177e4c
|
239 240 |
return len; } |
85a4ffad3
|
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
/** * sysfs_get_open_dirent - get or create sysfs_open_dirent * @sd: target sysfs_dirent * @buffer: sysfs_buffer for this instance of open * * If @sd->s_attr.open exists, increment its reference count; * otherwise, create one. @buffer is chained to the buffers * list. * * LOCKING: * Kernel thread context (may sleep). * * RETURNS: * 0 on success, -errno on failure. */ static int sysfs_get_open_dirent(struct sysfs_dirent *sd, struct sysfs_buffer *buffer) { struct sysfs_open_dirent *od, *new_od = NULL; retry: |
83db93f4d
|
262 |
spin_lock_irq(&sysfs_open_dirent_lock); |
85a4ffad3
|
263 264 265 266 267 268 269 270 271 272 273 |
if (!sd->s_attr.open && new_od) { sd->s_attr.open = new_od; new_od = NULL; } od = sd->s_attr.open; if (od) { atomic_inc(&od->refcnt); list_add_tail(&buffer->list, &od->buffers); } |
83db93f4d
|
274 |
spin_unlock_irq(&sysfs_open_dirent_lock); |
85a4ffad3
|
275 276 277 278 279 280 281 282 283 284 285 286 |
if (od) { kfree(new_od); return 0; } /* not there, initialize a new one and retry */ new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); if (!new_od) return -ENOMEM; atomic_set(&new_od->refcnt, 0); |
a4e8b9125
|
287 288 |
atomic_set(&new_od->event, 1); init_waitqueue_head(&new_od->poll); |
85a4ffad3
|
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
INIT_LIST_HEAD(&new_od->buffers); goto retry; } /** * sysfs_put_open_dirent - put sysfs_open_dirent * @sd: target sysfs_dirent * @buffer: associated sysfs_buffer * * Put @sd->s_attr.open and unlink @buffer from the buffers list. * If reference count reaches zero, disassociate and free it. * * LOCKING: * None. */ static void sysfs_put_open_dirent(struct sysfs_dirent *sd, struct sysfs_buffer *buffer) { struct sysfs_open_dirent *od = sd->s_attr.open; |
83db93f4d
|
308 |
unsigned long flags; |
85a4ffad3
|
309 |
|
83db93f4d
|
310 |
spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
85a4ffad3
|
311 312 313 314 315 316 |
list_del(&buffer->list); if (atomic_dec_and_test(&od->refcnt)) sd->s_attr.open = NULL; else od = NULL; |
83db93f4d
|
317 |
spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
85a4ffad3
|
318 319 320 |
kfree(od); } |
94bebf4d1
|
321 |
static int sysfs_open_file(struct inode *inode, struct file *file) |
1da177e4c
|
322 |
{ |
3e5190380
|
323 |
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d614
|
324 |
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
000f2a4d8
|
325 |
struct sysfs_buffer *buffer; |
52cf25d0a
|
326 |
const struct sysfs_ops *ops; |
000f2a4d8
|
327 |
int error = -EACCES; |
ae87221d3
|
328 329 330 |
char *p; p = d_path(&file->f_path, last_sysfs_file, sizeof(last_sysfs_file)); |
57f9bdac2
|
331 |
if (!IS_ERR(p)) |
ae87221d3
|
332 |
memmove(last_sysfs_file, p, strlen(p) + 1); |
1da177e4c
|
333 |
|
0ab66088c
|
334 |
/* need attr_sd for attr and ops, its parent for kobj */ |
e72ceb8cc
|
335 |
if (!sysfs_get_active(attr_sd)) |
0ab66088c
|
336 |
return -ENODEV; |
1da177e4c
|
337 |
|
000f2a4d8
|
338 339 |
/* every kobject with an attribute needs a ktype assigned */ if (kobj->ktype && kobj->ktype->sysfs_ops) |
1da177e4c
|
340 |
ops = kobj->ktype->sysfs_ops; |
000f2a4d8
|
341 |
else { |
99fcd77d1
|
342 |
WARN(1, KERN_ERR "missing sysfs attribute operations for " |
000f2a4d8
|
343 344 |
"kobject: %s ", kobject_name(kobj)); |
7b595756e
|
345 |
goto err_out; |
000f2a4d8
|
346 |
} |
1da177e4c
|
347 348 349 350 351 352 |
/* File needs write support. * The inode's perms must say it's ok, * and we must have a store method. */ if (file->f_mode & FMODE_WRITE) { |
1da177e4c
|
353 |
if (!(inode->i_mode & S_IWUGO) || !ops->store) |
7b595756e
|
354 |
goto err_out; |
1da177e4c
|
355 356 357 358 359 360 361 362 |
} /* File needs read support. * The inode's perms must say it's ok, and we there * must be a show method for it. */ if (file->f_mode & FMODE_READ) { if (!(inode->i_mode & S_IRUGO) || !ops->show) |
7b595756e
|
363 |
goto err_out; |
1da177e4c
|
364 365 366 367 368 |
} /* No error? Great, allocate a buffer for the file, and store it * it in file->private_data for easy access. */ |
0ab66088c
|
369 |
error = -ENOMEM; |
58d49283b
|
370 |
buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
0ab66088c
|
371 |
if (!buffer) |
7b595756e
|
372 |
goto err_out; |
1da177e4c
|
373 |
|
52e8c209d
|
374 |
mutex_init(&buffer->mutex); |
0ab66088c
|
375 376 |
buffer->needs_read_fill = 1; buffer->ops = ops; |
0ab66088c
|
377 |
file->private_data = buffer; |
85a4ffad3
|
378 379 380 381 |
/* make sure we have open dirent struct */ error = sysfs_get_open_dirent(attr_sd, buffer); if (error) goto err_free; |
b05f0548d
|
382 |
/* open succeeded, put active references */ |
e72ceb8cc
|
383 |
sysfs_put_active(attr_sd); |
0ab66088c
|
384 |
return 0; |
85a4ffad3
|
385 386 |
err_free: kfree(buffer); |
7b595756e
|
387 |
err_out: |
e72ceb8cc
|
388 |
sysfs_put_active(attr_sd); |
1da177e4c
|
389 390 |
return error; } |
85a4ffad3
|
391 |
static int sysfs_release(struct inode *inode, struct file *filp) |
1da177e4c
|
392 |
{ |
85a4ffad3
|
393 |
struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
73107cb3a
|
394 |
struct sysfs_buffer *buffer = filp->private_data; |
1da177e4c
|
395 |
|
85a4ffad3
|
396 |
sysfs_put_open_dirent(sd, buffer); |
50ab1a728
|
397 398 399 |
if (buffer->page) free_page((unsigned long)buffer->page); kfree(buffer); |
1da177e4c
|
400 401 |
return 0; } |
4508a7a73
|
402 403 404 405 406 407 408 |
/* Sysfs attribute files are pollable. The idea is that you read * the content and then you use 'poll' or 'select' to wait for * the content to change. When the content changes (assuming the * manager for the kobject supports notification), poll will * return POLLERR|POLLPRI, and select will return the fd whether * it is waiting for read, write, or exceptions. * Once poll/select indicates that the value has changed, you |
2424b5dd0
|
409 |
* need to close and re-open the file, or seek to 0 and read again. |
4508a7a73
|
410 411 |
* Reminder: this only works for attributes which actively support * it, and it is not possible to test an attribute from userspace |
a93720eeb
|
412 |
* to see if it supports poll (Neither 'poll' nor 'select' return |
4508a7a73
|
413 414 415 416 417 |
* an appropriate error code). When in doubt, set a suitable timeout value. */ static unsigned int sysfs_poll(struct file *filp, poll_table *wait) { struct sysfs_buffer * buffer = filp->private_data; |
0ab66088c
|
418 |
struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
a4e8b9125
|
419 |
struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
0ab66088c
|
420 421 |
/* need parent for the kobj, grab both */ |
e72ceb8cc
|
422 |
if (!sysfs_get_active(attr_sd)) |
0ab66088c
|
423 |
goto trigger; |
4508a7a73
|
424 |
|
a4e8b9125
|
425 |
poll_wait(filp, &od->poll, wait); |
4508a7a73
|
426 |
|
e72ceb8cc
|
427 |
sysfs_put_active(attr_sd); |
0ab66088c
|
428 |
|
a4e8b9125
|
429 |
if (buffer->event != atomic_read(&od->event)) |
0ab66088c
|
430 |
goto trigger; |
4508a7a73
|
431 |
|
1af3557ab
|
432 |
return DEFAULT_POLLMASK; |
0ab66088c
|
433 434 435 |
trigger: buffer->needs_read_fill = 1; |
1af3557ab
|
436 |
return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
4508a7a73
|
437 |
} |
f1282c844
|
438 439 440 |
void sysfs_notify_dirent(struct sysfs_dirent *sd) { struct sysfs_open_dirent *od; |
83db93f4d
|
441 |
unsigned long flags; |
f1282c844
|
442 |
|
83db93f4d
|
443 |
spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
f1282c844
|
444 445 446 447 448 449 |
od = sd->s_attr.open; if (od) { atomic_inc(&od->event); wake_up_interruptible(&od->poll); } |
83db93f4d
|
450 |
spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
f1282c844
|
451 452 |
} EXPORT_SYMBOL_GPL(sysfs_notify_dirent); |
8c0e3998f
|
453 |
void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
4508a7a73
|
454 |
{ |
51225039f
|
455 |
struct sysfs_dirent *sd = k->sd; |
4508a7a73
|
456 |
|
51225039f
|
457 458 459 |
mutex_lock(&sysfs_mutex); if (sd && dir) |
3ff195b01
|
460 461 462 463 |
/* Only directories are tagged, so no need to pass * a tag explicitly. */ sd = sysfs_find_dirent(sd, NULL, dir); |
51225039f
|
464 |
if (sd && attr) |
3ff195b01
|
465 |
sd = sysfs_find_dirent(sd, NULL, attr); |
f1282c844
|
466 467 |
if (sd) sysfs_notify_dirent(sd); |
51225039f
|
468 469 |
mutex_unlock(&sysfs_mutex); |
4508a7a73
|
470 471 |
} EXPORT_SYMBOL_GPL(sysfs_notify); |
4b6f5d20b
|
472 |
const struct file_operations sysfs_file_operations = { |
1da177e4c
|
473 474 475 476 477 |
.read = sysfs_read_file, .write = sysfs_write_file, .llseek = generic_file_llseek, .open = sysfs_open_file, .release = sysfs_release, |
4508a7a73
|
478 |
.poll = sysfs_poll, |
1da177e4c
|
479 |
}; |
0f4238958
|
480 481 |
int sysfs_add_file_mode(struct sysfs_dirent *dir_sd, const struct attribute *attr, int type, mode_t amode) |
1da177e4c
|
482 |
{ |
0f4238958
|
483 |
umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
fb6896da3
|
484 |
struct sysfs_addrm_cxt acxt; |
a26cd7226
|
485 |
struct sysfs_dirent *sd; |
23dc27995
|
486 |
int rc; |
1da177e4c
|
487 |
|
3007e997d
|
488 489 490 |
sd = sysfs_new_dirent(attr->name, mode, type); if (!sd) return -ENOMEM; |
b1fc3d614
|
491 |
sd->s_attr.attr = (void *)attr; |
a2db68428
|
492 |
sysfs_dirent_init_lockdep(sd); |
1da177e4c
|
493 |
|
fb6896da3
|
494 |
sysfs_addrm_start(&acxt, dir_sd); |
23dc27995
|
495 496 |
rc = sysfs_add_one(&acxt, sd); sysfs_addrm_finish(&acxt); |
a26cd7226
|
497 |
|
23dc27995
|
498 |
if (rc) |
967e35dcc
|
499 |
sysfs_put(sd); |
3007e997d
|
500 |
|
23dc27995
|
501 |
return rc; |
1da177e4c
|
502 |
} |
0f4238958
|
503 504 505 506 507 |
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, int type) { return sysfs_add_file_mode(dir_sd, attr, type, attr->mode); } |
1da177e4c
|
508 509 510 |
/** * sysfs_create_file - create an attribute file for an object. * @kobj: object we're creating for. |
3932bf605
|
511 |
* @attr: attribute descriptor. |
1da177e4c
|
512 513 514 515 |
*/ int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) { |
608e266a2
|
516 |
BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4c
|
517 |
|
608e266a2
|
518 |
return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR); |
1da177e4c
|
519 520 |
} |
1c205ae18
|
521 522 523 524 525 526 527 528 529 530 531 532 |
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; } |
1da177e4c
|
533 534 |
/** |
dfa87c824
|
535 536 537 538 539 540 541 542 |
* 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) { |
608e266a2
|
543 |
struct sysfs_dirent *dir_sd; |
dfa87c824
|
544 |
int error; |
11f24fbdf
|
545 |
if (group) |
3ff195b01
|
546 |
dir_sd = sysfs_get_dirent(kobj->sd, NULL, group); |
11f24fbdf
|
547 548 |
else dir_sd = sysfs_get(kobj->sd); |
608e266a2
|
549 550 551 552 553 |
if (!dir_sd) return -ENOENT; error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); sysfs_put(dir_sd); |
dfa87c824
|
554 555 556 |
return error; } EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
1da177e4c
|
557 |
/** |
31e5abe9a
|
558 559 560 561 562 563 |
* sysfs_chmod_file - update the modified mode value on an object attribute. * @kobj: object we're acting for. * @attr: attribute descriptor. * @mode: file permissions. * */ |
49c19400f
|
564 565 |
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, mode_t mode) |
31e5abe9a
|
566 |
{ |
06fc0d66f
|
567 |
struct sysfs_dirent *sd; |
bc062b1b5
|
568 |
struct iattr newattrs; |
51225039f
|
569 |
int rc; |
06fc0d66f
|
570 |
mutex_lock(&sysfs_mutex); |
51225039f
|
571 |
|
06fc0d66f
|
572 |
rc = -ENOENT; |
3ff195b01
|
573 |
sd = sysfs_find_dirent(kobj->sd, NULL, attr->name); |
06fc0d66f
|
574 |
if (!sd) |
51225039f
|
575 |
goto out; |
f88123eaf
|
576 |
|
06fc0d66f
|
577 |
newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
4c6974f51
|
578 |
newattrs.ia_valid = ATTR_MODE; |
06fc0d66f
|
579 |
rc = sysfs_sd_setattr(sd, &newattrs); |
f88123eaf
|
580 |
|
51225039f
|
581 |
out: |
06fc0d66f
|
582 |
mutex_unlock(&sysfs_mutex); |
51225039f
|
583 |
return rc; |
31e5abe9a
|
584 585 586 587 588 |
} EXPORT_SYMBOL_GPL(sysfs_chmod_file); /** |
1da177e4c
|
589 590 591 592 593 594 595 596 597 |
* sysfs_remove_file - remove an object attribute. * @kobj: object we're acting for. * @attr: attribute descriptor. * * Hash the attribute name and kill the victim. */ void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) { |
3ff195b01
|
598 |
sysfs_hash_and_remove(kobj->sd, NULL, attr->name); |
1da177e4c
|
599 |
} |
1c205ae18
|
600 601 602 603 604 605 |
void sysfs_remove_files(struct kobject * kobj, const struct attribute **ptr) { int i; for (i = 0; ptr[i]; i++) sysfs_remove_file(kobj, ptr[i]); } |
1da177e4c
|
606 |
|
dfa87c824
|
607 608 609 610 611 612 613 614 615 |
/** * 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) { |
608e266a2
|
616 |
struct sysfs_dirent *dir_sd; |
dfa87c824
|
617 |
|
11f24fbdf
|
618 |
if (group) |
3ff195b01
|
619 |
dir_sd = sysfs_get_dirent(kobj->sd, NULL, group); |
11f24fbdf
|
620 621 |
else dir_sd = sysfs_get(kobj->sd); |
608e266a2
|
622 |
if (dir_sd) { |
3ff195b01
|
623 |
sysfs_hash_and_remove(dir_sd, NULL, attr->name); |
608e266a2
|
624 |
sysfs_put(dir_sd); |
dfa87c824
|
625 626 627 |
} } EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
d9a9cdfb0
|
628 |
struct sysfs_schedule_callback_struct { |
669420644
|
629 630 |
struct list_head workq_list; struct kobject *kobj; |
d9a9cdfb0
|
631 632 |
void (*func)(void *); void *data; |
523ded71d
|
633 |
struct module *owner; |
d9a9cdfb0
|
634 635 |
struct work_struct work; }; |
d110271e1
|
636 |
static struct workqueue_struct *sysfs_workqueue; |
669420644
|
637 638 |
static DEFINE_MUTEX(sysfs_workq_mutex); static LIST_HEAD(sysfs_workq); |
d9a9cdfb0
|
639 640 641 642 643 644 645 |
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); |
523ded71d
|
646 |
module_put(ss->owner); |
669420644
|
647 648 649 |
mutex_lock(&sysfs_workq_mutex); list_del(&ss->workq_list); mutex_unlock(&sysfs_workq_mutex); |
d9a9cdfb0
|
650 651 652 653 654 655 656 657 |
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. |
523ded71d
|
658 |
* @owner: module owning the callback code |
d9a9cdfb0
|
659 660 661 662 663 664 665 666 667 668 669 670 |
* * 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 |
669420644
|
671 672 |
* be allocated, -ENODEV if a reference to @owner isn't available, * -EAGAIN if a callback has already been scheduled for @kobj. |
d9a9cdfb0
|
673 674 |
*/ int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), |
523ded71d
|
675 |
void *data, struct module *owner) |
d9a9cdfb0
|
676 |
{ |
669420644
|
677 |
struct sysfs_schedule_callback_struct *ss, *tmp; |
d9a9cdfb0
|
678 |
|
523ded71d
|
679 680 |
if (!try_module_get(owner)) return -ENODEV; |
669420644
|
681 682 683 684 |
mutex_lock(&sysfs_workq_mutex); list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) if (ss->kobj == kobj) { |
d110271e1
|
685 |
module_put(owner); |
669420644
|
686 687 688 689 |
mutex_unlock(&sysfs_workq_mutex); return -EAGAIN; } mutex_unlock(&sysfs_workq_mutex); |
d110271e1
|
690 |
if (sysfs_workqueue == NULL) { |
086a377ed
|
691 |
sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
d110271e1
|
692 693 694 695 696 |
if (sysfs_workqueue == NULL) { module_put(owner); return -ENOMEM; } } |
d9a9cdfb0
|
697 |
ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
523ded71d
|
698 699 |
if (!ss) { module_put(owner); |
d9a9cdfb0
|
700 |
return -ENOMEM; |
523ded71d
|
701 |
} |
d9a9cdfb0
|
702 703 704 705 |
kobject_get(kobj); ss->kobj = kobj; ss->func = func; ss->data = data; |
523ded71d
|
706 |
ss->owner = owner; |
d9a9cdfb0
|
707 |
INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
669420644
|
708 709 710 711 |
INIT_LIST_HEAD(&ss->workq_list); mutex_lock(&sysfs_workq_mutex); list_add_tail(&ss->workq_list, &sysfs_workq); mutex_unlock(&sysfs_workq_mutex); |
d110271e1
|
712 |
queue_work(sysfs_workqueue, &ss->work); |
d9a9cdfb0
|
713 714 715 |
return 0; } EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |
dfa87c824
|
716 |
|
1da177e4c
|
717 718 |
EXPORT_SYMBOL_GPL(sysfs_create_file); EXPORT_SYMBOL_GPL(sysfs_remove_file); |
1c205ae18
|
719 720 |
EXPORT_SYMBOL_GPL(sysfs_remove_files); EXPORT_SYMBOL_GPL(sysfs_create_files); |