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