]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/sysfs/file.c
[PATCH] remove duplicate get_dentry functions in various places
[mirror_ubuntu-bionic-kernel.git] / fs / sysfs / file.c
1 /*
2 * file.c - operations for regular (text) files.
3 */
4
5 #include <linux/module.h>
6 #include <linux/dnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <asm/uaccess.h>
10 #include <asm/semaphore.h>
11
12 #include "sysfs.h"
13
14 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
15 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
16
17 /**
18 * Subsystem file operations.
19 * These operations allow subsystems to have files that can be
20 * read/written.
21 */
22 static ssize_t
23 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
24 {
25 struct subsystem * s = to_subsys(kobj);
26 struct subsys_attribute * sattr = to_sattr(attr);
27 ssize_t ret = -EIO;
28
29 if (sattr->show)
30 ret = sattr->show(s,page);
31 return ret;
32 }
33
34 static ssize_t
35 subsys_attr_store(struct kobject * kobj, struct attribute * attr,
36 const char * page, size_t count)
37 {
38 struct subsystem * s = to_subsys(kobj);
39 struct subsys_attribute * sattr = to_sattr(attr);
40 ssize_t ret = -EIO;
41
42 if (sattr->store)
43 ret = sattr->store(s,page,count);
44 return ret;
45 }
46
47 static struct sysfs_ops subsys_sysfs_ops = {
48 .show = subsys_attr_show,
49 .store = subsys_attr_store,
50 };
51
52
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 };
61
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.
71 * This is called only once, on the file's first read.
72 */
73 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
74 {
75 struct attribute * attr = to_attr(dentry);
76 struct kobject * kobj = to_kobj(dentry->d_parent);
77 struct sysfs_ops * ops = buffer->ops;
78 int ret = 0;
79 ssize_t count;
80
81 if (!buffer->page)
82 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
83 if (!buffer->page)
84 return -ENOMEM;
85
86 count = ops->show(kobj,attr,buffer->page);
87 buffer->needs_read_fill = 0;
88 BUG_ON(count > (ssize_t)PAGE_SIZE);
89 if (count >= 0)
90 buffer->count = count;
91 else
92 ret = count;
93 return ret;
94 }
95
96
97 /**
98 * flush_read_buffer - push buffer to userspace.
99 * @buffer: data buffer for file.
100 * @buf: user-passed buffer.
101 * @count: number of bytes requested.
102 * @ppos: file position.
103 *
104 * Copy the buffer we filled in fill_read_buffer() to userspace.
105 * This is done at the reader's leisure, copying and advancing
106 * the amount they specify each time.
107 * This may be called continuously until the buffer is empty.
108 */
109 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
110 size_t count, loff_t * ppos)
111 {
112 int error;
113
114 if (*ppos > buffer->count)
115 return 0;
116
117 if (count > (buffer->count - *ppos))
118 count = buffer->count - *ppos;
119
120 error = copy_to_user(buf,buffer->page + *ppos,count);
121 if (!error)
122 *ppos += count;
123 return error ? -EFAULT : count;
124 }
125
126 /**
127 * sysfs_read_file - read an attribute.
128 * @file: file pointer.
129 * @buf: buffer to fill.
130 * @count: number of bytes to read.
131 * @ppos: starting offset in file.
132 *
133 * Userspace wants to read an attribute file. The attribute descriptor
134 * is in the file's ->d_fsdata. The target object is in the directory's
135 * ->d_fsdata.
136 *
137 * We call fill_read_buffer() to allocate and fill the buffer from the
138 * object's show() method exactly once (if the read is happening from
139 * the beginning of the file). That should fill the entire buffer with
140 * all the data the object has to offer for that attribute.
141 * We then call flush_read_buffer() to copy the buffer to userspace
142 * in the increments specified.
143 */
144
145 static ssize_t
146 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
147 {
148 struct sysfs_buffer * buffer = file->private_data;
149 ssize_t retval = 0;
150
151 down(&buffer->sem);
152 if (buffer->needs_read_fill) {
153 if ((retval = fill_read_buffer(file->f_dentry,buffer)))
154 goto out;
155 }
156 pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
157 __FUNCTION__,count,*ppos,buffer->page);
158 retval = flush_read_buffer(buffer,buf,count,ppos);
159 out:
160 up(&buffer->sem);
161 return retval;
162 }
163
164
165 /**
166 * fill_write_buffer - copy buffer from userspace.
167 * @buffer: data buffer for file.
168 * @buf: data from user.
169 * @count: number of bytes in @userbuf.
170 *
171 * Allocate @buffer->page if it hasn't been already, then
172 * copy the user-supplied buffer into it.
173 */
174
175 static int
176 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
177 {
178 int error;
179
180 if (!buffer->page)
181 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
182 if (!buffer->page)
183 return -ENOMEM;
184
185 if (count >= PAGE_SIZE)
186 count = PAGE_SIZE;
187 error = copy_from_user(buffer->page,buf,count);
188 buffer->needs_read_fill = 1;
189 return error ? -EFAULT : count;
190 }
191
192
193 /**
194 * flush_write_buffer - push buffer to kobject.
195 * @file: file pointer.
196 * @buffer: data buffer for file.
197 *
198 * Get the correct pointers for the kobject and the attribute we're
199 * dealing with, then call the store() method for the attribute,
200 * passing the buffer that we acquired in fill_write_buffer().
201 */
202
203 static int
204 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
205 {
206 struct attribute * attr = to_attr(dentry);
207 struct kobject * kobj = to_kobj(dentry->d_parent);
208 struct sysfs_ops * ops = buffer->ops;
209
210 return ops->store(kobj,attr,buffer->page,count);
211 }
212
213
214 /**
215 * sysfs_write_file - write an attribute.
216 * @file: file pointer
217 * @buf: data to write
218 * @count: number of bytes
219 * @ppos: starting offset
220 *
221 * Similar to sysfs_read_file(), though working in the opposite direction.
222 * We allocate and fill the data from the user in fill_write_buffer(),
223 * then push it to the kobject in flush_write_buffer().
224 * There is no easy way for us to know if userspace is only doing a partial
225 * write, so we don't support them. We expect the entire buffer to come
226 * on the first write.
227 * Hint: if you're writing a value, first read the file, modify only the
228 * the value you're changing, then write entire buffer back.
229 */
230
231 static ssize_t
232 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
233 {
234 struct sysfs_buffer * buffer = file->private_data;
235 ssize_t len;
236
237 down(&buffer->sem);
238 len = fill_write_buffer(buffer, buf, count);
239 if (len > 0)
240 len = flush_write_buffer(file->f_dentry, buffer, len);
241 if (len > 0)
242 *ppos += len;
243 up(&buffer->sem);
244 return len;
245 }
246
247 static int check_perm(struct inode * inode, struct file * file)
248 {
249 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
250 struct attribute * attr = to_attr(file->f_dentry);
251 struct sysfs_buffer * buffer;
252 struct sysfs_ops * ops = NULL;
253 int error = 0;
254
255 if (!kobj || !attr)
256 goto Einval;
257
258 /* Grab the module reference for this attribute if we have one */
259 if (!try_module_get(attr->owner)) {
260 error = -ENODEV;
261 goto Done;
262 }
263
264 /* if the kobject has no ktype, then we assume that it is a subsystem
265 * itself, and use ops for it.
266 */
267 if (kobj->kset && kobj->kset->ktype)
268 ops = kobj->kset->ktype->sysfs_ops;
269 else if (kobj->ktype)
270 ops = kobj->ktype->sysfs_ops;
271 else
272 ops = &subsys_sysfs_ops;
273
274 /* No sysfs operations, either from having no subsystem,
275 * or the subsystem have no operations.
276 */
277 if (!ops)
278 goto Eaccess;
279
280 /* File needs write support.
281 * The inode's perms must say it's ok,
282 * and we must have a store method.
283 */
284 if (file->f_mode & FMODE_WRITE) {
285
286 if (!(inode->i_mode & S_IWUGO) || !ops->store)
287 goto Eaccess;
288
289 }
290
291 /* File needs read support.
292 * The inode's perms must say it's ok, and we there
293 * must be a show method for it.
294 */
295 if (file->f_mode & FMODE_READ) {
296 if (!(inode->i_mode & S_IRUGO) || !ops->show)
297 goto Eaccess;
298 }
299
300 /* No error? Great, allocate a buffer for the file, and store it
301 * it in file->private_data for easy access.
302 */
303 buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
304 if (buffer) {
305 memset(buffer,0,sizeof(struct sysfs_buffer));
306 init_MUTEX(&buffer->sem);
307 buffer->needs_read_fill = 1;
308 buffer->ops = ops;
309 file->private_data = buffer;
310 } else
311 error = -ENOMEM;
312 goto Done;
313
314 Einval:
315 error = -EINVAL;
316 goto Done;
317 Eaccess:
318 error = -EACCES;
319 module_put(attr->owner);
320 Done:
321 if (error && kobj)
322 kobject_put(kobj);
323 return error;
324 }
325
326 static int sysfs_open_file(struct inode * inode, struct file * filp)
327 {
328 return check_perm(inode,filp);
329 }
330
331 static int sysfs_release(struct inode * inode, struct file * filp)
332 {
333 struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
334 struct attribute * attr = to_attr(filp->f_dentry);
335 struct module * owner = attr->owner;
336 struct sysfs_buffer * buffer = filp->private_data;
337
338 if (kobj)
339 kobject_put(kobj);
340 /* After this point, attr should not be accessed. */
341 module_put(owner);
342
343 if (buffer) {
344 if (buffer->page)
345 free_page((unsigned long)buffer->page);
346 kfree(buffer);
347 }
348 return 0;
349 }
350
351 struct file_operations sysfs_file_operations = {
352 .read = sysfs_read_file,
353 .write = sysfs_write_file,
354 .llseek = generic_file_llseek,
355 .open = sysfs_open_file,
356 .release = sysfs_release,
357 };
358
359
360 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
361 {
362 struct sysfs_dirent * parent_sd = dir->d_fsdata;
363 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
364 int error = 0;
365
366 down(&dir->d_inode->i_sem);
367 error = sysfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
368 up(&dir->d_inode->i_sem);
369
370 return error;
371 }
372
373
374 /**
375 * sysfs_create_file - create an attribute file for an object.
376 * @kobj: object we're creating for.
377 * @attr: atrribute descriptor.
378 */
379
380 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
381 {
382 BUG_ON(!kobj || !kobj->dentry || !attr);
383
384 return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
385
386 }
387
388
389 /**
390 * sysfs_update_file - update the modified timestamp on an object attribute.
391 * @kobj: object we're acting for.
392 * @attr: attribute descriptor.
393 *
394 * Also call dnotify for the dentry, which lots of userspace programs
395 * use.
396 */
397 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
398 {
399 struct dentry * dir = kobj->dentry;
400 struct dentry * victim;
401 int res = -ENOENT;
402
403 down(&dir->d_inode->i_sem);
404 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
405 if (!IS_ERR(victim)) {
406 /* make sure dentry is really there */
407 if (victim->d_inode &&
408 (victim->d_parent->d_inode == dir->d_inode)) {
409 victim->d_inode->i_mtime = CURRENT_TIME;
410 dnotify_parent(victim, DN_MODIFY);
411
412 /**
413 * Drop reference from initial sysfs_get_dentry().
414 */
415 dput(victim);
416 res = 0;
417 } else
418 d_drop(victim);
419
420 /**
421 * Drop the reference acquired from sysfs_get_dentry() above.
422 */
423 dput(victim);
424 }
425 up(&dir->d_inode->i_sem);
426
427 return res;
428 }
429
430
431 /**
432 * sysfs_chmod_file - update the modified mode value on an object attribute.
433 * @kobj: object we're acting for.
434 * @attr: attribute descriptor.
435 * @mode: file permissions.
436 *
437 */
438 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
439 {
440 struct dentry *dir = kobj->dentry;
441 struct dentry *victim;
442 struct sysfs_dirent *sd;
443 umode_t umode = (mode & S_IALLUGO) | S_IFREG;
444 int res = -ENOENT;
445
446 down(&dir->d_inode->i_sem);
447 victim = lookup_one_len(attr->name, dir, strlen(attr->name));
448 if (!IS_ERR(victim)) {
449 if (victim->d_inode &&
450 (victim->d_parent->d_inode == dir->d_inode)) {
451 sd = victim->d_fsdata;
452 attr->mode = mode;
453 sd->s_mode = umode;
454 victim->d_inode->i_mode = umode;
455 dput(victim);
456 res = 0;
457 }
458 }
459 up(&dir->d_inode->i_sem);
460
461 return res;
462 }
463 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
464
465
466 /**
467 * sysfs_remove_file - remove an object attribute.
468 * @kobj: object we're acting for.
469 * @attr: attribute descriptor.
470 *
471 * Hash the attribute name and kill the victim.
472 */
473
474 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
475 {
476 sysfs_hash_and_remove(kobj->dentry,attr->name);
477 }
478
479
480 EXPORT_SYMBOL_GPL(sysfs_create_file);
481 EXPORT_SYMBOL_GPL(sysfs_remove_file);
482 EXPORT_SYMBOL_GPL(sysfs_update_file);
483