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