Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * file.c - operations for regular (text) files. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public |
| 17 | * License along with this program; if not, write to the |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 021110-1307, USA. |
| 20 | * |
| 21 | * Based on sysfs: |
| 22 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel |
| 23 | * |
| 24 | * configfs Copyright (C) 2005 Oracle. All rights reserved. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/module.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 29 | #include <linux/slab.h> |
| 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/semaphore.h> |
| 32 | |
| 33 | #include <linux/configfs.h> |
| 34 | #include "configfs_internal.h" |
| 35 | |
| 36 | |
| 37 | struct configfs_buffer { |
| 38 | size_t count; |
| 39 | loff_t pos; |
| 40 | char * page; |
| 41 | struct configfs_item_operations * ops; |
| 42 | struct semaphore sem; |
| 43 | int needs_read_fill; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * fill_read_buffer - allocate and fill buffer from item. |
| 49 | * @dentry: dentry pointer. |
| 50 | * @buffer: data buffer for file. |
| 51 | * |
| 52 | * Allocate @buffer->page, if it hasn't been already, then call the |
| 53 | * config_item's show() method to fill the buffer with this attribute's |
| 54 | * data. |
| 55 | * This is called only once, on the file's first read. |
| 56 | */ |
| 57 | static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer) |
| 58 | { |
| 59 | struct configfs_attribute * attr = to_attr(dentry); |
| 60 | struct config_item * item = to_item(dentry->d_parent); |
| 61 | struct configfs_item_operations * ops = buffer->ops; |
| 62 | int ret = 0; |
| 63 | ssize_t count; |
| 64 | |
| 65 | if (!buffer->page) |
| 66 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); |
| 67 | if (!buffer->page) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | count = ops->show_attribute(item,attr,buffer->page); |
| 71 | buffer->needs_read_fill = 0; |
| 72 | BUG_ON(count > (ssize_t)PAGE_SIZE); |
| 73 | if (count >= 0) |
| 74 | buffer->count = count; |
| 75 | else |
| 76 | ret = count; |
| 77 | return ret; |
| 78 | } |
| 79 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 80 | /** |
| 81 | * configfs_read_file - read an attribute. |
| 82 | * @file: file pointer. |
| 83 | * @buf: buffer to fill. |
| 84 | * @count: number of bytes to read. |
| 85 | * @ppos: starting offset in file. |
| 86 | * |
| 87 | * Userspace wants to read an attribute file. The attribute descriptor |
| 88 | * is in the file's ->d_fsdata. The target item is in the directory's |
| 89 | * ->d_fsdata. |
| 90 | * |
| 91 | * We call fill_read_buffer() to allocate and fill the buffer from the |
| 92 | * item's show() method exactly once (if the read is happening from |
| 93 | * the beginning of the file). That should fill the entire buffer with |
| 94 | * all the data the item has to offer for that attribute. |
| 95 | * We then call flush_read_buffer() to copy the buffer to userspace |
| 96 | * in the increments specified. |
| 97 | */ |
| 98 | |
| 99 | static ssize_t |
| 100 | configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 101 | { |
| 102 | struct configfs_buffer * buffer = file->private_data; |
| 103 | ssize_t retval = 0; |
| 104 | |
| 105 | down(&buffer->sem); |
| 106 | if (buffer->needs_read_fill) { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 107 | if ((retval = fill_read_buffer(file->f_path.dentry,buffer))) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 108 | goto out; |
| 109 | } |
Zach Brown | 4779efc | 2006-10-03 01:16:05 -0700 | [diff] [blame] | 110 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
| 111 | __FUNCTION__, count, *ppos, buffer->page); |
Akinobu Mita | 92f4c70 | 2007-05-09 02:33:32 -0700 | [diff] [blame^] | 112 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, |
| 113 | buffer->count); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 114 | out: |
| 115 | up(&buffer->sem); |
| 116 | return retval; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * fill_write_buffer - copy buffer from userspace. |
| 122 | * @buffer: data buffer for file. |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 123 | * @buf: data from user. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 124 | * @count: number of bytes in @userbuf. |
| 125 | * |
| 126 | * Allocate @buffer->page if it hasn't been already, then |
| 127 | * copy the user-supplied buffer into it. |
| 128 | */ |
| 129 | |
| 130 | static int |
| 131 | fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count) |
| 132 | { |
| 133 | int error; |
| 134 | |
| 135 | if (!buffer->page) |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 136 | buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 137 | if (!buffer->page) |
| 138 | return -ENOMEM; |
| 139 | |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 140 | if (count >= PAGE_SIZE) |
| 141 | count = PAGE_SIZE - 1; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 142 | error = copy_from_user(buffer->page,buf,count); |
| 143 | buffer->needs_read_fill = 1; |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 144 | /* if buf is assumed to contain a string, terminate it by \0, |
| 145 | * so e.g. sscanf() can scan the string easily */ |
| 146 | buffer->page[count] = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 147 | return error ? -EFAULT : count; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * flush_write_buffer - push buffer to config_item. |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 153 | * @dentry: dentry to the attribute |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 154 | * @buffer: data buffer for file. |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 155 | * @count: number of bytes |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 156 | * |
| 157 | * Get the correct pointers for the config_item and the attribute we're |
| 158 | * dealing with, then call the store() method for the attribute, |
| 159 | * passing the buffer that we acquired in fill_write_buffer(). |
| 160 | */ |
| 161 | |
| 162 | static int |
| 163 | flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count) |
| 164 | { |
| 165 | struct configfs_attribute * attr = to_attr(dentry); |
| 166 | struct config_item * item = to_item(dentry->d_parent); |
| 167 | struct configfs_item_operations * ops = buffer->ops; |
| 168 | |
| 169 | return ops->store_attribute(item,attr,buffer->page,count); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * configfs_write_file - write an attribute. |
| 175 | * @file: file pointer |
| 176 | * @buf: data to write |
| 177 | * @count: number of bytes |
| 178 | * @ppos: starting offset |
| 179 | * |
| 180 | * Similar to configfs_read_file(), though working in the opposite direction. |
| 181 | * We allocate and fill the data from the user in fill_write_buffer(), |
| 182 | * then push it to the config_item in flush_write_buffer(). |
| 183 | * There is no easy way for us to know if userspace is only doing a partial |
| 184 | * write, so we don't support them. We expect the entire buffer to come |
| 185 | * on the first write. |
| 186 | * Hint: if you're writing a value, first read the file, modify only the |
| 187 | * the value you're changing, then write entire buffer back. |
| 188 | */ |
| 189 | |
| 190 | static ssize_t |
| 191 | configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 192 | { |
| 193 | struct configfs_buffer * buffer = file->private_data; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 194 | ssize_t len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 195 | |
| 196 | down(&buffer->sem); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 197 | len = fill_write_buffer(buffer, buf, count); |
| 198 | if (len > 0) |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 199 | len = flush_write_buffer(file->f_path.dentry, buffer, count); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 200 | if (len > 0) |
| 201 | *ppos += len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 202 | up(&buffer->sem); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 203 | return len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int check_perm(struct inode * inode, struct file * file) |
| 207 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 208 | struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent); |
| 209 | struct configfs_attribute * attr = to_attr(file->f_path.dentry); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 210 | struct configfs_buffer * buffer; |
| 211 | struct configfs_item_operations * ops = NULL; |
| 212 | int error = 0; |
| 213 | |
| 214 | if (!item || !attr) |
| 215 | goto Einval; |
| 216 | |
| 217 | /* Grab the module reference for this attribute if we have one */ |
| 218 | if (!try_module_get(attr->ca_owner)) { |
| 219 | error = -ENODEV; |
| 220 | goto Done; |
| 221 | } |
| 222 | |
| 223 | if (item->ci_type) |
| 224 | ops = item->ci_type->ct_item_ops; |
| 225 | else |
| 226 | goto Eaccess; |
| 227 | |
| 228 | /* File needs write support. |
| 229 | * The inode's perms must say it's ok, |
| 230 | * and we must have a store method. |
| 231 | */ |
| 232 | if (file->f_mode & FMODE_WRITE) { |
| 233 | |
| 234 | if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute) |
| 235 | goto Eaccess; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | /* File needs read support. |
| 240 | * The inode's perms must say it's ok, and we there |
| 241 | * must be a show method for it. |
| 242 | */ |
| 243 | if (file->f_mode & FMODE_READ) { |
| 244 | if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute) |
| 245 | goto Eaccess; |
| 246 | } |
| 247 | |
| 248 | /* No error? Great, allocate a buffer for the file, and store it |
| 249 | * it in file->private_data for easy access. |
| 250 | */ |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 251 | buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL); |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 252 | if (!buffer) { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 253 | error = -ENOMEM; |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 254 | goto Enomem; |
| 255 | } |
| 256 | init_MUTEX(&buffer->sem); |
| 257 | buffer->needs_read_fill = 1; |
| 258 | buffer->ops = ops; |
| 259 | file->private_data = buffer; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 260 | goto Done; |
| 261 | |
| 262 | Einval: |
| 263 | error = -EINVAL; |
| 264 | goto Done; |
| 265 | Eaccess: |
| 266 | error = -EACCES; |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 267 | Enomem: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 268 | module_put(attr->ca_owner); |
| 269 | Done: |
| 270 | if (error && item) |
| 271 | config_item_put(item); |
| 272 | return error; |
| 273 | } |
| 274 | |
| 275 | static int configfs_open_file(struct inode * inode, struct file * filp) |
| 276 | { |
| 277 | return check_perm(inode,filp); |
| 278 | } |
| 279 | |
| 280 | static int configfs_release(struct inode * inode, struct file * filp) |
| 281 | { |
Josef "Jeff" Sipek | 867fa49 | 2006-12-08 02:36:47 -0800 | [diff] [blame] | 282 | struct config_item * item = to_item(filp->f_path.dentry->d_parent); |
| 283 | struct configfs_attribute * attr = to_attr(filp->f_path.dentry); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 284 | struct module * owner = attr->ca_owner; |
| 285 | struct configfs_buffer * buffer = filp->private_data; |
| 286 | |
| 287 | if (item) |
| 288 | config_item_put(item); |
| 289 | /* After this point, attr should not be accessed. */ |
| 290 | module_put(owner); |
| 291 | |
| 292 | if (buffer) { |
| 293 | if (buffer->page) |
| 294 | free_page((unsigned long)buffer->page); |
| 295 | kfree(buffer); |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 300 | const struct file_operations configfs_file_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 301 | .read = configfs_read_file, |
| 302 | .write = configfs_write_file, |
| 303 | .llseek = generic_file_llseek, |
| 304 | .open = configfs_open_file, |
| 305 | .release = configfs_release, |
| 306 | }; |
| 307 | |
| 308 | |
| 309 | int configfs_add_file(struct dentry * dir, const struct configfs_attribute * attr, int type) |
| 310 | { |
| 311 | struct configfs_dirent * parent_sd = dir->d_fsdata; |
| 312 | umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG; |
| 313 | int error = 0; |
| 314 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 315 | mutex_lock(&dir->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 316 | error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 317 | mutex_unlock(&dir->d_inode->i_mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 318 | |
| 319 | return error; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * configfs_create_file - create an attribute file for an item. |
| 325 | * @item: item we're creating for. |
| 326 | * @attr: atrribute descriptor. |
| 327 | */ |
| 328 | |
| 329 | int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr) |
| 330 | { |
| 331 | BUG_ON(!item || !item->ci_dentry || !attr); |
| 332 | |
| 333 | return configfs_add_file(item->ci_dentry, attr, |
| 334 | CONFIGFS_ITEM_ATTR); |
| 335 | } |
| 336 | |