Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * fs/kernfs/file.c - kernfs file implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/poll.h> |
| 15 | #include <linux/pagemap.h> |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | |
| 18 | #include "kernfs-internal.h" |
| 19 | |
| 20 | /* |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 21 | * There's one kernfs_open_file for each open file and one kernfs_open_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 22 | * for each kernfs_node with one or more open files. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 23 | * |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 24 | * kernfs_node->attr.open points to kernfs_open_node. attr.open is |
| 25 | * protected by kernfs_open_node_lock. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 26 | * |
| 27 | * filp->private_data points to seq_file whose ->private points to |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 28 | * kernfs_open_file. kernfs_open_files are chained at |
| 29 | * kernfs_open_node->files, which is protected by kernfs_open_file_mutex. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 30 | */ |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 31 | static DEFINE_SPINLOCK(kernfs_open_node_lock); |
| 32 | static DEFINE_MUTEX(kernfs_open_file_mutex); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 33 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 34 | struct kernfs_open_node { |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 35 | atomic_t refcnt; |
| 36 | atomic_t event; |
| 37 | wait_queue_head_t poll; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 38 | struct list_head files; /* goes through kernfs_open_file.list */ |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 39 | }; |
| 40 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 41 | static struct kernfs_open_file *kernfs_of(struct file *file) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 42 | { |
| 43 | return ((struct seq_file *)file->private_data)->private; |
| 44 | } |
| 45 | |
| 46 | /* |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 47 | * Determine the kernfs_ops for the given kernfs_node. This function must |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 48 | * be called while holding an active reference. |
| 49 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 50 | static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 51 | { |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 52 | if (kn->flags & KERNFS_LOCKDEP) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 53 | lockdep_assert_held(kn); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 54 | return kn->attr.ops; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos) |
| 58 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 59 | struct kernfs_open_file *of = sf->private; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 60 | const struct kernfs_ops *ops; |
| 61 | |
| 62 | /* |
| 63 | * @of->mutex nests outside active ref and is just to ensure that |
| 64 | * the ops aren't called concurrently for the same open file. |
| 65 | */ |
| 66 | mutex_lock(&of->mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 67 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 68 | return ERR_PTR(-ENODEV); |
| 69 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 70 | ops = kernfs_ops(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 71 | if (ops->seq_start) { |
| 72 | return ops->seq_start(sf, ppos); |
| 73 | } else { |
| 74 | /* |
| 75 | * The same behavior and code as single_open(). Returns |
| 76 | * !NULL if pos is at the beginning; otherwise, NULL. |
| 77 | */ |
| 78 | return NULL + !*ppos; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos) |
| 83 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 84 | struct kernfs_open_file *of = sf->private; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 85 | const struct kernfs_ops *ops = kernfs_ops(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 86 | |
| 87 | if (ops->seq_next) { |
| 88 | return ops->seq_next(sf, v, ppos); |
| 89 | } else { |
| 90 | /* |
| 91 | * The same behavior and code as single_open(), always |
| 92 | * terminate after the initial read. |
| 93 | */ |
| 94 | ++*ppos; |
| 95 | return NULL; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void kernfs_seq_stop(struct seq_file *sf, void *v) |
| 100 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 101 | struct kernfs_open_file *of = sf->private; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 102 | const struct kernfs_ops *ops = kernfs_ops(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 103 | |
| 104 | if (ops->seq_stop) |
| 105 | ops->seq_stop(sf, v); |
| 106 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 107 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 108 | mutex_unlock(&of->mutex); |
| 109 | } |
| 110 | |
| 111 | static int kernfs_seq_show(struct seq_file *sf, void *v) |
| 112 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 113 | struct kernfs_open_file *of = sf->private; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 114 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 115 | of->event = atomic_read(&of->kn->attr.open->event); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 116 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 117 | return of->kn->attr.ops->seq_show(sf, v); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static const struct seq_operations kernfs_seq_ops = { |
| 121 | .start = kernfs_seq_start, |
| 122 | .next = kernfs_seq_next, |
| 123 | .stop = kernfs_seq_stop, |
| 124 | .show = kernfs_seq_show, |
| 125 | }; |
| 126 | |
| 127 | /* |
| 128 | * As reading a bin file can have side-effects, the exact offset and bytes |
| 129 | * specified in read(2) call should be passed to the read callback making |
| 130 | * it difficult to use seq_file. Implement simplistic custom buffering for |
| 131 | * bin files. |
| 132 | */ |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 133 | static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of, |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 134 | char __user *user_buf, size_t count, |
| 135 | loff_t *ppos) |
| 136 | { |
| 137 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
| 138 | const struct kernfs_ops *ops; |
| 139 | char *buf; |
| 140 | |
| 141 | buf = kmalloc(len, GFP_KERNEL); |
| 142 | if (!buf) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | /* |
| 146 | * @of->mutex nests outside active ref and is just to ensure that |
| 147 | * the ops aren't called concurrently for the same open file. |
| 148 | */ |
| 149 | mutex_lock(&of->mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 150 | if (!sysfs_get_active(of->kn)) { |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 151 | len = -ENODEV; |
| 152 | mutex_unlock(&of->mutex); |
| 153 | goto out_free; |
| 154 | } |
| 155 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 156 | ops = kernfs_ops(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 157 | if (ops->read) |
| 158 | len = ops->read(of, buf, len, *ppos); |
| 159 | else |
| 160 | len = -EINVAL; |
| 161 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 162 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 163 | mutex_unlock(&of->mutex); |
| 164 | |
| 165 | if (len < 0) |
| 166 | goto out_free; |
| 167 | |
| 168 | if (copy_to_user(user_buf, buf, len)) { |
| 169 | len = -EFAULT; |
| 170 | goto out_free; |
| 171 | } |
| 172 | |
| 173 | *ppos += len; |
| 174 | |
| 175 | out_free: |
| 176 | kfree(buf); |
| 177 | return len; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * kernfs_file_read - kernfs vfs read callback |
| 182 | * @file: file pointer |
| 183 | * @user_buf: data to write |
| 184 | * @count: number of bytes |
| 185 | * @ppos: starting offset |
| 186 | */ |
| 187 | static ssize_t kernfs_file_read(struct file *file, char __user *user_buf, |
| 188 | size_t count, loff_t *ppos) |
| 189 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 190 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 191 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 192 | if (of->kn->flags & KERNFS_HAS_SEQ_SHOW) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 193 | return seq_read(file, user_buf, count, ppos); |
| 194 | else |
| 195 | return kernfs_file_direct_read(of, user_buf, count, ppos); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * kernfs_file_write - kernfs vfs write callback |
| 200 | * @file: file pointer |
| 201 | * @user_buf: data to write |
| 202 | * @count: number of bytes |
| 203 | * @ppos: starting offset |
| 204 | * |
| 205 | * Copy data in from userland and pass it to the matching kernfs write |
| 206 | * operation. |
| 207 | * |
| 208 | * There is no easy way for us to know if userspace is only doing a partial |
| 209 | * write, so we don't support them. We expect the entire buffer to come on |
| 210 | * the first write. Hint: if you're writing a value, first read the file, |
| 211 | * modify only the the value you're changing, then write entire buffer |
| 212 | * back. |
| 213 | */ |
| 214 | static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf, |
| 215 | size_t count, loff_t *ppos) |
| 216 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 217 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 218 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
| 219 | const struct kernfs_ops *ops; |
| 220 | char *buf; |
| 221 | |
| 222 | buf = kmalloc(len + 1, GFP_KERNEL); |
| 223 | if (!buf) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | if (copy_from_user(buf, user_buf, len)) { |
| 227 | len = -EFAULT; |
| 228 | goto out_free; |
| 229 | } |
| 230 | buf[len] = '\0'; /* guarantee string termination */ |
| 231 | |
| 232 | /* |
| 233 | * @of->mutex nests outside active ref and is just to ensure that |
| 234 | * the ops aren't called concurrently for the same open file. |
| 235 | */ |
| 236 | mutex_lock(&of->mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 237 | if (!sysfs_get_active(of->kn)) { |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 238 | mutex_unlock(&of->mutex); |
| 239 | len = -ENODEV; |
| 240 | goto out_free; |
| 241 | } |
| 242 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 243 | ops = kernfs_ops(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 244 | if (ops->write) |
| 245 | len = ops->write(of, buf, len, *ppos); |
| 246 | else |
| 247 | len = -EINVAL; |
| 248 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 249 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 250 | mutex_unlock(&of->mutex); |
| 251 | |
| 252 | if (len > 0) |
| 253 | *ppos += len; |
| 254 | out_free: |
| 255 | kfree(buf); |
| 256 | return len; |
| 257 | } |
| 258 | |
| 259 | static void kernfs_vma_open(struct vm_area_struct *vma) |
| 260 | { |
| 261 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 262 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 263 | |
| 264 | if (!of->vm_ops) |
| 265 | return; |
| 266 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 267 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 268 | return; |
| 269 | |
| 270 | if (of->vm_ops->open) |
| 271 | of->vm_ops->open(vma); |
| 272 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 273 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 277 | { |
| 278 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 279 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 280 | int ret; |
| 281 | |
| 282 | if (!of->vm_ops) |
| 283 | return VM_FAULT_SIGBUS; |
| 284 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 285 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 286 | return VM_FAULT_SIGBUS; |
| 287 | |
| 288 | ret = VM_FAULT_SIGBUS; |
| 289 | if (of->vm_ops->fault) |
| 290 | ret = of->vm_ops->fault(vma, vmf); |
| 291 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 292 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma, |
| 297 | struct vm_fault *vmf) |
| 298 | { |
| 299 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 300 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 301 | int ret; |
| 302 | |
| 303 | if (!of->vm_ops) |
| 304 | return VM_FAULT_SIGBUS; |
| 305 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 306 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 307 | return VM_FAULT_SIGBUS; |
| 308 | |
| 309 | ret = 0; |
| 310 | if (of->vm_ops->page_mkwrite) |
| 311 | ret = of->vm_ops->page_mkwrite(vma, vmf); |
| 312 | else |
| 313 | file_update_time(file); |
| 314 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 315 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr, |
| 320 | void *buf, int len, int write) |
| 321 | { |
| 322 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 323 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 324 | int ret; |
| 325 | |
| 326 | if (!of->vm_ops) |
| 327 | return -EINVAL; |
| 328 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 329 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | |
| 332 | ret = -EINVAL; |
| 333 | if (of->vm_ops->access) |
| 334 | ret = of->vm_ops->access(vma, addr, buf, len, write); |
| 335 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 336 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | #ifdef CONFIG_NUMA |
| 341 | static int kernfs_vma_set_policy(struct vm_area_struct *vma, |
| 342 | struct mempolicy *new) |
| 343 | { |
| 344 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 345 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 346 | int ret; |
| 347 | |
| 348 | if (!of->vm_ops) |
| 349 | return 0; |
| 350 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 351 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | |
| 354 | ret = 0; |
| 355 | if (of->vm_ops->set_policy) |
| 356 | ret = of->vm_ops->set_policy(vma, new); |
| 357 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 358 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma, |
| 363 | unsigned long addr) |
| 364 | { |
| 365 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 366 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 367 | struct mempolicy *pol; |
| 368 | |
| 369 | if (!of->vm_ops) |
| 370 | return vma->vm_policy; |
| 371 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 372 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 373 | return vma->vm_policy; |
| 374 | |
| 375 | pol = vma->vm_policy; |
| 376 | if (of->vm_ops->get_policy) |
| 377 | pol = of->vm_ops->get_policy(vma, addr); |
| 378 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 379 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 380 | return pol; |
| 381 | } |
| 382 | |
| 383 | static int kernfs_vma_migrate(struct vm_area_struct *vma, |
| 384 | const nodemask_t *from, const nodemask_t *to, |
| 385 | unsigned long flags) |
| 386 | { |
| 387 | struct file *file = vma->vm_file; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 388 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 389 | int ret; |
| 390 | |
| 391 | if (!of->vm_ops) |
| 392 | return 0; |
| 393 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 394 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 395 | return 0; |
| 396 | |
| 397 | ret = 0; |
| 398 | if (of->vm_ops->migrate) |
| 399 | ret = of->vm_ops->migrate(vma, from, to, flags); |
| 400 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 401 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 402 | return ret; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | static const struct vm_operations_struct kernfs_vm_ops = { |
| 407 | .open = kernfs_vma_open, |
| 408 | .fault = kernfs_vma_fault, |
| 409 | .page_mkwrite = kernfs_vma_page_mkwrite, |
| 410 | .access = kernfs_vma_access, |
| 411 | #ifdef CONFIG_NUMA |
| 412 | .set_policy = kernfs_vma_set_policy, |
| 413 | .get_policy = kernfs_vma_get_policy, |
| 414 | .migrate = kernfs_vma_migrate, |
| 415 | #endif |
| 416 | }; |
| 417 | |
| 418 | static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 419 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 420 | struct kernfs_open_file *of = kernfs_of(file); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 421 | const struct kernfs_ops *ops; |
| 422 | int rc; |
| 423 | |
Tejun Heo | 9b2db6e | 2013-12-10 09:29:17 -0500 | [diff] [blame] | 424 | /* |
| 425 | * mmap path and of->mutex are prone to triggering spurious lockdep |
| 426 | * warnings and we don't want to add spurious locking dependency |
| 427 | * between the two. Check whether mmap is actually implemented |
| 428 | * without grabbing @of->mutex by testing HAS_MMAP flag. See the |
| 429 | * comment in kernfs_file_open() for more details. |
| 430 | */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 431 | if (!(of->kn->flags & KERNFS_HAS_MMAP)) |
Tejun Heo | 9b2db6e | 2013-12-10 09:29:17 -0500 | [diff] [blame] | 432 | return -ENODEV; |
| 433 | |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 434 | mutex_lock(&of->mutex); |
| 435 | |
| 436 | rc = -ENODEV; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 437 | if (!sysfs_get_active(of->kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 438 | goto out_unlock; |
| 439 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 440 | ops = kernfs_ops(of->kn); |
Tejun Heo | 9b2db6e | 2013-12-10 09:29:17 -0500 | [diff] [blame] | 441 | rc = ops->mmap(of, vma); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() |
| 445 | * to satisfy versions of X which crash if the mmap fails: that |
| 446 | * substitutes a new vm_file, and we don't then want bin_vm_ops. |
| 447 | */ |
| 448 | if (vma->vm_file != file) |
| 449 | goto out_put; |
| 450 | |
| 451 | rc = -EINVAL; |
| 452 | if (of->mmapped && of->vm_ops != vma->vm_ops) |
| 453 | goto out_put; |
| 454 | |
| 455 | /* |
| 456 | * It is not possible to successfully wrap close. |
| 457 | * So error if someone is trying to use close. |
| 458 | */ |
| 459 | rc = -EINVAL; |
| 460 | if (vma->vm_ops && vma->vm_ops->close) |
| 461 | goto out_put; |
| 462 | |
| 463 | rc = 0; |
| 464 | of->mmapped = 1; |
| 465 | of->vm_ops = vma->vm_ops; |
| 466 | vma->vm_ops = &kernfs_vm_ops; |
| 467 | out_put: |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 468 | sysfs_put_active(of->kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 469 | out_unlock: |
| 470 | mutex_unlock(&of->mutex); |
| 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | /** |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 476 | * sysfs_get_open_dirent - get or create kernfs_open_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 477 | * @kn: target kernfs_node |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 478 | * @of: kernfs_open_file for this instance of open |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 479 | * |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 480 | * If @kn->attr.open exists, increment its reference count; otherwise, |
| 481 | * create one. @of is chained to the files list. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 482 | * |
| 483 | * LOCKING: |
| 484 | * Kernel thread context (may sleep). |
| 485 | * |
| 486 | * RETURNS: |
| 487 | * 0 on success, -errno on failure. |
| 488 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 489 | static int sysfs_get_open_dirent(struct kernfs_node *kn, |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 490 | struct kernfs_open_file *of) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 491 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 492 | struct kernfs_open_node *on, *new_on = NULL; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 493 | |
| 494 | retry: |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 495 | mutex_lock(&kernfs_open_file_mutex); |
| 496 | spin_lock_irq(&kernfs_open_node_lock); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 497 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 498 | if (!kn->attr.open && new_on) { |
| 499 | kn->attr.open = new_on; |
| 500 | new_on = NULL; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 503 | on = kn->attr.open; |
| 504 | if (on) { |
| 505 | atomic_inc(&on->refcnt); |
| 506 | list_add_tail(&of->list, &on->files); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 509 | spin_unlock_irq(&kernfs_open_node_lock); |
| 510 | mutex_unlock(&kernfs_open_file_mutex); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 511 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 512 | if (on) { |
| 513 | kfree(new_on); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /* not there, initialize a new one and retry */ |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 518 | new_on = kmalloc(sizeof(*new_on), GFP_KERNEL); |
| 519 | if (!new_on) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 520 | return -ENOMEM; |
| 521 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 522 | atomic_set(&new_on->refcnt, 0); |
| 523 | atomic_set(&new_on->event, 1); |
| 524 | init_waitqueue_head(&new_on->poll); |
| 525 | INIT_LIST_HEAD(&new_on->files); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 526 | goto retry; |
| 527 | } |
| 528 | |
| 529 | /** |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 530 | * sysfs_put_open_dirent - put kernfs_open_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 531 | * @kn: target kernfs_nodet |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 532 | * @of: associated kernfs_open_file |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 533 | * |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 534 | * Put @kn->attr.open and unlink @of from the files list. If |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 535 | * reference count reaches zero, disassociate and free it. |
| 536 | * |
| 537 | * LOCKING: |
| 538 | * None. |
| 539 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 540 | static void sysfs_put_open_dirent(struct kernfs_node *kn, |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 541 | struct kernfs_open_file *of) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 542 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 543 | struct kernfs_open_node *on = kn->attr.open; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 544 | unsigned long flags; |
| 545 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 546 | mutex_lock(&kernfs_open_file_mutex); |
| 547 | spin_lock_irqsave(&kernfs_open_node_lock, flags); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 548 | |
| 549 | if (of) |
| 550 | list_del(&of->list); |
| 551 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 552 | if (atomic_dec_and_test(&on->refcnt)) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 553 | kn->attr.open = NULL; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 554 | else |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 555 | on = NULL; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 556 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 557 | spin_unlock_irqrestore(&kernfs_open_node_lock, flags); |
| 558 | mutex_unlock(&kernfs_open_file_mutex); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 559 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 560 | kfree(on); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static int kernfs_file_open(struct inode *inode, struct file *file) |
| 564 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 565 | struct kernfs_node *kn = file->f_path.dentry->d_fsdata; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 566 | const struct kernfs_ops *ops; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 567 | struct kernfs_open_file *of; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 568 | bool has_read, has_write, has_mmap; |
| 569 | int error = -EACCES; |
| 570 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 571 | if (!sysfs_get_active(kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 572 | return -ENODEV; |
| 573 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 574 | ops = kernfs_ops(kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 575 | |
| 576 | has_read = ops->seq_show || ops->read || ops->mmap; |
| 577 | has_write = ops->write || ops->mmap; |
| 578 | has_mmap = ops->mmap; |
| 579 | |
| 580 | /* check perms and supported operations */ |
| 581 | if ((file->f_mode & FMODE_WRITE) && |
| 582 | (!(inode->i_mode & S_IWUGO) || !has_write)) |
| 583 | goto err_out; |
| 584 | |
| 585 | if ((file->f_mode & FMODE_READ) && |
| 586 | (!(inode->i_mode & S_IRUGO) || !has_read)) |
| 587 | goto err_out; |
| 588 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 589 | /* allocate a kernfs_open_file for the file */ |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 590 | error = -ENOMEM; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 591 | of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 592 | if (!of) |
| 593 | goto err_out; |
| 594 | |
| 595 | /* |
| 596 | * The following is done to give a different lockdep key to |
| 597 | * @of->mutex for files which implement mmap. This is a rather |
| 598 | * crude way to avoid false positive lockdep warning around |
| 599 | * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and |
| 600 | * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under |
| 601 | * which mm->mmap_sem nests, while holding @of->mutex. As each |
| 602 | * open file has a separate mutex, it's okay as long as those don't |
| 603 | * happen on the same file. At this point, we can't easily give |
| 604 | * each file a separate locking class. Let's differentiate on |
| 605 | * whether the file has mmap or not for now. |
Tejun Heo | 9b2db6e | 2013-12-10 09:29:17 -0500 | [diff] [blame] | 606 | * |
| 607 | * Both paths of the branch look the same. They're supposed to |
| 608 | * look that way and give @of->mutex different static lockdep keys. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 609 | */ |
| 610 | if (has_mmap) |
| 611 | mutex_init(&of->mutex); |
| 612 | else |
| 613 | mutex_init(&of->mutex); |
| 614 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 615 | of->kn = kn; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 616 | of->file = file; |
| 617 | |
| 618 | /* |
| 619 | * Always instantiate seq_file even if read access doesn't use |
| 620 | * seq_file or is not requested. This unifies private data access |
| 621 | * and readable regular files are the vast majority anyway. |
| 622 | */ |
| 623 | if (ops->seq_show) |
| 624 | error = seq_open(file, &kernfs_seq_ops); |
| 625 | else |
| 626 | error = seq_open(file, NULL); |
| 627 | if (error) |
| 628 | goto err_free; |
| 629 | |
| 630 | ((struct seq_file *)file->private_data)->private = of; |
| 631 | |
| 632 | /* seq_file clears PWRITE unconditionally, restore it if WRITE */ |
| 633 | if (file->f_mode & FMODE_WRITE) |
| 634 | file->f_mode |= FMODE_PWRITE; |
| 635 | |
| 636 | /* make sure we have open dirent struct */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 637 | error = sysfs_get_open_dirent(kn, of); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 638 | if (error) |
| 639 | goto err_close; |
| 640 | |
| 641 | /* open succeeded, put active references */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 642 | sysfs_put_active(kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 643 | return 0; |
| 644 | |
| 645 | err_close: |
| 646 | seq_release(inode, file); |
| 647 | err_free: |
| 648 | kfree(of); |
| 649 | err_out: |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 650 | sysfs_put_active(kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 651 | return error; |
| 652 | } |
| 653 | |
| 654 | static int kernfs_file_release(struct inode *inode, struct file *filp) |
| 655 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 656 | struct kernfs_node *kn = filp->f_path.dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 657 | struct kernfs_open_file *of = kernfs_of(filp); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 658 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 659 | sysfs_put_open_dirent(kn, of); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 660 | seq_release(inode, filp); |
| 661 | kfree(of); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 666 | void sysfs_unmap_bin_file(struct kernfs_node *kn) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 667 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 668 | struct kernfs_open_node *on; |
| 669 | struct kernfs_open_file *of; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 670 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 671 | if (!(kn->flags & KERNFS_HAS_MMAP)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 672 | return; |
| 673 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 674 | spin_lock_irq(&kernfs_open_node_lock); |
| 675 | on = kn->attr.open; |
| 676 | if (on) |
| 677 | atomic_inc(&on->refcnt); |
| 678 | spin_unlock_irq(&kernfs_open_node_lock); |
| 679 | if (!on) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 680 | return; |
| 681 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 682 | mutex_lock(&kernfs_open_file_mutex); |
| 683 | list_for_each_entry(of, &on->files, list) { |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 684 | struct inode *inode = file_inode(of->file); |
| 685 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 686 | } |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 687 | mutex_unlock(&kernfs_open_file_mutex); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 688 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 689 | sysfs_put_open_dirent(kn, NULL); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* Sysfs attribute files are pollable. The idea is that you read |
| 693 | * the content and then you use 'poll' or 'select' to wait for |
| 694 | * the content to change. When the content changes (assuming the |
| 695 | * manager for the kobject supports notification), poll will |
| 696 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 697 | * it is waiting for read, write, or exceptions. |
| 698 | * Once poll/select indicates that the value has changed, you |
| 699 | * need to close and re-open the file, or seek to 0 and read again. |
| 700 | * Reminder: this only works for attributes which actively support |
| 701 | * it, and it is not possible to test an attribute from userspace |
| 702 | * to see if it supports poll (Neither 'poll' nor 'select' return |
| 703 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 704 | */ |
| 705 | static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait) |
| 706 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 707 | struct kernfs_open_file *of = kernfs_of(filp); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 708 | struct kernfs_node *kn = filp->f_path.dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 709 | struct kernfs_open_node *on = kn->attr.open; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 710 | |
| 711 | /* need parent for the kobj, grab both */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 712 | if (!sysfs_get_active(kn)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 713 | goto trigger; |
| 714 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 715 | poll_wait(filp, &on->poll, wait); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 716 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 717 | sysfs_put_active(kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 718 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 719 | if (of->event != atomic_read(&on->event)) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 720 | goto trigger; |
| 721 | |
| 722 | return DEFAULT_POLLMASK; |
| 723 | |
| 724 | trigger: |
| 725 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * kernfs_notify - notify a kernfs file |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 730 | * @kn: file to notify |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 731 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 732 | * Notify @kn such that poll(2) on @kn wakes up. |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 733 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 734 | void kernfs_notify(struct kernfs_node *kn) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 735 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 736 | struct kernfs_open_node *on; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 737 | unsigned long flags; |
| 738 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 739 | spin_lock_irqsave(&kernfs_open_node_lock, flags); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 740 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 741 | if (!WARN_ON(kernfs_type(kn) != KERNFS_FILE)) { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 742 | on = kn->attr.open; |
| 743 | if (on) { |
| 744 | atomic_inc(&on->event); |
| 745 | wake_up_interruptible(&on->poll); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 749 | spin_unlock_irqrestore(&kernfs_open_node_lock, flags); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 750 | } |
| 751 | EXPORT_SYMBOL_GPL(kernfs_notify); |
| 752 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 753 | const struct file_operations kernfs_file_fops = { |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 754 | .read = kernfs_file_read, |
| 755 | .write = kernfs_file_write, |
| 756 | .llseek = generic_file_llseek, |
| 757 | .mmap = kernfs_file_mmap, |
| 758 | .open = kernfs_file_open, |
| 759 | .release = kernfs_file_release, |
| 760 | .poll = kernfs_file_poll, |
| 761 | }; |
| 762 | |
| 763 | /** |
| 764 | * kernfs_create_file_ns_key - create a file |
| 765 | * @parent: directory to create the file in |
| 766 | * @name: name of the file |
| 767 | * @mode: mode of the file |
| 768 | * @size: size of the file |
| 769 | * @ops: kernfs operations for the file |
| 770 | * @priv: private data for the file |
| 771 | * @ns: optional namespace tag of the file |
| 772 | * @key: lockdep key for the file's active_ref, %NULL to disable lockdep |
| 773 | * |
| 774 | * Returns the created node on success, ERR_PTR() value on error. |
| 775 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 776 | struct kernfs_node *kernfs_create_file_ns_key(struct kernfs_node *parent, |
| 777 | const char *name, |
| 778 | umode_t mode, loff_t size, |
| 779 | const struct kernfs_ops *ops, |
| 780 | void *priv, const void *ns, |
| 781 | struct lock_class_key *key) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 782 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 783 | struct kernfs_addrm_cxt acxt; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 784 | struct kernfs_node *kn; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 785 | int rc; |
| 786 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 787 | kn = sysfs_new_dirent(kernfs_root(parent), name, |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 788 | (mode & S_IALLUGO) | S_IFREG, KERNFS_FILE); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 789 | if (!kn) |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 790 | return ERR_PTR(-ENOMEM); |
| 791 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 792 | kn->attr.ops = ops; |
| 793 | kn->attr.size = size; |
| 794 | kn->ns = ns; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 795 | kn->priv = priv; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 796 | |
| 797 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 798 | if (key) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 799 | lockdep_init_map(&kn->dep_map, "s_active", key, 0); |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 800 | kn->flags |= KERNFS_LOCKDEP; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 801 | } |
| 802 | #endif |
| 803 | |
| 804 | /* |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 805 | * kn->attr.ops is accesible only while holding active ref. We |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 806 | * need to know whether some ops are implemented outside active |
| 807 | * ref. Cache their existence in flags. |
| 808 | */ |
| 809 | if (ops->seq_show) |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 810 | kn->flags |= KERNFS_HAS_SEQ_SHOW; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 811 | if (ops->mmap) |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 812 | kn->flags |= KERNFS_HAS_MMAP; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 813 | |
| 814 | sysfs_addrm_start(&acxt); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 815 | rc = sysfs_add_one(&acxt, kn, parent); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 816 | sysfs_addrm_finish(&acxt); |
| 817 | |
| 818 | if (rc) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 819 | kernfs_put(kn); |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 820 | return ERR_PTR(rc); |
| 821 | } |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 822 | return kn; |
Tejun Heo | 414985a | 2013-11-28 14:54:34 -0500 | [diff] [blame] | 823 | } |