diff options
-rw-r--r-- | fs/debugfs/file.c | 67 | ||||
-rw-r--r-- | fs/libfs.c | 100 | ||||
-rw-r--r-- | include/linux/fs.h | 46 |
3 files changed, 175 insertions, 38 deletions
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c index 548556ff2506..efc97d9b7860 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c | |||
@@ -45,44 +45,15 @@ struct file_operations debugfs_file_operations = { | |||
45 | .open = default_open, | 45 | .open = default_open, |
46 | }; | 46 | }; |
47 | 47 | ||
48 | #define simple_type(type, format, temptype, strtolfn) \ | 48 | static void debugfs_u8_set(void *data, u64 val) |
49 | static ssize_t read_file_##type(struct file *file, char __user *user_buf, \ | 49 | { |
50 | size_t count, loff_t *ppos) \ | 50 | *(u8 *)data = val; |
51 | { \ | 51 | } |
52 | char buf[32]; \ | 52 | static u64 debugfs_u8_get(void *data) |
53 | type *val = file->private_data; \ | 53 | { |
54 | \ | 54 | return *(u8 *)data; |
55 | snprintf(buf, sizeof(buf), format "\n", *val); \ | 55 | } |
56 | return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));\ | 56 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
57 | } \ | ||
58 | static ssize_t write_file_##type(struct file *file, const char __user *user_buf,\ | ||
59 | size_t count, loff_t *ppos) \ | ||
60 | { \ | ||
61 | char *endp; \ | ||
62 | char buf[32]; \ | ||
63 | int buf_size; \ | ||
64 | type *val = file->private_data; \ | ||
65 | temptype tmp; \ | ||
66 | \ | ||
67 | memset(buf, 0x00, sizeof(buf)); \ | ||
68 | buf_size = min(count, (sizeof(buf)-1)); \ | ||
69 | if (copy_from_user(buf, user_buf, buf_size)) \ | ||
70 | return -EFAULT; \ | ||
71 | \ | ||
72 | tmp = strtolfn(buf, &endp, 0); \ | ||
73 | if ((endp == buf) || ((type)tmp != tmp)) \ | ||
74 | return -EINVAL; \ | ||
75 | *val = tmp; \ | ||
76 | return count; \ | ||
77 | } \ | ||
78 | static struct file_operations fops_##type = { \ | ||
79 | .read = read_file_##type, \ | ||
80 | .write = write_file_##type, \ | ||
81 | .open = default_open, \ | ||
82 | }; | ||
83 | simple_type(u8, "%c", unsigned long, simple_strtoul); | ||
84 | simple_type(u16, "%hi", unsigned long, simple_strtoul); | ||
85 | simple_type(u32, "%i", unsigned long, simple_strtoul); | ||
86 | 57 | ||
87 | /** | 58 | /** |
88 | * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | 59 | * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. |
@@ -116,6 +87,16 @@ struct dentry *debugfs_create_u8(const char *name, mode_t mode, | |||
116 | } | 87 | } |
117 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | 88 | EXPORT_SYMBOL_GPL(debugfs_create_u8); |
118 | 89 | ||
90 | static void debugfs_u16_set(void *data, u64 val) | ||
91 | { | ||
92 | *(u16 *)data = val; | ||
93 | } | ||
94 | static u64 debugfs_u16_get(void *data) | ||
95 | { | ||
96 | return *(u16 *)data; | ||
97 | } | ||
98 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); | ||
99 | |||
119 | /** | 100 | /** |
120 | * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | 101 | * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. |
121 | * | 102 | * |
@@ -148,6 +129,16 @@ struct dentry *debugfs_create_u16(const char *name, mode_t mode, | |||
148 | } | 129 | } |
149 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | 130 | EXPORT_SYMBOL_GPL(debugfs_create_u16); |
150 | 131 | ||
132 | static void debugfs_u32_set(void *data, u64 val) | ||
133 | { | ||
134 | *(u32 *)data = val; | ||
135 | } | ||
136 | static u64 debugfs_u32_get(void *data) | ||
137 | { | ||
138 | return *(u32 *)data; | ||
139 | } | ||
140 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); | ||
141 | |||
151 | /** | 142 | /** |
152 | * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | 143 | * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. |
153 | * | 144 | * |
diff --git a/fs/libfs.c b/fs/libfs.c index f90b29595927..5025563e7379 100644 --- a/fs/libfs.c +++ b/fs/libfs.c | |||
@@ -519,6 +519,102 @@ int simple_transaction_release(struct inode *inode, struct file *file) | |||
519 | return 0; | 519 | return 0; |
520 | } | 520 | } |
521 | 521 | ||
522 | /* Simple attribute files */ | ||
523 | |||
524 | struct simple_attr { | ||
525 | u64 (*get)(void *); | ||
526 | void (*set)(void *, u64); | ||
527 | char get_buf[24]; /* enough to store a u64 and "\n\0" */ | ||
528 | char set_buf[24]; | ||
529 | void *data; | ||
530 | const char *fmt; /* format for read operation */ | ||
531 | struct semaphore sem; /* protects access to these buffers */ | ||
532 | }; | ||
533 | |||
534 | /* simple_attr_open is called by an actual attribute open file operation | ||
535 | * to set the attribute specific access operations. */ | ||
536 | int simple_attr_open(struct inode *inode, struct file *file, | ||
537 | u64 (*get)(void *), void (*set)(void *, u64), | ||
538 | const char *fmt) | ||
539 | { | ||
540 | struct simple_attr *attr; | ||
541 | |||
542 | attr = kmalloc(sizeof(*attr), GFP_KERNEL); | ||
543 | if (!attr) | ||
544 | return -ENOMEM; | ||
545 | |||
546 | attr->get = get; | ||
547 | attr->set = set; | ||
548 | attr->data = inode->u.generic_ip; | ||
549 | attr->fmt = fmt; | ||
550 | init_MUTEX(&attr->sem); | ||
551 | |||
552 | file->private_data = attr; | ||
553 | |||
554 | return nonseekable_open(inode, file); | ||
555 | } | ||
556 | |||
557 | int simple_attr_close(struct inode *inode, struct file *file) | ||
558 | { | ||
559 | kfree(file->private_data); | ||
560 | return 0; | ||
561 | } | ||
562 | |||
563 | /* read from the buffer that is filled with the get function */ | ||
564 | ssize_t simple_attr_read(struct file *file, char __user *buf, | ||
565 | size_t len, loff_t *ppos) | ||
566 | { | ||
567 | struct simple_attr *attr; | ||
568 | size_t size; | ||
569 | ssize_t ret; | ||
570 | |||
571 | attr = file->private_data; | ||
572 | |||
573 | if (!attr->get) | ||
574 | return -EACCES; | ||
575 | |||
576 | down(&attr->sem); | ||
577 | if (*ppos) /* continued read */ | ||
578 | size = strlen(attr->get_buf); | ||
579 | else /* first read */ | ||
580 | size = scnprintf(attr->get_buf, sizeof(attr->get_buf), | ||
581 | attr->fmt, | ||
582 | (unsigned long long)attr->get(attr->data)); | ||
583 | |||
584 | ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); | ||
585 | up(&attr->sem); | ||
586 | return ret; | ||
587 | } | ||
588 | |||
589 | /* interpret the buffer as a number to call the set function with */ | ||
590 | ssize_t simple_attr_write(struct file *file, const char __user *buf, | ||
591 | size_t len, loff_t *ppos) | ||
592 | { | ||
593 | struct simple_attr *attr; | ||
594 | u64 val; | ||
595 | size_t size; | ||
596 | ssize_t ret; | ||
597 | |||
598 | attr = file->private_data; | ||
599 | |||
600 | if (!attr->set) | ||
601 | return -EACCES; | ||
602 | |||
603 | down(&attr->sem); | ||
604 | ret = -EFAULT; | ||
605 | size = min(sizeof(attr->set_buf) - 1, len); | ||
606 | if (copy_from_user(attr->set_buf, buf, size)) | ||
607 | goto out; | ||
608 | |||
609 | ret = len; /* claim we got the whole input */ | ||
610 | attr->set_buf[size] = '\0'; | ||
611 | val = simple_strtol(attr->set_buf, NULL, 0); | ||
612 | attr->set(attr->data, val); | ||
613 | out: | ||
614 | up(&attr->sem); | ||
615 | return ret; | ||
616 | } | ||
617 | |||
522 | EXPORT_SYMBOL(dcache_dir_close); | 618 | EXPORT_SYMBOL(dcache_dir_close); |
523 | EXPORT_SYMBOL(dcache_dir_lseek); | 619 | EXPORT_SYMBOL(dcache_dir_lseek); |
524 | EXPORT_SYMBOL(dcache_dir_open); | 620 | EXPORT_SYMBOL(dcache_dir_open); |
@@ -547,3 +643,7 @@ EXPORT_SYMBOL(simple_read_from_buffer); | |||
547 | EXPORT_SYMBOL(simple_transaction_get); | 643 | EXPORT_SYMBOL(simple_transaction_get); |
548 | EXPORT_SYMBOL(simple_transaction_read); | 644 | EXPORT_SYMBOL(simple_transaction_read); |
549 | EXPORT_SYMBOL(simple_transaction_release); | 645 | EXPORT_SYMBOL(simple_transaction_release); |
646 | EXPORT_SYMBOL_GPL(simple_attr_open); | ||
647 | EXPORT_SYMBOL_GPL(simple_attr_close); | ||
648 | EXPORT_SYMBOL_GPL(simple_attr_read); | ||
649 | EXPORT_SYMBOL_GPL(simple_attr_write); | ||
diff --git a/include/linux/fs.h b/include/linux/fs.h index 0180102dace1..9b8b696d4f15 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h | |||
@@ -1657,6 +1657,52 @@ static inline void simple_transaction_set(struct file *file, size_t n) | |||
1657 | ar->size = n; | 1657 | ar->size = n; |
1658 | } | 1658 | } |
1659 | 1659 | ||
1660 | /* | ||
1661 | * simple attribute files | ||
1662 | * | ||
1663 | * These attributes behave similar to those in sysfs: | ||
1664 | * | ||
1665 | * Writing to an attribute immediately sets a value, an open file can be | ||
1666 | * written to multiple times. | ||
1667 | * | ||
1668 | * Reading from an attribute creates a buffer from the value that might get | ||
1669 | * read with multiple read calls. When the attribute has been read | ||
1670 | * completely, no further read calls are possible until the file is opened | ||
1671 | * again. | ||
1672 | * | ||
1673 | * All attributes contain a text representation of a numeric value | ||
1674 | * that are accessed with the get() and set() functions. | ||
1675 | */ | ||
1676 | #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \ | ||
1677 | static int __fops ## _open(struct inode *inode, struct file *file) \ | ||
1678 | { \ | ||
1679 | __simple_attr_check_format(__fmt, 0ull); \ | ||
1680 | return simple_attr_open(inode, file, __get, __set, __fmt); \ | ||
1681 | } \ | ||
1682 | static struct file_operations __fops = { \ | ||
1683 | .owner = THIS_MODULE, \ | ||
1684 | .open = __fops ## _open, \ | ||
1685 | .release = simple_attr_close, \ | ||
1686 | .read = simple_attr_read, \ | ||
1687 | .write = simple_attr_write, \ | ||
1688 | }; | ||
1689 | |||
1690 | static inline void __attribute__((format(printf, 1, 2))) | ||
1691 | __simple_attr_check_format(const char *fmt, ...) | ||
1692 | { | ||
1693 | /* don't do anything, just let the compiler check the arguments; */ | ||
1694 | } | ||
1695 | |||
1696 | int simple_attr_open(struct inode *inode, struct file *file, | ||
1697 | u64 (*get)(void *), void (*set)(void *, u64), | ||
1698 | const char *fmt); | ||
1699 | int simple_attr_close(struct inode *inode, struct file *file); | ||
1700 | ssize_t simple_attr_read(struct file *file, char __user *buf, | ||
1701 | size_t len, loff_t *ppos); | ||
1702 | ssize_t simple_attr_write(struct file *file, const char __user *buf, | ||
1703 | size_t len, loff_t *ppos); | ||
1704 | |||
1705 | |||
1660 | #ifdef CONFIG_SECURITY | 1706 | #ifdef CONFIG_SECURITY |
1661 | static inline char *alloc_secdata(void) | 1707 | static inline char *alloc_secdata(void) |
1662 | { | 1708 | { |