aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/kernfs/file.c49
-rw-r--r--include/linux/kernfs.h8
2 files changed, 37 insertions, 20 deletions
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 10a8c91c49d6..ddcb471b9cc9 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -252,19 +252,9 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
252 size_t count, loff_t *ppos) 252 size_t count, loff_t *ppos)
253{ 253{
254 struct kernfs_open_file *of = kernfs_of(file); 254 struct kernfs_open_file *of = kernfs_of(file);
255 ssize_t len = min_t(size_t, count, PAGE_SIZE);
256 const struct kernfs_ops *ops; 255 const struct kernfs_ops *ops;
257 char *buf; 256 char *buf = NULL;
258 257 ssize_t len;
259 buf = kmalloc(len + 1, GFP_KERNEL);
260 if (!buf)
261 return -ENOMEM;
262
263 if (copy_from_user(buf, user_buf, len)) {
264 len = -EFAULT;
265 goto out_free;
266 }
267 buf[len] = '\0'; /* guarantee string termination */
268 258
269 /* 259 /*
270 * @of->mutex nests outside active ref and is just to ensure that 260 * @of->mutex nests outside active ref and is just to ensure that
@@ -273,22 +263,45 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
273 mutex_lock(&of->mutex); 263 mutex_lock(&of->mutex);
274 if (!kernfs_get_active(of->kn)) { 264 if (!kernfs_get_active(of->kn)) {
275 mutex_unlock(&of->mutex); 265 mutex_unlock(&of->mutex);
276 len = -ENODEV; 266 return -ENODEV;
277 goto out_free;
278 } 267 }
279 268
280 ops = kernfs_ops(of->kn); 269 ops = kernfs_ops(of->kn);
281 if (ops->write) 270 if (!ops->write) {
282 len = ops->write(of, buf, len, *ppos);
283 else
284 len = -EINVAL; 271 len = -EINVAL;
272 goto out_unlock;
273 }
274
275 if (ops->atomic_write_len) {
276 len = count;
277 if (len > ops->atomic_write_len) {
278 len = -E2BIG;
279 goto out_unlock;
280 }
281 } else {
282 len = min_t(size_t, count, PAGE_SIZE);
283 }
284
285 buf = kmalloc(len + 1, GFP_KERNEL);
286 if (!buf) {
287 len = -ENOMEM;
288 goto out_unlock;
289 }
285 290
291 if (copy_from_user(buf, user_buf, len)) {
292 len = -EFAULT;
293 goto out_unlock;
294 }
295 buf[len] = '\0'; /* guarantee string termination */
296
297 len = ops->write(of, buf, len, *ppos);
298out_unlock:
286 kernfs_put_active(of->kn); 299 kernfs_put_active(of->kn);
287 mutex_unlock(&of->mutex); 300 mutex_unlock(&of->mutex);
288 301
289 if (len > 0) 302 if (len > 0)
290 *ppos += len; 303 *ppos += len;
291out_free: 304
292 kfree(buf); 305 kfree(buf);
293 return len; 306 return len;
294} 307}
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 4520c86f5cb4..47f5235a097a 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -178,9 +178,13 @@ struct kernfs_ops {
178 loff_t off); 178 loff_t off);
179 179
180 /* 180 /*
181 * write() is bounced through kernel buffer and a write larger than 181 * write() is bounced through kernel buffer. If atomic_write_len
182 * PAGE_SIZE results in partial operation of PAGE_SIZE. 182 * is not set, a write larger than PAGE_SIZE results in partial
183 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
184 * writes upto the specified size are executed atomically but
185 * larger ones are rejected with -E2BIG.
183 */ 186 */
187 size_t atomic_write_len;
184 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes, 188 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
185 loff_t off); 189 loff_t off);
186 190