UBI: do not change file pointer while updating
[deliverable/linux.git] / drivers / mtd / ubi / cdev.c
CommitLineData
801c135c
AB
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * This file includes implementation of UBI character device operations.
23 *
24 * There are two kinds of character devices in UBI: UBI character devices and
25 * UBI volume character devices. UBI character devices allow users to
26 * manipulate whole volumes: create, remove, and re-size them. Volume character
27 * devices provide volume I/O capabilities.
28 *
29 * Major and minor numbers are assigned dynamically to both UBI and volume
30 * character devices.
9f961b57
AB
31 *
32 * Well, there is the third kind of character devices - the UBI control
33 * character device, which allows to manipulate by UBI devices - create and
34 * delete them. In other words, it is used for attaching and detaching MTD
35 * devices.
801c135c
AB
36 */
37
38#include <linux/module.h>
39#include <linux/stat.h>
40#include <linux/ioctl.h>
41#include <linux/capability.h>
42#include <mtd/ubi-user.h>
43#include <asm/uaccess.h>
44#include <asm/div64.h>
45#include "ubi.h"
46
801c135c
AB
47/**
48 * get_exclusive - get exclusive access to an UBI volume.
49 * @desc: volume descriptor
50 *
51 * This function changes UBI volume open mode to "exclusive". Returns previous
52 * mode value (positive integer) in case of success and a negative error code
53 * in case of failure.
54 */
55static int get_exclusive(struct ubi_volume_desc *desc)
56{
57 int users, err;
58 struct ubi_volume *vol = desc->vol;
59
60 spin_lock(&vol->ubi->volumes_lock);
61 users = vol->readers + vol->writers + vol->exclusive;
62 ubi_assert(users > 0);
63 if (users > 1) {
64 dbg_err("%d users for volume %d", users, vol->vol_id);
65 err = -EBUSY;
66 } else {
67 vol->readers = vol->writers = 0;
68 vol->exclusive = 1;
69 err = desc->mode;
70 desc->mode = UBI_EXCLUSIVE;
71 }
72 spin_unlock(&vol->ubi->volumes_lock);
73
74 return err;
75}
76
77/**
78 * revoke_exclusive - revoke exclusive mode.
79 * @desc: volume descriptor
80 * @mode: new mode to switch to
81 */
82static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
83{
84 struct ubi_volume *vol = desc->vol;
85
86 spin_lock(&vol->ubi->volumes_lock);
87 ubi_assert(vol->readers == 0 && vol->writers == 0);
88 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
89 vol->exclusive = 0;
90 if (mode == UBI_READONLY)
91 vol->readers = 1;
92 else if (mode == UBI_READWRITE)
93 vol->writers = 1;
94 else
95 vol->exclusive = 1;
96 spin_unlock(&vol->ubi->volumes_lock);
97
98 desc->mode = mode;
99}
100
101static int vol_cdev_open(struct inode *inode, struct file *file)
102{
103 struct ubi_volume_desc *desc;
e73f4459
AB
104 int vol_id = iminor(inode) - 1, mode, ubi_num;
105
106 ubi_num = ubi_major2num(imajor(inode));
107 if (ubi_num < 0)
108 return ubi_num;
801c135c
AB
109
110 if (file->f_mode & FMODE_WRITE)
111 mode = UBI_READWRITE;
112 else
113 mode = UBI_READONLY;
114
115 dbg_msg("open volume %d, mode %d", vol_id, mode);
116
e73f4459 117 desc = ubi_open_volume(ubi_num, vol_id, mode);
801c135c
AB
118 if (IS_ERR(desc))
119 return PTR_ERR(desc);
120
121 file->private_data = desc;
122 return 0;
123}
124
125static int vol_cdev_release(struct inode *inode, struct file *file)
126{
127 struct ubi_volume_desc *desc = file->private_data;
128 struct ubi_volume *vol = desc->vol;
129
130 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
131
132 if (vol->updating) {
133 ubi_warn("update of volume %d not finished, volume is damaged",
134 vol->vol_id);
135 vol->updating = 0;
92ad8f37 136 vfree(vol->upd_buf);
801c135c
AB
137 }
138
139 ubi_close_volume(desc);
140 return 0;
141}
142
143static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
144{
145 struct ubi_volume_desc *desc = file->private_data;
146 struct ubi_volume *vol = desc->vol;
147 loff_t new_offset;
148
149 if (vol->updating) {
150 /* Update is in progress, seeking is prohibited */
151 dbg_err("updating");
152 return -EBUSY;
153 }
154
155 switch (origin) {
156 case 0: /* SEEK_SET */
157 new_offset = offset;
158 break;
159 case 1: /* SEEK_CUR */
160 new_offset = file->f_pos + offset;
161 break;
162 case 2: /* SEEK_END */
163 new_offset = vol->used_bytes + offset;
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 if (new_offset < 0 || new_offset > vol->used_bytes) {
170 dbg_err("bad seek %lld", new_offset);
171 return -EINVAL;
172 }
173
174 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
175 vol->vol_id, offset, origin, new_offset);
176
177 file->f_pos = new_offset;
178 return new_offset;
179}
180
181static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
182 loff_t *offp)
183{
184 struct ubi_volume_desc *desc = file->private_data;
185 struct ubi_volume *vol = desc->vol;
186 struct ubi_device *ubi = vol->ubi;
ae616e1b 187 int err, lnum, off, len, tbuf_size;
801c135c
AB
188 size_t count_save = count;
189 void *tbuf;
190 uint64_t tmp;
191
192 dbg_msg("read %zd bytes from offset %lld of volume %d",
ae616e1b 193 count, *offp, vol->vol_id);
801c135c
AB
194
195 if (vol->updating) {
196 dbg_err("updating");
197 return -EBUSY;
198 }
199 if (vol->upd_marker) {
200 dbg_err("damaged volume, update marker is set");
201 return -EBADF;
202 }
203 if (*offp == vol->used_bytes || count == 0)
204 return 0;
205
206 if (vol->corrupted)
ae616e1b 207 dbg_msg("read from corrupted volume %d", vol->vol_id);
801c135c
AB
208
209 if (*offp + count > vol->used_bytes)
210 count_save = count = vol->used_bytes - *offp;
211
212 tbuf_size = vol->usable_leb_size;
213 if (count < tbuf_size)
214 tbuf_size = ALIGN(count, ubi->min_io_size);
92ad8f37 215 tbuf = vmalloc(tbuf_size);
801c135c
AB
216 if (!tbuf)
217 return -ENOMEM;
218
219 len = count > tbuf_size ? tbuf_size : count;
220
221 tmp = *offp;
222 off = do_div(tmp, vol->usable_leb_size);
223 lnum = tmp;
224
225 do {
226 cond_resched();
227
228 if (off + len >= vol->usable_leb_size)
229 len = vol->usable_leb_size - off;
230
89b96b69 231 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
801c135c
AB
232 if (err)
233 break;
234
235 off += len;
236 if (off == vol->usable_leb_size) {
237 lnum += 1;
238 off -= vol->usable_leb_size;
239 }
240
241 count -= len;
242 *offp += len;
243
244 err = copy_to_user(buf, tbuf, len);
245 if (err) {
246 err = -EFAULT;
247 break;
248 }
249
250 buf += len;
251 len = count > tbuf_size ? tbuf_size : count;
252 } while (count);
253
92ad8f37 254 vfree(tbuf);
801c135c
AB
255 return err ? err : count_save - count;
256}
257
258#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
259
260/*
261 * This function allows to directly write to dynamic UBI volumes, without
262 * issuing the volume update operation. Available only as a debugging feature.
263 * Very useful for testing UBI.
264 */
265static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
266 size_t count, loff_t *offp)
267{
268 struct ubi_volume_desc *desc = file->private_data;
269 struct ubi_volume *vol = desc->vol;
270 struct ubi_device *ubi = vol->ubi;
ae616e1b 271 int lnum, off, len, tbuf_size, err = 0;
801c135c
AB
272 size_t count_save = count;
273 char *tbuf;
274 uint64_t tmp;
275
276 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
ae616e1b 277 count, *offp, vol->vol_id);
801c135c
AB
278
279 if (vol->vol_type == UBI_STATIC_VOLUME)
280 return -EROFS;
281
282 tmp = *offp;
283 off = do_div(tmp, vol->usable_leb_size);
284 lnum = tmp;
285
286 if (off % ubi->min_io_size) {
287 dbg_err("unaligned position");
288 return -EINVAL;
289 }
290
291 if (*offp + count > vol->used_bytes)
292 count_save = count = vol->used_bytes - *offp;
293
294 /* We can write only in fractions of the minimum I/O unit */
295 if (count % ubi->min_io_size) {
296 dbg_err("unaligned write length");
297 return -EINVAL;
298 }
299
300 tbuf_size = vol->usable_leb_size;
301 if (count < tbuf_size)
302 tbuf_size = ALIGN(count, ubi->min_io_size);
92ad8f37 303 tbuf = vmalloc(tbuf_size);
801c135c
AB
304 if (!tbuf)
305 return -ENOMEM;
306
307 len = count > tbuf_size ? tbuf_size : count;
308
309 while (count) {
310 cond_resched();
311
312 if (off + len >= vol->usable_leb_size)
313 len = vol->usable_leb_size - off;
314
315 err = copy_from_user(tbuf, buf, len);
316 if (err) {
317 err = -EFAULT;
318 break;
319 }
320
89b96b69 321 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
801c135c
AB
322 UBI_UNKNOWN);
323 if (err)
324 break;
325
326 off += len;
327 if (off == vol->usable_leb_size) {
328 lnum += 1;
329 off -= vol->usable_leb_size;
330 }
331
332 count -= len;
333 *offp += len;
334 buf += len;
335 len = count > tbuf_size ? tbuf_size : count;
336 }
337
92ad8f37 338 vfree(tbuf);
801c135c
AB
339 return err ? err : count_save - count;
340}
341
342#else
343#define vol_cdev_direct_write(file, buf, count, offp) -EPERM
344#endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
345
346static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
347 size_t count, loff_t *offp)
348{
349 int err = 0;
350 struct ubi_volume_desc *desc = file->private_data;
351 struct ubi_volume *vol = desc->vol;
352 struct ubi_device *ubi = vol->ubi;
353
354 if (!vol->updating)
355 return vol_cdev_direct_write(file, buf, count, offp);
356
357 err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
358 if (err < 0) {
01f7b309
AB
359 ubi_err("cannot write %zd bytes of update data, error %d",
360 count, err);
801c135c
AB
361 return err;
362 }
363
364 if (err) {
365 /*
366 * Update is finished, @err contains number of actually written
367 * bytes now.
368 */
369 count = err;
370
0411e735 371 vol->updating = 0;
801c135c
AB
372 err = ubi_check_volume(ubi, vol->vol_id);
373 if (err < 0)
374 return err;
375
376 if (err) {
377 ubi_warn("volume %d on UBI device %d is corrupted",
378 vol->vol_id, ubi->ubi_num);
379 vol->corrupted = 1;
380 }
381 vol->checked = 1;
941dfb07 382 ubi_gluebi_updated(vol);
801c135c
AB
383 revoke_exclusive(desc, UBI_READWRITE);
384 }
385
801c135c
AB
386 return count;
387}
388
389static int vol_cdev_ioctl(struct inode *inode, struct file *file,
390 unsigned int cmd, unsigned long arg)
391{
392 int err = 0;
393 struct ubi_volume_desc *desc = file->private_data;
394 struct ubi_volume *vol = desc->vol;
395 struct ubi_device *ubi = vol->ubi;
396 void __user *argp = (void __user *)arg;
397
801c135c 398 switch (cmd) {
801c135c
AB
399 /* Volume update command */
400 case UBI_IOCVOLUP:
401 {
402 int64_t bytes, rsvd_bytes;
403
404 if (!capable(CAP_SYS_RESOURCE)) {
405 err = -EPERM;
406 break;
407 }
408
409 err = copy_from_user(&bytes, argp, sizeof(int64_t));
410 if (err) {
411 err = -EFAULT;
412 break;
413 }
414
415 if (desc->mode == UBI_READONLY) {
416 err = -EROFS;
417 break;
418 }
419
420 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
421 if (bytes < 0 || bytes > rsvd_bytes) {
422 err = -EINVAL;
423 break;
424 }
425
426 err = get_exclusive(desc);
427 if (err < 0)
428 break;
429
430 err = ubi_start_update(ubi, vol->vol_id, bytes);
431 if (bytes == 0)
432 revoke_exclusive(desc, UBI_READWRITE);
801c135c
AB
433 break;
434 }
435
436#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
437 /* Logical eraseblock erasure command */
438 case UBI_IOCEBER:
439 {
440 int32_t lnum;
441
bf07803a 442 err = get_user(lnum, (__user int32_t *)argp);
801c135c
AB
443 if (err) {
444 err = -EFAULT;
445 break;
446 }
447
448 if (desc->mode == UBI_READONLY) {
449 err = -EROFS;
450 break;
451 }
452
453 if (lnum < 0 || lnum >= vol->reserved_pebs) {
454 err = -EINVAL;
455 break;
456 }
457
458 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
459 err = -EROFS;
460 break;
461 }
462
463 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
89b96b69 464 err = ubi_eba_unmap_leb(ubi, vol, lnum);
801c135c
AB
465 if (err)
466 break;
467
468 err = ubi_wl_flush(ubi);
469 break;
470 }
471#endif
472
473 default:
474 err = -ENOTTY;
475 break;
476 }
477
478 return err;
479}
480
481/**
482 * verify_mkvol_req - verify volume creation request.
483 * @ubi: UBI device description object
484 * @req: the request to check
485 *
486 * This function zero if the request is correct, and %-EINVAL if not.
487 */
488static int verify_mkvol_req(const struct ubi_device *ubi,
489 const struct ubi_mkvol_req *req)
490{
491 int n, err = -EINVAL;
492
493 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
494 req->name_len < 0)
495 goto bad;
496
497 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
498 req->vol_id != UBI_VOL_NUM_AUTO)
499 goto bad;
500
501 if (req->alignment == 0)
502 goto bad;
503
504 if (req->bytes == 0)
505 goto bad;
506
507 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
508 req->vol_type != UBI_STATIC_VOLUME)
509 goto bad;
510
511 if (req->alignment > ubi->leb_size)
512 goto bad;
513
514 n = req->alignment % ubi->min_io_size;
515 if (req->alignment != 1 && n)
516 goto bad;
517
518 if (req->name_len > UBI_VOL_NAME_MAX) {
519 err = -ENAMETOOLONG;
520 goto bad;
521 }
522
523 return 0;
524
525bad:
526 dbg_err("bad volume creation request");
527 ubi_dbg_dump_mkvol_req(req);
528 return err;
529}
530
531/**
532 * verify_rsvol_req - verify volume re-size request.
533 * @ubi: UBI device description object
534 * @req: the request to check
535 *
536 * This function returns zero if the request is correct, and %-EINVAL if not.
537 */
538static int verify_rsvol_req(const struct ubi_device *ubi,
539 const struct ubi_rsvol_req *req)
540{
541 if (req->bytes <= 0)
542 return -EINVAL;
543
544 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
545 return -EINVAL;
546
547 return 0;
548}
549
550static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
551 unsigned int cmd, unsigned long arg)
552{
553 int err = 0;
554 struct ubi_device *ubi;
555 struct ubi_volume_desc *desc;
556 void __user *argp = (void __user *)arg;
557
801c135c
AB
558 if (!capable(CAP_SYS_RESOURCE))
559 return -EPERM;
560
e73f4459
AB
561 ubi = ubi_get_by_major(imajor(inode));
562 if (!ubi)
563 return -ENODEV;
801c135c
AB
564
565 switch (cmd) {
566 /* Create volume command */
567 case UBI_IOCMKVOL:
568 {
569 struct ubi_mkvol_req req;
570
571 dbg_msg("create volume");
897a316c 572 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
801c135c
AB
573 if (err) {
574 err = -EFAULT;
575 break;
576 }
577
578 err = verify_mkvol_req(ubi, &req);
579 if (err)
580 break;
581
582 req.name[req.name_len] = '\0';
583
40e4d0c1 584 mutex_lock(&ubi->volumes_mutex);
801c135c 585 err = ubi_create_volume(ubi, &req);
40e4d0c1 586 mutex_unlock(&ubi->volumes_mutex);
801c135c
AB
587 if (err)
588 break;
589
bf07803a 590 err = put_user(req.vol_id, (__user int32_t *)argp);
801c135c
AB
591 if (err)
592 err = -EFAULT;
593
594 break;
595 }
596
597 /* Remove volume command */
598 case UBI_IOCRMVOL:
599 {
600 int vol_id;
601
602 dbg_msg("remove volume");
bf07803a 603 err = get_user(vol_id, (__user int32_t *)argp);
801c135c
AB
604 if (err) {
605 err = -EFAULT;
606 break;
607 }
608
609 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
610 if (IS_ERR(desc)) {
611 err = PTR_ERR(desc);
612 break;
613 }
614
40e4d0c1 615 mutex_lock(&ubi->volumes_mutex);
801c135c 616 err = ubi_remove_volume(desc);
40e4d0c1
AB
617 mutex_unlock(&ubi->volumes_mutex);
618
450f872a 619 /*
40e4d0c1
AB
620 * The volume is deleted (unless an error occurred), and the
621 * 'struct ubi_volume' object will be freed when
622 * 'ubi_close_volume()' will call 'put_device()'.
450f872a
AB
623 */
624 ubi_close_volume(desc);
801c135c
AB
625 break;
626 }
627
628 /* Re-size volume command */
629 case UBI_IOCRSVOL:
630 {
631 int pebs;
632 uint64_t tmp;
633 struct ubi_rsvol_req req;
634
635 dbg_msg("re-size volume");
897a316c 636 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
801c135c
AB
637 if (err) {
638 err = -EFAULT;
639 break;
640 }
641
642 err = verify_rsvol_req(ubi, &req);
643 if (err)
644 break;
645
646 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
647 if (IS_ERR(desc)) {
648 err = PTR_ERR(desc);
649 break;
650 }
651
652 tmp = req.bytes;
653 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
654 pebs += tmp;
655
40e4d0c1 656 mutex_lock(&ubi->volumes_mutex);
801c135c 657 err = ubi_resize_volume(desc, pebs);
40e4d0c1 658 mutex_unlock(&ubi->volumes_mutex);
801c135c
AB
659 ubi_close_volume(desc);
660 break;
661 }
662
663 default:
664 err = -ENOTTY;
665 break;
666 }
667
e73f4459 668 ubi_put_device(ubi);
801c135c
AB
669 return err;
670}
671
897a316c
AB
672static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
673 unsigned int cmd, unsigned long arg)
674{
675 int err = 0;
676 void __user *argp = (void __user *)arg;
677
678 if (!capable(CAP_SYS_RESOURCE))
679 return -EPERM;
680
681 switch (cmd) {
682 /* Attach an MTD device command */
683 case UBI_IOCATT:
684 {
685 struct ubi_attach_req req;
686 struct mtd_info *mtd;
687
688 dbg_msg("attach MTD device");
689 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
690 if (err) {
691 err = -EFAULT;
692 break;
693 }
694
695 if (req.mtd_num < 0 ||
696 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
697 err = -EINVAL;
698 break;
699 }
700
701 mtd = get_mtd_device(NULL, req.mtd_num);
702 if (IS_ERR(mtd)) {
703 err = PTR_ERR(mtd);
704 break;
705 }
706
707 /*
708 * Note, further request verification is done by
709 * 'ubi_attach_mtd_dev()'.
710 */
711 mutex_lock(&ubi_devices_mutex);
712 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
713 mutex_unlock(&ubi_devices_mutex);
714 if (err < 0)
715 put_mtd_device(mtd);
716 else
717 /* @err contains UBI device number */
718 err = put_user(err, (__user int32_t *)argp);
719
720 break;
721 }
722
723 /* Detach an MTD device command */
724 case UBI_IOCDET:
725 {
726 int ubi_num;
727
728 dbg_msg("dettach MTD device");
729 err = get_user(ubi_num, (__user int32_t *)argp);
730 if (err) {
731 err = -EFAULT;
732 break;
733 }
734
735 mutex_lock(&ubi_devices_mutex);
736 err = ubi_detach_mtd_dev(ubi_num, 0);
737 mutex_unlock(&ubi_devices_mutex);
738 break;
739 }
740
741 default:
742 err = -ENOTTY;
743 break;
744 }
745
746 return err;
747}
748
9f961b57
AB
749/* UBI control character device operations */
750struct file_operations ubi_ctrl_cdev_operations = {
897a316c 751 .ioctl = ctrl_cdev_ioctl,
9f961b57
AB
752 .owner = THIS_MODULE,
753};
754
801c135c
AB
755/* UBI character device operations */
756struct file_operations ubi_cdev_operations = {
757 .owner = THIS_MODULE,
758 .ioctl = ubi_cdev_ioctl,
f800f09b 759 .llseek = no_llseek,
801c135c
AB
760};
761
762/* UBI volume character device operations */
763struct file_operations ubi_vol_cdev_operations = {
764 .owner = THIS_MODULE,
765 .open = vol_cdev_open,
766 .release = vol_cdev_release,
767 .llseek = vol_cdev_llseek,
768 .read = vol_cdev_read,
769 .write = vol_cdev_write,
f800f09b 770 .ioctl = vol_cdev_ioctl,
801c135c 771};
This page took 0.147317 seconds and 5 git commands to generate.