blob: cba4c1c7383c1834692268bd6b6a7e86c41e3777 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - operations for regular (text) files.
3 */
4
5#include <linux/module.h>
Robert Love0eeca282005-07-12 17:06:03 -04006#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -07008#include <linux/namei.h>
NeilBrown4508a7a2006-03-20 17:53:53 +11009#include <linux/poll.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010010#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
12#include <asm/semaphore.h>
13
14#include "sysfs.h"
15
16#define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
17#define to_sattr(a) container_of(a,struct subsys_attribute,attr)
18
Martin Waitz3d410882005-06-23 22:05:21 -070019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Subsystem file operations.
21 * These operations allow subsystems to have files that can be
22 * read/written.
23 */
24static ssize_t
25subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
26{
27 struct subsystem * s = to_subsys(kobj);
28 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050029 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 if (sattr->show)
32 ret = sattr->show(s,page);
33 return ret;
34}
35
36static ssize_t
37subsys_attr_store(struct kobject * kobj, struct attribute * attr,
38 const char * page, size_t count)
39{
40 struct subsystem * s = to_subsys(kobj);
41 struct subsys_attribute * sattr = to_sattr(attr);
Dmitry Torokhovc76d0ab2005-04-29 01:22:00 -050042 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if (sattr->store)
45 ret = sattr->store(s,page,count);
46 return ret;
47}
48
49static struct sysfs_ops subsys_sysfs_ops = {
50 .show = subsys_attr_show,
51 .store = subsys_attr_store,
52};
53
Oliver Neukum94bebf42006-12-20 10:52:44 +010054/**
55 * add_to_collection - add buffer to a collection
56 * @buffer: buffer to be added
57 * @node inode of set to add to
58 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Oliver Neukum94bebf42006-12-20 10:52:44 +010060static inline void
61add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
62{
63 struct sysfs_buffer_collection *set = node->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Oliver Neukum94bebf42006-12-20 10:52:44 +010065 mutex_lock(&node->i_mutex);
66 list_add(&buffer->associates, &set->associates);
67 mutex_unlock(&node->i_mutex);
68}
69
70static inline void
71remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
72{
73 mutex_lock(&node->i_mutex);
74 list_del(&buffer->associates);
75 mutex_unlock(&node->i_mutex);
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/**
79 * fill_read_buffer - allocate and fill buffer from object.
80 * @dentry: dentry pointer.
81 * @buffer: data buffer for file.
82 *
83 * Allocate @buffer->page, if it hasn't been already, then call the
84 * kobject's show() method to fill the buffer with this attribute's
85 * data.
86 * This is called only once, on the file's first read.
87 */
88static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
89{
NeilBrown4508a7a2006-03-20 17:53:53 +110090 struct sysfs_dirent * sd = dentry->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct attribute * attr = to_attr(dentry);
92 struct kobject * kobj = to_kobj(dentry->d_parent);
93 struct sysfs_ops * ops = buffer->ops;
94 int ret = 0;
95 ssize_t count;
96
97 if (!buffer->page)
98 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
99 if (!buffer->page)
100 return -ENOMEM;
101
NeilBrown4508a7a2006-03-20 17:53:53 +1100102 buffer->event = atomic_read(&sd->s_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 count = ops->show(kobj,attr,buffer->page);
104 buffer->needs_read_fill = 0;
105 BUG_ON(count > (ssize_t)PAGE_SIZE);
106 if (count >= 0)
107 buffer->count = count;
108 else
109 ret = count;
110 return ret;
111}
112
113
114/**
115 * flush_read_buffer - push buffer to userspace.
116 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700117 * @buf: user-passed buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * @count: number of bytes requested.
119 * @ppos: file position.
120 *
121 * Copy the buffer we filled in fill_read_buffer() to userspace.
122 * This is done at the reader's leisure, copying and advancing
123 * the amount they specify each time.
124 * This may be called continuously until the buffer is empty.
125 */
126static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
127 size_t count, loff_t * ppos)
128{
129 int error;
130
131 if (*ppos > buffer->count)
132 return 0;
133
134 if (count > (buffer->count - *ppos))
135 count = buffer->count - *ppos;
136
137 error = copy_to_user(buf,buffer->page + *ppos,count);
138 if (!error)
139 *ppos += count;
140 return error ? -EFAULT : count;
141}
142
143/**
144 * sysfs_read_file - read an attribute.
145 * @file: file pointer.
146 * @buf: buffer to fill.
147 * @count: number of bytes to read.
148 * @ppos: starting offset in file.
149 *
150 * Userspace wants to read an attribute file. The attribute descriptor
151 * is in the file's ->d_fsdata. The target object is in the directory's
152 * ->d_fsdata.
153 *
154 * We call fill_read_buffer() to allocate and fill the buffer from the
155 * object's show() method exactly once (if the read is happening from
156 * the beginning of the file). That should fill the entire buffer with
157 * all the data the object has to offer for that attribute.
158 * We then call flush_read_buffer() to copy the buffer to userspace
159 * in the increments specified.
160 */
161
162static ssize_t
163sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
164{
165 struct sysfs_buffer * buffer = file->private_data;
166 ssize_t retval = 0;
167
168 down(&buffer->sem);
Oliver Neukum94bebf42006-12-20 10:52:44 +0100169 if (buffer->orphaned) {
170 retval = -ENODEV;
171 goto out;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (buffer->needs_read_fill) {
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800174 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto out;
176 }
Zach Brown5c1fdf42006-10-03 01:16:06 -0700177 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
178 __FUNCTION__, count, *ppos, buffer->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 retval = flush_read_buffer(buffer,buf,count,ppos);
180out:
181 up(&buffer->sem);
182 return retval;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/**
186 * fill_write_buffer - copy buffer from userspace.
187 * @buffer: data buffer for file.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700188 * @buf: data from user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * @count: number of bytes in @userbuf.
190 *
191 * Allocate @buffer->page if it hasn't been already, then
192 * copy the user-supplied buffer into it.
193 */
194
195static int
196fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
197{
198 int error;
199
200 if (!buffer->page)
201 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
202 if (!buffer->page)
203 return -ENOMEM;
204
205 if (count >= PAGE_SIZE)
Greg Kroah-Hartman6e0dd742006-03-31 15:37:06 -0800206 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 error = copy_from_user(buffer->page,buf,count);
208 buffer->needs_read_fill = 1;
Thomas Maier035ed7a2006-10-22 19:17:47 +0200209 /* if buf is assumed to contain a string, terminate it by \0,
210 so e.g. sscanf() can scan the string easily */
211 buffer->page[count] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return error ? -EFAULT : count;
213}
214
215
216/**
217 * flush_write_buffer - push buffer to kobject.
Martin Waitz3d410882005-06-23 22:05:21 -0700218 * @dentry: dentry to the attribute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * @buffer: data buffer for file.
Martin Waitz3d410882005-06-23 22:05:21 -0700220 * @count: number of bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Get the correct pointers for the kobject and the attribute we're
223 * dealing with, then call the store() method for the attribute,
224 * passing the buffer that we acquired in fill_write_buffer().
225 */
226
227static int
228flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
229{
230 struct attribute * attr = to_attr(dentry);
231 struct kobject * kobj = to_kobj(dentry->d_parent);
232 struct sysfs_ops * ops = buffer->ops;
233
234 return ops->store(kobj,attr,buffer->page,count);
235}
236
237
238/**
239 * sysfs_write_file - write an attribute.
240 * @file: file pointer
241 * @buf: data to write
242 * @count: number of bytes
243 * @ppos: starting offset
244 *
245 * Similar to sysfs_read_file(), though working in the opposite direction.
246 * We allocate and fill the data from the user in fill_write_buffer(),
247 * then push it to the kobject in flush_write_buffer().
248 * There is no easy way for us to know if userspace is only doing a partial
249 * write, so we don't support them. We expect the entire buffer to come
250 * on the first write.
251 * Hint: if you're writing a value, first read the file, modify only the
252 * the value you're changing, then write entire buffer back.
253 */
254
255static ssize_t
256sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
257{
258 struct sysfs_buffer * buffer = file->private_data;
259 ssize_t len;
260
261 down(&buffer->sem);
Oliver Neukum94bebf42006-12-20 10:52:44 +0100262 if (buffer->orphaned) {
263 len = -ENODEV;
264 goto out;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 len = fill_write_buffer(buffer, buf, count);
267 if (len > 0)
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800268 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (len > 0)
270 *ppos += len;
Oliver Neukum94bebf42006-12-20 10:52:44 +0100271out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 up(&buffer->sem);
273 return len;
274}
275
Oliver Neukum94bebf42006-12-20 10:52:44 +0100276static int sysfs_open_file(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800278 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
279 struct attribute * attr = to_attr(file->f_path.dentry);
Oliver Neukum94bebf42006-12-20 10:52:44 +0100280 struct sysfs_buffer_collection *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct sysfs_buffer * buffer;
282 struct sysfs_ops * ops = NULL;
283 int error = 0;
284
285 if (!kobj || !attr)
286 goto Einval;
287
288 /* Grab the module reference for this attribute if we have one */
289 if (!try_module_get(attr->owner)) {
290 error = -ENODEV;
291 goto Done;
292 }
293
294 /* if the kobject has no ktype, then we assume that it is a subsystem
295 * itself, and use ops for it.
296 */
297 if (kobj->kset && kobj->kset->ktype)
298 ops = kobj->kset->ktype->sysfs_ops;
299 else if (kobj->ktype)
300 ops = kobj->ktype->sysfs_ops;
301 else
302 ops = &subsys_sysfs_ops;
303
304 /* No sysfs operations, either from having no subsystem,
305 * or the subsystem have no operations.
306 */
307 if (!ops)
308 goto Eaccess;
309
Oliver Neukum94bebf42006-12-20 10:52:44 +0100310 /* make sure we have a collection to add our buffers to */
311 mutex_lock(&inode->i_mutex);
312 if (!(set = inode->i_private)) {
313 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
314 error = -ENOMEM;
315 goto Done;
316 } else {
317 INIT_LIST_HEAD(&set->associates);
318 }
319 }
320 mutex_unlock(&inode->i_mutex);
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* File needs write support.
323 * The inode's perms must say it's ok,
324 * and we must have a store method.
325 */
326 if (file->f_mode & FMODE_WRITE) {
327
328 if (!(inode->i_mode & S_IWUGO) || !ops->store)
329 goto Eaccess;
330
331 }
332
333 /* File needs read support.
334 * The inode's perms must say it's ok, and we there
335 * must be a show method for it.
336 */
337 if (file->f_mode & FMODE_READ) {
338 if (!(inode->i_mode & S_IRUGO) || !ops->show)
339 goto Eaccess;
340 }
341
342 /* No error? Great, allocate a buffer for the file, and store it
343 * it in file->private_data for easy access.
344 */
Eric Sesterhenn58d49282006-02-22 11:18:15 +0100345 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (buffer) {
Oliver Neukum94bebf42006-12-20 10:52:44 +0100347 INIT_LIST_HEAD(&buffer->associates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 init_MUTEX(&buffer->sem);
349 buffer->needs_read_fill = 1;
350 buffer->ops = ops;
Oliver Neukum94bebf42006-12-20 10:52:44 +0100351 add_to_collection(buffer, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 file->private_data = buffer;
353 } else
354 error = -ENOMEM;
355 goto Done;
356
357 Einval:
358 error = -EINVAL;
359 goto Done;
360 Eaccess:
361 error = -EACCES;
362 module_put(attr->owner);
363 Done:
364 if (error && kobj)
365 kobject_put(kobj);
366 return error;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int sysfs_release(struct inode * inode, struct file * filp)
370{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800371 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
372 struct attribute * attr = to_attr(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct module * owner = attr->owner;
374 struct sysfs_buffer * buffer = filp->private_data;
375
Oliver Neukum94bebf42006-12-20 10:52:44 +0100376 if (buffer)
377 remove_from_collection(buffer, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (kobj)
379 kobject_put(kobj);
380 /* After this point, attr should not be accessed. */
381 module_put(owner);
382
383 if (buffer) {
384 if (buffer->page)
385 free_page((unsigned long)buffer->page);
386 kfree(buffer);
387 }
388 return 0;
389}
390
NeilBrown4508a7a2006-03-20 17:53:53 +1100391/* Sysfs attribute files are pollable. The idea is that you read
392 * the content and then you use 'poll' or 'select' to wait for
393 * the content to change. When the content changes (assuming the
394 * manager for the kobject supports notification), poll will
395 * return POLLERR|POLLPRI, and select will return the fd whether
396 * it is waiting for read, write, or exceptions.
397 * Once poll/select indicates that the value has changed, you
398 * need to close and re-open the file, as simply seeking and reading
399 * again will not get new data, or reset the state of 'poll'.
400 * Reminder: this only works for attributes which actively support
401 * it, and it is not possible to test an attribute from userspace
402 * to see if it supports poll (Nether 'poll' or 'select' return
403 * an appropriate error code). When in doubt, set a suitable timeout value.
404 */
405static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
406{
407 struct sysfs_buffer * buffer = filp->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800408 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
409 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
NeilBrown4508a7a2006-03-20 17:53:53 +1100410 int res = 0;
411
412 poll_wait(filp, &kobj->poll, wait);
413
414 if (buffer->event != atomic_read(&sd->s_event)) {
415 res = POLLERR|POLLPRI;
416 buffer->needs_read_fill = 1;
417 }
418
419 return res;
420}
421
422
423static struct dentry *step_down(struct dentry *dir, const char * name)
424{
425 struct dentry * de;
426
427 if (dir == NULL || dir->d_inode == NULL)
428 return NULL;
429
430 mutex_lock(&dir->d_inode->i_mutex);
431 de = lookup_one_len(name, dir, strlen(name));
432 mutex_unlock(&dir->d_inode->i_mutex);
433 dput(dir);
434 if (IS_ERR(de))
435 return NULL;
436 if (de->d_inode == NULL) {
437 dput(de);
438 return NULL;
439 }
440 return de;
441}
442
443void sysfs_notify(struct kobject * k, char *dir, char *attr)
444{
445 struct dentry *de = k->dentry;
446 if (de)
447 dget(de);
448 if (de && dir)
449 de = step_down(de, dir);
450 if (de && attr)
451 de = step_down(de, attr);
452 if (de) {
453 struct sysfs_dirent * sd = de->d_fsdata;
454 if (sd)
455 atomic_inc(&sd->s_event);
456 wake_up_interruptible(&k->poll);
457 dput(de);
458 }
459}
460EXPORT_SYMBOL_GPL(sysfs_notify);
461
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800462const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 .read = sysfs_read_file,
464 .write = sysfs_write_file,
465 .llseek = generic_file_llseek,
466 .open = sysfs_open_file,
467 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100468 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469};
470
471
472int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
473{
474 struct sysfs_dirent * parent_sd = dir->d_fsdata;
475 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Maneesh Sonic5168652006-03-09 19:40:14 +0530476 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800478 mutex_lock(&dir->d_inode->i_mutex);
Maneesh Sonic5168652006-03-09 19:40:14 +0530479 if (!sysfs_dirent_exist(parent_sd, attr->name))
480 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
481 mode, type);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800482 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return error;
485}
486
487
488/**
489 * sysfs_create_file - create an attribute file for an object.
490 * @kobj: object we're creating for.
491 * @attr: atrribute descriptor.
492 */
493
494int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
495{
496 BUG_ON(!kobj || !kobj->dentry || !attr);
497
498 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
499
500}
501
502
503/**
504 * sysfs_update_file - update the modified timestamp on an object attribute.
505 * @kobj: object we're acting for.
506 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
508int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
509{
510 struct dentry * dir = kobj->dentry;
511 struct dentry * victim;
512 int res = -ENOENT;
513
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800514 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700515 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!IS_ERR(victim)) {
517 /* make sure dentry is really there */
518 if (victim->d_inode &&
519 (victim->d_parent->d_inode == dir->d_inode)) {
520 victim->d_inode->i_mtime = CURRENT_TIME;
Robert Love0eeca282005-07-12 17:06:03 -0400521 fsnotify_modify(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 res = 0;
523 } else
524 d_drop(victim);
525
526 /**
Hidetoshi Seto97a50182006-09-20 16:49:02 +0900527 * Drop the reference acquired from lookup_one_len() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529 dput(victim);
530 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800531 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 return res;
534}
535
536
537/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700538 * sysfs_chmod_file - update the modified mode value on an object attribute.
539 * @kobj: object we're acting for.
540 * @attr: attribute descriptor.
541 * @mode: file permissions.
542 *
543 */
544int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
545{
546 struct dentry *dir = kobj->dentry;
547 struct dentry *victim;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700548 struct inode * inode;
549 struct iattr newattrs;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700550 int res = -ENOENT;
551
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800552 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700553 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700554 if (!IS_ERR(victim)) {
555 if (victim->d_inode &&
556 (victim->d_parent->d_inode == dir->d_inode)) {
Maneesh Sonibc062b12005-07-29 12:13:35 -0700557 inode = victim->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800558 mutex_lock(&inode->i_mutex);
Maneesh Sonibc062b12005-07-29 12:13:35 -0700559 newattrs.ia_mode = (mode & S_IALLUGO) |
560 (inode->i_mode & ~S_IALLUGO);
561 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
562 res = notify_change(victim, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800563 mutex_unlock(&inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700564 }
Maneesh Sonibc062b12005-07-29 12:13:35 -0700565 dput(victim);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700566 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800567 mutex_unlock(&dir->d_inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700568
569 return res;
570}
571EXPORT_SYMBOL_GPL(sysfs_chmod_file);
572
573
574/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * sysfs_remove_file - remove an object attribute.
576 * @kobj: object we're acting for.
577 * @attr: attribute descriptor.
578 *
579 * Hash the attribute name and kill the victim.
580 */
581
582void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
583{
Oliver Neukum94bebf42006-12-20 10:52:44 +0100584 sysfs_hash_and_remove(kobj->dentry, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587
588EXPORT_SYMBOL_GPL(sysfs_create_file);
589EXPORT_SYMBOL_GPL(sysfs_remove_file);
590EXPORT_SYMBOL_GPL(sysfs_update_file);