-
Notifications
You must be signed in to change notification settings - Fork 0
/
append.c
245 lines (225 loc) · 5.73 KB
/
append.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* SPDX-License-Identifier: GPL-2.0 */
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/sysfs.h>
struct append_device {
struct mutex lock;
size_t size;
size_t alloc;
void *data;
struct cdev cdev;
struct device base;
};
static struct append_driver {
dev_t devt;
struct file_operations fops;
struct device_driver base;
struct append_device devs[4]; /* four devices */
} append_driver = {
.base.name = "append",
.base.owner = THIS_MODULE,
};
static ssize_t read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
{
struct append_device *dev = fp->private_data;
char *data = dev->data;
ssize_t len;
printk(KERN_DEBUG "[%s:%d] read(buf=%p,count=%ld,*pos=%lld)\n",
dev_name(&dev->base), task_pid_nr(current), buf, count, *pos);
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
len = 0;
if (count == 0 || *pos >= dev->size)
goto out;
if (*pos+count > dev->size)
count = dev->size-*pos;
data += *pos;
len = count;
do {
unsigned long rem = copy_to_user(buf, data, len);
if (!rem)
break;
data += len-rem;
buf += len-rem;
len = rem;
} while (len);
*pos += count;
len = count;
out:
mutex_unlock(&dev->lock);
return len;
}
static ssize_t write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
{
struct append_device *dev = fp->private_data;
char *data;
loff_t lpos;
size_t len;
printk(KERN_DEBUG "[%s:%d] write(buf=%p,count=%ld,*pos=%lld)\n",
dev_name(&dev->base), task_pid_nr(current), buf, count, *pos);
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
len = 0;
if (count == 0)
goto out;
lpos = *pos;
if (unlikely(fp->f_flags&O_APPEND))
lpos = dev->size;
if (lpos+count > dev->alloc) {
size_t alloc = ((lpos+count)/PAGE_SIZE+1)*PAGE_SIZE;
data = krealloc(dev->data, alloc, GFP_KERNEL);
if (IS_ERR(data)) {
len = PTR_ERR(data);
goto out;
}
dev->alloc = alloc;
dev->data = data;
}
data = dev->data+lpos;
len = count;
do {
unsigned long rem = copy_from_user(data, buf, len);
if (!rem)
break;
data += len-rem;
buf += len-rem;
len = rem;
} while (len);
if (lpos+count > dev->size)
dev->size = lpos+count;
*pos = lpos+count;
len = count;
out:
mutex_unlock(&dev->lock);
return len;
}
static int open(struct inode *ip, struct file *fp)
{
struct append_device *dev = container_of(ip->i_cdev,
struct append_device, cdev);
printk(KERN_DEBUG "[%s:%d] open\n", dev_name(&dev->base),
task_pid_nr(current));
fp->private_data = dev;
if ((fp->f_flags&O_ACCMODE) == O_RDONLY)
return 0;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
if (fp->f_flags&O_TRUNC) {
if (dev->data)
kfree(dev->data);
dev->data = NULL;
dev->alloc = 0;
dev->size = 0;
}
mutex_unlock(&dev->lock);
return 0;
}
static ssize_t size_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct append_device *dev = container_of(base, struct append_device,
base);
size_t size;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
size = dev->size;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%ld\n", size);
}
static DEVICE_ATTR_RO(size);
static ssize_t alloc_show(struct device *base, struct device_attribute *attr,
char *page)
{
struct append_device *dev = container_of(base, struct append_device,
base);
size_t alloc;
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;
alloc = dev->alloc;
mutex_unlock(&dev->lock);
return snprintf(page, PAGE_SIZE, "%ld\n", alloc);
}
static DEVICE_ATTR_RO(alloc);
struct attribute *append_attrs[] = {
&dev_attr_size.attr,
&dev_attr_alloc.attr,
NULL,
};
ATTRIBUTE_GROUPS(append);
static int __init init_driver(struct append_driver *drv)
{
memset(&drv->fops, 0, sizeof(struct file_operations));
drv->fops.owner = drv->base.owner;
drv->fops.read = read;
drv->fops.write = write;
drv->fops.open = open;
return 0;
}
static int __init init(void)
{
struct append_driver *drv = &append_driver;
struct append_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct append_device *dev;
char name[8]; /* strlen(drv->base.name)+2 */
int i, err;
err = alloc_chrdev_region(&drv->devt, 0, ARRAY_SIZE(drv->devs),
drv->base.name);
if (err)
return err;
err = init_driver(drv);
if (err)
return err;
for (dev = drv->devs, i = 0; dev != end; dev++, i++) {
err = snprintf(name, sizeof(name), "%s%d", drv->base.name, i);
if (err < 0) {
end = dev;
goto err;
}
memset(dev, 0, sizeof(struct append_device));
device_initialize(&dev->base);
cdev_init(&dev->cdev, &drv->fops);
mutex_init(&dev->lock);
dev->data = NULL;
dev->alloc = dev->size = 0;
dev->base.init_name = name;
dev->base.groups = append_groups;
dev->base.devt = MKDEV(MAJOR(drv->devt),
MINOR(drv->devt)+i);
err = cdev_device_add(&dev->cdev, &dev->base);
if (err) {
end = dev;
goto err;
}
}
return 0;
err:
for (dev = drv->devs; dev != end; dev++)
cdev_device_del(&dev->cdev, &dev->base);
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
return err;
}
module_init(init);
static void __exit term(void)
{
struct append_driver *drv = &append_driver;
struct append_device *end = drv->devs+ARRAY_SIZE(drv->devs);
struct append_device *dev;
for (dev = drv->devs; dev != end; dev++)
cdev_device_del(&dev->cdev, &dev->base);
unregister_chrdev_region(drv->devt, ARRAY_SIZE(drv->devs));
}
module_exit(term);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kei Nohguchi <kei@nohguchi.com>");
MODULE_DESCRIPTION("O_APPEND example");