sysfs: prepare path write for unified regular / bin file handling
[deliverable/linux.git] / fs / sysfs / file.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
11 */
12
13#include <linux/module.h>
1da177e4 14#include <linux/kobject.h>
815d2d50 15#include <linux/kallsyms.h>
c6f87733 16#include <linux/slab.h>
93265d13 17#include <linux/fsnotify.h>
5f45f1a7 18#include <linux/namei.h>
4508a7a7 19#include <linux/poll.h>
94bebf4d 20#include <linux/list.h>
52e8c209 21#include <linux/mutex.h>
ae87221d 22#include <linux/limits.h>
060cc749 23#include <linux/uaccess.h>
13c589d5 24#include <linux/seq_file.h>
1da177e4
LT
25
26#include "sysfs.h"
27
85a4ffad 28/*
58282d8d 29 * There's one sysfs_open_file for each open file and one sysfs_open_dirent
c75ec764 30 * for each sysfs_dirent with one or more open files.
85a4ffad 31 *
c75ec764
TH
32 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
33 * protected by sysfs_open_dirent_lock.
34 *
13c589d5
TH
35 * filp->private_data points to seq_file whose ->private points to
36 * sysfs_open_file. sysfs_open_files are chained at
58282d8d 37 * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
85a4ffad 38 */
d7b37889 39static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
c75ec764 40static DEFINE_MUTEX(sysfs_open_file_mutex);
85a4ffad
TH
41
42struct sysfs_open_dirent {
43 atomic_t refcnt;
a4e8b912
TH
44 atomic_t event;
45 wait_queue_head_t poll;
58282d8d 46 struct list_head files; /* goes through sysfs_open_file.list */
85a4ffad
TH
47};
48
58282d8d 49struct sysfs_open_file {
bcafe4ee
TH
50 struct sysfs_dirent *sd;
51 struct file *file;
52e8c209 52 struct mutex mutex;
73107cb3 53 int event;
85a4ffad 54 struct list_head list;
73107cb3 55};
1da177e4 56
f9b9a621
TH
57static bool sysfs_is_bin(struct sysfs_dirent *sd)
58{
59 return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
60}
61
13c589d5
TH
62static struct sysfs_open_file *sysfs_of(struct file *file)
63{
64 return ((struct seq_file *)file->private_data)->private;
65}
66
375b611e
TH
67/*
68 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
69 * must be called while holding an active reference.
70 */
71static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
72{
73 struct kobject *kobj = sd->s_parent->s_dir.kobj;
74
75 lockdep_assert_held(sd);
76 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
77}
78
13c589d5
TH
79/*
80 * Reads on sysfs are handled through seq_file, which takes care of hairy
81 * details like buffering and seeking. The following function pipes
82 * sysfs_ops->show() result through seq_file.
1da177e4 83 */
13c589d5 84static int sysfs_seq_show(struct seq_file *sf, void *v)
1da177e4 85{
13c589d5
TH
86 struct sysfs_open_file *of = sf->private;
87 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
375b611e 88 const struct sysfs_ops *ops;
13c589d5 89 char *buf;
1da177e4
LT
90 ssize_t count;
91
13c589d5
TH
92 /* acquire buffer and ensure that it's >= PAGE_SIZE */
93 count = seq_get_buf(sf, &buf);
94 if (count < PAGE_SIZE) {
95 seq_commit(sf, -1);
96 return 0;
97 }
1da177e4 98
13c589d5
TH
99 /*
100 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
101 * nests outside active ref and is just to ensure that the ops
102 * aren't called concurrently for the same open file.
103 */
104 mutex_lock(&of->mutex);
105 if (!sysfs_get_active(of->sd)) {
106 mutex_unlock(&of->mutex);
0ab66088 107 return -ENODEV;
13c589d5 108 }
0ab66088 109
13c589d5 110 of->event = atomic_read(&of->sd->s_attr.open->event);
375b611e 111
13c589d5
TH
112 /*
113 * Lookup @ops and invoke show(). Control may reach here via seq
114 * file lseek even if @ops->show() isn't implemented.
115 */
116 ops = sysfs_file_ops(of->sd);
117 if (ops->show)
118 count = ops->show(kobj, of->sd->s_attr.attr, buf);
119 else
120 count = 0;
0ab66088 121
13c589d5
TH
122 sysfs_put_active(of->sd);
123 mutex_unlock(&of->mutex);
124
125 if (count < 0)
126 return count;
0ab66088 127
8118a859
MX
128 /*
129 * The code works fine with PAGE_SIZE return but it's likely to
130 * indicate truncated result or overflow in normal use cases.
131 */
815d2d50
AM
132 if (count >= (ssize_t)PAGE_SIZE) {
133 print_symbol("fill_read_buffer: %s returned bad count\n",
134 (unsigned long)ops->show);
135 /* Try to struggle along */
136 count = PAGE_SIZE - 1;
137 }
13c589d5
TH
138 seq_commit(sf, count);
139 return 0;
1da177e4
LT
140}
141
1da177e4 142/**
8ef445f0
TH
143 * flush_write_buffer - push buffer to kobject
144 * @of: open file
145 * @buf: data buffer for file
f9b9a621 146 * @off: file offset to write to
8ef445f0 147 * @count: number of bytes
1da177e4 148 *
8ef445f0
TH
149 * Get the correct pointers for the kobject and the attribute we're dealing
150 * with, then call the store() method for it with @buf.
1da177e4 151 */
f9b9a621 152static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
8ef445f0 153 size_t count)
1da177e4 154{
bcafe4ee 155 struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
8ef445f0 156 int rc = 0;
0ab66088 157
8ef445f0
TH
158 /*
159 * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
160 * nests outside active ref and is just to ensure that the ops
161 * aren't called concurrently for the same open file.
162 */
163 mutex_lock(&of->mutex);
164 if (!sysfs_get_active(of->sd)) {
165 mutex_unlock(&of->mutex);
0ab66088 166 return -ENODEV;
8ef445f0 167 }
0ab66088 168
f9b9a621
TH
169 if (sysfs_is_bin(of->sd)) {
170 struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
171
172 rc = -EIO;
173 if (battr->write)
174 rc = battr->write(of->file, kobj, battr, buf, off,
175 count);
176 } else {
177 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
178
179 rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
180 }
0ab66088 181
bcafe4ee 182 sysfs_put_active(of->sd);
8ef445f0 183 mutex_unlock(&of->mutex);
1da177e4 184
0ab66088 185 return rc;
1da177e4
LT
186}
187
1da177e4 188/**
8ef445f0
TH
189 * sysfs_write_file - write an attribute
190 * @file: file pointer
191 * @user_buf: data to write
192 * @count: number of bytes
193 * @ppos: starting offset
194 *
195 * Copy data in from userland and pass it to the matching
196 * sysfs_ops->store() by invoking flush_write_buffer().
1da177e4 197 *
8ef445f0
TH
198 * There is no easy way for us to know if userspace is only doing a partial
199 * write, so we don't support them. We expect the entire buffer to come on
200 * the first write. Hint: if you're writing a value, first read the file,
201 * modify only the the value you're changing, then write entire buffer
202 * back.
1da177e4 203 */
8ef445f0 204static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
ddfd6d07 205 size_t count, loff_t *ppos)
1da177e4 206{
13c589d5 207 struct sysfs_open_file *of = sysfs_of(file);
f9b9a621 208 ssize_t len = min_t(size_t, count, PAGE_SIZE);
8ef445f0 209 char *buf;
1da177e4 210
f9b9a621
TH
211 if (sysfs_is_bin(of->sd)) {
212 loff_t size = file_inode(file)->i_size;
213
214 if (size <= *ppos)
215 return 0;
216 len = min_t(ssize_t, len, size - *ppos);
217 }
218
8ef445f0
TH
219 if (!len)
220 return 0;
221
222 buf = kmalloc(len + 1, GFP_KERNEL);
223 if (!buf)
224 return -ENOMEM;
225
226 if (copy_from_user(buf, user_buf, len)) {
227 len = -EFAULT;
228 goto out_free;
229 }
230 buf[len] = '\0'; /* guarantee string termination */
231
f9b9a621 232 len = flush_write_buffer(of, buf, *ppos, len);
1da177e4
LT
233 if (len > 0)
234 *ppos += len;
8ef445f0
TH
235out_free:
236 kfree(buf);
1da177e4
LT
237 return len;
238}
239
85a4ffad
TH
240/**
241 * sysfs_get_open_dirent - get or create sysfs_open_dirent
242 * @sd: target sysfs_dirent
58282d8d 243 * @of: sysfs_open_file for this instance of open
85a4ffad
TH
244 *
245 * If @sd->s_attr.open exists, increment its reference count;
58282d8d 246 * otherwise, create one. @of is chained to the files list.
85a4ffad
TH
247 *
248 * LOCKING:
249 * Kernel thread context (may sleep).
250 *
251 * RETURNS:
252 * 0 on success, -errno on failure.
253 */
254static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
58282d8d 255 struct sysfs_open_file *of)
85a4ffad
TH
256{
257 struct sysfs_open_dirent *od, *new_od = NULL;
258
259 retry:
c75ec764 260 mutex_lock(&sysfs_open_file_mutex);
83db93f4 261 spin_lock_irq(&sysfs_open_dirent_lock);
85a4ffad
TH
262
263 if (!sd->s_attr.open && new_od) {
264 sd->s_attr.open = new_od;
265 new_od = NULL;
266 }
267
268 od = sd->s_attr.open;
269 if (od) {
270 atomic_inc(&od->refcnt);
58282d8d 271 list_add_tail(&of->list, &od->files);
85a4ffad
TH
272 }
273
83db93f4 274 spin_unlock_irq(&sysfs_open_dirent_lock);
c75ec764 275 mutex_unlock(&sysfs_open_file_mutex);
85a4ffad
TH
276
277 if (od) {
278 kfree(new_od);
279 return 0;
280 }
281
282 /* not there, initialize a new one and retry */
283 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
284 if (!new_od)
285 return -ENOMEM;
286
287 atomic_set(&new_od->refcnt, 0);
a4e8b912
TH
288 atomic_set(&new_od->event, 1);
289 init_waitqueue_head(&new_od->poll);
58282d8d 290 INIT_LIST_HEAD(&new_od->files);
85a4ffad
TH
291 goto retry;
292}
293
294/**
295 * sysfs_put_open_dirent - put sysfs_open_dirent
296 * @sd: target sysfs_dirent
58282d8d 297 * @of: associated sysfs_open_file
85a4ffad 298 *
58282d8d
TH
299 * Put @sd->s_attr.open and unlink @of from the files list. If
300 * reference count reaches zero, disassociate and free it.
85a4ffad
TH
301 *
302 * LOCKING:
303 * None.
304 */
305static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
58282d8d 306 struct sysfs_open_file *of)
85a4ffad
TH
307{
308 struct sysfs_open_dirent *od = sd->s_attr.open;
83db93f4 309 unsigned long flags;
85a4ffad 310
c75ec764 311 mutex_lock(&sysfs_open_file_mutex);
83db93f4 312 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
85a4ffad 313
58282d8d 314 list_del(&of->list);
85a4ffad
TH
315 if (atomic_dec_and_test(&od->refcnt))
316 sd->s_attr.open = NULL;
317 else
318 od = NULL;
319
83db93f4 320 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
c75ec764 321 mutex_unlock(&sysfs_open_file_mutex);
85a4ffad
TH
322
323 kfree(od);
324}
325
94bebf4d 326static int sysfs_open_file(struct inode *inode, struct file *file)
1da177e4 327{
3e519038 328 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61 329 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
58282d8d 330 struct sysfs_open_file *of;
52cf25d0 331 const struct sysfs_ops *ops;
000f2a4d 332 int error = -EACCES;
1da177e4 333
0ab66088 334 /* need attr_sd for attr and ops, its parent for kobj */
e72ceb8c 335 if (!sysfs_get_active(attr_sd))
0ab66088 336 return -ENODEV;
1da177e4 337
000f2a4d 338 /* every kobject with an attribute needs a ktype assigned */
375b611e
TH
339 ops = sysfs_file_ops(attr_sd);
340 if (WARN(!ops, KERN_ERR
341 "missing sysfs attribute operations for kobject: %s\n",
342 kobject_name(kobj)))
7b595756 343 goto err_out;
1da177e4
LT
344
345 /* File needs write support.
ab9bf4be 346 * The inode's perms must say it's ok,
1da177e4
LT
347 * and we must have a store method.
348 */
349 if (file->f_mode & FMODE_WRITE) {
1da177e4 350 if (!(inode->i_mode & S_IWUGO) || !ops->store)
7b595756 351 goto err_out;
1da177e4
LT
352 }
353
354 /* File needs read support.
355 * The inode's perms must say it's ok, and we there
356 * must be a show method for it.
357 */
358 if (file->f_mode & FMODE_READ) {
359 if (!(inode->i_mode & S_IRUGO) || !ops->show)
7b595756 360 goto err_out;
1da177e4
LT
361 }
362
13c589d5 363 /* allocate a sysfs_open_file for the file */
0ab66088 364 error = -ENOMEM;
58282d8d
TH
365 of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
366 if (!of)
7b595756 367 goto err_out;
1da177e4 368
58282d8d 369 mutex_init(&of->mutex);
bcafe4ee
TH
370 of->sd = attr_sd;
371 of->file = file;
13c589d5
TH
372
373 /*
374 * Always instantiate seq_file even if read access is not
375 * implemented or requested. This unifies private data access and
376 * most files are readable anyway.
377 */
378 error = single_open(file, sysfs_seq_show, of);
379 if (error)
380 goto err_free;
381
382 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
383 if (file->f_mode & FMODE_WRITE)
384 file->f_mode |= FMODE_PWRITE;
0ab66088 385
85a4ffad 386 /* make sure we have open dirent struct */
58282d8d 387 error = sysfs_get_open_dirent(attr_sd, of);
85a4ffad 388 if (error)
13c589d5 389 goto err_close;
85a4ffad 390
b05f0548 391 /* open succeeded, put active references */
e72ceb8c 392 sysfs_put_active(attr_sd);
0ab66088
TH
393 return 0;
394
13c589d5
TH
395err_close:
396 single_release(inode, file);
397err_free:
58282d8d 398 kfree(of);
13c589d5 399err_out:
e72ceb8c 400 sysfs_put_active(attr_sd);
1da177e4
LT
401 return error;
402}
403
85a4ffad 404static int sysfs_release(struct inode *inode, struct file *filp)
1da177e4 405{
85a4ffad 406 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
13c589d5 407 struct sysfs_open_file *of = sysfs_of(filp);
1da177e4 408
58282d8d 409 sysfs_put_open_dirent(sd, of);
13c589d5 410 single_release(inode, filp);
58282d8d 411 kfree(of);
50ab1a72 412
1da177e4
LT
413 return 0;
414}
415
4508a7a7
N
416/* Sysfs attribute files are pollable. The idea is that you read
417 * the content and then you use 'poll' or 'select' to wait for
418 * the content to change. When the content changes (assuming the
419 * manager for the kobject supports notification), poll will
420 * return POLLERR|POLLPRI, and select will return the fd whether
421 * it is waiting for read, write, or exceptions.
422 * Once poll/select indicates that the value has changed, you
2424b5dd 423 * need to close and re-open the file, or seek to 0 and read again.
4508a7a7
N
424 * Reminder: this only works for attributes which actively support
425 * it, and it is not possible to test an attribute from userspace
a93720ee 426 * to see if it supports poll (Neither 'poll' nor 'select' return
4508a7a7
N
427 * an appropriate error code). When in doubt, set a suitable timeout value.
428 */
429static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
430{
13c589d5 431 struct sysfs_open_file *of = sysfs_of(filp);
0ab66088 432 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
a4e8b912 433 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
0ab66088
TH
434
435 /* need parent for the kobj, grab both */
e72ceb8c 436 if (!sysfs_get_active(attr_sd))
0ab66088 437 goto trigger;
4508a7a7 438
a4e8b912 439 poll_wait(filp, &od->poll, wait);
4508a7a7 440
e72ceb8c 441 sysfs_put_active(attr_sd);
0ab66088 442
58282d8d 443 if (of->event != atomic_read(&od->event))
0ab66088 444 goto trigger;
4508a7a7 445
1af3557a 446 return DEFAULT_POLLMASK;
0ab66088
TH
447
448 trigger:
1af3557a 449 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
4508a7a7
N
450}
451
f1282c84
NB
452void sysfs_notify_dirent(struct sysfs_dirent *sd)
453{
454 struct sysfs_open_dirent *od;
83db93f4 455 unsigned long flags;
f1282c84 456
83db93f4 457 spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
f1282c84 458
fc60bb83
ND
459 if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
460 od = sd->s_attr.open;
461 if (od) {
462 atomic_inc(&od->event);
463 wake_up_interruptible(&od->poll);
464 }
f1282c84
NB
465 }
466
83db93f4 467 spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
f1282c84
NB
468}
469EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
470
8c0e3998 471void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
4508a7a7 472{
51225039 473 struct sysfs_dirent *sd = k->sd;
4508a7a7 474
51225039
TH
475 mutex_lock(&sysfs_mutex);
476
477 if (sd && dir)
cfec0bc8 478 sd = sysfs_find_dirent(sd, dir, NULL);
51225039 479 if (sd && attr)
cfec0bc8 480 sd = sysfs_find_dirent(sd, attr, NULL);
f1282c84
NB
481 if (sd)
482 sysfs_notify_dirent(sd);
51225039
TH
483
484 mutex_unlock(&sysfs_mutex);
4508a7a7
N
485}
486EXPORT_SYMBOL_GPL(sysfs_notify);
487
4b6f5d20 488const struct file_operations sysfs_file_operations = {
13c589d5 489 .read = seq_read,
1da177e4 490 .write = sysfs_write_file,
13c589d5 491 .llseek = seq_lseek,
1da177e4
LT
492 .open = sysfs_open_file,
493 .release = sysfs_release,
4508a7a7 494 .poll = sysfs_poll,
1da177e4
LT
495};
496
f9b9a621
TH
497const struct file_operations sysfs_bin_operations = {
498 .write = sysfs_write_file,
499 .llseek = generic_file_llseek,
500};
501
58292cbe
TH
502int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
503 const struct attribute *attr, int type,
504 umode_t amode, const void *ns)
1da177e4 505{
0f423895 506 umode_t mode = (amode & S_IALLUGO) | S_IFREG;
fb6896da 507 struct sysfs_addrm_cxt acxt;
a26cd722 508 struct sysfs_dirent *sd;
23dc2799 509 int rc;
1da177e4 510
3007e997
TH
511 sd = sysfs_new_dirent(attr->name, mode, type);
512 if (!sd)
513 return -ENOMEM;
487505c2
EB
514
515 sd->s_ns = ns;
b1fc3d61 516 sd->s_attr.attr = (void *)attr;
a2db6842 517 sysfs_dirent_init_lockdep(sd);
1da177e4 518
d69ac5a0
TH
519 sysfs_addrm_start(&acxt);
520 rc = sysfs_add_one(&acxt, sd, dir_sd);
23dc2799 521 sysfs_addrm_finish(&acxt);
a26cd722 522
23dc2799 523 if (rc)
967e35dc 524 sysfs_put(sd);
3007e997 525
23dc2799 526 return rc;
1da177e4
LT
527}
528
529
0f423895
JB
530int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
531 int type)
532{
58292cbe 533 return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
0f423895
JB
534}
535
1da177e4 536/**
58292cbe
TH
537 * sysfs_create_file_ns - create an attribute file for an object with custom ns
538 * @kobj: object we're creating for
539 * @attr: attribute descriptor
540 * @ns: namespace the new file should belong to
1da177e4 541 */
58292cbe
TH
542int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
543 const void *ns)
1da177e4 544{
608e266a 545 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 546
58292cbe
TH
547 return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
548 attr->mode, ns);
1da177e4
LT
549
550}
58292cbe 551EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
1da177e4 552
1c205ae1
AK
553int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
554{
555 int err = 0;
556 int i;
557
558 for (i = 0; ptr[i] && !err; i++)
559 err = sysfs_create_file(kobj, ptr[i]);
560 if (err)
561 while (--i >= 0)
562 sysfs_remove_file(kobj, ptr[i]);
563 return err;
564}
1b866757 565EXPORT_SYMBOL_GPL(sysfs_create_files);
1da177e4 566
dfa87c82
AS
567/**
568 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
569 * @kobj: object we're acting for.
570 * @attr: attribute descriptor.
571 * @group: group name.
572 */
573int sysfs_add_file_to_group(struct kobject *kobj,
574 const struct attribute *attr, const char *group)
575{
608e266a 576 struct sysfs_dirent *dir_sd;
dfa87c82
AS
577 int error;
578
11f24fbd 579 if (group)
388975cc 580 dir_sd = sysfs_get_dirent(kobj->sd, group);
11f24fbd
JB
581 else
582 dir_sd = sysfs_get(kobj->sd);
583
608e266a
TH
584 if (!dir_sd)
585 return -ENOENT;
586
587 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
588 sysfs_put(dir_sd);
589
dfa87c82
AS
590 return error;
591}
592EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
593
31e5abe9
KS
594/**
595 * sysfs_chmod_file - update the modified mode value on an object attribute.
596 * @kobj: object we're acting for.
597 * @attr: attribute descriptor.
598 * @mode: file permissions.
599 *
600 */
49c19400 601int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
48176a97 602 umode_t mode)
31e5abe9 603{
06fc0d66 604 struct sysfs_dirent *sd;
bc062b1b 605 struct iattr newattrs;
51225039
TH
606 int rc;
607
06fc0d66 608 mutex_lock(&sysfs_mutex);
51225039 609
06fc0d66 610 rc = -ENOENT;
cfec0bc8 611 sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
06fc0d66 612 if (!sd)
51225039 613 goto out;
f88123ea 614
06fc0d66 615 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
4c6974f5 616 newattrs.ia_valid = ATTR_MODE;
06fc0d66 617 rc = sysfs_sd_setattr(sd, &newattrs);
f88123ea 618
51225039 619 out:
06fc0d66 620 mutex_unlock(&sysfs_mutex);
51225039 621 return rc;
31e5abe9
KS
622}
623EXPORT_SYMBOL_GPL(sysfs_chmod_file);
624
1da177e4 625/**
58292cbe
TH
626 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
627 * @kobj: object we're acting for
628 * @attr: attribute descriptor
629 * @ns: namespace tag of the file to remove
1da177e4 630 *
58292cbe 631 * Hash the attribute name and namespace tag and kill the victim.
1da177e4 632 */
58292cbe
TH
633void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
634 const void *ns)
1da177e4 635{
58292cbe 636 struct sysfs_dirent *dir_sd = kobj->sd;
487505c2 637
cfec0bc8 638 sysfs_hash_and_remove(dir_sd, attr->name, ns);
1da177e4 639}
58292cbe 640EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
1da177e4 641
1b18dc2b 642void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
1c205ae1
AK
643{
644 int i;
645 for (i = 0; ptr[i]; i++)
646 sysfs_remove_file(kobj, ptr[i]);
647}
1b866757 648EXPORT_SYMBOL_GPL(sysfs_remove_files);
1da177e4 649
dfa87c82
AS
650/**
651 * sysfs_remove_file_from_group - remove an attribute file from a group.
652 * @kobj: object we're acting for.
653 * @attr: attribute descriptor.
654 * @group: group name.
655 */
656void sysfs_remove_file_from_group(struct kobject *kobj,
657 const struct attribute *attr, const char *group)
658{
608e266a 659 struct sysfs_dirent *dir_sd;
dfa87c82 660
11f24fbd 661 if (group)
388975cc 662 dir_sd = sysfs_get_dirent(kobj->sd, group);
11f24fbd
JB
663 else
664 dir_sd = sysfs_get(kobj->sd);
608e266a 665 if (dir_sd) {
cfec0bc8 666 sysfs_hash_and_remove(dir_sd, attr->name, NULL);
608e266a 667 sysfs_put(dir_sd);
dfa87c82
AS
668 }
669}
670EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
671
d9a9cdfb 672struct sysfs_schedule_callback_struct {
66942064
AC
673 struct list_head workq_list;
674 struct kobject *kobj;
d9a9cdfb
AS
675 void (*func)(void *);
676 void *data;
523ded71 677 struct module *owner;
d9a9cdfb
AS
678 struct work_struct work;
679};
680
d110271e 681static struct workqueue_struct *sysfs_workqueue;
66942064
AC
682static DEFINE_MUTEX(sysfs_workq_mutex);
683static LIST_HEAD(sysfs_workq);
d9a9cdfb
AS
684static void sysfs_schedule_callback_work(struct work_struct *work)
685{
686 struct sysfs_schedule_callback_struct *ss = container_of(work,
687 struct sysfs_schedule_callback_struct, work);
688
689 (ss->func)(ss->data);
690 kobject_put(ss->kobj);
523ded71 691 module_put(ss->owner);
66942064
AC
692 mutex_lock(&sysfs_workq_mutex);
693 list_del(&ss->workq_list);
694 mutex_unlock(&sysfs_workq_mutex);
d9a9cdfb
AS
695 kfree(ss);
696}
697
698/**
699 * sysfs_schedule_callback - helper to schedule a callback for a kobject
700 * @kobj: object we're acting for.
701 * @func: callback function to invoke later.
702 * @data: argument to pass to @func.
523ded71 703 * @owner: module owning the callback code
d9a9cdfb
AS
704 *
705 * sysfs attribute methods must not unregister themselves or their parent
706 * kobject (which would amount to the same thing). Attempts to do so will
707 * deadlock, since unregistration is mutually exclusive with driver
708 * callbacks.
709 *
710 * Instead methods can call this routine, which will attempt to allocate
711 * and schedule a workqueue request to call back @func with @data as its
712 * argument in the workqueue's process context. @kobj will be pinned
713 * until @func returns.
714 *
715 * Returns 0 if the request was submitted, -ENOMEM if storage could not
66942064
AC
716 * be allocated, -ENODEV if a reference to @owner isn't available,
717 * -EAGAIN if a callback has already been scheduled for @kobj.
d9a9cdfb
AS
718 */
719int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
523ded71 720 void *data, struct module *owner)
d9a9cdfb 721{
66942064 722 struct sysfs_schedule_callback_struct *ss, *tmp;
d9a9cdfb 723
523ded71
AS
724 if (!try_module_get(owner))
725 return -ENODEV;
66942064
AC
726
727 mutex_lock(&sysfs_workq_mutex);
728 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
729 if (ss->kobj == kobj) {
d110271e 730 module_put(owner);
66942064
AC
731 mutex_unlock(&sysfs_workq_mutex);
732 return -EAGAIN;
733 }
734 mutex_unlock(&sysfs_workq_mutex);
735
d110271e 736 if (sysfs_workqueue == NULL) {
086a377e 737 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
d110271e
AC
738 if (sysfs_workqueue == NULL) {
739 module_put(owner);
740 return -ENOMEM;
741 }
742 }
743
d9a9cdfb 744 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
523ded71
AS
745 if (!ss) {
746 module_put(owner);
d9a9cdfb 747 return -ENOMEM;
523ded71 748 }
d9a9cdfb
AS
749 kobject_get(kobj);
750 ss->kobj = kobj;
751 ss->func = func;
752 ss->data = data;
523ded71 753 ss->owner = owner;
d9a9cdfb 754 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
66942064
AC
755 INIT_LIST_HEAD(&ss->workq_list);
756 mutex_lock(&sysfs_workq_mutex);
757 list_add_tail(&ss->workq_list, &sysfs_workq);
758 mutex_unlock(&sysfs_workq_mutex);
d110271e 759 queue_work(sysfs_workqueue, &ss->work);
d9a9cdfb
AS
760 return 0;
761}
762EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
This page took 1.214851 seconds and 5 git commands to generate.