diff options
author | Tejun Heo <tj@kernel.org> | 2013-10-01 17:41:59 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2013-10-05 20:16:28 -0400 |
commit | 58282d8dc2e7cf2b87c8fee94d7138ed08e0a2e5 (patch) | |
tree | 269a8fb4164ad733c184299023ba45cbaecdc44e /fs/sysfs | |
parent | c75ec764cf4746a2406278ffa16f590c5db290a7 (diff) |
sysfs: rename sysfs_buffer to sysfs_open_file
sysfs read path will be converted to use seq_file which will handle
buffering making sysfs_buffer a misnomer. Rename sysfs_buffer to
sysfs_open_file, and sysfs_open_dirent->buffers to ->files.
This path is pure rename.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/sysfs')
-rw-r--r-- | fs/sysfs/file.c | 127 |
1 files changed, 63 insertions, 64 deletions
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index 499cff8554fc..4b55bcf4422e 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c | |||
@@ -25,14 +25,14 @@ | |||
25 | #include "sysfs.h" | 25 | #include "sysfs.h" |
26 | 26 | ||
27 | /* | 27 | /* |
28 | * There's one sysfs_buffer for each open file and one sysfs_open_dirent | 28 | * There's one sysfs_open_file for each open file and one sysfs_open_dirent |
29 | * for each sysfs_dirent with one or more open files. | 29 | * for each sysfs_dirent with one or more open files. |
30 | * | 30 | * |
31 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is | 31 | * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is |
32 | * protected by sysfs_open_dirent_lock. | 32 | * protected by sysfs_open_dirent_lock. |
33 | * | 33 | * |
34 | * filp->private_data points to sysfs_buffer which is chained at | 34 | * filp->private_data points to sysfs_open_file which is chained at |
35 | * sysfs_open_dirent->buffers, which is protected by sysfs_open_file_mutex. | 35 | * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex. |
36 | */ | 36 | */ |
37 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); | 37 | static DEFINE_SPINLOCK(sysfs_open_dirent_lock); |
38 | static DEFINE_MUTEX(sysfs_open_file_mutex); | 38 | static DEFINE_MUTEX(sysfs_open_file_mutex); |
@@ -41,10 +41,10 @@ struct sysfs_open_dirent { | |||
41 | atomic_t refcnt; | 41 | atomic_t refcnt; |
42 | atomic_t event; | 42 | atomic_t event; |
43 | wait_queue_head_t poll; | 43 | wait_queue_head_t poll; |
44 | struct list_head buffers; /* goes through sysfs_buffer.list */ | 44 | struct list_head files; /* goes through sysfs_open_file.list */ |
45 | }; | 45 | }; |
46 | 46 | ||
47 | struct sysfs_buffer { | 47 | struct sysfs_open_file { |
48 | size_t count; | 48 | size_t count; |
49 | char *page; | 49 | char *page; |
50 | struct mutex mutex; | 50 | struct mutex mutex; |
@@ -75,7 +75,7 @@ static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd) | |||
75 | * This is called only once, on the file's first read unless an error | 75 | * This is called only once, on the file's first read unless an error |
76 | * is returned. | 76 | * is returned. |
77 | */ | 77 | */ |
78 | static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer) | 78 | static int fill_read_buffer(struct dentry *dentry, struct sysfs_open_file *of) |
79 | { | 79 | { |
80 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; | 80 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
81 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | 81 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
@@ -83,19 +83,19 @@ static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer) | |||
83 | int ret = 0; | 83 | int ret = 0; |
84 | ssize_t count; | 84 | ssize_t count; |
85 | 85 | ||
86 | if (!buffer->page) | 86 | if (!of->page) |
87 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); | 87 | of->page = (char *) get_zeroed_page(GFP_KERNEL); |
88 | if (!buffer->page) | 88 | if (!of->page) |
89 | return -ENOMEM; | 89 | return -ENOMEM; |
90 | 90 | ||
91 | /* need attr_sd for attr and ops, its parent for kobj */ | 91 | /* need attr_sd for attr and ops, its parent for kobj */ |
92 | if (!sysfs_get_active(attr_sd)) | 92 | if (!sysfs_get_active(attr_sd)) |
93 | return -ENODEV; | 93 | return -ENODEV; |
94 | 94 | ||
95 | buffer->event = atomic_read(&attr_sd->s_attr.open->event); | 95 | of->event = atomic_read(&attr_sd->s_attr.open->event); |
96 | 96 | ||
97 | ops = sysfs_file_ops(attr_sd); | 97 | ops = sysfs_file_ops(attr_sd); |
98 | count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page); | 98 | count = ops->show(kobj, attr_sd->s_attr.attr, of->page); |
99 | 99 | ||
100 | sysfs_put_active(attr_sd); | 100 | sysfs_put_active(attr_sd); |
101 | 101 | ||
@@ -110,7 +110,7 @@ static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer) | |||
110 | count = PAGE_SIZE - 1; | 110 | count = PAGE_SIZE - 1; |
111 | } | 111 | } |
112 | if (count >= 0) | 112 | if (count >= 0) |
113 | buffer->count = count; | 113 | of->count = count; |
114 | else | 114 | else |
115 | ret = count; | 115 | ret = count; |
116 | return ret; | 116 | return ret; |
@@ -138,63 +138,62 @@ static int fill_read_buffer(struct dentry *dentry, struct sysfs_buffer *buffer) | |||
138 | static ssize_t | 138 | static ssize_t |
139 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) | 139 | sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
140 | { | 140 | { |
141 | struct sysfs_buffer *buffer = file->private_data; | 141 | struct sysfs_open_file *of = file->private_data; |
142 | ssize_t retval = 0; | 142 | ssize_t retval = 0; |
143 | 143 | ||
144 | mutex_lock(&buffer->mutex); | 144 | mutex_lock(&of->mutex); |
145 | /* | 145 | /* |
146 | * Fill on zero offset and the first read so that silly things like | 146 | * Fill on zero offset and the first read so that silly things like |
147 | * "dd bs=1 skip=N" can work on sysfs files. | 147 | * "dd bs=1 skip=N" can work on sysfs files. |
148 | */ | 148 | */ |
149 | if (*ppos == 0 || !buffer->page) { | 149 | if (*ppos == 0 || !of->page) { |
150 | retval = fill_read_buffer(file->f_path.dentry, buffer); | 150 | retval = fill_read_buffer(file->f_path.dentry, of); |
151 | if (retval) | 151 | if (retval) |
152 | goto out; | 152 | goto out; |
153 | } | 153 | } |
154 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", | 154 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
155 | __func__, count, *ppos, buffer->page); | 155 | __func__, count, *ppos, of->page); |
156 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, | 156 | retval = simple_read_from_buffer(buf, count, ppos, of->page, of->count); |
157 | buffer->count); | ||
158 | out: | 157 | out: |
159 | mutex_unlock(&buffer->mutex); | 158 | mutex_unlock(&of->mutex); |
160 | return retval; | 159 | return retval; |
161 | } | 160 | } |
162 | 161 | ||
163 | /** | 162 | /** |
164 | * fill_write_buffer - copy buffer from userspace. | 163 | * fill_write_buffer - copy buffer from userspace. |
165 | * @buffer: data buffer for file. | 164 | * @of: open file struct. |
166 | * @buf: data from user. | 165 | * @buf: data from user. |
167 | * @count: number of bytes in @userbuf. | 166 | * @count: number of bytes in @userbuf. |
168 | * | 167 | * |
169 | * Allocate @buffer->page if it hasn't been already, then | 168 | * Allocate @of->page if it hasn't been already, then copy the |
170 | * copy the user-supplied buffer into it. | 169 | * user-supplied buffer into it. |
171 | */ | 170 | */ |
172 | static int fill_write_buffer(struct sysfs_buffer *buffer, | 171 | static int fill_write_buffer(struct sysfs_open_file *of, |
173 | const char __user *buf, size_t count) | 172 | const char __user *buf, size_t count) |
174 | { | 173 | { |
175 | int error; | 174 | int error; |
176 | 175 | ||
177 | if (!buffer->page) | 176 | if (!of->page) |
178 | buffer->page = (char *)get_zeroed_page(GFP_KERNEL); | 177 | of->page = (char *)get_zeroed_page(GFP_KERNEL); |
179 | if (!buffer->page) | 178 | if (!of->page) |
180 | return -ENOMEM; | 179 | return -ENOMEM; |
181 | 180 | ||
182 | if (count >= PAGE_SIZE) | 181 | if (count >= PAGE_SIZE) |
183 | count = PAGE_SIZE - 1; | 182 | count = PAGE_SIZE - 1; |
184 | error = copy_from_user(buffer->page, buf, count); | 183 | error = copy_from_user(of->page, buf, count); |
185 | 184 | ||
186 | /* | 185 | /* |
187 | * If buf is assumed to contain a string, terminate it by \0, so | 186 | * If buf is assumed to contain a string, terminate it by \0, so |
188 | * e.g. sscanf() can scan the string easily. | 187 | * e.g. sscanf() can scan the string easily. |
189 | */ | 188 | */ |
190 | buffer->page[count] = 0; | 189 | of->page[count] = 0; |
191 | return error ? -EFAULT : count; | 190 | return error ? -EFAULT : count; |
192 | } | 191 | } |
193 | 192 | ||
194 | /** | 193 | /** |
195 | * flush_write_buffer - push buffer to kobject. | 194 | * flush_write_buffer - push buffer to kobject. |
196 | * @dentry: dentry to the attribute | 195 | * @dentry: dentry to the attribute |
197 | * @buffer: data buffer for file. | 196 | * @of: open file |
198 | * @count: number of bytes | 197 | * @count: number of bytes |
199 | * | 198 | * |
200 | * Get the correct pointers for the kobject and the attribute we're | 199 | * Get the correct pointers for the kobject and the attribute we're |
@@ -202,7 +201,7 @@ static int fill_write_buffer(struct sysfs_buffer *buffer, | |||
202 | * passing the buffer that we acquired in fill_write_buffer(). | 201 | * passing the buffer that we acquired in fill_write_buffer(). |
203 | */ | 202 | */ |
204 | static int flush_write_buffer(struct dentry *dentry, | 203 | static int flush_write_buffer(struct dentry *dentry, |
205 | struct sysfs_buffer *buffer, size_t count) | 204 | struct sysfs_open_file *of, size_t count) |
206 | { | 205 | { |
207 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; | 206 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
208 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | 207 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
@@ -214,7 +213,7 @@ static int flush_write_buffer(struct dentry *dentry, | |||
214 | return -ENODEV; | 213 | return -ENODEV; |
215 | 214 | ||
216 | ops = sysfs_file_ops(attr_sd); | 215 | ops = sysfs_file_ops(attr_sd); |
217 | rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count); | 216 | rc = ops->store(kobj, attr_sd->s_attr.attr, of->page, count); |
218 | 217 | ||
219 | sysfs_put_active(attr_sd); | 218 | sysfs_put_active(attr_sd); |
220 | 219 | ||
@@ -240,27 +239,26 @@ static int flush_write_buffer(struct dentry *dentry, | |||
240 | static ssize_t sysfs_write_file(struct file *file, const char __user *buf, | 239 | static ssize_t sysfs_write_file(struct file *file, const char __user *buf, |
241 | size_t count, loff_t *ppos) | 240 | size_t count, loff_t *ppos) |
242 | { | 241 | { |
243 | struct sysfs_buffer *buffer = file->private_data; | 242 | struct sysfs_open_file *of = file->private_data; |
244 | ssize_t len; | 243 | ssize_t len; |
245 | 244 | ||
246 | mutex_lock(&buffer->mutex); | 245 | mutex_lock(&of->mutex); |
247 | len = fill_write_buffer(buffer, buf, count); | 246 | len = fill_write_buffer(of, buf, count); |
248 | if (len > 0) | 247 | if (len > 0) |
249 | len = flush_write_buffer(file->f_path.dentry, buffer, len); | 248 | len = flush_write_buffer(file->f_path.dentry, of, len); |
250 | if (len > 0) | 249 | if (len > 0) |
251 | *ppos += len; | 250 | *ppos += len; |
252 | mutex_unlock(&buffer->mutex); | 251 | mutex_unlock(&of->mutex); |
253 | return len; | 252 | return len; |
254 | } | 253 | } |
255 | 254 | ||
256 | /** | 255 | /** |
257 | * sysfs_get_open_dirent - get or create sysfs_open_dirent | 256 | * sysfs_get_open_dirent - get or create sysfs_open_dirent |
258 | * @sd: target sysfs_dirent | 257 | * @sd: target sysfs_dirent |
259 | * @buffer: sysfs_buffer for this instance of open | 258 | * @of: sysfs_open_file for this instance of open |
260 | * | 259 | * |
261 | * If @sd->s_attr.open exists, increment its reference count; | 260 | * If @sd->s_attr.open exists, increment its reference count; |
262 | * otherwise, create one. @buffer is chained to the buffers | 261 | * otherwise, create one. @of is chained to the files list. |
263 | * list. | ||
264 | * | 262 | * |
265 | * LOCKING: | 263 | * LOCKING: |
266 | * Kernel thread context (may sleep). | 264 | * Kernel thread context (may sleep). |
@@ -269,7 +267,7 @@ static ssize_t sysfs_write_file(struct file *file, const char __user *buf, | |||
269 | * 0 on success, -errno on failure. | 267 | * 0 on success, -errno on failure. |
270 | */ | 268 | */ |
271 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | 269 | static int sysfs_get_open_dirent(struct sysfs_dirent *sd, |
272 | struct sysfs_buffer *buffer) | 270 | struct sysfs_open_file *of) |
273 | { | 271 | { |
274 | struct sysfs_open_dirent *od, *new_od = NULL; | 272 | struct sysfs_open_dirent *od, *new_od = NULL; |
275 | 273 | ||
@@ -285,7 +283,7 @@ static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | |||
285 | od = sd->s_attr.open; | 283 | od = sd->s_attr.open; |
286 | if (od) { | 284 | if (od) { |
287 | atomic_inc(&od->refcnt); | 285 | atomic_inc(&od->refcnt); |
288 | list_add_tail(&buffer->list, &od->buffers); | 286 | list_add_tail(&of->list, &od->files); |
289 | } | 287 | } |
290 | 288 | ||
291 | spin_unlock_irq(&sysfs_open_dirent_lock); | 289 | spin_unlock_irq(&sysfs_open_dirent_lock); |
@@ -304,23 +302,23 @@ static int sysfs_get_open_dirent(struct sysfs_dirent *sd, | |||
304 | atomic_set(&new_od->refcnt, 0); | 302 | atomic_set(&new_od->refcnt, 0); |
305 | atomic_set(&new_od->event, 1); | 303 | atomic_set(&new_od->event, 1); |
306 | init_waitqueue_head(&new_od->poll); | 304 | init_waitqueue_head(&new_od->poll); |
307 | INIT_LIST_HEAD(&new_od->buffers); | 305 | INIT_LIST_HEAD(&new_od->files); |
308 | goto retry; | 306 | goto retry; |
309 | } | 307 | } |
310 | 308 | ||
311 | /** | 309 | /** |
312 | * sysfs_put_open_dirent - put sysfs_open_dirent | 310 | * sysfs_put_open_dirent - put sysfs_open_dirent |
313 | * @sd: target sysfs_dirent | 311 | * @sd: target sysfs_dirent |
314 | * @buffer: associated sysfs_buffer | 312 | * @of: associated sysfs_open_file |
315 | * | 313 | * |
316 | * Put @sd->s_attr.open and unlink @buffer from the buffers list. | 314 | * Put @sd->s_attr.open and unlink @of from the files list. If |
317 | * If reference count reaches zero, disassociate and free it. | 315 | * reference count reaches zero, disassociate and free it. |
318 | * | 316 | * |
319 | * LOCKING: | 317 | * LOCKING: |
320 | * None. | 318 | * None. |
321 | */ | 319 | */ |
322 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, | 320 | static void sysfs_put_open_dirent(struct sysfs_dirent *sd, |
323 | struct sysfs_buffer *buffer) | 321 | struct sysfs_open_file *of) |
324 | { | 322 | { |
325 | struct sysfs_open_dirent *od = sd->s_attr.open; | 323 | struct sysfs_open_dirent *od = sd->s_attr.open; |
326 | unsigned long flags; | 324 | unsigned long flags; |
@@ -328,7 +326,7 @@ static void sysfs_put_open_dirent(struct sysfs_dirent *sd, | |||
328 | mutex_lock(&sysfs_open_file_mutex); | 326 | mutex_lock(&sysfs_open_file_mutex); |
329 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); | 327 | spin_lock_irqsave(&sysfs_open_dirent_lock, flags); |
330 | 328 | ||
331 | list_del(&buffer->list); | 329 | list_del(&of->list); |
332 | if (atomic_dec_and_test(&od->refcnt)) | 330 | if (atomic_dec_and_test(&od->refcnt)) |
333 | sd->s_attr.open = NULL; | 331 | sd->s_attr.open = NULL; |
334 | else | 332 | else |
@@ -344,7 +342,7 @@ static int sysfs_open_file(struct inode *inode, struct file *file) | |||
344 | { | 342 | { |
345 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; | 343 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
346 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | 344 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
347 | struct sysfs_buffer *buffer; | 345 | struct sysfs_open_file *of; |
348 | const struct sysfs_ops *ops; | 346 | const struct sysfs_ops *ops; |
349 | int error = -EACCES; | 347 | int error = -EACCES; |
350 | 348 | ||
@@ -377,19 +375,20 @@ static int sysfs_open_file(struct inode *inode, struct file *file) | |||
377 | goto err_out; | 375 | goto err_out; |
378 | } | 376 | } |
379 | 377 | ||
380 | /* No error? Great, allocate a buffer for the file, and store it | 378 | /* |
381 | * it in file->private_data for easy access. | 379 | * No error? Great, allocate a sysfs_open_file for the file, and |
380 | * store it it in file->private_data for easy access. | ||
382 | */ | 381 | */ |
383 | error = -ENOMEM; | 382 | error = -ENOMEM; |
384 | buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL); | 383 | of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL); |
385 | if (!buffer) | 384 | if (!of) |
386 | goto err_out; | 385 | goto err_out; |
387 | 386 | ||
388 | mutex_init(&buffer->mutex); | 387 | mutex_init(&of->mutex); |
389 | file->private_data = buffer; | 388 | file->private_data = of; |
390 | 389 | ||
391 | /* make sure we have open dirent struct */ | 390 | /* make sure we have open dirent struct */ |
392 | error = sysfs_get_open_dirent(attr_sd, buffer); | 391 | error = sysfs_get_open_dirent(attr_sd, of); |
393 | if (error) | 392 | if (error) |
394 | goto err_free; | 393 | goto err_free; |
395 | 394 | ||
@@ -398,7 +397,7 @@ static int sysfs_open_file(struct inode *inode, struct file *file) | |||
398 | return 0; | 397 | return 0; |
399 | 398 | ||
400 | err_free: | 399 | err_free: |
401 | kfree(buffer); | 400 | kfree(of); |
402 | err_out: | 401 | err_out: |
403 | sysfs_put_active(attr_sd); | 402 | sysfs_put_active(attr_sd); |
404 | return error; | 403 | return error; |
@@ -407,13 +406,13 @@ static int sysfs_open_file(struct inode *inode, struct file *file) | |||
407 | static int sysfs_release(struct inode *inode, struct file *filp) | 406 | static int sysfs_release(struct inode *inode, struct file *filp) |
408 | { | 407 | { |
409 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; | 408 | struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata; |
410 | struct sysfs_buffer *buffer = filp->private_data; | 409 | struct sysfs_open_file *of = filp->private_data; |
411 | 410 | ||
412 | sysfs_put_open_dirent(sd, buffer); | 411 | sysfs_put_open_dirent(sd, of); |
413 | 412 | ||
414 | if (buffer->page) | 413 | if (of->page) |
415 | free_page((unsigned long)buffer->page); | 414 | free_page((unsigned long)of->page); |
416 | kfree(buffer); | 415 | kfree(of); |
417 | 416 | ||
418 | return 0; | 417 | return 0; |
419 | } | 418 | } |
@@ -433,7 +432,7 @@ static int sysfs_release(struct inode *inode, struct file *filp) | |||
433 | */ | 432 | */ |
434 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) | 433 | static unsigned int sysfs_poll(struct file *filp, poll_table *wait) |
435 | { | 434 | { |
436 | struct sysfs_buffer *buffer = filp->private_data; | 435 | struct sysfs_open_file *of = filp->private_data; |
437 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; | 436 | struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata; |
438 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; | 437 | struct sysfs_open_dirent *od = attr_sd->s_attr.open; |
439 | 438 | ||
@@ -445,7 +444,7 @@ static unsigned int sysfs_poll(struct file *filp, poll_table *wait) | |||
445 | 444 | ||
446 | sysfs_put_active(attr_sd); | 445 | sysfs_put_active(attr_sd); |
447 | 446 | ||
448 | if (buffer->event != atomic_read(&od->event)) | 447 | if (of->event != atomic_read(&od->event)) |
449 | goto trigger; | 448 | goto trigger; |
450 | 449 | ||
451 | return DEFAULT_POLLMASK; | 450 | return DEFAULT_POLLMASK; |