Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kobject.h> |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 15 | #include <linux/kallsyms.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
Miklos Szeredi | 93265d1 | 2008-06-16 13:46:47 +0200 | [diff] [blame] | 17 | #include <linux/fsnotify.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 18 | #include <linux/namei.h> |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 19 | #include <linux/poll.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 20 | #include <linux/list.h> |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Andrew Morton | ae87221d | 2007-08-24 16:11:54 -0700 | [diff] [blame] | 22 | #include <linux/limits.h> |
Greg Kroah-Hartman | 060cc74 | 2013-08-21 16:34:59 -0700 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 25 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "sysfs.h" |
| 28 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 29 | /* |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 30 | * There's one sysfs_open_file for each open file and one sysfs_open_dirent |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 31 | * for each sysfs_dirent with one or more open files. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 32 | * |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 33 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is |
| 34 | * protected by sysfs_open_dirent_lock. |
| 35 | * |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 36 | * filp->private_data points to seq_file whose ->private points to |
| 37 | * sysfs_open_file. sysfs_open_files are chained at |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 38 | * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 39 | */ |
Jiri Slaby | d7b3788 | 2007-11-21 14:55:19 -0800 | [diff] [blame] | 40 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 41 | static DEFINE_MUTEX(sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 42 | |
| 43 | struct sysfs_open_dirent { |
| 44 | atomic_t refcnt; |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 45 | atomic_t event; |
| 46 | wait_queue_head_t poll; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 47 | struct list_head files; /* goes through sysfs_open_file.list */ |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 50 | struct sysfs_open_file { |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 51 | struct sysfs_dirent *sd; |
| 52 | struct file *file; |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 53 | struct mutex mutex; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 54 | int event; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 55 | struct list_head list; |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 56 | |
| 57 | bool mmapped; |
| 58 | const struct vm_operations_struct *vm_ops; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 59 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 61 | static bool sysfs_is_bin(struct sysfs_dirent *sd) |
| 62 | { |
| 63 | return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR; |
| 64 | } |
| 65 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 66 | static struct sysfs_open_file *sysfs_of(struct file *file) |
| 67 | { |
| 68 | return ((struct seq_file *)file->private_data)->private; |
| 69 | } |
| 70 | |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 71 | /* |
| 72 | * Determine ktype->sysfs_ops for the given sysfs_dirent. This function |
| 73 | * must be called while holding an active reference. |
| 74 | */ |
| 75 | static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) |
| 76 | { |
| 77 | struct kobject *kobj = sd->s_parent->s_dir.kobj; |
| 78 | |
| 79 | lockdep_assert_held(sd); |
| 80 | return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; |
| 81 | } |
| 82 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 83 | /* |
| 84 | * Reads on sysfs are handled through seq_file, which takes care of hairy |
| 85 | * details like buffering and seeking. The following function pipes |
| 86 | * sysfs_ops->show() result through seq_file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 88 | static int sysfs_seq_show(struct seq_file *sf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 90 | struct sysfs_open_file *of = sf->private; |
| 91 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 92 | const struct sysfs_ops *ops; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 93 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | ssize_t count; |
| 95 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 96 | /* acquire buffer and ensure that it's >= PAGE_SIZE */ |
| 97 | count = seq_get_buf(sf, &buf); |
| 98 | if (count < PAGE_SIZE) { |
| 99 | seq_commit(sf, -1); |
| 100 | return 0; |
| 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 103 | /* |
| 104 | * Need @of->sd for attr and ops, its parent for kobj. @of->mutex |
| 105 | * nests outside active ref and is just to ensure that the ops |
| 106 | * aren't called concurrently for the same open file. |
| 107 | */ |
| 108 | mutex_lock(&of->mutex); |
| 109 | if (!sysfs_get_active(of->sd)) { |
| 110 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 111 | return -ENODEV; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 112 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 113 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 114 | of->event = atomic_read(&of->sd->s_attr.open->event); |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 115 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 116 | /* |
| 117 | * Lookup @ops and invoke show(). Control may reach here via seq |
| 118 | * file lseek even if @ops->show() isn't implemented. |
| 119 | */ |
| 120 | ops = sysfs_file_ops(of->sd); |
| 121 | if (ops->show) |
| 122 | count = ops->show(kobj, of->sd->s_attr.attr, buf); |
| 123 | else |
| 124 | count = 0; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 125 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 126 | sysfs_put_active(of->sd); |
| 127 | mutex_unlock(&of->mutex); |
| 128 | |
| 129 | if (count < 0) |
| 130 | return count; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 131 | |
Miao Xie | 8118a85 | 2007-11-21 14:55:19 -0800 | [diff] [blame] | 132 | /* |
| 133 | * The code works fine with PAGE_SIZE return but it's likely to |
| 134 | * indicate truncated result or overflow in normal use cases. |
| 135 | */ |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 136 | if (count >= (ssize_t)PAGE_SIZE) { |
| 137 | print_symbol("fill_read_buffer: %s returned bad count\n", |
| 138 | (unsigned long)ops->show); |
| 139 | /* Try to struggle along */ |
| 140 | count = PAGE_SIZE - 1; |
| 141 | } |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 142 | seq_commit(sf, count); |
| 143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 146 | /* |
| 147 | * Read method for bin files. As reading a bin file can have side-effects, |
| 148 | * the exact offset and bytes specified in read(2) call should be passed to |
| 149 | * the read callback making it difficult to use seq_file. Implement |
| 150 | * simplistic custom buffering for bin files. |
| 151 | */ |
| 152 | static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf, |
| 153 | size_t bytes, loff_t *off) |
| 154 | { |
| 155 | struct sysfs_open_file *of = sysfs_of(file); |
| 156 | struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr; |
| 157 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
| 158 | int size = file_inode(file)->i_size; |
| 159 | int count = min_t(size_t, bytes, PAGE_SIZE); |
| 160 | loff_t offs = *off; |
| 161 | char *buf; |
| 162 | |
| 163 | if (!bytes) |
| 164 | return 0; |
| 165 | |
| 166 | if (size) { |
| 167 | if (offs > size) |
| 168 | return 0; |
| 169 | if (offs + count > size) |
| 170 | count = size - offs; |
| 171 | } |
| 172 | |
| 173 | buf = kmalloc(count, GFP_KERNEL); |
| 174 | if (!buf) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | /* need of->sd for battr, its parent for kobj */ |
| 178 | mutex_lock(&of->mutex); |
| 179 | if (!sysfs_get_active(of->sd)) { |
| 180 | count = -ENODEV; |
| 181 | mutex_unlock(&of->mutex); |
| 182 | goto out_free; |
| 183 | } |
| 184 | |
| 185 | if (battr->read) |
| 186 | count = battr->read(file, kobj, battr, buf, offs, count); |
| 187 | else |
| 188 | count = -EIO; |
| 189 | |
| 190 | sysfs_put_active(of->sd); |
| 191 | mutex_unlock(&of->mutex); |
| 192 | |
| 193 | if (count < 0) |
| 194 | goto out_free; |
| 195 | |
| 196 | if (copy_to_user(userbuf, buf, count)) { |
| 197 | count = -EFAULT; |
| 198 | goto out_free; |
| 199 | } |
| 200 | |
| 201 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
| 202 | |
| 203 | *off = offs + count; |
| 204 | |
| 205 | out_free: |
| 206 | kfree(buf); |
| 207 | return count; |
| 208 | } |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /** |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 211 | * flush_write_buffer - push buffer to kobject |
| 212 | * @of: open file |
| 213 | * @buf: data buffer for file |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 214 | * @off: file offset to write to |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 215 | * @count: number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | * |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 217 | * Get the correct pointers for the kobject and the attribute we're dealing |
| 218 | * with, then call the store() method for it with @buf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 220 | static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off, |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 221 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 223 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 224 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 226 | /* |
| 227 | * Need @of->sd for attr and ops, its parent for kobj. @of->mutex |
| 228 | * nests outside active ref and is just to ensure that the ops |
| 229 | * aren't called concurrently for the same open file. |
| 230 | */ |
| 231 | mutex_lock(&of->mutex); |
| 232 | if (!sysfs_get_active(of->sd)) { |
| 233 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 234 | return -ENODEV; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 235 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 236 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 237 | if (sysfs_is_bin(of->sd)) { |
| 238 | struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr; |
| 239 | |
| 240 | rc = -EIO; |
| 241 | if (battr->write) |
| 242 | rc = battr->write(of->file, kobj, battr, buf, off, |
| 243 | count); |
| 244 | } else { |
| 245 | const struct sysfs_ops *ops = sysfs_file_ops(of->sd); |
| 246 | |
| 247 | rc = ops->store(kobj, of->sd->s_attr.attr, buf, count); |
| 248 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 249 | |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 250 | sysfs_put_active(of->sd); |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 251 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 252 | |
| 253 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /** |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 257 | * sysfs_write_file - write an attribute |
| 258 | * @file: file pointer |
| 259 | * @user_buf: data to write |
| 260 | * @count: number of bytes |
| 261 | * @ppos: starting offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 263 | * Copy data in from userland and pass it to the matching |
| 264 | * sysfs_ops->store() by invoking flush_write_buffer(). |
| 265 | * |
| 266 | * There is no easy way for us to know if userspace is only doing a partial |
| 267 | * write, so we don't support them. We expect the entire buffer to come on |
| 268 | * the first write. Hint: if you're writing a value, first read the file, |
| 269 | * modify only the the value you're changing, then write entire buffer |
| 270 | * back. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | */ |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 272 | static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf, |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 273 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 275 | struct sysfs_open_file *of = sysfs_of(file); |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 276 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 277 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 279 | if (sysfs_is_bin(of->sd)) { |
| 280 | loff_t size = file_inode(file)->i_size; |
| 281 | |
| 282 | if (size <= *ppos) |
| 283 | return 0; |
| 284 | len = min_t(ssize_t, len, size - *ppos); |
| 285 | } |
| 286 | |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 287 | if (!len) |
| 288 | return 0; |
| 289 | |
| 290 | buf = kmalloc(len + 1, GFP_KERNEL); |
| 291 | if (!buf) |
| 292 | return -ENOMEM; |
| 293 | |
| 294 | if (copy_from_user(buf, user_buf, len)) { |
| 295 | len = -EFAULT; |
| 296 | goto out_free; |
| 297 | } |
| 298 | buf[len] = '\0'; /* guarantee string termination */ |
| 299 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 300 | len = flush_write_buffer(of, buf, *ppos, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (len > 0) |
| 302 | *ppos += len; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 303 | out_free: |
| 304 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return len; |
| 306 | } |
| 307 | |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 308 | static void sysfs_bin_vma_open(struct vm_area_struct *vma) |
| 309 | { |
| 310 | struct file *file = vma->vm_file; |
| 311 | struct sysfs_open_file *of = sysfs_of(file); |
| 312 | |
| 313 | if (!of->vm_ops) |
| 314 | return; |
| 315 | |
| 316 | if (!sysfs_get_active(of->sd)) |
| 317 | return; |
| 318 | |
| 319 | if (of->vm_ops->open) |
| 320 | of->vm_ops->open(vma); |
| 321 | |
| 322 | sysfs_put_active(of->sd); |
| 323 | } |
| 324 | |
| 325 | static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 326 | { |
| 327 | struct file *file = vma->vm_file; |
| 328 | struct sysfs_open_file *of = sysfs_of(file); |
| 329 | int ret; |
| 330 | |
| 331 | if (!of->vm_ops) |
| 332 | return VM_FAULT_SIGBUS; |
| 333 | |
| 334 | if (!sysfs_get_active(of->sd)) |
| 335 | return VM_FAULT_SIGBUS; |
| 336 | |
| 337 | ret = VM_FAULT_SIGBUS; |
| 338 | if (of->vm_ops->fault) |
| 339 | ret = of->vm_ops->fault(vma, vmf); |
| 340 | |
| 341 | sysfs_put_active(of->sd); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma, |
| 346 | struct vm_fault *vmf) |
| 347 | { |
| 348 | struct file *file = vma->vm_file; |
| 349 | struct sysfs_open_file *of = sysfs_of(file); |
| 350 | int ret; |
| 351 | |
| 352 | if (!of->vm_ops) |
| 353 | return VM_FAULT_SIGBUS; |
| 354 | |
| 355 | if (!sysfs_get_active(of->sd)) |
| 356 | return VM_FAULT_SIGBUS; |
| 357 | |
| 358 | ret = 0; |
| 359 | if (of->vm_ops->page_mkwrite) |
| 360 | ret = of->vm_ops->page_mkwrite(vma, vmf); |
| 361 | else |
| 362 | file_update_time(file); |
| 363 | |
| 364 | sysfs_put_active(of->sd); |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr, |
| 369 | void *buf, int len, int write) |
| 370 | { |
| 371 | struct file *file = vma->vm_file; |
| 372 | struct sysfs_open_file *of = sysfs_of(file); |
| 373 | int ret; |
| 374 | |
| 375 | if (!of->vm_ops) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | if (!sysfs_get_active(of->sd)) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | ret = -EINVAL; |
| 382 | if (of->vm_ops->access) |
| 383 | ret = of->vm_ops->access(vma, addr, buf, len, write); |
| 384 | |
| 385 | sysfs_put_active(of->sd); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | #ifdef CONFIG_NUMA |
| 390 | static int sysfs_bin_set_policy(struct vm_area_struct *vma, |
| 391 | struct mempolicy *new) |
| 392 | { |
| 393 | struct file *file = vma->vm_file; |
| 394 | struct sysfs_open_file *of = sysfs_of(file); |
| 395 | int ret; |
| 396 | |
| 397 | if (!of->vm_ops) |
| 398 | return 0; |
| 399 | |
| 400 | if (!sysfs_get_active(of->sd)) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | ret = 0; |
| 404 | if (of->vm_ops->set_policy) |
| 405 | ret = of->vm_ops->set_policy(vma, new); |
| 406 | |
| 407 | sysfs_put_active(of->sd); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma, |
| 412 | unsigned long addr) |
| 413 | { |
| 414 | struct file *file = vma->vm_file; |
| 415 | struct sysfs_open_file *of = sysfs_of(file); |
| 416 | struct mempolicy *pol; |
| 417 | |
| 418 | if (!of->vm_ops) |
| 419 | return vma->vm_policy; |
| 420 | |
| 421 | if (!sysfs_get_active(of->sd)) |
| 422 | return vma->vm_policy; |
| 423 | |
| 424 | pol = vma->vm_policy; |
| 425 | if (of->vm_ops->get_policy) |
| 426 | pol = of->vm_ops->get_policy(vma, addr); |
| 427 | |
| 428 | sysfs_put_active(of->sd); |
| 429 | return pol; |
| 430 | } |
| 431 | |
| 432 | static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, |
| 433 | const nodemask_t *to, unsigned long flags) |
| 434 | { |
| 435 | struct file *file = vma->vm_file; |
| 436 | struct sysfs_open_file *of = sysfs_of(file); |
| 437 | int ret; |
| 438 | |
| 439 | if (!of->vm_ops) |
| 440 | return 0; |
| 441 | |
| 442 | if (!sysfs_get_active(of->sd)) |
| 443 | return 0; |
| 444 | |
| 445 | ret = 0; |
| 446 | if (of->vm_ops->migrate) |
| 447 | ret = of->vm_ops->migrate(vma, from, to, flags); |
| 448 | |
| 449 | sysfs_put_active(of->sd); |
| 450 | return ret; |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | static const struct vm_operations_struct sysfs_bin_vm_ops = { |
| 455 | .open = sysfs_bin_vma_open, |
| 456 | .fault = sysfs_bin_fault, |
| 457 | .page_mkwrite = sysfs_bin_page_mkwrite, |
| 458 | .access = sysfs_bin_access, |
| 459 | #ifdef CONFIG_NUMA |
| 460 | .set_policy = sysfs_bin_set_policy, |
| 461 | .get_policy = sysfs_bin_get_policy, |
| 462 | .migrate = sysfs_bin_migrate, |
| 463 | #endif |
| 464 | }; |
| 465 | |
| 466 | static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma) |
| 467 | { |
| 468 | struct sysfs_open_file *of = sysfs_of(file); |
| 469 | struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr; |
| 470 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
| 471 | int rc; |
| 472 | |
| 473 | mutex_lock(&of->mutex); |
| 474 | |
| 475 | /* need of->sd for battr, its parent for kobj */ |
| 476 | rc = -ENODEV; |
| 477 | if (!sysfs_get_active(of->sd)) |
| 478 | goto out_unlock; |
| 479 | |
| 480 | rc = -EINVAL; |
| 481 | if (!battr->mmap) |
| 482 | goto out_put; |
| 483 | |
| 484 | rc = battr->mmap(file, kobj, battr, vma); |
| 485 | if (rc) |
| 486 | goto out_put; |
| 487 | |
| 488 | /* |
| 489 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() |
| 490 | * to satisfy versions of X which crash if the mmap fails: that |
| 491 | * substitutes a new vm_file, and we don't then want bin_vm_ops. |
| 492 | */ |
| 493 | if (vma->vm_file != file) |
| 494 | goto out_put; |
| 495 | |
| 496 | rc = -EINVAL; |
| 497 | if (of->mmapped && of->vm_ops != vma->vm_ops) |
| 498 | goto out_put; |
| 499 | |
| 500 | /* |
| 501 | * It is not possible to successfully wrap close. |
| 502 | * So error if someone is trying to use close. |
| 503 | */ |
| 504 | rc = -EINVAL; |
| 505 | if (vma->vm_ops && vma->vm_ops->close) |
| 506 | goto out_put; |
| 507 | |
| 508 | rc = 0; |
| 509 | of->mmapped = 1; |
| 510 | of->vm_ops = vma->vm_ops; |
| 511 | vma->vm_ops = &sysfs_bin_vm_ops; |
| 512 | out_put: |
| 513 | sysfs_put_active(of->sd); |
| 514 | out_unlock: |
| 515 | mutex_unlock(&of->mutex); |
| 516 | |
| 517 | return rc; |
| 518 | } |
| 519 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 520 | /** |
| 521 | * sysfs_get_open_dirent - get or create sysfs_open_dirent |
| 522 | * @sd: target sysfs_dirent |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 523 | * @of: sysfs_open_file for this instance of open |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 524 | * |
| 525 | * If @sd->s_attr.open exists, increment its reference count; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 526 | * otherwise, create one. @of is chained to the files list. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 527 | * |
| 528 | * LOCKING: |
| 529 | * Kernel thread context (may sleep). |
| 530 | * |
| 531 | * RETURNS: |
| 532 | * 0 on success, -errno on failure. |
| 533 | */ |
| 534 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 535 | struct sysfs_open_file *of) |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 536 | { |
| 537 | struct sysfs_open_dirent *od, *new_od = NULL; |
| 538 | |
| 539 | retry: |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 540 | mutex_lock(&sysfs_open_file_mutex); |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 541 | spin_lock_irq(&sysfs_open_dirent_lock); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 542 | |
| 543 | if (!sd->s_attr.open && new_od) { |
| 544 | sd->s_attr.open = new_od; |
| 545 | new_od = NULL; |
| 546 | } |
| 547 | |
| 548 | od = sd->s_attr.open; |
| 549 | if (od) { |
| 550 | atomic_inc(&od->refcnt); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 551 | list_add_tail(&of->list, &od->files); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 552 | } |
| 553 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 554 | spin_unlock_irq(&sysfs_open_dirent_lock); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 555 | mutex_unlock(&sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 556 | |
| 557 | if (od) { |
| 558 | kfree(new_od); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* not there, initialize a new one and retry */ |
| 563 | new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); |
| 564 | if (!new_od) |
| 565 | return -ENOMEM; |
| 566 | |
| 567 | atomic_set(&new_od->refcnt, 0); |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 568 | atomic_set(&new_od->event, 1); |
| 569 | init_waitqueue_head(&new_od->poll); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 570 | INIT_LIST_HEAD(&new_od->files); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 571 | goto retry; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * sysfs_put_open_dirent - put sysfs_open_dirent |
| 576 | * @sd: target sysfs_dirent |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 577 | * @of: associated sysfs_open_file |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 578 | * |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 579 | * Put @sd->s_attr.open and unlink @of from the files list. If |
| 580 | * reference count reaches zero, disassociate and free it. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 581 | * |
| 582 | * LOCKING: |
| 583 | * None. |
| 584 | */ |
| 585 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 586 | struct sysfs_open_file *of) |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 587 | { |
| 588 | struct sysfs_open_dirent *od = sd->s_attr.open; |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 589 | unsigned long flags; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 590 | |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 591 | mutex_lock(&sysfs_open_file_mutex); |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 592 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 593 | |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 594 | if (of) |
| 595 | list_del(&of->list); |
| 596 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 597 | if (atomic_dec_and_test(&od->refcnt)) |
| 598 | sd->s_attr.open = NULL; |
| 599 | else |
| 600 | od = NULL; |
| 601 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 602 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 603 | mutex_unlock(&sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 604 | |
| 605 | kfree(od); |
| 606 | } |
| 607 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 608 | static int sysfs_open_file(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 610 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 611 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 612 | struct sysfs_open_file *of; |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 613 | bool has_read, has_write; |
Kay Sievers | 000f2a4 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 614 | int error = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 616 | /* need attr_sd for attr and ops, its parent for kobj */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 617 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 618 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 620 | if (sysfs_is_bin(attr_sd)) { |
| 621 | struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr; |
| 622 | |
| 623 | has_read = battr->read || battr->mmap; |
| 624 | has_write = battr->write || battr->mmap; |
| 625 | } else { |
| 626 | const struct sysfs_ops *ops = sysfs_file_ops(attr_sd); |
| 627 | |
| 628 | /* every kobject with an attribute needs a ktype assigned */ |
| 629 | if (WARN(!ops, KERN_ERR |
| 630 | "missing sysfs attribute operations for kobject: %s\n", |
| 631 | kobject_name(kobj))) |
| 632 | goto err_out; |
| 633 | |
| 634 | has_read = ops->show; |
| 635 | has_write = ops->store; |
| 636 | } |
| 637 | |
| 638 | /* check perms and supported operations */ |
| 639 | if ((file->f_mode & FMODE_WRITE) && |
| 640 | (!(inode->i_mode & S_IWUGO) || !has_write)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 641 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 643 | if ((file->f_mode & FMODE_READ) && |
| 644 | (!(inode->i_mode & S_IRUGO) || !has_read)) |
| 645 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 647 | /* allocate a sysfs_open_file for the file */ |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 648 | error = -ENOMEM; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 649 | of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); |
| 650 | if (!of) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 651 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 653 | mutex_init(&of->mutex); |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 654 | of->sd = attr_sd; |
| 655 | of->file = file; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 656 | |
| 657 | /* |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 658 | * Always instantiate seq_file even if read access doesn't use |
| 659 | * seq_file or is not requested. This unifies private data access |
| 660 | * and readable regular files are the vast majority anyway. |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 661 | */ |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 662 | if (sysfs_is_bin(attr_sd)) |
| 663 | error = single_open(file, NULL, of); |
| 664 | else |
| 665 | error = single_open(file, sysfs_seq_show, of); |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 666 | if (error) |
| 667 | goto err_free; |
| 668 | |
| 669 | /* seq_file clears PWRITE unconditionally, restore it if WRITE */ |
| 670 | if (file->f_mode & FMODE_WRITE) |
| 671 | file->f_mode |= FMODE_PWRITE; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 672 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 673 | /* make sure we have open dirent struct */ |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 674 | error = sysfs_get_open_dirent(attr_sd, of); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 675 | if (error) |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 676 | goto err_close; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 677 | |
Tejun Heo | b05f054 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 678 | /* open succeeded, put active references */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 679 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 680 | return 0; |
| 681 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 682 | err_close: |
| 683 | single_release(inode, file); |
| 684 | err_free: |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 685 | kfree(of); |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 686 | err_out: |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 687 | sysfs_put_active(attr_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return error; |
| 689 | } |
| 690 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 691 | static int sysfs_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 693 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 694 | struct sysfs_open_file *of = sysfs_of(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 696 | sysfs_put_open_dirent(sd, of); |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 697 | single_release(inode, filp); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 698 | kfree(of); |
Tejun Heo | 50ab1a7 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 703 | void sysfs_unmap_bin_file(struct sysfs_dirent *sd) |
| 704 | { |
| 705 | struct sysfs_open_dirent *od; |
| 706 | struct sysfs_open_file *of; |
| 707 | |
| 708 | if (!sysfs_is_bin(sd)) |
| 709 | return; |
| 710 | |
| 711 | spin_lock_irq(&sysfs_open_dirent_lock); |
| 712 | od = sd->s_attr.open; |
| 713 | if (od) |
| 714 | atomic_inc(&od->refcnt); |
| 715 | spin_unlock_irq(&sysfs_open_dirent_lock); |
| 716 | if (!od) |
| 717 | return; |
| 718 | |
| 719 | mutex_lock(&sysfs_open_file_mutex); |
| 720 | list_for_each_entry(of, &od->files, list) { |
| 721 | struct inode *inode = file_inode(of->file); |
| 722 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 723 | } |
| 724 | mutex_unlock(&sysfs_open_file_mutex); |
| 725 | |
| 726 | sysfs_put_open_dirent(sd, NULL); |
| 727 | } |
| 728 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 729 | /* Sysfs attribute files are pollable. The idea is that you read |
| 730 | * the content and then you use 'poll' or 'select' to wait for |
| 731 | * the content to change. When the content changes (assuming the |
| 732 | * manager for the kobject supports notification), poll will |
| 733 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 734 | * it is waiting for read, write, or exceptions. |
| 735 | * Once poll/select indicates that the value has changed, you |
Dan Williams | 2424b5d | 2008-04-07 15:35:01 -0700 | [diff] [blame] | 736 | * need to close and re-open the file, or seek to 0 and read again. |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 737 | * Reminder: this only works for attributes which actively support |
| 738 | * it, and it is not possible to test an attribute from userspace |
Rolf Eike Beer | a93720e | 2007-08-10 13:51:07 -0700 | [diff] [blame] | 739 | * to see if it supports poll (Neither 'poll' nor 'select' return |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 740 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 741 | */ |
| 742 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
| 743 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 744 | struct sysfs_open_file *of = sysfs_of(filp); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 745 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 746 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 747 | |
| 748 | /* need parent for the kobj, grab both */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 749 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 750 | goto trigger; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 751 | |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 752 | poll_wait(filp, &od->poll, wait); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 753 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 754 | sysfs_put_active(attr_sd); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 755 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 756 | if (of->event != atomic_read(&od->event)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 757 | goto trigger; |
| 758 | |
KOSAKI Motohiro | 1af3557 | 2009-04-09 13:53:22 +0900 | [diff] [blame] | 759 | return DEFAULT_POLLMASK; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 760 | |
| 761 | trigger: |
KOSAKI Motohiro | 1af3557 | 2009-04-09 13:53:22 +0900 | [diff] [blame] | 762 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 763 | } |
| 764 | |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 765 | void sysfs_notify_dirent(struct sysfs_dirent *sd) |
| 766 | { |
| 767 | struct sysfs_open_dirent *od; |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 768 | unsigned long flags; |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 769 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 770 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 771 | |
Nick Dyer | fc60bb8 | 2013-06-07 15:45:13 +0100 | [diff] [blame] | 772 | if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) { |
| 773 | od = sd->s_attr.open; |
| 774 | if (od) { |
| 775 | atomic_inc(&od->event); |
| 776 | wake_up_interruptible(&od->poll); |
| 777 | } |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 778 | } |
| 779 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 780 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 781 | } |
| 782 | EXPORT_SYMBOL_GPL(sysfs_notify_dirent); |
| 783 | |
Trent Piepho | 8c0e399 | 2008-09-25 16:45:13 -0700 | [diff] [blame] | 784 | void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 785 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 786 | struct sysfs_dirent *sd = k->sd; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 787 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 788 | mutex_lock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 789 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 790 | if (sd && dir) |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 791 | sd = sysfs_find_dirent(sd, dir, NULL); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 792 | if (sd && attr) |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 793 | sd = sysfs_find_dirent(sd, attr, NULL); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 794 | if (sd) |
| 795 | sysfs_notify_dirent(sd); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 796 | |
| 797 | mutex_unlock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 798 | } |
| 799 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 800 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 801 | const struct file_operations sysfs_file_operations = { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 802 | .read = seq_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | .write = sysfs_write_file, |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 804 | .llseek = seq_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | .open = sysfs_open_file, |
| 806 | .release = sysfs_release, |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 807 | .poll = sysfs_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | }; |
| 809 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 810 | const struct file_operations sysfs_bin_operations = { |
Tejun Heo | 2f0c6b7 | 2013-10-01 17:42:06 -0400 | [diff] [blame] | 811 | .read = sysfs_bin_read, |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 812 | .write = sysfs_write_file, |
| 813 | .llseek = generic_file_llseek, |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 814 | .mmap = sysfs_bin_mmap, |
Tejun Heo | 49fe604 | 2013-10-01 17:42:08 -0400 | [diff] [blame] | 815 | .open = sysfs_open_file, |
| 816 | .release = sysfs_release, |
| 817 | .poll = sysfs_poll, |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame] | 818 | }; |
| 819 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 820 | int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, |
| 821 | const struct attribute *attr, int type, |
| 822 | umode_t amode, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 824 | umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 825 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 826 | struct sysfs_dirent *sd; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 827 | int rc; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 828 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 829 | sd = sysfs_new_dirent(attr->name, mode, type); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 830 | if (!sd) |
| 831 | return -ENOMEM; |
Eric W. Biederman | 487505c | 2011-10-12 21:53:38 +0000 | [diff] [blame] | 832 | |
| 833 | sd->s_ns = ns; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 834 | sd->s_attr.attr = (void *)attr; |
Eric W. Biederman | a2db684 | 2010-02-11 15:20:00 -0800 | [diff] [blame] | 835 | sysfs_dirent_init_lockdep(sd); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 836 | |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 837 | sysfs_addrm_start(&acxt); |
| 838 | rc = sysfs_add_one(&acxt, sd, dir_sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 839 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 840 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 841 | if (rc) |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 842 | sysfs_put(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 843 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 844 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 848 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
| 849 | int type) |
| 850 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 851 | return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 852 | } |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 855 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
| 856 | * @kobj: object we're creating for |
| 857 | * @attr: attribute descriptor |
| 858 | * @ns: namespace the new file should belong to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 860 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 861 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 863 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 865 | return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, |
| 866 | attr->mode, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 869 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 871 | int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr) |
| 872 | { |
| 873 | int err = 0; |
| 874 | int i; |
| 875 | |
| 876 | for (i = 0; ptr[i] && !err; i++) |
| 877 | err = sysfs_create_file(kobj, ptr[i]); |
| 878 | if (err) |
| 879 | while (--i >= 0) |
| 880 | sysfs_remove_file(kobj, ptr[i]); |
| 881 | return err; |
| 882 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 883 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | /** |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 886 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. |
| 887 | * @kobj: object we're acting for. |
| 888 | * @attr: attribute descriptor. |
| 889 | * @group: group name. |
| 890 | */ |
| 891 | int sysfs_add_file_to_group(struct kobject *kobj, |
| 892 | const struct attribute *attr, const char *group) |
| 893 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 894 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 895 | int error; |
| 896 | |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 897 | if (group) |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 898 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 899 | else |
| 900 | dir_sd = sysfs_get(kobj->sd); |
| 901 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 902 | if (!dir_sd) |
| 903 | return -ENOENT; |
| 904 | |
| 905 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); |
| 906 | sysfs_put(dir_sd); |
| 907 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 908 | return error; |
| 909 | } |
| 910 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 913 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 914 | * @kobj: object we're acting for. |
| 915 | * @attr: attribute descriptor. |
| 916 | * @mode: file permissions. |
| 917 | * |
| 918 | */ |
Jean Delvare | 49c1940 | 2010-07-02 16:54:05 +0200 | [diff] [blame] | 919 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
Al Viro | 48176a9 | 2011-07-24 03:40:40 -0400 | [diff] [blame] | 920 | umode_t mode) |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 921 | { |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 922 | struct sysfs_dirent *sd; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 923 | struct iattr newattrs; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 924 | int rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 925 | |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 926 | mutex_lock(&sysfs_mutex); |
| 927 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 928 | rc = -ENOENT; |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 929 | sd = sysfs_find_dirent(kobj->sd, attr->name, NULL); |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 930 | if (!sd) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 931 | goto out; |
| 932 | |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 933 | newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
Eric W. Biederman | 4c6974f | 2009-11-07 23:27:02 -0800 | [diff] [blame] | 934 | newattrs.ia_valid = ATTR_MODE; |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 935 | rc = sysfs_sd_setattr(sd, &newattrs); |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 936 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 937 | out: |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 938 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 939 | return rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 940 | } |
| 941 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 942 | |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 943 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 944 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
| 945 | * @kobj: object we're acting for |
| 946 | * @attr: attribute descriptor |
| 947 | * @ns: namespace tag of the file to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | * |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 949 | * Hash the attribute name and namespace tag and kill the victim. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 951 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 952 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 954 | struct sysfs_dirent *dir_sd = kobj->sd; |
Eric W. Biederman | 487505c | 2011-10-12 21:53:38 +0000 | [diff] [blame] | 955 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 956 | sysfs_hash_and_remove(dir_sd, attr->name, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 958 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 960 | void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr) |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 961 | { |
| 962 | int i; |
| 963 | for (i = 0; ptr[i]; i++) |
| 964 | sysfs_remove_file(kobj, ptr[i]); |
| 965 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 966 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 968 | /** |
| 969 | * sysfs_remove_file_from_group - remove an attribute file from a group. |
| 970 | * @kobj: object we're acting for. |
| 971 | * @attr: attribute descriptor. |
| 972 | * @group: group name. |
| 973 | */ |
| 974 | void sysfs_remove_file_from_group(struct kobject *kobj, |
| 975 | const struct attribute *attr, const char *group) |
| 976 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 977 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 978 | |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 979 | if (group) |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 980 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 981 | else |
| 982 | dir_sd = sysfs_get(kobj->sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 983 | if (dir_sd) { |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 984 | sysfs_hash_and_remove(dir_sd, attr->name, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 985 | sysfs_put(dir_sd); |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
| 989 | |
Tejun Heo | 3124eb1 | 2013-10-01 17:42:09 -0400 | [diff] [blame^] | 990 | /** |
| 991 | * sysfs_create_bin_file - create binary file for object. |
| 992 | * @kobj: object. |
| 993 | * @attr: attribute descriptor. |
| 994 | */ |
| 995 | int sysfs_create_bin_file(struct kobject *kobj, |
| 996 | const struct bin_attribute *attr) |
| 997 | { |
| 998 | BUG_ON(!kobj || !kobj->sd || !attr); |
| 999 | |
| 1000 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
| 1001 | } |
| 1002 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
| 1003 | |
| 1004 | /** |
| 1005 | * sysfs_remove_bin_file - remove binary file for object. |
| 1006 | * @kobj: object. |
| 1007 | * @attr: attribute descriptor. |
| 1008 | */ |
| 1009 | void sysfs_remove_bin_file(struct kobject *kobj, |
| 1010 | const struct bin_attribute *attr) |
| 1011 | { |
| 1012 | sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL); |
| 1013 | } |
| 1014 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |
| 1015 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1016 | struct sysfs_schedule_callback_struct { |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1017 | struct list_head workq_list; |
| 1018 | struct kobject *kobj; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1019 | void (*func)(void *); |
| 1020 | void *data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1021 | struct module *owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1022 | struct work_struct work; |
| 1023 | }; |
| 1024 | |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 1025 | static struct workqueue_struct *sysfs_workqueue; |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1026 | static DEFINE_MUTEX(sysfs_workq_mutex); |
| 1027 | static LIST_HEAD(sysfs_workq); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1028 | static void sysfs_schedule_callback_work(struct work_struct *work) |
| 1029 | { |
| 1030 | struct sysfs_schedule_callback_struct *ss = container_of(work, |
| 1031 | struct sysfs_schedule_callback_struct, work); |
| 1032 | |
| 1033 | (ss->func)(ss->data); |
| 1034 | kobject_put(ss->kobj); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1035 | module_put(ss->owner); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1036 | mutex_lock(&sysfs_workq_mutex); |
| 1037 | list_del(&ss->workq_list); |
| 1038 | mutex_unlock(&sysfs_workq_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1039 | kfree(ss); |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * sysfs_schedule_callback - helper to schedule a callback for a kobject |
| 1044 | * @kobj: object we're acting for. |
| 1045 | * @func: callback function to invoke later. |
| 1046 | * @data: argument to pass to @func. |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1047 | * @owner: module owning the callback code |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1048 | * |
| 1049 | * sysfs attribute methods must not unregister themselves or their parent |
| 1050 | * kobject (which would amount to the same thing). Attempts to do so will |
| 1051 | * deadlock, since unregistration is mutually exclusive with driver |
| 1052 | * callbacks. |
| 1053 | * |
| 1054 | * Instead methods can call this routine, which will attempt to allocate |
| 1055 | * and schedule a workqueue request to call back @func with @data as its |
| 1056 | * argument in the workqueue's process context. @kobj will be pinned |
| 1057 | * until @func returns. |
| 1058 | * |
| 1059 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1060 | * be allocated, -ENODEV if a reference to @owner isn't available, |
| 1061 | * -EAGAIN if a callback has already been scheduled for @kobj. |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1062 | */ |
| 1063 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1064 | void *data, struct module *owner) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1065 | { |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1066 | struct sysfs_schedule_callback_struct *ss, *tmp; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1067 | |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1068 | if (!try_module_get(owner)) |
| 1069 | return -ENODEV; |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1070 | |
| 1071 | mutex_lock(&sysfs_workq_mutex); |
| 1072 | list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) |
| 1073 | if (ss->kobj == kobj) { |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 1074 | module_put(owner); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1075 | mutex_unlock(&sysfs_workq_mutex); |
| 1076 | return -EAGAIN; |
| 1077 | } |
| 1078 | mutex_unlock(&sysfs_workq_mutex); |
| 1079 | |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 1080 | if (sysfs_workqueue == NULL) { |
Andrew Morton | 086a377 | 2009-05-07 12:36:53 -0700 | [diff] [blame] | 1081 | sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 1082 | if (sysfs_workqueue == NULL) { |
| 1083 | module_put(owner); |
| 1084 | return -ENOMEM; |
| 1085 | } |
| 1086 | } |
| 1087 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1088 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1089 | if (!ss) { |
| 1090 | module_put(owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1091 | return -ENOMEM; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1092 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1093 | kobject_get(kobj); |
| 1094 | ss->kobj = kobj; |
| 1095 | ss->func = func; |
| 1096 | ss->data = data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 1097 | ss->owner = owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1098 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 1099 | INIT_LIST_HEAD(&ss->workq_list); |
| 1100 | mutex_lock(&sysfs_workq_mutex); |
| 1101 | list_add_tail(&ss->workq_list, &sysfs_workq); |
| 1102 | mutex_unlock(&sysfs_workq_mutex); |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 1103 | queue_work(sysfs_workqueue, &ss->work); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |