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/file.c - sysfs regular (text) file implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | * |
| 10 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kobject.h> |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 15 | #include <linux/kallsyms.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
Miklos Szeredi | 93265d1 | 2008-06-16 13:46:47 +0200 | [diff] [blame] | 17 | #include <linux/fsnotify.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 18 | #include <linux/namei.h> |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 19 | #include <linux/poll.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 20 | #include <linux/list.h> |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Andrew Morton | ae87221d | 2007-08-24 16:11:54 -0700 | [diff] [blame] | 22 | #include <linux/limits.h> |
Greg Kroah-Hartman | 060cc74 | 2013-08-21 16:34:59 -0700 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include "sysfs.h" |
| 27 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 28 | /* |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 29 | * There's one sysfs_open_file for each open file and one sysfs_open_dirent |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 30 | * for each sysfs_dirent with one or more open files. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 31 | * |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 32 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is |
| 33 | * protected by sysfs_open_dirent_lock. |
| 34 | * |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 35 | * filp->private_data points to seq_file whose ->private points to |
| 36 | * sysfs_open_file. sysfs_open_files are chained at |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 37 | * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 38 | */ |
Jiri Slaby | d7b3788 | 2007-11-21 14:55:19 -0800 | [diff] [blame] | 39 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 40 | static DEFINE_MUTEX(sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 41 | |
| 42 | struct sysfs_open_dirent { |
| 43 | atomic_t refcnt; |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 44 | atomic_t event; |
| 45 | wait_queue_head_t poll; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 46 | struct list_head files; /* goes through sysfs_open_file.list */ |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 47 | }; |
| 48 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 49 | struct sysfs_open_file { |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 50 | struct sysfs_dirent *sd; |
| 51 | struct file *file; |
Dave Young | 52e8c20 | 2007-07-26 11:03:54 +0000 | [diff] [blame] | 52 | struct mutex mutex; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 53 | int event; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 54 | struct list_head list; |
Tejun Heo | 73107cb | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 55 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 57 | static bool sysfs_is_bin(struct sysfs_dirent *sd) |
| 58 | { |
| 59 | return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR; |
| 60 | } |
| 61 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 62 | static struct sysfs_open_file *sysfs_of(struct file *file) |
| 63 | { |
| 64 | return ((struct seq_file *)file->private_data)->private; |
| 65 | } |
| 66 | |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 67 | /* |
| 68 | * Determine ktype->sysfs_ops for the given sysfs_dirent. This function |
| 69 | * must be called while holding an active reference. |
| 70 | */ |
| 71 | static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) |
| 72 | { |
| 73 | struct kobject *kobj = sd->s_parent->s_dir.kobj; |
| 74 | |
| 75 | lockdep_assert_held(sd); |
| 76 | return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; |
| 77 | } |
| 78 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 79 | /* |
| 80 | * Reads on sysfs are handled through seq_file, which takes care of hairy |
| 81 | * details like buffering and seeking. The following function pipes |
| 82 | * sysfs_ops->show() result through seq_file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | */ |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 84 | static int sysfs_seq_show(struct seq_file *sf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 86 | struct sysfs_open_file *of = sf->private; |
| 87 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 88 | const struct sysfs_ops *ops; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 89 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | ssize_t count; |
| 91 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 92 | /* acquire buffer and ensure that it's >= PAGE_SIZE */ |
| 93 | count = seq_get_buf(sf, &buf); |
| 94 | if (count < PAGE_SIZE) { |
| 95 | seq_commit(sf, -1); |
| 96 | return 0; |
| 97 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 99 | /* |
| 100 | * Need @of->sd for attr and ops, its parent for kobj. @of->mutex |
| 101 | * nests outside active ref and is just to ensure that the ops |
| 102 | * aren't called concurrently for the same open file. |
| 103 | */ |
| 104 | mutex_lock(&of->mutex); |
| 105 | if (!sysfs_get_active(of->sd)) { |
| 106 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 107 | return -ENODEV; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 108 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 109 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 110 | of->event = atomic_read(&of->sd->s_attr.open->event); |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 111 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 112 | /* |
| 113 | * Lookup @ops and invoke show(). Control may reach here via seq |
| 114 | * file lseek even if @ops->show() isn't implemented. |
| 115 | */ |
| 116 | ops = sysfs_file_ops(of->sd); |
| 117 | if (ops->show) |
| 118 | count = ops->show(kobj, of->sd->s_attr.attr, buf); |
| 119 | else |
| 120 | count = 0; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 121 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 122 | sysfs_put_active(of->sd); |
| 123 | mutex_unlock(&of->mutex); |
| 124 | |
| 125 | if (count < 0) |
| 126 | return count; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 127 | |
Miao Xie | 8118a85 | 2007-11-21 14:55:19 -0800 | [diff] [blame] | 128 | /* |
| 129 | * The code works fine with PAGE_SIZE return but it's likely to |
| 130 | * indicate truncated result or overflow in normal use cases. |
| 131 | */ |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 132 | if (count >= (ssize_t)PAGE_SIZE) { |
| 133 | print_symbol("fill_read_buffer: %s returned bad count\n", |
| 134 | (unsigned long)ops->show); |
| 135 | /* Try to struggle along */ |
| 136 | count = PAGE_SIZE - 1; |
| 137 | } |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 138 | seq_commit(sf, count); |
| 139 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /** |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 143 | * flush_write_buffer - push buffer to kobject |
| 144 | * @of: open file |
| 145 | * @buf: data buffer for file |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 146 | * @off: file offset to write to |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 147 | * @count: number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 149 | * Get the correct pointers for the kobject and the attribute we're dealing |
| 150 | * with, then call the store() method for it with @buf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | */ |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 152 | static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off, |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 153 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 155 | struct kobject *kobj = of->sd->s_parent->s_dir.kobj; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 156 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 158 | /* |
| 159 | * Need @of->sd for attr and ops, its parent for kobj. @of->mutex |
| 160 | * nests outside active ref and is just to ensure that the ops |
| 161 | * aren't called concurrently for the same open file. |
| 162 | */ |
| 163 | mutex_lock(&of->mutex); |
| 164 | if (!sysfs_get_active(of->sd)) { |
| 165 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 166 | return -ENODEV; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 167 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 168 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 169 | if (sysfs_is_bin(of->sd)) { |
| 170 | struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr; |
| 171 | |
| 172 | rc = -EIO; |
| 173 | if (battr->write) |
| 174 | rc = battr->write(of->file, kobj, battr, buf, off, |
| 175 | count); |
| 176 | } else { |
| 177 | const struct sysfs_ops *ops = sysfs_file_ops(of->sd); |
| 178 | |
| 179 | rc = ops->store(kobj, of->sd->s_attr.attr, buf, count); |
| 180 | } |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 181 | |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 182 | sysfs_put_active(of->sd); |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 183 | mutex_unlock(&of->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 184 | |
| 185 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /** |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 189 | * sysfs_write_file - write an attribute |
| 190 | * @file: file pointer |
| 191 | * @user_buf: data to write |
| 192 | * @count: number of bytes |
| 193 | * @ppos: starting offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 195 | * Copy data in from userland and pass it to the matching |
| 196 | * sysfs_ops->store() by invoking flush_write_buffer(). |
| 197 | * |
| 198 | * There is no easy way for us to know if userspace is only doing a partial |
| 199 | * write, so we don't support them. We expect the entire buffer to come on |
| 200 | * the first write. Hint: if you're writing a value, first read the file, |
| 201 | * modify only the the value you're changing, then write entire buffer |
| 202 | * back. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | */ |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 204 | static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf, |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 205 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 207 | struct sysfs_open_file *of = sysfs_of(file); |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 208 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 209 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 211 | if (sysfs_is_bin(of->sd)) { |
| 212 | loff_t size = file_inode(file)->i_size; |
| 213 | |
| 214 | if (size <= *ppos) |
| 215 | return 0; |
| 216 | len = min_t(ssize_t, len, size - *ppos); |
| 217 | } |
| 218 | |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 219 | if (!len) |
| 220 | return 0; |
| 221 | |
| 222 | buf = kmalloc(len + 1, GFP_KERNEL); |
| 223 | if (!buf) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | if (copy_from_user(buf, user_buf, len)) { |
| 227 | len = -EFAULT; |
| 228 | goto out_free; |
| 229 | } |
| 230 | buf[len] = '\0'; /* guarantee string termination */ |
| 231 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 232 | len = flush_write_buffer(of, buf, *ppos, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (len > 0) |
| 234 | *ppos += len; |
Tejun Heo | 8ef445f | 2013-10-01 17:42:01 -0400 | [diff] [blame] | 235 | out_free: |
| 236 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return len; |
| 238 | } |
| 239 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 240 | /** |
| 241 | * sysfs_get_open_dirent - get or create sysfs_open_dirent |
| 242 | * @sd: target sysfs_dirent |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 243 | * @of: sysfs_open_file for this instance of open |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 244 | * |
| 245 | * If @sd->s_attr.open exists, increment its reference count; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 246 | * otherwise, create one. @of is chained to the files list. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 247 | * |
| 248 | * LOCKING: |
| 249 | * Kernel thread context (may sleep). |
| 250 | * |
| 251 | * RETURNS: |
| 252 | * 0 on success, -errno on failure. |
| 253 | */ |
| 254 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 255 | struct sysfs_open_file *of) |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 256 | { |
| 257 | struct sysfs_open_dirent *od, *new_od = NULL; |
| 258 | |
| 259 | retry: |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 260 | mutex_lock(&sysfs_open_file_mutex); |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 261 | spin_lock_irq(&sysfs_open_dirent_lock); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 262 | |
| 263 | if (!sd->s_attr.open && new_od) { |
| 264 | sd->s_attr.open = new_od; |
| 265 | new_od = NULL; |
| 266 | } |
| 267 | |
| 268 | od = sd->s_attr.open; |
| 269 | if (od) { |
| 270 | atomic_inc(&od->refcnt); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 271 | list_add_tail(&of->list, &od->files); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 274 | spin_unlock_irq(&sysfs_open_dirent_lock); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 275 | mutex_unlock(&sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 276 | |
| 277 | if (od) { |
| 278 | kfree(new_od); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* not there, initialize a new one and retry */ |
| 283 | new_od = kmalloc(sizeof(*new_od), GFP_KERNEL); |
| 284 | if (!new_od) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | atomic_set(&new_od->refcnt, 0); |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 288 | atomic_set(&new_od->event, 1); |
| 289 | init_waitqueue_head(&new_od->poll); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 290 | INIT_LIST_HEAD(&new_od->files); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 291 | goto retry; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * sysfs_put_open_dirent - put sysfs_open_dirent |
| 296 | * @sd: target sysfs_dirent |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 297 | * @of: associated sysfs_open_file |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 298 | * |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 299 | * Put @sd->s_attr.open and unlink @of from the files list. If |
| 300 | * reference count reaches zero, disassociate and free it. |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 301 | * |
| 302 | * LOCKING: |
| 303 | * None. |
| 304 | */ |
| 305 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 306 | struct sysfs_open_file *of) |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 307 | { |
| 308 | struct sysfs_open_dirent *od = sd->s_attr.open; |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 309 | unsigned long flags; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 310 | |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 311 | mutex_lock(&sysfs_open_file_mutex); |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 312 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 313 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 314 | list_del(&of->list); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 315 | if (atomic_dec_and_test(&od->refcnt)) |
| 316 | sd->s_attr.open = NULL; |
| 317 | else |
| 318 | od = NULL; |
| 319 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 320 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
Tejun Heo | c75ec76 | 2013-10-01 17:41:58 -0400 | [diff] [blame] | 321 | mutex_unlock(&sysfs_open_file_mutex); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 322 | |
| 323 | kfree(od); |
| 324 | } |
| 325 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 326 | static int sysfs_open_file(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 328 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 329 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 330 | struct sysfs_open_file *of; |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 331 | const struct sysfs_ops *ops; |
Kay Sievers | 000f2a4 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 332 | int error = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 334 | /* need attr_sd for attr and ops, its parent for kobj */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 335 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 336 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Kay Sievers | 000f2a4 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 338 | /* every kobject with an attribute needs a ktype assigned */ |
Tejun Heo | 375b611 | 2013-10-01 17:41:57 -0400 | [diff] [blame] | 339 | ops = sysfs_file_ops(attr_sd); |
| 340 | if (WARN(!ops, KERN_ERR |
| 341 | "missing sysfs attribute operations for kobject: %s\n", |
| 342 | kobject_name(kobj))) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 343 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | /* File needs write support. |
Greg Kroah-Hartman | ab9bf4b | 2013-08-21 16:21:17 -0700 | [diff] [blame] | 346 | * The inode's perms must say it's ok, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * and we must have a store method. |
| 348 | */ |
| 349 | if (file->f_mode & FMODE_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (!(inode->i_mode & S_IWUGO) || !ops->store) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 351 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* File needs read support. |
| 355 | * The inode's perms must say it's ok, and we there |
| 356 | * must be a show method for it. |
| 357 | */ |
| 358 | if (file->f_mode & FMODE_READ) { |
| 359 | if (!(inode->i_mode & S_IRUGO) || !ops->show) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 360 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 363 | /* allocate a sysfs_open_file for the file */ |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 364 | error = -ENOMEM; |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 365 | of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); |
| 366 | if (!of) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 367 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 369 | mutex_init(&of->mutex); |
Tejun Heo | bcafe4e | 2013-10-01 17:42:00 -0400 | [diff] [blame] | 370 | of->sd = attr_sd; |
| 371 | of->file = file; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * Always instantiate seq_file even if read access is not |
| 375 | * implemented or requested. This unifies private data access and |
| 376 | * most files are readable anyway. |
| 377 | */ |
| 378 | error = single_open(file, sysfs_seq_show, of); |
| 379 | if (error) |
| 380 | goto err_free; |
| 381 | |
| 382 | /* seq_file clears PWRITE unconditionally, restore it if WRITE */ |
| 383 | if (file->f_mode & FMODE_WRITE) |
| 384 | file->f_mode |= FMODE_PWRITE; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 385 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 386 | /* make sure we have open dirent struct */ |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 387 | error = sysfs_get_open_dirent(attr_sd, of); |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 388 | if (error) |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 389 | goto err_close; |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 390 | |
Tejun Heo | b05f054 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 391 | /* open succeeded, put active references */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 392 | sysfs_put_active(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 393 | return 0; |
| 394 | |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 395 | err_close: |
| 396 | single_release(inode, file); |
| 397 | err_free: |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 398 | kfree(of); |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 399 | err_out: |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 400 | sysfs_put_active(attr_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return error; |
| 402 | } |
| 403 | |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 404 | static int sysfs_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Tejun Heo | 85a4ffa | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 406 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 407 | struct sysfs_open_file *of = sysfs_of(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 409 | sysfs_put_open_dirent(sd, of); |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 410 | single_release(inode, filp); |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 411 | kfree(of); |
Tejun Heo | 50ab1a7 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 416 | /* Sysfs attribute files are pollable. The idea is that you read |
| 417 | * the content and then you use 'poll' or 'select' to wait for |
| 418 | * the content to change. When the content changes (assuming the |
| 419 | * manager for the kobject supports notification), poll will |
| 420 | * return POLLERR|POLLPRI, and select will return the fd whether |
| 421 | * it is waiting for read, write, or exceptions. |
| 422 | * Once poll/select indicates that the value has changed, you |
Dan Williams | 2424b5d | 2008-04-07 15:35:01 -0700 | [diff] [blame] | 423 | * need to close and re-open the file, or seek to 0 and read again. |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 424 | * Reminder: this only works for attributes which actively support |
| 425 | * it, and it is not possible to test an attribute from userspace |
Rolf Eike Beer | a93720e | 2007-08-10 13:51:07 -0700 | [diff] [blame] | 426 | * to see if it supports poll (Neither 'poll' nor 'select' return |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 427 | * an appropriate error code). When in doubt, set a suitable timeout value. |
| 428 | */ |
| 429 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
| 430 | { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 431 | struct sysfs_open_file *of = sysfs_of(filp); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 432 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 433 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 434 | |
| 435 | /* need parent for the kobj, grab both */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 436 | if (!sysfs_get_active(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 437 | goto trigger; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 438 | |
Tejun Heo | a4e8b91 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 439 | poll_wait(filp, &od->poll, wait); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 440 | |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 441 | sysfs_put_active(attr_sd); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 442 | |
Tejun Heo | 58282d8 | 2013-10-01 17:41:59 -0400 | [diff] [blame] | 443 | if (of->event != atomic_read(&od->event)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 444 | goto trigger; |
| 445 | |
KOSAKI Motohiro | 1af3557 | 2009-04-09 13:53:22 +0900 | [diff] [blame] | 446 | return DEFAULT_POLLMASK; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 447 | |
| 448 | trigger: |
KOSAKI Motohiro | 1af3557 | 2009-04-09 13:53:22 +0900 | [diff] [blame] | 449 | return DEFAULT_POLLMASK|POLLERR|POLLPRI; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 450 | } |
| 451 | |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 452 | void sysfs_notify_dirent(struct sysfs_dirent *sd) |
| 453 | { |
| 454 | struct sysfs_open_dirent *od; |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 455 | unsigned long flags; |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 456 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 457 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 458 | |
Nick Dyer | fc60bb8 | 2013-06-07 15:45:13 +0100 | [diff] [blame] | 459 | if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) { |
| 460 | od = sd->s_attr.open; |
| 461 | if (od) { |
| 462 | atomic_inc(&od->event); |
| 463 | wake_up_interruptible(&od->poll); |
| 464 | } |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 465 | } |
| 466 | |
Neil Brown | 83db93f | 2009-09-15 16:05:51 -0700 | [diff] [blame] | 467 | spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 468 | } |
| 469 | EXPORT_SYMBOL_GPL(sysfs_notify_dirent); |
| 470 | |
Trent Piepho | 8c0e399 | 2008-09-25 16:45:13 -0700 | [diff] [blame] | 471 | void sysfs_notify(struct kobject *k, const char *dir, const char *attr) |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 472 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 473 | struct sysfs_dirent *sd = k->sd; |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 474 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 475 | mutex_lock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 476 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 477 | if (sd && dir) |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 478 | sd = sysfs_find_dirent(sd, dir, NULL); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 479 | if (sd && attr) |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 480 | sd = sysfs_find_dirent(sd, attr, NULL); |
Neil Brown | f1282c8 | 2008-07-16 08:58:04 +1000 | [diff] [blame] | 481 | if (sd) |
| 482 | sysfs_notify_dirent(sd); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 483 | |
| 484 | mutex_unlock(&sysfs_mutex); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 485 | } |
| 486 | EXPORT_SYMBOL_GPL(sysfs_notify); |
| 487 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 488 | const struct file_operations sysfs_file_operations = { |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 489 | .read = seq_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | .write = sysfs_write_file, |
Tejun Heo | 13c589d | 2013-10-01 17:42:02 -0400 | [diff] [blame] | 491 | .llseek = seq_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | .open = sysfs_open_file, |
| 493 | .release = sysfs_release, |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 494 | .poll = sysfs_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
Tejun Heo | f9b9a62 | 2013-10-01 17:42:05 -0400 | [diff] [blame^] | 497 | const struct file_operations sysfs_bin_operations = { |
| 498 | .write = sysfs_write_file, |
| 499 | .llseek = generic_file_llseek, |
| 500 | }; |
| 501 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 502 | int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd, |
| 503 | const struct attribute *attr, int type, |
| 504 | umode_t amode, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 506 | umode_t mode = (amode & S_IALLUGO) | S_IFREG; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 507 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 508 | struct sysfs_dirent *sd; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 509 | int rc; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 510 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 511 | sd = sysfs_new_dirent(attr->name, mode, type); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 512 | if (!sd) |
| 513 | return -ENOMEM; |
Eric W. Biederman | 487505c | 2011-10-12 21:53:38 +0000 | [diff] [blame] | 514 | |
| 515 | sd->s_ns = ns; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 516 | sd->s_attr.attr = (void *)attr; |
Eric W. Biederman | a2db684 | 2010-02-11 15:20:00 -0800 | [diff] [blame] | 517 | sysfs_dirent_init_lockdep(sd); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 518 | |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 519 | sysfs_addrm_start(&acxt); |
| 520 | rc = sysfs_add_one(&acxt, sd, dir_sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 521 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 522 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 523 | if (rc) |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 524 | sysfs_put(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 525 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 526 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 530 | int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, |
| 531 | int type) |
| 532 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 533 | return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 537 | * sysfs_create_file_ns - create an attribute file for an object with custom ns |
| 538 | * @kobj: object we're creating for |
| 539 | * @attr: attribute descriptor |
| 540 | * @ns: namespace the new file should belong to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 542 | int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 543 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 545 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 547 | return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR, |
| 548 | attr->mode, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 551 | EXPORT_SYMBOL_GPL(sysfs_create_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 553 | int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr) |
| 554 | { |
| 555 | int err = 0; |
| 556 | int i; |
| 557 | |
| 558 | for (i = 0; ptr[i] && !err; i++) |
| 559 | err = sysfs_create_file(kobj, ptr[i]); |
| 560 | if (err) |
| 561 | while (--i >= 0) |
| 562 | sysfs_remove_file(kobj, ptr[i]); |
| 563 | return err; |
| 564 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 565 | EXPORT_SYMBOL_GPL(sysfs_create_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 567 | /** |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 568 | * sysfs_add_file_to_group - add an attribute file to a pre-existing group. |
| 569 | * @kobj: object we're acting for. |
| 570 | * @attr: attribute descriptor. |
| 571 | * @group: group name. |
| 572 | */ |
| 573 | int sysfs_add_file_to_group(struct kobject *kobj, |
| 574 | const struct attribute *attr, const char *group) |
| 575 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 576 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 577 | int error; |
| 578 | |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 579 | if (group) |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 580 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 581 | else |
| 582 | dir_sd = sysfs_get(kobj->sd); |
| 583 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 584 | if (!dir_sd) |
| 585 | return -ENOENT; |
| 586 | |
| 587 | error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR); |
| 588 | sysfs_put(dir_sd); |
| 589 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 590 | return error; |
| 591 | } |
| 592 | EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | /** |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 595 | * sysfs_chmod_file - update the modified mode value on an object attribute. |
| 596 | * @kobj: object we're acting for. |
| 597 | * @attr: attribute descriptor. |
| 598 | * @mode: file permissions. |
| 599 | * |
| 600 | */ |
Jean Delvare | 49c1940 | 2010-07-02 16:54:05 +0200 | [diff] [blame] | 601 | int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr, |
Al Viro | 48176a9 | 2011-07-24 03:40:40 -0400 | [diff] [blame] | 602 | umode_t mode) |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 603 | { |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 604 | struct sysfs_dirent *sd; |
Maneesh Soni | bc062b1 | 2005-07-29 12:13:35 -0700 | [diff] [blame] | 605 | struct iattr newattrs; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 606 | int rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 607 | |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 608 | mutex_lock(&sysfs_mutex); |
| 609 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 610 | rc = -ENOENT; |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 611 | sd = sysfs_find_dirent(kobj->sd, attr->name, NULL); |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 612 | if (!sd) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 613 | goto out; |
| 614 | |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 615 | newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO); |
Eric W. Biederman | 4c6974f | 2009-11-07 23:27:02 -0800 | [diff] [blame] | 616 | newattrs.ia_valid = ATTR_MODE; |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 617 | rc = sysfs_sd_setattr(sd, &newattrs); |
Tejun Heo | f88123e | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 618 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 619 | out: |
Eric W. Biederman | 06fc0d6 | 2009-11-20 16:08:54 -0800 | [diff] [blame] | 620 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 621 | return rc; |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 622 | } |
| 623 | EXPORT_SYMBOL_GPL(sysfs_chmod_file); |
| 624 | |
Kay Sievers | 31e5abe | 2005-04-18 21:57:32 -0700 | [diff] [blame] | 625 | /** |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 626 | * sysfs_remove_file_ns - remove an object attribute with a custom ns tag |
| 627 | * @kobj: object we're acting for |
| 628 | * @attr: attribute descriptor |
| 629 | * @ns: namespace tag of the file to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 631 | * Hash the attribute name and namespace tag and kill the victim. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | */ |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 633 | void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, |
| 634 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 636 | struct sysfs_dirent *dir_sd = kobj->sd; |
Eric W. Biederman | 487505c | 2011-10-12 21:53:38 +0000 | [diff] [blame] | 637 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 638 | sysfs_hash_and_remove(dir_sd, attr->name, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
Tejun Heo | 58292cbe | 2013-09-11 22:29:04 -0400 | [diff] [blame] | 640 | EXPORT_SYMBOL_GPL(sysfs_remove_file_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 642 | void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr) |
Andi Kleen | 1c205ae | 2010-01-05 12:48:01 +0100 | [diff] [blame] | 643 | { |
| 644 | int i; |
| 645 | for (i = 0; ptr[i]; i++) |
| 646 | sysfs_remove_file(kobj, ptr[i]); |
| 647 | } |
Greg Kroah-Hartman | 1b86675 | 2013-08-21 16:17:47 -0700 | [diff] [blame] | 648 | EXPORT_SYMBOL_GPL(sysfs_remove_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 650 | /** |
| 651 | * sysfs_remove_file_from_group - remove an attribute file from a group. |
| 652 | * @kobj: object we're acting for. |
| 653 | * @attr: attribute descriptor. |
| 654 | * @group: group name. |
| 655 | */ |
| 656 | void sysfs_remove_file_from_group(struct kobject *kobj, |
| 657 | const struct attribute *attr, const char *group) |
| 658 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 659 | struct sysfs_dirent *dir_sd; |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 660 | |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 661 | if (group) |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 662 | dir_sd = sysfs_get_dirent(kobj->sd, group); |
James Bottomley | 11f24fb | 2008-01-02 18:44:05 -0600 | [diff] [blame] | 663 | else |
| 664 | dir_sd = sysfs_get(kobj->sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 665 | if (dir_sd) { |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 666 | sysfs_hash_and_remove(dir_sd, attr->name, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 667 | sysfs_put(dir_sd); |
Alan Stern | dfa87c8 | 2007-02-20 15:02:44 -0500 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); |
| 671 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 672 | struct sysfs_schedule_callback_struct { |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 673 | struct list_head workq_list; |
| 674 | struct kobject *kobj; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 675 | void (*func)(void *); |
| 676 | void *data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 677 | struct module *owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 678 | struct work_struct work; |
| 679 | }; |
| 680 | |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 681 | static struct workqueue_struct *sysfs_workqueue; |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 682 | static DEFINE_MUTEX(sysfs_workq_mutex); |
| 683 | static LIST_HEAD(sysfs_workq); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 684 | static void sysfs_schedule_callback_work(struct work_struct *work) |
| 685 | { |
| 686 | struct sysfs_schedule_callback_struct *ss = container_of(work, |
| 687 | struct sysfs_schedule_callback_struct, work); |
| 688 | |
| 689 | (ss->func)(ss->data); |
| 690 | kobject_put(ss->kobj); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 691 | module_put(ss->owner); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 692 | mutex_lock(&sysfs_workq_mutex); |
| 693 | list_del(&ss->workq_list); |
| 694 | mutex_unlock(&sysfs_workq_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 695 | kfree(ss); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * sysfs_schedule_callback - helper to schedule a callback for a kobject |
| 700 | * @kobj: object we're acting for. |
| 701 | * @func: callback function to invoke later. |
| 702 | * @data: argument to pass to @func. |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 703 | * @owner: module owning the callback code |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 704 | * |
| 705 | * sysfs attribute methods must not unregister themselves or their parent |
| 706 | * kobject (which would amount to the same thing). Attempts to do so will |
| 707 | * deadlock, since unregistration is mutually exclusive with driver |
| 708 | * callbacks. |
| 709 | * |
| 710 | * Instead methods can call this routine, which will attempt to allocate |
| 711 | * and schedule a workqueue request to call back @func with @data as its |
| 712 | * argument in the workqueue's process context. @kobj will be pinned |
| 713 | * until @func returns. |
| 714 | * |
| 715 | * Returns 0 if the request was submitted, -ENOMEM if storage could not |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 716 | * be allocated, -ENODEV if a reference to @owner isn't available, |
| 717 | * -EAGAIN if a callback has already been scheduled for @kobj. |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 718 | */ |
| 719 | int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *), |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 720 | void *data, struct module *owner) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 721 | { |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 722 | struct sysfs_schedule_callback_struct *ss, *tmp; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 723 | |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 724 | if (!try_module_get(owner)) |
| 725 | return -ENODEV; |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 726 | |
| 727 | mutex_lock(&sysfs_workq_mutex); |
| 728 | list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list) |
| 729 | if (ss->kobj == kobj) { |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 730 | module_put(owner); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 731 | mutex_unlock(&sysfs_workq_mutex); |
| 732 | return -EAGAIN; |
| 733 | } |
| 734 | mutex_unlock(&sysfs_workq_mutex); |
| 735 | |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 736 | if (sysfs_workqueue == NULL) { |
Andrew Morton | 086a377 | 2009-05-07 12:36:53 -0700 | [diff] [blame] | 737 | sysfs_workqueue = create_singlethread_workqueue("sysfsd"); |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 738 | if (sysfs_workqueue == NULL) { |
| 739 | module_put(owner); |
| 740 | return -ENOMEM; |
| 741 | } |
| 742 | } |
| 743 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 744 | ss = kmalloc(sizeof(*ss), GFP_KERNEL); |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 745 | if (!ss) { |
| 746 | module_put(owner); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 747 | return -ENOMEM; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 748 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 749 | kobject_get(kobj); |
| 750 | ss->kobj = kobj; |
| 751 | ss->func = func; |
| 752 | ss->data = data; |
Alan Stern | 523ded7 | 2007-04-26 00:12:04 -0700 | [diff] [blame] | 753 | ss->owner = owner; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 754 | INIT_WORK(&ss->work, sysfs_schedule_callback_work); |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 755 | INIT_LIST_HEAD(&ss->workq_list); |
| 756 | mutex_lock(&sysfs_workq_mutex); |
| 757 | list_add_tail(&ss->workq_list, &sysfs_workq); |
| 758 | mutex_unlock(&sysfs_workq_mutex); |
Alex Chiang | d110271 | 2009-03-25 15:11:36 -0600 | [diff] [blame] | 759 | queue_work(sysfs_workqueue, &ss->work); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | EXPORT_SYMBOL_GPL(sysfs_schedule_callback); |