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