blob: 46618f81ae48bb86f6a6af736bb9862d85a61807 [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:
Mariusz Kozlowskif7506532007-01-02 13:41:10 +0100364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 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);
Mariusz Kozlowskif7506532007-01-02 13:41:10 +0100378 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* After this point, attr should not be accessed. */
380 module_put(owner);
381
382 if (buffer) {
383 if (buffer->page)
384 free_page((unsigned long)buffer->page);
385 kfree(buffer);
386 }
387 return 0;
388}
389
NeilBrown4508a7a2006-03-20 17:53:53 +1100390/* Sysfs attribute files are pollable. The idea is that you read
391 * the content and then you use 'poll' or 'select' to wait for
392 * the content to change. When the content changes (assuming the
393 * manager for the kobject supports notification), poll will
394 * return POLLERR|POLLPRI, and select will return the fd whether
395 * it is waiting for read, write, or exceptions.
396 * Once poll/select indicates that the value has changed, you
397 * need to close and re-open the file, as simply seeking and reading
398 * again will not get new data, or reset the state of 'poll'.
399 * Reminder: this only works for attributes which actively support
400 * it, and it is not possible to test an attribute from userspace
401 * to see if it supports poll (Nether 'poll' or 'select' return
402 * an appropriate error code). When in doubt, set a suitable timeout value.
403 */
404static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
405{
406 struct sysfs_buffer * buffer = filp->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800407 struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
408 struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
NeilBrown4508a7a2006-03-20 17:53:53 +1100409 int res = 0;
410
411 poll_wait(filp, &kobj->poll, wait);
412
413 if (buffer->event != atomic_read(&sd->s_event)) {
414 res = POLLERR|POLLPRI;
415 buffer->needs_read_fill = 1;
416 }
417
418 return res;
419}
420
421
422static struct dentry *step_down(struct dentry *dir, const char * name)
423{
424 struct dentry * de;
425
426 if (dir == NULL || dir->d_inode == NULL)
427 return NULL;
428
429 mutex_lock(&dir->d_inode->i_mutex);
430 de = lookup_one_len(name, dir, strlen(name));
431 mutex_unlock(&dir->d_inode->i_mutex);
432 dput(dir);
433 if (IS_ERR(de))
434 return NULL;
435 if (de->d_inode == NULL) {
436 dput(de);
437 return NULL;
438 }
439 return de;
440}
441
442void sysfs_notify(struct kobject * k, char *dir, char *attr)
443{
444 struct dentry *de = k->dentry;
445 if (de)
446 dget(de);
447 if (de && dir)
448 de = step_down(de, dir);
449 if (de && attr)
450 de = step_down(de, attr);
451 if (de) {
452 struct sysfs_dirent * sd = de->d_fsdata;
453 if (sd)
454 atomic_inc(&sd->s_event);
455 wake_up_interruptible(&k->poll);
456 dput(de);
457 }
458}
459EXPORT_SYMBOL_GPL(sysfs_notify);
460
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800461const struct file_operations sysfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 .read = sysfs_read_file,
463 .write = sysfs_write_file,
464 .llseek = generic_file_llseek,
465 .open = sysfs_open_file,
466 .release = sysfs_release,
NeilBrown4508a7a2006-03-20 17:53:53 +1100467 .poll = sysfs_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468};
469
470
471int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
472{
473 struct sysfs_dirent * parent_sd = dir->d_fsdata;
474 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
Maneesh Sonic5168652006-03-09 19:40:14 +0530475 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800477 mutex_lock(&dir->d_inode->i_mutex);
Maneesh Sonic5168652006-03-09 19:40:14 +0530478 if (!sysfs_dirent_exist(parent_sd, attr->name))
479 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
480 mode, type);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800481 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return error;
484}
485
486
487/**
488 * sysfs_create_file - create an attribute file for an object.
489 * @kobj: object we're creating for.
490 * @attr: atrribute descriptor.
491 */
492
493int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
494{
495 BUG_ON(!kobj || !kobj->dentry || !attr);
496
497 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
498
499}
500
501
502/**
503 * sysfs_update_file - update the modified timestamp on an object attribute.
504 * @kobj: object we're acting for.
505 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
507int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
508{
509 struct dentry * dir = kobj->dentry;
510 struct dentry * victim;
511 int res = -ENOENT;
512
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800513 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700514 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (!IS_ERR(victim)) {
516 /* make sure dentry is really there */
517 if (victim->d_inode &&
518 (victim->d_parent->d_inode == dir->d_inode)) {
519 victim->d_inode->i_mtime = CURRENT_TIME;
Robert Love0eeca282005-07-12 17:06:03 -0400520 fsnotify_modify(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 res = 0;
522 } else
523 d_drop(victim);
524
525 /**
Hidetoshi Seto97a50182006-09-20 16:49:02 +0900526 * Drop the reference acquired from lookup_one_len() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528 dput(victim);
529 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800530 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return res;
533}
534
535
536/**
Kay Sievers31e5abe2005-04-18 21:57:32 -0700537 * sysfs_chmod_file - update the modified mode value on an object attribute.
538 * @kobj: object we're acting for.
539 * @attr: attribute descriptor.
540 * @mode: file permissions.
541 *
542 */
543int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
544{
545 struct dentry *dir = kobj->dentry;
546 struct dentry *victim;
Maneesh Sonibc062b12005-07-29 12:13:35 -0700547 struct inode * inode;
548 struct iattr newattrs;
Kay Sievers31e5abe2005-04-18 21:57:32 -0700549 int res = -ENOENT;
550
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800551 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700552 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
Kay Sievers31e5abe2005-04-18 21:57:32 -0700553 if (!IS_ERR(victim)) {
554 if (victim->d_inode &&
555 (victim->d_parent->d_inode == dir->d_inode)) {
Maneesh Sonibc062b12005-07-29 12:13:35 -0700556 inode = victim->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800557 mutex_lock(&inode->i_mutex);
Maneesh Sonibc062b12005-07-29 12:13:35 -0700558 newattrs.ia_mode = (mode & S_IALLUGO) |
559 (inode->i_mode & ~S_IALLUGO);
560 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
561 res = notify_change(victim, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800562 mutex_unlock(&inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700563 }
Maneesh Sonibc062b12005-07-29 12:13:35 -0700564 dput(victim);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700565 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800566 mutex_unlock(&dir->d_inode->i_mutex);
Kay Sievers31e5abe2005-04-18 21:57:32 -0700567
568 return res;
569}
570EXPORT_SYMBOL_GPL(sysfs_chmod_file);
571
572
573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * sysfs_remove_file - remove an object attribute.
575 * @kobj: object we're acting for.
576 * @attr: attribute descriptor.
577 *
578 * Hash the attribute name and kill the victim.
579 */
580
581void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
582{
Oliver Neukum94bebf42006-12-20 10:52:44 +0100583 sysfs_hash_and_remove(kobj->dentry, attr->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586
587EXPORT_SYMBOL_GPL(sysfs_create_file);
588EXPORT_SYMBOL_GPL(sysfs_remove_file);
589EXPORT_SYMBOL_GPL(sysfs_update_file);