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/bin.c - sysfs binary file implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Matthew Wilcox |
| 6 | * Copyright (c) 2004 Silicon Graphics, Inc. |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 7 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 8 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 9 | * |
| 10 | * This file is released under the GPLv2. |
| 11 | * |
| 12 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #undef DEBUG |
| 16 | |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/fs.h> |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/kobject.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
Dave Young | 869512a | 2007-07-26 14:53:53 +0000 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 24 | #include <linux/mm.h> |
Greg Kroah-Hartman | 060cc74 | 2013-08-21 16:34:59 -0700 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "sysfs.h" |
| 28 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 29 | /* |
| 30 | * There's one bin_buffer for each open file. |
| 31 | * |
| 32 | * filp->private_data points to bin_buffer and |
| 33 | * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s |
| 34 | * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock |
| 35 | */ |
| 36 | static DEFINE_MUTEX(sysfs_bin_lock); |
| 37 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 38 | struct bin_buffer { |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 39 | struct mutex mutex; |
| 40 | void *buffer; |
| 41 | int mmapped; |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 42 | const struct vm_operations_struct *vm_ops; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 43 | struct file *file; |
| 44 | struct hlist_node list; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static int |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 48 | fill_read(struct file *file, char *buffer, loff_t off, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 50 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 51 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 52 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 53 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 55 | /* need attr_sd for attr, its parent for kobj */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 56 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 57 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 59 | rc = -EIO; |
| 60 | if (attr->read) |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 61 | rc = attr->read(file, kobj, attr, buffer, off, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 62 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 63 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 64 | |
| 65 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static ssize_t |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 69 | read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 71 | struct bin_buffer *bb = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 72 | int size = file_inode(file)->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 74 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 75 | char *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Greg Kroah-Hartman | 4503efd | 2009-01-20 15:51:16 -0800 | [diff] [blame] | 77 | if (!bytes) |
| 78 | return 0; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (size) { |
| 81 | if (offs > size) |
| 82 | return 0; |
| 83 | if (offs + count > size) |
| 84 | count = size - offs; |
| 85 | } |
| 86 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 87 | temp = kmalloc(count, GFP_KERNEL); |
| 88 | if (!temp) |
| 89 | return -ENOMEM; |
| 90 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 91 | mutex_lock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 93 | count = fill_read(file, bb->buffer, offs, count); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 94 | if (count < 0) { |
| 95 | mutex_unlock(&bb->mutex); |
| 96 | goto out_free; |
| 97 | } |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 98 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 99 | memcpy(temp, bb->buffer, count); |
| 100 | |
| 101 | mutex_unlock(&bb->mutex); |
| 102 | |
| 103 | if (copy_to_user(userbuf, temp, count)) { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 104 | count = -EFAULT; |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 105 | goto out_free; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 108 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | *off = offs + count; |
| 111 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 112 | out_free: |
| 113 | kfree(temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return count; |
| 115 | } |
| 116 | |
| 117 | static int |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 118 | flush_write(struct file *file, char *buffer, loff_t offset, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 120 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 121 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 122 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 123 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 125 | /* need attr_sd for attr, its parent for kobj */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 126 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 127 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 129 | rc = -EIO; |
| 130 | if (attr->write) |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 131 | rc = attr->write(file, kobj, attr, buffer, offset, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 132 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 133 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 134 | |
| 135 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 138 | static ssize_t write(struct file *file, const char __user *userbuf, |
| 139 | size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 141 | struct bin_buffer *bb = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 142 | int size = file_inode(file)->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 144 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 145 | char *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Greg Kroah-Hartman | 4503efd | 2009-01-20 15:51:16 -0800 | [diff] [blame] | 147 | if (!bytes) |
| 148 | return 0; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (size) { |
| 151 | if (offs > size) |
| 152 | return 0; |
| 153 | if (offs + count > size) |
| 154 | count = size - offs; |
| 155 | } |
| 156 | |
Li Zefan | 1c8542c | 2009-04-08 15:07:30 +0800 | [diff] [blame] | 157 | temp = memdup_user(userbuf, count); |
| 158 | if (IS_ERR(temp)) |
| 159 | return PTR_ERR(temp); |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 160 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 161 | mutex_lock(&bb->mutex); |
| 162 | |
| 163 | memcpy(bb->buffer, temp, count); |
| 164 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 165 | count = flush_write(file, bb->buffer, offs, count); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 166 | mutex_unlock(&bb->mutex); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (count > 0) |
| 169 | *off = offs + count; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 170 | |
Catalin Marinas | d5ce5b4 | 2009-07-08 11:17:34 +0100 | [diff] [blame] | 171 | kfree(temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return count; |
| 173 | } |
| 174 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 175 | static void bin_vma_open(struct vm_area_struct *vma) |
| 176 | { |
| 177 | struct file *file = vma->vm_file; |
| 178 | struct bin_buffer *bb = file->private_data; |
| 179 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 180 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 181 | if (!bb->vm_ops) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 182 | return; |
| 183 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 184 | if (!sysfs_get_active(attr_sd)) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 185 | return; |
| 186 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 187 | if (bb->vm_ops->open) |
| 188 | bb->vm_ops->open(vma); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 189 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 190 | sysfs_put_active(attr_sd); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 193 | static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 194 | { |
| 195 | struct file *file = vma->vm_file; |
| 196 | struct bin_buffer *bb = file->private_data; |
| 197 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 198 | int ret; |
| 199 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 200 | if (!bb->vm_ops) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 201 | return VM_FAULT_SIGBUS; |
| 202 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 203 | if (!sysfs_get_active(attr_sd)) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 204 | return VM_FAULT_SIGBUS; |
| 205 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 206 | ret = VM_FAULT_SIGBUS; |
| 207 | if (bb->vm_ops->fault) |
| 208 | ret = bb->vm_ops->fault(vma, vmf); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 209 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 210 | sysfs_put_active(attr_sd); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 211 | return ret; |
| 212 | } |
| 213 | |
Hugh Dickins | 851a039 | 2009-03-31 15:23:24 -0700 | [diff] [blame] | 214 | static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 215 | { |
| 216 | struct file *file = vma->vm_file; |
| 217 | struct bin_buffer *bb = file->private_data; |
| 218 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 219 | int ret; |
| 220 | |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 221 | if (!bb->vm_ops) |
Hugh Dickins | 851a039 | 2009-03-31 15:23:24 -0700 | [diff] [blame] | 222 | return VM_FAULT_SIGBUS; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 223 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 224 | if (!sysfs_get_active(attr_sd)) |
Hugh Dickins | 851a039 | 2009-03-31 15:23:24 -0700 | [diff] [blame] | 225 | return VM_FAULT_SIGBUS; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 226 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 227 | ret = 0; |
| 228 | if (bb->vm_ops->page_mkwrite) |
| 229 | ret = bb->vm_ops->page_mkwrite(vma, vmf); |
Jan Kara | 14ae417 | 2012-06-12 16:20:27 +0200 | [diff] [blame] | 230 | else |
| 231 | file_update_time(file); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 232 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 233 | sysfs_put_active(attr_sd); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | static int bin_access(struct vm_area_struct *vma, unsigned long addr, |
| 238 | void *buf, int len, int write) |
| 239 | { |
| 240 | struct file *file = vma->vm_file; |
| 241 | struct bin_buffer *bb = file->private_data; |
| 242 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 243 | int ret; |
| 244 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 245 | if (!bb->vm_ops) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 248 | if (!sysfs_get_active(attr_sd)) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 251 | ret = -EINVAL; |
| 252 | if (bb->vm_ops->access) |
| 253 | ret = bb->vm_ops->access(vma, addr, buf, len, write); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 254 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 255 | sysfs_put_active(attr_sd); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 259 | #ifdef CONFIG_NUMA |
| 260 | static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new) |
| 261 | { |
| 262 | struct file *file = vma->vm_file; |
| 263 | struct bin_buffer *bb = file->private_data; |
| 264 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 265 | int ret; |
| 266 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 267 | if (!bb->vm_ops) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 270 | if (!sysfs_get_active(attr_sd)) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 271 | return -EINVAL; |
| 272 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 273 | ret = 0; |
| 274 | if (bb->vm_ops->set_policy) |
| 275 | ret = bb->vm_ops->set_policy(vma, new); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 276 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 277 | sysfs_put_active(attr_sd); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static struct mempolicy *bin_get_policy(struct vm_area_struct *vma, |
| 282 | unsigned long addr) |
| 283 | { |
| 284 | struct file *file = vma->vm_file; |
| 285 | struct bin_buffer *bb = file->private_data; |
| 286 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 287 | struct mempolicy *pol; |
| 288 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 289 | if (!bb->vm_ops) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 290 | return vma->vm_policy; |
| 291 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 292 | if (!sysfs_get_active(attr_sd)) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 293 | return vma->vm_policy; |
| 294 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 295 | pol = vma->vm_policy; |
| 296 | if (bb->vm_ops->get_policy) |
| 297 | pol = bb->vm_ops->get_policy(vma, addr); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 298 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 299 | sysfs_put_active(attr_sd); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 300 | return pol; |
| 301 | } |
| 302 | |
| 303 | static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from, |
| 304 | const nodemask_t *to, unsigned long flags) |
| 305 | { |
| 306 | struct file *file = vma->vm_file; |
| 307 | struct bin_buffer *bb = file->private_data; |
| 308 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 309 | int ret; |
| 310 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 311 | if (!bb->vm_ops) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 312 | return 0; |
| 313 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 314 | if (!sysfs_get_active(attr_sd)) |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | |
Eric W. Biederman | 38f49a5 | 2010-09-20 00:57:03 -0700 | [diff] [blame] | 317 | ret = 0; |
| 318 | if (bb->vm_ops->migrate) |
| 319 | ret = bb->vm_ops->migrate(vma, from, to, flags); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 320 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 321 | sysfs_put_active(attr_sd); |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | #endif |
| 325 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 326 | static const struct vm_operations_struct bin_vm_ops = { |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 327 | .open = bin_vma_open, |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 328 | .fault = bin_fault, |
| 329 | .page_mkwrite = bin_page_mkwrite, |
| 330 | .access = bin_access, |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 331 | #ifdef CONFIG_NUMA |
| 332 | .set_policy = bin_set_policy, |
| 333 | .get_policy = bin_get_policy, |
| 334 | .migrate = bin_migrate, |
| 335 | #endif |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | static int mmap(struct file *file, struct vm_area_struct *vma) |
| 339 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 340 | struct bin_buffer *bb = file->private_data; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 341 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 342 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 343 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 344 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 346 | mutex_lock(&bb->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 347 | |
| 348 | /* need attr_sd for attr, its parent for kobj */ |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 349 | rc = -ENODEV; |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 350 | if (!sysfs_get_active(attr_sd)) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 351 | goto out_unlock; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 352 | |
| 353 | rc = -EINVAL; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 354 | if (!attr->mmap) |
| 355 | goto out_put; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 356 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 357 | rc = attr->mmap(file, kobj, attr, vma); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 358 | if (rc) |
| 359 | goto out_put; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 360 | |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 361 | /* |
| 362 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() |
| 363 | * to satisfy versions of X which crash if the mmap fails: that |
| 364 | * substitutes a new vm_file, and we don't then want bin_vm_ops. |
| 365 | */ |
| 366 | if (vma->vm_file != file) |
| 367 | goto out_put; |
| 368 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 369 | rc = -EINVAL; |
| 370 | if (bb->mmapped && bb->vm_ops != vma->vm_ops) |
| 371 | goto out_put; |
| 372 | |
Eric W. Biederman | a6849fa | 2010-09-20 00:56:27 -0700 | [diff] [blame] | 373 | /* |
| 374 | * It is not possible to successfully wrap close. |
| 375 | * So error if someone is trying to use close. |
| 376 | */ |
| 377 | rc = -EINVAL; |
| 378 | if (vma->vm_ops && vma->vm_ops->close) |
| 379 | goto out_put; |
| 380 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 381 | rc = 0; |
| 382 | bb->mmapped = 1; |
Hugh Dickins | 095160a | 2009-03-23 01:41:27 +0000 | [diff] [blame] | 383 | bb->vm_ops = vma->vm_ops; |
| 384 | vma->vm_ops = &bin_vm_ops; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 385 | out_put: |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 386 | sysfs_put_active(attr_sd); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 387 | out_unlock: |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 388 | mutex_unlock(&bb->mutex); |
| 389 | |
| 390 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 393 | static int open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 395 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 396 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 397 | struct bin_buffer *bb = NULL; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 398 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Tejun Heo | 078ce64 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 400 | /* binary file operations requires both @sd and its parent */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 401 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 402 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | error = -EACCES; |
| 405 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 406 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 408 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | error = -ENOMEM; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 411 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
| 412 | if (!bb) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 413 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 415 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 416 | if (!bb->buffer) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 417 | goto err_out; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 418 | |
| 419 | mutex_init(&bb->mutex); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 420 | bb->file = file; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 421 | file->private_data = bb; |
| 422 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 423 | mutex_lock(&sysfs_bin_lock); |
| 424 | hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers); |
| 425 | mutex_unlock(&sysfs_bin_lock); |
| 426 | |
Tejun Heo | 078ce64 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 427 | /* open succeeded, put active references */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 428 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 429 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 431 | err_out: |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 432 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 433 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return error; |
| 435 | } |
| 436 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 437 | static int release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 439 | struct bin_buffer *bb = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 441 | mutex_lock(&sysfs_bin_lock); |
| 442 | hlist_del(&bb->list); |
| 443 | mutex_unlock(&sysfs_bin_lock); |
| 444 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 445 | kfree(bb->buffer); |
| 446 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 450 | const struct file_operations bin_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | .read = read, |
| 452 | .write = write, |
| 453 | .mmap = mmap, |
| 454 | .llseek = generic_file_llseek, |
| 455 | .open = open, |
| 456 | .release = release, |
| 457 | }; |
| 458 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 459 | |
| 460 | void unmap_bin_file(struct sysfs_dirent *attr_sd) |
| 461 | { |
| 462 | struct bin_buffer *bb; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 463 | |
| 464 | if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR) |
| 465 | return; |
| 466 | |
| 467 | mutex_lock(&sysfs_bin_lock); |
| 468 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 469 | hlist_for_each_entry(bb, &attr_sd->s_bin_attr.buffers, list) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 470 | struct inode *inode = file_inode(bb->file); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame] | 471 | |
| 472 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 473 | } |
| 474 | |
| 475 | mutex_unlock(&sysfs_bin_lock); |
| 476 | } |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /** |
| 479 | * sysfs_create_bin_file - create binary file for object. |
| 480 | * @kobj: object. |
| 481 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 483 | int sysfs_create_bin_file(struct kobject *kobj, |
| 484 | const struct bin_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 486 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 488 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 490 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /** |
| 493 | * sysfs_remove_bin_file - remove binary file for object. |
| 494 | * @kobj: object. |
| 495 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 497 | void sysfs_remove_bin_file(struct kobject *kobj, |
| 498 | const struct bin_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 500 | sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |