Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * file.c - operations for regular (text) files. | |
3 | */ | |
4 | ||
5 | #include <linux/module.h> | |
0eeca283 | 6 | #include <linux/fsnotify.h> |
1da177e4 | 7 | #include <linux/kobject.h> |
5f45f1a7 | 8 | #include <linux/namei.h> |
4508a7a7 | 9 | #include <linux/poll.h> |
94bebf4d | 10 | #include <linux/list.h> |
1da177e4 LT |
11 | #include <asm/uaccess.h> |
12 | #include <asm/semaphore.h> | |
13 | ||
14 | #include "sysfs.h" | |
15 | ||
823bccfc | 16 | #define to_sattr(a) container_of(a,struct subsys_attribute, attr) |
1da177e4 | 17 | |
3d41088f | 18 | /* |
1da177e4 LT |
19 | * Subsystem file operations. |
20 | * These operations allow subsystems to have files that can be | |
21 | * read/written. | |
22 | */ | |
23 | static ssize_t | |
24 | subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page) | |
25 | { | |
823bccfc | 26 | struct kset *kset = to_kset(kobj); |
1da177e4 | 27 | struct subsys_attribute * sattr = to_sattr(attr); |
c76d0abd | 28 | ssize_t ret = -EIO; |
1da177e4 LT |
29 | |
30 | if (sattr->show) | |
823bccfc | 31 | ret = sattr->show(kset, page); |
1da177e4 LT |
32 | return ret; |
33 | } | |
34 | ||
35 | static ssize_t | |
36 | subsys_attr_store(struct kobject * kobj, struct attribute * attr, | |
37 | const char * page, size_t count) | |
38 | { | |
823bccfc | 39 | struct kset *kset = to_kset(kobj); |
1da177e4 | 40 | struct subsys_attribute * sattr = to_sattr(attr); |
c76d0abd | 41 | ssize_t ret = -EIO; |
1da177e4 LT |
42 | |
43 | if (sattr->store) | |
823bccfc | 44 | ret = sattr->store(kset, page, count); |
1da177e4 LT |
45 | return ret; |
46 | } | |
47 | ||
48 | static struct sysfs_ops subsys_sysfs_ops = { | |
49 | .show = subsys_attr_show, | |
50 | .store = subsys_attr_store, | |
51 | }; | |
52 | ||
73107cb3 TH |
53 | struct sysfs_buffer { |
54 | size_t count; | |
55 | loff_t pos; | |
56 | char * page; | |
57 | struct sysfs_ops * ops; | |
58 | struct semaphore sem; | |
59 | int needs_read_fill; | |
60 | int event; | |
61 | }; | |
1da177e4 LT |
62 | |
63 | /** | |
64 | * fill_read_buffer - allocate and fill buffer from object. | |
65 | * @dentry: dentry pointer. | |
66 | * @buffer: data buffer for file. | |
67 | * | |
68 | * Allocate @buffer->page, if it hasn't been already, then call the | |
69 | * kobject's show() method to fill the buffer with this attribute's | |
70 | * data. | |
82244b16 ON |
71 | * This is called only once, on the file's first read unless an error |
72 | * is returned. | |
1da177e4 LT |
73 | */ |
74 | static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer) | |
75 | { | |
0ab66088 TH |
76 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
77 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; | |
1da177e4 LT |
78 | struct sysfs_ops * ops = buffer->ops; |
79 | int ret = 0; | |
80 | ssize_t count; | |
81 | ||
82 | if (!buffer->page) | |
83 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); | |
84 | if (!buffer->page) | |
85 | return -ENOMEM; | |
86 | ||
0ab66088 TH |
87 | /* need attr_sd for attr and ops, its parent for kobj */ |
88 | if (!sysfs_get_active_two(attr_sd)) | |
89 | return -ENODEV; | |
90 | ||
91 | buffer->event = atomic_read(&attr_sd->s_event); | |
92 | count = ops->show(kobj, attr_sd->s_elem.attr.attr, buffer->page); | |
93 | ||
94 | sysfs_put_active_two(attr_sd); | |
95 | ||
1da177e4 | 96 | BUG_ON(count > (ssize_t)PAGE_SIZE); |
82244b16 ON |
97 | if (count >= 0) { |
98 | buffer->needs_read_fill = 0; | |
1da177e4 | 99 | buffer->count = count; |
82244b16 | 100 | } else { |
1da177e4 | 101 | ret = count; |
82244b16 | 102 | } |
1da177e4 LT |
103 | return ret; |
104 | } | |
105 | ||
1da177e4 LT |
106 | /** |
107 | * sysfs_read_file - read an attribute. | |
108 | * @file: file pointer. | |
109 | * @buf: buffer to fill. | |
110 | * @count: number of bytes to read. | |
111 | * @ppos: starting offset in file. | |
112 | * | |
113 | * Userspace wants to read an attribute file. The attribute descriptor | |
114 | * is in the file's ->d_fsdata. The target object is in the directory's | |
115 | * ->d_fsdata. | |
116 | * | |
117 | * We call fill_read_buffer() to allocate and fill the buffer from the | |
118 | * object's show() method exactly once (if the read is happening from | |
119 | * the beginning of the file). That should fill the entire buffer with | |
120 | * all the data the object has to offer for that attribute. | |
121 | * We then call flush_read_buffer() to copy the buffer to userspace | |
122 | * in the increments specified. | |
123 | */ | |
124 | ||
125 | static ssize_t | |
126 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
127 | { | |
128 | struct sysfs_buffer * buffer = file->private_data; | |
129 | ssize_t retval = 0; | |
130 | ||
131 | down(&buffer->sem); | |
132 | if (buffer->needs_read_fill) { | |
73107cb3 | 133 | retval = fill_read_buffer(file->f_path.dentry,buffer); |
e7b0d26a | 134 | if (retval) |
1da177e4 LT |
135 | goto out; |
136 | } | |
5c1fdf41 ZB |
137 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
138 | __FUNCTION__, count, *ppos, buffer->page); | |
92f4c701 AM |
139 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, |
140 | buffer->count); | |
1da177e4 LT |
141 | out: |
142 | up(&buffer->sem); | |
143 | return retval; | |
144 | } | |
145 | ||
1da177e4 LT |
146 | /** |
147 | * fill_write_buffer - copy buffer from userspace. | |
148 | * @buffer: data buffer for file. | |
67be2dd1 | 149 | * @buf: data from user. |
1da177e4 LT |
150 | * @count: number of bytes in @userbuf. |
151 | * | |
152 | * Allocate @buffer->page if it hasn't been already, then | |
153 | * copy the user-supplied buffer into it. | |
154 | */ | |
155 | ||
156 | static int | |
157 | fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) | |
158 | { | |
159 | int error; | |
160 | ||
161 | if (!buffer->page) | |
162 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); | |
163 | if (!buffer->page) | |
164 | return -ENOMEM; | |
165 | ||
166 | if (count >= PAGE_SIZE) | |
6e0dd741 | 167 | count = PAGE_SIZE - 1; |
1da177e4 LT |
168 | error = copy_from_user(buffer->page,buf,count); |
169 | buffer->needs_read_fill = 1; | |
035ed7a4 TM |
170 | /* if buf is assumed to contain a string, terminate it by \0, |
171 | so e.g. sscanf() can scan the string easily */ | |
172 | buffer->page[count] = 0; | |
1da177e4 LT |
173 | return error ? -EFAULT : count; |
174 | } | |
175 | ||
176 | ||
177 | /** | |
178 | * flush_write_buffer - push buffer to kobject. | |
3d41088f | 179 | * @dentry: dentry to the attribute |
1da177e4 | 180 | * @buffer: data buffer for file. |
3d41088f | 181 | * @count: number of bytes |
1da177e4 LT |
182 | * |
183 | * Get the correct pointers for the kobject and the attribute we're | |
184 | * dealing with, then call the store() method for the attribute, | |
185 | * passing the buffer that we acquired in fill_write_buffer(). | |
186 | */ | |
187 | ||
0ab66088 | 188 | static int |
1da177e4 LT |
189 | flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count) |
190 | { | |
3e519038 | 191 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
0ab66088 | 192 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
1da177e4 | 193 | struct sysfs_ops * ops = buffer->ops; |
0ab66088 TH |
194 | int rc; |
195 | ||
196 | /* need attr_sd for attr and ops, its parent for kobj */ | |
197 | if (!sysfs_get_active_two(attr_sd)) | |
198 | return -ENODEV; | |
199 | ||
200 | rc = ops->store(kobj, attr_sd->s_elem.attr.attr, buffer->page, count); | |
201 | ||
202 | sysfs_put_active_two(attr_sd); | |
1da177e4 | 203 | |
0ab66088 | 204 | return rc; |
1da177e4 LT |
205 | } |
206 | ||
207 | ||
208 | /** | |
209 | * sysfs_write_file - write an attribute. | |
210 | * @file: file pointer | |
211 | * @buf: data to write | |
212 | * @count: number of bytes | |
213 | * @ppos: starting offset | |
214 | * | |
215 | * Similar to sysfs_read_file(), though working in the opposite direction. | |
216 | * We allocate and fill the data from the user in fill_write_buffer(), | |
217 | * then push it to the kobject in flush_write_buffer(). | |
218 | * There is no easy way for us to know if userspace is only doing a partial | |
219 | * write, so we don't support them. We expect the entire buffer to come | |
220 | * on the first write. | |
221 | * Hint: if you're writing a value, first read the file, modify only the | |
222 | * the value you're changing, then write entire buffer back. | |
223 | */ | |
224 | ||
225 | static ssize_t | |
226 | sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | |
227 | { | |
228 | struct sysfs_buffer * buffer = file->private_data; | |
229 | ssize_t len; | |
230 | ||
231 | down(&buffer->sem); | |
232 | len = fill_write_buffer(buffer, buf, count); | |
233 | if (len > 0) | |
f427f5d5 | 234 | len = flush_write_buffer(file->f_path.dentry, buffer, len); |
1da177e4 LT |
235 | if (len > 0) |
236 | *ppos += len; | |
237 | up(&buffer->sem); | |
238 | return len; | |
239 | } | |
240 | ||
94bebf4d | 241 | static int sysfs_open_file(struct inode *inode, struct file *file) |
1da177e4 | 242 | { |
3e519038 | 243 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
0ab66088 | 244 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
1da177e4 LT |
245 | struct sysfs_buffer * buffer; |
246 | struct sysfs_ops * ops = NULL; | |
0ab66088 | 247 | int error; |
1da177e4 | 248 | |
0ab66088 TH |
249 | /* need attr_sd for attr and ops, its parent for kobj */ |
250 | if (!sysfs_get_active_two(attr_sd)) | |
251 | return -ENODEV; | |
1da177e4 | 252 | |
1da177e4 LT |
253 | /* if the kobject has no ktype, then we assume that it is a subsystem |
254 | * itself, and use ops for it. | |
255 | */ | |
256 | if (kobj->kset && kobj->kset->ktype) | |
257 | ops = kobj->kset->ktype->sysfs_ops; | |
258 | else if (kobj->ktype) | |
259 | ops = kobj->ktype->sysfs_ops; | |
260 | else | |
261 | ops = &subsys_sysfs_ops; | |
262 | ||
73107cb3 TH |
263 | error = -EACCES; |
264 | ||
1da177e4 LT |
265 | /* No sysfs operations, either from having no subsystem, |
266 | * or the subsystem have no operations. | |
267 | */ | |
268 | if (!ops) | |
7b595756 | 269 | goto err_out; |
1da177e4 LT |
270 | |
271 | /* File needs write support. | |
272 | * The inode's perms must say it's ok, | |
273 | * and we must have a store method. | |
274 | */ | |
275 | if (file->f_mode & FMODE_WRITE) { | |
1da177e4 | 276 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
7b595756 | 277 | goto err_out; |
1da177e4 LT |
278 | } |
279 | ||
280 | /* File needs read support. | |
281 | * The inode's perms must say it's ok, and we there | |
282 | * must be a show method for it. | |
283 | */ | |
284 | if (file->f_mode & FMODE_READ) { | |
285 | if (!(inode->i_mode & S_IRUGO) || !ops->show) | |
7b595756 | 286 | goto err_out; |
1da177e4 LT |
287 | } |
288 | ||
289 | /* No error? Great, allocate a buffer for the file, and store it | |
290 | * it in file->private_data for easy access. | |
291 | */ | |
0ab66088 | 292 | error = -ENOMEM; |
58d49283 | 293 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
0ab66088 | 294 | if (!buffer) |
7b595756 | 295 | goto err_out; |
1da177e4 | 296 | |
0ab66088 TH |
297 | init_MUTEX(&buffer->sem); |
298 | buffer->needs_read_fill = 1; | |
299 | buffer->ops = ops; | |
0ab66088 TH |
300 | file->private_data = buffer; |
301 | ||
302 | /* open succeeded, put active references and pin attr_sd */ | |
303 | sysfs_put_active_two(attr_sd); | |
304 | sysfs_get(attr_sd); | |
305 | return 0; | |
306 | ||
7b595756 | 307 | err_out: |
0ab66088 | 308 | sysfs_put_active_two(attr_sd); |
1da177e4 LT |
309 | return error; |
310 | } | |
311 | ||
1da177e4 LT |
312 | static int sysfs_release(struct inode * inode, struct file * filp) |
313 | { | |
3e519038 | 314 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
73107cb3 | 315 | struct sysfs_buffer *buffer = filp->private_data; |
1da177e4 | 316 | |
0ab66088 | 317 | sysfs_put(attr_sd); |
1da177e4 LT |
318 | |
319 | if (buffer) { | |
320 | if (buffer->page) | |
321 | free_page((unsigned long)buffer->page); | |
322 | kfree(buffer); | |
323 | } | |
324 | return 0; | |
325 | } | |
326 | ||
4508a7a7 N |
327 | /* Sysfs attribute files are pollable. The idea is that you read |
328 | * the content and then you use 'poll' or 'select' to wait for | |
329 | * the content to change. When the content changes (assuming the | |
330 | * manager for the kobject supports notification), poll will | |
331 | * return POLLERR|POLLPRI, and select will return the fd whether | |
332 | * it is waiting for read, write, or exceptions. | |
333 | * Once poll/select indicates that the value has changed, you | |
334 | * need to close and re-open the file, as simply seeking and reading | |
335 | * again will not get new data, or reset the state of 'poll'. | |
336 | * Reminder: this only works for attributes which actively support | |
337 | * it, and it is not possible to test an attribute from userspace | |
338 | * to see if it supports poll (Nether 'poll' or 'select' return | |
339 | * an appropriate error code). When in doubt, set a suitable timeout value. | |
340 | */ | |
341 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) | |
342 | { | |
343 | struct sysfs_buffer * buffer = filp->private_data; | |
0ab66088 TH |
344 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
345 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; | |
346 | ||
347 | /* need parent for the kobj, grab both */ | |
348 | if (!sysfs_get_active_two(attr_sd)) | |
349 | goto trigger; | |
4508a7a7 N |
350 | |
351 | poll_wait(filp, &kobj->poll, wait); | |
352 | ||
0ab66088 TH |
353 | sysfs_put_active_two(attr_sd); |
354 | ||
355 | if (buffer->event != atomic_read(&attr_sd->s_event)) | |
356 | goto trigger; | |
4508a7a7 | 357 | |
0ab66088 TH |
358 | return 0; |
359 | ||
360 | trigger: | |
361 | buffer->needs_read_fill = 1; | |
362 | return POLLERR|POLLPRI; | |
4508a7a7 N |
363 | } |
364 | ||
365 | ||
366 | static struct dentry *step_down(struct dentry *dir, const char * name) | |
367 | { | |
368 | struct dentry * de; | |
369 | ||
370 | if (dir == NULL || dir->d_inode == NULL) | |
371 | return NULL; | |
372 | ||
373 | mutex_lock(&dir->d_inode->i_mutex); | |
374 | de = lookup_one_len(name, dir, strlen(name)); | |
375 | mutex_unlock(&dir->d_inode->i_mutex); | |
376 | dput(dir); | |
377 | if (IS_ERR(de)) | |
378 | return NULL; | |
379 | if (de->d_inode == NULL) { | |
380 | dput(de); | |
381 | return NULL; | |
382 | } | |
383 | return de; | |
384 | } | |
385 | ||
386 | void sysfs_notify(struct kobject * k, char *dir, char *attr) | |
387 | { | |
608e266a | 388 | struct dentry *de = k->sd->s_dentry; |
4508a7a7 N |
389 | if (de) |
390 | dget(de); | |
391 | if (de && dir) | |
392 | de = step_down(de, dir); | |
393 | if (de && attr) | |
394 | de = step_down(de, attr); | |
395 | if (de) { | |
396 | struct sysfs_dirent * sd = de->d_fsdata; | |
397 | if (sd) | |
398 | atomic_inc(&sd->s_event); | |
399 | wake_up_interruptible(&k->poll); | |
400 | dput(de); | |
401 | } | |
402 | } | |
403 | EXPORT_SYMBOL_GPL(sysfs_notify); | |
404 | ||
4b6f5d20 | 405 | const struct file_operations sysfs_file_operations = { |
1da177e4 LT |
406 | .read = sysfs_read_file, |
407 | .write = sysfs_write_file, | |
408 | .llseek = generic_file_llseek, | |
409 | .open = sysfs_open_file, | |
410 | .release = sysfs_release, | |
4508a7a7 | 411 | .poll = sysfs_poll, |
1da177e4 LT |
412 | }; |
413 | ||
414 | ||
608e266a TH |
415 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
416 | int type) | |
1da177e4 | 417 | { |
608e266a | 418 | struct dentry *dir = dir_sd->s_dentry; |
1da177e4 | 419 | umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG; |
a26cd722 TH |
420 | struct sysfs_dirent *sd; |
421 | int error = 0; | |
1da177e4 | 422 | |
1b1dcc1b | 423 | mutex_lock(&dir->d_inode->i_mutex); |
1da177e4 | 424 | |
608e266a | 425 | if (sysfs_find_dirent(dir_sd, attr->name)) { |
a26cd722 TH |
426 | error = -EEXIST; |
427 | goto out_unlock; | |
428 | } | |
429 | ||
3e519038 | 430 | sd = sysfs_new_dirent(attr->name, mode, type); |
a26cd722 TH |
431 | if (!sd) { |
432 | error = -ENOMEM; | |
433 | goto out_unlock; | |
434 | } | |
3e519038 | 435 | sd->s_elem.attr.attr = (void *)attr; |
608e266a | 436 | sysfs_attach_dirent(sd, dir_sd, NULL); |
a26cd722 TH |
437 | |
438 | out_unlock: | |
439 | mutex_unlock(&dir->d_inode->i_mutex); | |
1da177e4 LT |
440 | return error; |
441 | } | |
442 | ||
443 | ||
444 | /** | |
445 | * sysfs_create_file - create an attribute file for an object. | |
446 | * @kobj: object we're creating for. | |
447 | * @attr: atrribute descriptor. | |
448 | */ | |
449 | ||
450 | int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) | |
451 | { | |
608e266a | 452 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 453 | |
608e266a | 454 | return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR); |
1da177e4 LT |
455 | |
456 | } | |
457 | ||
458 | ||
dfa87c82 AS |
459 | /** |
460 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. | |
461 | * @kobj: object we're acting for. | |
462 | * @attr: attribute descriptor. | |
463 | * @group: group name. | |
464 | */ | |
465 | int sysfs_add_file_to_group(struct kobject *kobj, | |
466 | const struct attribute *attr, const char *group) | |
467 | { | |
608e266a | 468 | struct sysfs_dirent *dir_sd; |
dfa87c82 AS |
469 | int error; |
470 | ||
608e266a TH |
471 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
472 | if (!dir_sd) | |
473 | return -ENOENT; | |
474 | ||
475 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); | |
476 | sysfs_put(dir_sd); | |
477 | ||
dfa87c82 AS |
478 | return error; |
479 | } | |
480 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); | |
481 | ||
482 | ||
1da177e4 LT |
483 | /** |
484 | * sysfs_update_file - update the modified timestamp on an object attribute. | |
485 | * @kobj: object we're acting for. | |
486 | * @attr: attribute descriptor. | |
1da177e4 LT |
487 | */ |
488 | int sysfs_update_file(struct kobject * kobj, const struct attribute * attr) | |
489 | { | |
608e266a | 490 | struct dentry *dir = kobj->sd->s_dentry; |
1da177e4 LT |
491 | struct dentry * victim; |
492 | int res = -ENOENT; | |
493 | ||
1b1dcc1b | 494 | mutex_lock(&dir->d_inode->i_mutex); |
5f45f1a7 | 495 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
1da177e4 LT |
496 | if (!IS_ERR(victim)) { |
497 | /* make sure dentry is really there */ | |
498 | if (victim->d_inode && | |
499 | (victim->d_parent->d_inode == dir->d_inode)) { | |
500 | victim->d_inode->i_mtime = CURRENT_TIME; | |
0eeca283 | 501 | fsnotify_modify(victim); |
1da177e4 LT |
502 | res = 0; |
503 | } else | |
504 | d_drop(victim); | |
505 | ||
506 | /** | |
97a50184 | 507 | * Drop the reference acquired from lookup_one_len() above. |
1da177e4 LT |
508 | */ |
509 | dput(victim); | |
510 | } | |
1b1dcc1b | 511 | mutex_unlock(&dir->d_inode->i_mutex); |
1da177e4 LT |
512 | |
513 | return res; | |
514 | } | |
515 | ||
516 | ||
31e5abe9 KS |
517 | /** |
518 | * sysfs_chmod_file - update the modified mode value on an object attribute. | |
519 | * @kobj: object we're acting for. | |
520 | * @attr: attribute descriptor. | |
521 | * @mode: file permissions. | |
522 | * | |
523 | */ | |
524 | int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode) | |
525 | { | |
608e266a | 526 | struct dentry *dir = kobj->sd->s_dentry; |
31e5abe9 | 527 | struct dentry *victim; |
bc062b1b MS |
528 | struct inode * inode; |
529 | struct iattr newattrs; | |
31e5abe9 KS |
530 | int res = -ENOENT; |
531 | ||
1b1dcc1b | 532 | mutex_lock(&dir->d_inode->i_mutex); |
5f45f1a7 | 533 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
31e5abe9 KS |
534 | if (!IS_ERR(victim)) { |
535 | if (victim->d_inode && | |
536 | (victim->d_parent->d_inode == dir->d_inode)) { | |
bc062b1b | 537 | inode = victim->d_inode; |
1b1dcc1b | 538 | mutex_lock(&inode->i_mutex); |
bc062b1b MS |
539 | newattrs.ia_mode = (mode & S_IALLUGO) | |
540 | (inode->i_mode & ~S_IALLUGO); | |
541 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; | |
542 | res = notify_change(victim, &newattrs); | |
1b1dcc1b | 543 | mutex_unlock(&inode->i_mutex); |
31e5abe9 | 544 | } |
bc062b1b | 545 | dput(victim); |
31e5abe9 | 546 | } |
1b1dcc1b | 547 | mutex_unlock(&dir->d_inode->i_mutex); |
31e5abe9 KS |
548 | |
549 | return res; | |
550 | } | |
551 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); | |
552 | ||
553 | ||
1da177e4 LT |
554 | /** |
555 | * sysfs_remove_file - remove an object attribute. | |
556 | * @kobj: object we're acting for. | |
557 | * @attr: attribute descriptor. | |
558 | * | |
559 | * Hash the attribute name and kill the victim. | |
560 | */ | |
561 | ||
562 | void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) | |
563 | { | |
608e266a | 564 | sysfs_hash_and_remove(kobj->sd, attr->name); |
1da177e4 LT |
565 | } |
566 | ||
567 | ||
dfa87c82 AS |
568 | /** |
569 | * sysfs_remove_file_from_group - remove an attribute file from a group. | |
570 | * @kobj: object we're acting for. | |
571 | * @attr: attribute descriptor. | |
572 | * @group: group name. | |
573 | */ | |
574 | void sysfs_remove_file_from_group(struct kobject *kobj, | |
575 | const struct attribute *attr, const char *group) | |
576 | { | |
608e266a | 577 | struct sysfs_dirent *dir_sd; |
dfa87c82 | 578 | |
608e266a TH |
579 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
580 | if (dir_sd) { | |
581 | sysfs_hash_and_remove(dir_sd, attr->name); | |
582 | sysfs_put(dir_sd); | |
dfa87c82 AS |
583 | } |
584 | } | |
585 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); | |
586 | ||
d9a9cdfb AS |
587 | struct sysfs_schedule_callback_struct { |
588 | struct kobject *kobj; | |
589 | void (*func)(void *); | |
590 | void *data; | |
523ded71 | 591 | struct module *owner; |
d9a9cdfb AS |
592 | struct work_struct work; |
593 | }; | |
594 | ||
595 | static void sysfs_schedule_callback_work(struct work_struct *work) | |
596 | { | |
597 | struct sysfs_schedule_callback_struct *ss = container_of(work, | |
598 | struct sysfs_schedule_callback_struct, work); | |
599 | ||
600 | (ss->func)(ss->data); | |
601 | kobject_put(ss->kobj); | |
523ded71 | 602 | module_put(ss->owner); |
d9a9cdfb AS |
603 | kfree(ss); |
604 | } | |
605 | ||
606 | /** | |
607 | * sysfs_schedule_callback - helper to schedule a callback for a kobject | |
608 | * @kobj: object we're acting for. | |
609 | * @func: callback function to invoke later. | |
610 | * @data: argument to pass to @func. | |
523ded71 | 611 | * @owner: module owning the callback code |
d9a9cdfb AS |
612 | * |
613 | * sysfs attribute methods must not unregister themselves or their parent | |
614 | * kobject (which would amount to the same thing). Attempts to do so will | |
615 | * deadlock, since unregistration is mutually exclusive with driver | |
616 | * callbacks. | |
617 | * | |
618 | * Instead methods can call this routine, which will attempt to allocate | |
619 | * and schedule a workqueue request to call back @func with @data as its | |
620 | * argument in the workqueue's process context. @kobj will be pinned | |
621 | * until @func returns. | |
622 | * | |
623 | * Returns 0 if the request was submitted, -ENOMEM if storage could not | |
523ded71 | 624 | * be allocated, -ENODEV if a reference to @owner isn't available. |
d9a9cdfb AS |
625 | */ |
626 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), | |
523ded71 | 627 | void *data, struct module *owner) |
d9a9cdfb AS |
628 | { |
629 | struct sysfs_schedule_callback_struct *ss; | |
630 | ||
523ded71 AS |
631 | if (!try_module_get(owner)) |
632 | return -ENODEV; | |
d9a9cdfb | 633 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
523ded71 AS |
634 | if (!ss) { |
635 | module_put(owner); | |
d9a9cdfb | 636 | return -ENOMEM; |
523ded71 | 637 | } |
d9a9cdfb AS |
638 | kobject_get(kobj); |
639 | ss->kobj = kobj; | |
640 | ss->func = func; | |
641 | ss->data = data; | |
523ded71 | 642 | ss->owner = owner; |
d9a9cdfb AS |
643 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
644 | schedule_work(&ss->work); | |
645 | return 0; | |
646 | } | |
647 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); | |
648 | ||
dfa87c82 | 649 | |
1da177e4 LT |
650 | EXPORT_SYMBOL_GPL(sysfs_create_file); |
651 | EXPORT_SYMBOL_GPL(sysfs_remove_file); | |
652 | EXPORT_SYMBOL_GPL(sysfs_update_file); |