Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file.c - operations for regular (text) files. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/module.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 6 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kobject.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 8 | #include <linux/namei.h> |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 9 | #include <linux/poll.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 10 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/uaccess.h> |
| 12 | #include <asm/semaphore.h> |
| 13 | |
| 14 | #include "sysfs.h" |
| 15 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 16 | #define to_sattr(a) container_of(a,struct subsys_attribute, attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 18 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Subsystem file operations. |
| 20 | * These operations allow subsystems to have files that can be |
| 21 | * read/written. |
| 22 | */ |
| 23 | static ssize_t |
| 24 | subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page) |
| 25 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 26 | struct kset *kset = to_kset(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 28 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | if (sattr->show) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 31 | ret = sattr->show(kset, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | static ssize_t |
| 36 | subsys_attr_store(struct kobject * kobj, struct attribute * attr, |
| 37 | const char * page, size_t count) |
| 38 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 39 | struct kset *kset = to_kset(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct subsys_attribute * sattr = to_sattr(attr); |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 41 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (sattr->store) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 44 | ret = sattr->store(kset, page, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static struct sysfs_ops subsys_sysfs_ops = { |
| 49 | .show = subsys_attr_show, |
| 50 | .store = subsys_attr_store, |
| 51 | }; |
| 52 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 53 | /** |
| 54 | * add_to_collection - add buffer to a collection |
| 55 | * @buffer: buffer to be added |
Randy Dunlap | f95d882 | 2007-02-10 14:41:56 -0800 | [diff] [blame] | 56 | * @node: inode of set to add to |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 57 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 59 | static inline void |
| 60 | add_to_collection(struct sysfs_buffer *buffer, struct inode *node) |
| 61 | { |
| 62 | struct sysfs_buffer_collection *set = node->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 64 | mutex_lock(&node->i_mutex); |
| 65 | list_add(&buffer->associates, &set->associates); |
| 66 | mutex_unlock(&node->i_mutex); |
| 67 | } |
| 68 | |
| 69 | static inline void |
| 70 | remove_from_collection(struct sysfs_buffer *buffer, struct inode *node) |
| 71 | { |
| 72 | mutex_lock(&node->i_mutex); |
| 73 | list_del(&buffer->associates); |
| 74 | mutex_unlock(&node->i_mutex); |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * fill_read_buffer - allocate and fill buffer from object. |
| 79 | * @dentry: dentry pointer. |
| 80 | * @buffer: data buffer for file. |
| 81 | * |
| 82 | * Allocate @buffer->page, if it hasn't been already, then call the |
| 83 | * kobject's show() method to fill the buffer with this attribute's |
| 84 | * data. |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 85 | * This is called only once, on the file's first read unless an error |
| 86 | * is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
| 88 | static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer) |
| 89 | { |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 90 | struct sysfs_dirent * sd = dentry->d_fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct attribute * attr = to_attr(dentry); |
| 92 | struct kobject * kobj = to_kobj(dentry->d_parent); |
| 93 | struct sysfs_ops * ops = buffer->ops; |
| 94 | int ret = 0; |
| 95 | ssize_t count; |
| 96 | |
| 97 | if (!buffer->page) |
| 98 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); |
| 99 | if (!buffer->page) |
| 100 | return -ENOMEM; |
| 101 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 102 | buffer->event = atomic_read(&sd->s_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | count = ops->show(kobj,attr,buffer->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | BUG_ON(count > (ssize_t)PAGE_SIZE); |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 105 | if (count >= 0) { |
| 106 | buffer->needs_read_fill = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | buffer->count = count; |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 108 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | ret = count; |
Oliver Neukum | 82244b1 | 2007-01-02 08:48:08 +0100 | [diff] [blame] | 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * flush_read_buffer - push buffer to userspace. |
| 117 | * @buffer: data buffer for file. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 118 | * @buf: user-passed buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * @count: number of bytes requested. |
| 120 | * @ppos: file position. |
| 121 | * |
| 122 | * Copy the buffer we filled in fill_read_buffer() to userspace. |
| 123 | * This is done at the reader's leisure, copying and advancing |
| 124 | * the amount they specify each time. |
| 125 | * This may be called continuously until the buffer is empty. |
| 126 | */ |
| 127 | static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf, |
| 128 | size_t count, loff_t * ppos) |
| 129 | { |
| 130 | int error; |
| 131 | |
| 132 | if (*ppos > buffer->count) |
| 133 | return 0; |
| 134 | |
| 135 | if (count > (buffer->count - *ppos)) |
| 136 | count = buffer->count - *ppos; |
| 137 | |
| 138 | error = copy_to_user(buf,buffer->page + *ppos,count); |
| 139 | if (!error) |
| 140 | *ppos += count; |
| 141 | return error ? -EFAULT : count; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * sysfs_read_file - read an attribute. |
| 146 | * @file: file pointer. |
| 147 | * @buf: buffer to fill. |
| 148 | * @count: number of bytes to read. |
| 149 | * @ppos: starting offset in file. |
| 150 | * |
| 151 | * Userspace wants to read an attribute file. The attribute descriptor |
| 152 | * is in the file's ->d_fsdata. The target object is in the directory's |
| 153 | * ->d_fsdata. |
| 154 | * |
| 155 | * We call fill_read_buffer() to allocate and fill the buffer from the |
| 156 | * object's show() method exactly once (if the read is happening from |
| 157 | * the beginning of the file). That should fill the entire buffer with |
| 158 | * all the data the object has to offer for that attribute. |
| 159 | * We then call flush_read_buffer() to copy the buffer to userspace |
| 160 | * in the increments specified. |
| 161 | */ |
| 162 | |
| 163 | static ssize_t |
| 164 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 165 | { |
| 166 | struct sysfs_buffer * buffer = file->private_data; |
| 167 | ssize_t retval = 0; |
| 168 | |
| 169 | down(&buffer->sem); |
| 170 | if (buffer->needs_read_fill) { |
Alan Stern | e7b0d26 | 2007-03-15 15:51:28 -0400 | [diff] [blame] | 171 | if (buffer->orphaned) |
| 172 | retval = -ENODEV; |
| 173 | else |
| 174 | retval = fill_read_buffer(file->f_path.dentry,buffer); |
| 175 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | goto out; |
| 177 | } |
Zach Brown | 5c1fdf4 | 2006-10-03 01:16:06 -0700 | [diff] [blame] | 178 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
| 179 | __FUNCTION__, count, *ppos, buffer->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | retval = flush_read_buffer(buffer,buf,count,ppos); |
| 181 | out: |
| 182 | up(&buffer->sem); |
| 183 | return retval; |
| 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /** |
| 187 | * fill_write_buffer - copy buffer from userspace. |
| 188 | * @buffer: data buffer for file. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 189 | * @buf: data from user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * @count: number of bytes in @userbuf. |
| 191 | * |
| 192 | * Allocate @buffer->page if it hasn't been already, then |
| 193 | * copy the user-supplied buffer into it. |
| 194 | */ |
| 195 | |
| 196 | static int |
| 197 | fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) |
| 198 | { |
| 199 | int error; |
| 200 | |
| 201 | if (!buffer->page) |
| 202 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); |
| 203 | if (!buffer->page) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | if (count >= PAGE_SIZE) |
Greg Kroah-Hartman | 6e0dd74 | 2006-03-31 15:37:06 -0800 | [diff] [blame] | 207 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | error = copy_from_user(buffer->page,buf,count); |
| 209 | buffer->needs_read_fill = 1; |
Thomas Maier | 035ed7a | 2006-10-22 19:17:47 +0200 | [diff] [blame] | 210 | /* if buf is assumed to contain a string, terminate it by \0, |
| 211 | so e.g. sscanf() can scan the string easily */ |
| 212 | buffer->page[count] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return error ? -EFAULT : count; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * flush_write_buffer - push buffer to kobject. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 219 | * @dentry: dentry to the attribute |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | * @buffer: data buffer for file. |
Martin Waitz | 3d41088 | 2005-06-23 22:05:21 -0700 | [diff] [blame] | 221 | * @count: number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | * |
| 223 | * Get the correct pointers for the kobject and the attribute we're |
| 224 | * dealing with, then call the store() method for the attribute, |
| 225 | * passing the buffer that we acquired in fill_write_buffer(). |
| 226 | */ |
| 227 | |
| 228 | static int |
| 229 | flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count) |
| 230 | { |
| 231 | struct attribute * attr = to_attr(dentry); |
| 232 | struct kobject * kobj = to_kobj(dentry->d_parent); |
| 233 | struct sysfs_ops * ops = buffer->ops; |
| 234 | |
| 235 | return ops->store(kobj,attr,buffer->page,count); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * sysfs_write_file - write an attribute. |
| 241 | * @file: file pointer |
| 242 | * @buf: data to write |
| 243 | * @count: number of bytes |
| 244 | * @ppos: starting offset |
| 245 | * |
| 246 | * Similar to sysfs_read_file(), though working in the opposite direction. |
| 247 | * We allocate and fill the data from the user in fill_write_buffer(), |
| 248 | * then push it to the kobject in flush_write_buffer(). |
| 249 | * There is no easy way for us to know if userspace is only doing a partial |
| 250 | * write, so we don't support them. We expect the entire buffer to come |
| 251 | * on the first write. |
| 252 | * Hint: if you're writing a value, first read the file, modify only the |
| 253 | * the value you're changing, then write entire buffer back. |
| 254 | */ |
| 255 | |
| 256 | static ssize_t |
| 257 | sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 258 | { |
| 259 | struct sysfs_buffer * buffer = file->private_data; |
| 260 | ssize_t len; |
| 261 | |
| 262 | down(&buffer->sem); |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 263 | if (buffer->orphaned) { |
| 264 | len = -ENODEV; |
| 265 | goto out; |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | len = fill_write_buffer(buffer, buf, count); |
| 268 | if (len > 0) |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 269 | len = flush_write_buffer(file->f_path.dentry, buffer, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | if (len > 0) |
| 271 | *ppos += len; |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 272 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | up(&buffer->sem); |
| 274 | return len; |
| 275 | } |
| 276 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 277 | static int sysfs_open_file(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 279 | struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent); |
| 280 | struct attribute * attr = to_attr(file->f_path.dentry); |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 281 | struct sysfs_buffer_collection *set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | struct sysfs_buffer * buffer; |
| 283 | struct sysfs_ops * ops = NULL; |
| 284 | int error = 0; |
| 285 | |
| 286 | if (!kobj || !attr) |
| 287 | goto Einval; |
| 288 | |
| 289 | /* Grab the module reference for this attribute if we have one */ |
| 290 | if (!try_module_get(attr->owner)) { |
| 291 | error = -ENODEV; |
| 292 | goto Done; |
| 293 | } |
| 294 | |
| 295 | /* if the kobject has no ktype, then we assume that it is a subsystem |
| 296 | * itself, and use ops for it. |
| 297 | */ |
| 298 | if (kobj->kset && kobj->kset->ktype) |
| 299 | ops = kobj->kset->ktype->sysfs_ops; |
| 300 | else if (kobj->ktype) |
| 301 | ops = kobj->ktype->sysfs_ops; |
| 302 | else |
| 303 | ops = &subsys_sysfs_ops; |
| 304 | |
| 305 | /* No sysfs operations, either from having no subsystem, |
| 306 | * or the subsystem have no operations. |
| 307 | */ |
| 308 | if (!ops) |
| 309 | goto Eaccess; |
| 310 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 311 | /* make sure we have a collection to add our buffers to */ |
| 312 | mutex_lock(&inode->i_mutex); |
| 313 | if (!(set = inode->i_private)) { |
| 314 | if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) { |
| 315 | error = -ENOMEM; |
| 316 | goto Done; |
| 317 | } else { |
| 318 | INIT_LIST_HEAD(&set->associates); |
| 319 | } |
| 320 | } |
| 321 | mutex_unlock(&inode->i_mutex); |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* File needs write support. |
| 324 | * The inode's perms must say it's ok, |
| 325 | * and we must have a store method. |
| 326 | */ |
| 327 | if (file->f_mode & FMODE_WRITE) { |
| 328 | |
| 329 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
| 330 | goto Eaccess; |
| 331 | |
| 332 | } |
| 333 | |
| 334 | /* File needs read support. |
| 335 | * The inode's perms must say it's ok, and we there |
| 336 | * must be a show method for it. |
| 337 | */ |
| 338 | if (file->f_mode & FMODE_READ) { |
| 339 | if (!(inode->i_mode & S_IRUGO) || !ops->show) |
| 340 | goto Eaccess; |
| 341 | } |
| 342 | |
| 343 | /* No error? Great, allocate a buffer for the file, and store it |
| 344 | * it in file->private_data for easy access. |
| 345 | */ |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 346 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (buffer) { |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 348 | INIT_LIST_HEAD(&buffer->associates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | init_MUTEX(&buffer->sem); |
| 350 | buffer->needs_read_fill = 1; |
| 351 | buffer->ops = ops; |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 352 | add_to_collection(buffer, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | file->private_data = buffer; |
| 354 | } else |
| 355 | error = -ENOMEM; |
| 356 | goto Done; |
| 357 | |
| 358 | Einval: |
| 359 | error = -EINVAL; |
| 360 | goto Done; |
| 361 | Eaccess: |
| 362 | error = -EACCES; |
| 363 | module_put(attr->owner); |
| 364 | Done: |
Mariusz Kozlowski | f750653 | 2007-01-02 13:41:10 +0100 | [diff] [blame] | 365 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | kobject_put(kobj); |
| 367 | return error; |
| 368 | } |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | static int sysfs_release(struct inode * inode, struct file * filp) |
| 371 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 372 | struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent); |
| 373 | struct attribute * attr = to_attr(filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | struct module * owner = attr->owner; |
| 375 | struct sysfs_buffer * buffer = filp->private_data; |
| 376 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 377 | if (buffer) |
| 378 | remove_from_collection(buffer, inode); |
Mariusz Kozlowski | f750653 | 2007-01-02 13:41:10 +0100 | [diff] [blame] | 379 | kobject_put(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* After this point, attr should not be accessed. */ |
| 381 | module_put(owner); |
| 382 | |
| 383 | if (buffer) { |
| 384 | if (buffer->page) |
| 385 | free_page((unsigned long)buffer->page); |
| 386 | kfree(buffer); |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 391 | /* Sysfs attribute files are pollable. The idea is that you read |
| 392 | * the content and then you use 'poll' or 'select' to wait for |
| 393 | * the content to change. When the content changes (assuming the |
| 394 | * manager for the kobject supports notification), poll will |
| 395 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 396 | * it is waiting for read, write, or exceptions. |
| 397 | * Once poll/select indicates that the value has changed, you |
| 398 | * need to close and re-open the file, as simply seeking and reading |
| 399 | * again will not get new data, or reset the state of 'poll'. |
| 400 | * Reminder: this only works for attributes which actively support |
| 401 | * it, and it is not possible to test an attribute from userspace |
| 402 | * to see if it supports poll (Nether 'poll' or 'select' return |
| 403 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 404 | */ |
| 405 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
| 406 | { |
| 407 | struct sysfs_buffer * buffer = filp->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 408 | struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent); |
| 409 | struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 410 | int res = 0; |
| 411 | |
| 412 | poll_wait(filp, &kobj->poll, wait); |
| 413 | |
| 414 | if (buffer->event != atomic_read(&sd->s_event)) { |
| 415 | res = POLLERR|POLLPRI; |
| 416 | buffer->needs_read_fill = 1; |
| 417 | } |
| 418 | |
| 419 | return res; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static struct dentry *step_down(struct dentry *dir, const char * name) |
| 424 | { |
| 425 | struct dentry * de; |
| 426 | |
| 427 | if (dir == NULL || dir->d_inode == NULL) |
| 428 | return NULL; |
| 429 | |
| 430 | mutex_lock(&dir->d_inode->i_mutex); |
| 431 | de = lookup_one_len(name, dir, strlen(name)); |
| 432 | mutex_unlock(&dir->d_inode->i_mutex); |
| 433 | dput(dir); |
| 434 | if (IS_ERR(de)) |
| 435 | return NULL; |
| 436 | if (de->d_inode == NULL) { |
| 437 | dput(de); |
| 438 | return NULL; |
| 439 | } |
| 440 | return de; |
| 441 | } |
| 442 | |
| 443 | void sysfs_notify(struct kobject * k, char *dir, char *attr) |
| 444 | { |
| 445 | struct dentry *de = k->dentry; |
| 446 | if (de) |
| 447 | dget(de); |
| 448 | if (de && dir) |
| 449 | de = step_down(de, dir); |
| 450 | if (de && attr) |
| 451 | de = step_down(de, attr); |
| 452 | if (de) { |
| 453 | struct sysfs_dirent * sd = de->d_fsdata; |
| 454 | if (sd) |
| 455 | atomic_inc(&sd->s_event); |
| 456 | wake_up_interruptible(&k->poll); |
| 457 | dput(de); |
| 458 | } |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 461 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 462 | const struct file_operations sysfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | .read = sysfs_read_file, |
| 464 | .write = sysfs_write_file, |
| 465 | .llseek = generic_file_llseek, |
| 466 | .open = sysfs_open_file, |
| 467 | .release = sysfs_release, |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 468 | .poll = sysfs_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | |
| 472 | int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type) |
| 473 | { |
| 474 | struct sysfs_dirent * parent_sd = dir->d_fsdata; |
| 475 | umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 476 | int error = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 478 | mutex_lock(&dir->d_inode->i_mutex); |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 479 | if (!sysfs_dirent_exist(parent_sd, attr->name)) |
| 480 | error = sysfs_make_dirent(parent_sd, NULL, (void *)attr, |
| 481 | mode, type); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 482 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | return error; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | /** |
| 489 | * sysfs_create_file - create an attribute file for an object. |
| 490 | * @kobj: object we're creating for. |
| 491 | * @attr: atrribute descriptor. |
| 492 | */ |
| 493 | |
| 494 | int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) |
| 495 | { |
| 496 | BUG_ON(!kobj || !kobj->dentry || !attr); |
| 497 | |
| 498 | return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR); |
| 499 | |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /** |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 504 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. |
| 505 | * @kobj: object we're acting for. |
| 506 | * @attr: attribute descriptor. |
| 507 | * @group: group name. |
| 508 | */ |
| 509 | int sysfs_add_file_to_group(struct kobject *kobj, |
| 510 | const struct attribute *attr, const char *group) |
| 511 | { |
| 512 | struct dentry *dir; |
| 513 | int error; |
| 514 | |
| 515 | dir = lookup_one_len(group, kobj->dentry, strlen(group)); |
| 516 | if (IS_ERR(dir)) |
| 517 | error = PTR_ERR(dir); |
| 518 | else { |
| 519 | error = sysfs_add_file(dir, attr, SYSFS_KOBJ_ATTR); |
| 520 | dput(dir); |
| 521 | } |
| 522 | return error; |
| 523 | } |
| 524 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
| 525 | |
| 526 | |
| 527 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * sysfs_update_file - update the modified timestamp on an object attribute. |
| 529 | * @kobj: object we're acting for. |
| 530 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | */ |
| 532 | int sysfs_update_file(struct kobject * kobj, const struct attribute * attr) |
| 533 | { |
| 534 | struct dentry * dir = kobj->dentry; |
| 535 | struct dentry * victim; |
| 536 | int res = -ENOENT; |
| 537 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 538 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 539 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (!IS_ERR(victim)) { |
| 541 | /* make sure dentry is really there */ |
| 542 | if (victim->d_inode && |
| 543 | (victim->d_parent->d_inode == dir->d_inode)) { |
| 544 | victim->d_inode->i_mtime = CURRENT_TIME; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 545 | fsnotify_modify(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | res = 0; |
| 547 | } else |
| 548 | d_drop(victim); |
| 549 | |
| 550 | /** |
Hidetoshi Seto | 97a5018 | 2006-09-20 16:49:02 +0900 | [diff] [blame] | 551 | * Drop the reference acquired from lookup_one_len() above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | */ |
| 553 | dput(victim); |
| 554 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 555 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | return res; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 562 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 563 | * @kobj: object we're acting for. |
| 564 | * @attr: attribute descriptor. |
| 565 | * @mode: file permissions. |
| 566 | * |
| 567 | */ |
| 568 | int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode) |
| 569 | { |
| 570 | struct dentry *dir = kobj->dentry; |
| 571 | struct dentry *victim; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 572 | struct inode * inode; |
| 573 | struct iattr newattrs; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 574 | int res = -ENOENT; |
| 575 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 576 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 577 | victim = lookup_one_len(attr->name, dir, strlen(attr->name)); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 578 | if (!IS_ERR(victim)) { |
| 579 | if (victim->d_inode && |
| 580 | (victim->d_parent->d_inode == dir->d_inode)) { |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 581 | inode = victim->d_inode; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 582 | mutex_lock(&inode->i_mutex); |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 583 | newattrs.ia_mode = (mode & S_IALLUGO) | |
| 584 | (inode->i_mode & ~S_IALLUGO); |
| 585 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 586 | res = notify_change(victim, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 587 | mutex_unlock(&inode->i_mutex); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 588 | } |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 589 | dput(victim); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 590 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 591 | mutex_unlock(&dir->d_inode->i_mutex); |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 592 | |
| 593 | return res; |
| 594 | } |
| 595 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 596 | |
| 597 | |
| 598 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * sysfs_remove_file - remove an object attribute. |
| 600 | * @kobj: object we're acting for. |
| 601 | * @attr: attribute descriptor. |
| 602 | * |
| 603 | * Hash the attribute name and kill the victim. |
| 604 | */ |
| 605 | |
| 606 | void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) |
| 607 | { |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 608 | sysfs_hash_and_remove(kobj->dentry, attr->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 612 | /** |
| 613 | * sysfs_remove_file_from_group - remove an attribute file from a group. |
| 614 | * @kobj: object we're acting for. |
| 615 | * @attr: attribute descriptor. |
| 616 | * @group: group name. |
| 617 | */ |
| 618 | void sysfs_remove_file_from_group(struct kobject *kobj, |
| 619 | const struct attribute *attr, const char *group) |
| 620 | { |
| 621 | struct dentry *dir; |
| 622 | |
| 623 | dir = lookup_one_len(group, kobj->dentry, strlen(group)); |
| 624 | if (!IS_ERR(dir)) { |
| 625 | sysfs_hash_and_remove(dir, attr->name); |
| 626 | dput(dir); |
| 627 | } |
| 628 | } |
| 629 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
| 630 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 631 | struct sysfs_schedule_callback_struct { |
| 632 | struct kobject *kobj; |
| 633 | void (*func)(void *); |
| 634 | void *data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 635 | struct module *owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 636 | struct work_struct work; |
| 637 | }; |
| 638 | |
| 639 | static void sysfs_schedule_callback_work(struct work_struct *work) |
| 640 | { |
| 641 | struct sysfs_schedule_callback_struct *ss = container_of(work, |
| 642 | struct sysfs_schedule_callback_struct, work); |
| 643 | |
| 644 | (ss->func)(ss->data); |
| 645 | kobject_put(ss->kobj); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 646 | module_put(ss->owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 647 | kfree(ss); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * sysfs_schedule_callback - helper to schedule a callback for a kobject |
| 652 | * @kobj: object we're acting for. |
| 653 | * @func: callback function to invoke later. |
| 654 | * @data: argument to pass to @func. |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 655 | * @owner: module owning the callback code |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 656 | * |
| 657 | * sysfs attribute methods must not unregister themselves or their parent |
| 658 | * kobject (which would amount to the same thing). Attempts to do so will |
| 659 | * deadlock, since unregistration is mutually exclusive with driver |
| 660 | * callbacks. |
| 661 | * |
| 662 | * Instead methods can call this routine, which will attempt to allocate |
| 663 | * and schedule a workqueue request to call back @func with @data as its |
| 664 | * argument in the workqueue's process context. @kobj will be pinned |
| 665 | * until @func returns. |
| 666 | * |
| 667 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 668 | * be allocated, -ENODEV if a reference to @owner isn't available. |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 669 | */ |
| 670 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 671 | void *data, struct module *owner) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 672 | { |
| 673 | struct sysfs_schedule_callback_struct *ss; |
| 674 | |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 675 | if (!try_module_get(owner)) |
| 676 | return -ENODEV; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 677 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 678 | if (!ss) { |
| 679 | module_put(owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 680 | return -ENOMEM; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 681 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 682 | kobject_get(kobj); |
| 683 | ss->kobj = kobj; |
| 684 | ss->func = func; |
| 685 | ss->data = data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 686 | ss->owner = owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 687 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
| 688 | schedule_work(&ss->work); |
| 689 | return 0; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |
| 692 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | EXPORT_SYMBOL_GPL(sysfs_create_file); |
| 695 | EXPORT_SYMBOL_GPL(sysfs_remove_file); |
| 696 | EXPORT_SYMBOL_GPL(sysfs_update_file); |