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