UBI: add UBI control device
[safe/jmp/linux-2.6] / drivers / mtd / ubi / cdev.c
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.
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.
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
47 /*
48  * Maximum sequence numbers of UBI and volume character device IOCTLs (direct
49  * logical eraseblock erase is a debug-only feature).
50  */
51 #define UBI_CDEV_IOC_MAX_SEQ 2
52 #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
53 #define VOL_CDEV_IOC_MAX_SEQ 1
54 #else
55 #define VOL_CDEV_IOC_MAX_SEQ 2
56 #endif
57
58 /**
59  * major_to_device - get UBI device object by character device major number.
60  * @major: major number
61  *
62  * This function returns a pointer to the UBI device object.
63  */
64 static struct ubi_device *major_to_device(int major)
65 {
66         int i;
67
68         for (i = 0; i < UBI_MAX_DEVICES; i++)
69                 if (ubi_devices[i] && MAJOR(ubi_devices[i]->cdev.dev) == major)
70                         return ubi_devices[i];
71         BUG();
72         return NULL;
73 }
74
75 /**
76  * get_exclusive - get exclusive access to an UBI volume.
77  * @desc: volume descriptor
78  *
79  * This function changes UBI volume open mode to "exclusive". Returns previous
80  * mode value (positive integer) in case of success and a negative error code
81  * in case of failure.
82  */
83 static int get_exclusive(struct ubi_volume_desc *desc)
84 {
85         int users, err;
86         struct ubi_volume *vol = desc->vol;
87
88         spin_lock(&vol->ubi->volumes_lock);
89         users = vol->readers + vol->writers + vol->exclusive;
90         ubi_assert(users > 0);
91         if (users > 1) {
92                 dbg_err("%d users for volume %d", users, vol->vol_id);
93                 err = -EBUSY;
94         } else {
95                 vol->readers = vol->writers = 0;
96                 vol->exclusive = 1;
97                 err = desc->mode;
98                 desc->mode = UBI_EXCLUSIVE;
99         }
100         spin_unlock(&vol->ubi->volumes_lock);
101
102         return err;
103 }
104
105 /**
106  * revoke_exclusive - revoke exclusive mode.
107  * @desc: volume descriptor
108  * @mode: new mode to switch to
109  */
110 static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
111 {
112         struct ubi_volume *vol = desc->vol;
113
114         spin_lock(&vol->ubi->volumes_lock);
115         ubi_assert(vol->readers == 0 && vol->writers == 0);
116         ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
117         vol->exclusive = 0;
118         if (mode == UBI_READONLY)
119                 vol->readers = 1;
120         else if (mode == UBI_READWRITE)
121                 vol->writers = 1;
122         else
123                 vol->exclusive = 1;
124         spin_unlock(&vol->ubi->volumes_lock);
125
126         desc->mode = mode;
127 }
128
129 static int vol_cdev_open(struct inode *inode, struct file *file)
130 {
131         struct ubi_volume_desc *desc;
132         const struct ubi_device *ubi = major_to_device(imajor(inode));
133         int vol_id = iminor(inode) - 1;
134         int mode;
135
136         if (file->f_mode & FMODE_WRITE)
137                 mode = UBI_READWRITE;
138         else
139                 mode = UBI_READONLY;
140
141         dbg_msg("open volume %d, mode %d", vol_id, mode);
142
143         desc = ubi_open_volume(ubi->ubi_num, vol_id, mode);
144         if (IS_ERR(desc))
145                 return PTR_ERR(desc);
146
147         file->private_data = desc;
148         return 0;
149 }
150
151 static int vol_cdev_release(struct inode *inode, struct file *file)
152 {
153         struct ubi_volume_desc *desc = file->private_data;
154         struct ubi_volume *vol = desc->vol;
155
156         dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
157
158         if (vol->updating) {
159                 ubi_warn("update of volume %d not finished, volume is damaged",
160                          vol->vol_id);
161                 vol->updating = 0;
162                 vfree(vol->upd_buf);
163         }
164
165         ubi_close_volume(desc);
166         return 0;
167 }
168
169 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
170 {
171         struct ubi_volume_desc *desc = file->private_data;
172         struct ubi_volume *vol = desc->vol;
173         loff_t new_offset;
174
175         if (vol->updating) {
176                  /* Update is in progress, seeking is prohibited */
177                 dbg_err("updating");
178                 return -EBUSY;
179         }
180
181         switch (origin) {
182         case 0: /* SEEK_SET */
183                 new_offset = offset;
184                 break;
185         case 1: /* SEEK_CUR */
186                 new_offset = file->f_pos + offset;
187                 break;
188         case 2: /* SEEK_END */
189                 new_offset = vol->used_bytes + offset;
190                 break;
191         default:
192                 return -EINVAL;
193         }
194
195         if (new_offset < 0 || new_offset > vol->used_bytes) {
196                 dbg_err("bad seek %lld", new_offset);
197                 return -EINVAL;
198         }
199
200         dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
201                 vol->vol_id, offset, origin, new_offset);
202
203         file->f_pos = new_offset;
204         return new_offset;
205 }
206
207 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
208                              loff_t *offp)
209 {
210         struct ubi_volume_desc *desc = file->private_data;
211         struct ubi_volume *vol = desc->vol;
212         struct ubi_device *ubi = vol->ubi;
213         int err, lnum, off, len,  vol_id = desc->vol->vol_id, tbuf_size;
214         size_t count_save = count;
215         void *tbuf;
216         uint64_t tmp;
217
218         dbg_msg("read %zd bytes from offset %lld of volume %d",
219                 count, *offp, vol_id);
220
221         if (vol->updating) {
222                 dbg_err("updating");
223                 return -EBUSY;
224         }
225         if (vol->upd_marker) {
226                 dbg_err("damaged volume, update marker is set");
227                 return -EBADF;
228         }
229         if (*offp == vol->used_bytes || count == 0)
230                 return 0;
231
232         if (vol->corrupted)
233                 dbg_msg("read from corrupted volume %d", vol_id);
234
235         if (*offp + count > vol->used_bytes)
236                 count_save = count = vol->used_bytes - *offp;
237
238         tbuf_size = vol->usable_leb_size;
239         if (count < tbuf_size)
240                 tbuf_size = ALIGN(count, ubi->min_io_size);
241         tbuf = vmalloc(tbuf_size);
242         if (!tbuf)
243                 return -ENOMEM;
244
245         len = count > tbuf_size ? tbuf_size : count;
246
247         tmp = *offp;
248         off = do_div(tmp, vol->usable_leb_size);
249         lnum = tmp;
250
251         do {
252                 cond_resched();
253
254                 if (off + len >= vol->usable_leb_size)
255                         len = vol->usable_leb_size - off;
256
257                 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
258                 if (err)
259                         break;
260
261                 off += len;
262                 if (off == vol->usable_leb_size) {
263                         lnum += 1;
264                         off -= vol->usable_leb_size;
265                 }
266
267                 count -= len;
268                 *offp += len;
269
270                 err = copy_to_user(buf, tbuf, len);
271                 if (err) {
272                         err = -EFAULT;
273                         break;
274                 }
275
276                 buf += len;
277                 len = count > tbuf_size ? tbuf_size : count;
278         } while (count);
279
280         vfree(tbuf);
281         return err ? err : count_save - count;
282 }
283
284 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
285
286 /*
287  * This function allows to directly write to dynamic UBI volumes, without
288  * issuing the volume update operation. Available only as a debugging feature.
289  * Very useful for testing UBI.
290  */
291 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
292                                      size_t count, loff_t *offp)
293 {
294         struct ubi_volume_desc *desc = file->private_data;
295         struct ubi_volume *vol = desc->vol;
296         struct ubi_device *ubi = vol->ubi;
297         int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0;
298         size_t count_save = count;
299         char *tbuf;
300         uint64_t tmp;
301
302         dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
303                 count, *offp, desc->vol->vol_id);
304
305         if (vol->vol_type == UBI_STATIC_VOLUME)
306                 return -EROFS;
307
308         tmp = *offp;
309         off = do_div(tmp, vol->usable_leb_size);
310         lnum = tmp;
311
312         if (off % ubi->min_io_size) {
313                 dbg_err("unaligned position");
314                 return -EINVAL;
315         }
316
317         if (*offp + count > vol->used_bytes)
318                 count_save = count = vol->used_bytes - *offp;
319
320         /* We can write only in fractions of the minimum I/O unit */
321         if (count % ubi->min_io_size) {
322                 dbg_err("unaligned write length");
323                 return -EINVAL;
324         }
325
326         tbuf_size = vol->usable_leb_size;
327         if (count < tbuf_size)
328                 tbuf_size = ALIGN(count, ubi->min_io_size);
329         tbuf = vmalloc(tbuf_size);
330         if (!tbuf)
331                 return -ENOMEM;
332
333         len = count > tbuf_size ? tbuf_size : count;
334
335         while (count) {
336                 cond_resched();
337
338                 if (off + len >= vol->usable_leb_size)
339                         len = vol->usable_leb_size - off;
340
341                 err = copy_from_user(tbuf, buf, len);
342                 if (err) {
343                         err = -EFAULT;
344                         break;
345                 }
346
347                 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
348                                         UBI_UNKNOWN);
349                 if (err)
350                         break;
351
352                 off += len;
353                 if (off == vol->usable_leb_size) {
354                         lnum += 1;
355                         off -= vol->usable_leb_size;
356                 }
357
358                 count -= len;
359                 *offp += len;
360                 buf += len;
361                 len = count > tbuf_size ? tbuf_size : count;
362         }
363
364         vfree(tbuf);
365         return err ? err : count_save - count;
366 }
367
368 #else
369 #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
370 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
371
372 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
373                               size_t count, loff_t *offp)
374 {
375         int err = 0;
376         struct ubi_volume_desc *desc = file->private_data;
377         struct ubi_volume *vol = desc->vol;
378         struct ubi_device *ubi = vol->ubi;
379
380         if (!vol->updating)
381                 return vol_cdev_direct_write(file, buf, count, offp);
382
383         err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
384         if (err < 0) {
385                 ubi_err("cannot write %zd bytes of update data, error %d",
386                         count, err);
387                 return err;
388         }
389
390         if (err) {
391                 /*
392                  * Update is finished, @err contains number of actually written
393                  * bytes now.
394                  */
395                 count = err;
396
397                 err = ubi_check_volume(ubi, vol->vol_id);
398                 if (err < 0)
399                         return err;
400
401                 if (err) {
402                         ubi_warn("volume %d on UBI device %d is corrupted",
403                                  vol->vol_id, ubi->ubi_num);
404                         vol->corrupted = 1;
405                 }
406                 vol->checked = 1;
407                 ubi_gluebi_updated(vol);
408                 revoke_exclusive(desc, UBI_READWRITE);
409         }
410
411         *offp += count;
412         return count;
413 }
414
415 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
416                           unsigned int cmd, unsigned long arg)
417 {
418         int err = 0;
419         struct ubi_volume_desc *desc = file->private_data;
420         struct ubi_volume *vol = desc->vol;
421         struct ubi_device *ubi = vol->ubi;
422         void __user *argp = (void __user *)arg;
423
424         switch (cmd) {
425         /* Volume update command */
426         case UBI_IOCVOLUP:
427         {
428                 int64_t bytes, rsvd_bytes;
429
430                 if (!capable(CAP_SYS_RESOURCE)) {
431                         err = -EPERM;
432                         break;
433                 }
434
435                 err = copy_from_user(&bytes, argp, sizeof(int64_t));
436                 if (err) {
437                         err = -EFAULT;
438                         break;
439                 }
440
441                 if (desc->mode == UBI_READONLY) {
442                         err = -EROFS;
443                         break;
444                 }
445
446                 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
447                 if (bytes < 0 || bytes > rsvd_bytes) {
448                         err = -EINVAL;
449                         break;
450                 }
451
452                 err = get_exclusive(desc);
453                 if (err < 0)
454                         break;
455
456                 err = ubi_start_update(ubi, vol->vol_id, bytes);
457                 if (bytes == 0)
458                         revoke_exclusive(desc, UBI_READWRITE);
459
460                 file->f_pos = 0;
461                 break;
462         }
463
464 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
465         /* Logical eraseblock erasure command */
466         case UBI_IOCEBER:
467         {
468                 int32_t lnum;
469
470                 err = get_user(lnum, (__user int32_t *)argp);
471                 if (err) {
472                         err = -EFAULT;
473                         break;
474                 }
475
476                 if (desc->mode == UBI_READONLY) {
477                         err = -EROFS;
478                         break;
479                 }
480
481                 if (lnum < 0 || lnum >= vol->reserved_pebs) {
482                         err = -EINVAL;
483                         break;
484                 }
485
486                 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
487                         err = -EROFS;
488                         break;
489                 }
490
491                 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
492                 err = ubi_eba_unmap_leb(ubi, vol, lnum);
493                 if (err)
494                         break;
495
496                 err = ubi_wl_flush(ubi);
497                 break;
498         }
499 #endif
500
501         default:
502                 err = -ENOTTY;
503                 break;
504         }
505
506         return err;
507 }
508
509 /**
510  * verify_mkvol_req - verify volume creation request.
511  * @ubi: UBI device description object
512  * @req: the request to check
513  *
514  * This function zero if the request is correct, and %-EINVAL if not.
515  */
516 static int verify_mkvol_req(const struct ubi_device *ubi,
517                             const struct ubi_mkvol_req *req)
518 {
519         int n, err = -EINVAL;
520
521         if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
522             req->name_len < 0)
523                 goto bad;
524
525         if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
526             req->vol_id != UBI_VOL_NUM_AUTO)
527                 goto bad;
528
529         if (req->alignment == 0)
530                 goto bad;
531
532         if (req->bytes == 0)
533                 goto bad;
534
535         if (req->vol_type != UBI_DYNAMIC_VOLUME &&
536             req->vol_type != UBI_STATIC_VOLUME)
537                 goto bad;
538
539         if (req->alignment > ubi->leb_size)
540                 goto bad;
541
542         n = req->alignment % ubi->min_io_size;
543         if (req->alignment != 1 && n)
544                 goto bad;
545
546         if (req->name_len > UBI_VOL_NAME_MAX) {
547                 err = -ENAMETOOLONG;
548                 goto bad;
549         }
550
551         return 0;
552
553 bad:
554         dbg_err("bad volume creation request");
555         ubi_dbg_dump_mkvol_req(req);
556         return err;
557 }
558
559 /**
560  * verify_rsvol_req - verify volume re-size request.
561  * @ubi: UBI device description object
562  * @req: the request to check
563  *
564  * This function returns zero if the request is correct, and %-EINVAL if not.
565  */
566 static int verify_rsvol_req(const struct ubi_device *ubi,
567                             const struct ubi_rsvol_req *req)
568 {
569         if (req->bytes <= 0)
570                 return -EINVAL;
571
572         if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
573                 return -EINVAL;
574
575         return 0;
576 }
577
578 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
579                           unsigned int cmd, unsigned long arg)
580 {
581         int err = 0;
582         struct ubi_device *ubi;
583         struct ubi_volume_desc *desc;
584         void __user *argp = (void __user *)arg;
585
586         if (!capable(CAP_SYS_RESOURCE))
587                 return -EPERM;
588
589         ubi = major_to_device(imajor(inode));
590         if (IS_ERR(ubi))
591                 return PTR_ERR(ubi);
592
593         switch (cmd) {
594         /* Create volume command */
595         case UBI_IOCMKVOL:
596         {
597                 struct ubi_mkvol_req req;
598
599                 dbg_msg("create volume");
600                 err = copy_from_user(&req, argp,
601                                        sizeof(struct ubi_mkvol_req));
602                 if (err) {
603                         err = -EFAULT;
604                         break;
605                 }
606
607                 err = verify_mkvol_req(ubi, &req);
608                 if (err)
609                         break;
610
611                 req.name[req.name_len] = '\0';
612
613                 mutex_lock(&ubi->volumes_mutex);
614                 err = ubi_create_volume(ubi, &req);
615                 mutex_unlock(&ubi->volumes_mutex);
616                 if (err)
617                         break;
618
619                 err = put_user(req.vol_id, (__user int32_t *)argp);
620                 if (err)
621                         err = -EFAULT;
622
623                 break;
624         }
625
626         /* Remove volume command */
627         case UBI_IOCRMVOL:
628         {
629                 int vol_id;
630
631                 dbg_msg("remove volume");
632                 err = get_user(vol_id, (__user int32_t *)argp);
633                 if (err) {
634                         err = -EFAULT;
635                         break;
636                 }
637
638                 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
639                 if (IS_ERR(desc)) {
640                         err = PTR_ERR(desc);
641                         break;
642                 }
643
644                 mutex_lock(&ubi->volumes_mutex);
645                 err = ubi_remove_volume(desc);
646                 mutex_unlock(&ubi->volumes_mutex);
647
648                 /*
649                  * The volume is deleted (unless an error occurred), and the
650                  * 'struct ubi_volume' object will be freed when
651                  * 'ubi_close_volume()' will call 'put_device()'.
652                  */
653                 ubi_close_volume(desc);
654                 break;
655         }
656
657         /* Re-size volume command */
658         case UBI_IOCRSVOL:
659         {
660                 int pebs;
661                 uint64_t tmp;
662                 struct ubi_rsvol_req req;
663
664                 dbg_msg("re-size volume");
665                 err = copy_from_user(&req, argp,
666                                        sizeof(struct ubi_rsvol_req));
667                 if (err) {
668                         err = -EFAULT;
669                         break;
670                 }
671
672                 err = verify_rsvol_req(ubi, &req);
673                 if (err)
674                         break;
675
676                 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
677                 if (IS_ERR(desc)) {
678                         err = PTR_ERR(desc);
679                         break;
680                 }
681
682                 tmp = req.bytes;
683                 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
684                 pebs += tmp;
685
686                 mutex_lock(&ubi->volumes_mutex);
687                 err = ubi_resize_volume(desc, pebs);
688                 mutex_unlock(&ubi->volumes_mutex);
689                 ubi_close_volume(desc);
690                 break;
691         }
692
693         default:
694                 err = -ENOTTY;
695                 break;
696         }
697
698         return err;
699 }
700
701 /* UBI control character device operations */
702 struct file_operations ubi_ctrl_cdev_operations = {
703         .owner = THIS_MODULE,
704 };
705
706 /* UBI character device operations */
707 struct file_operations ubi_cdev_operations = {
708         .owner = THIS_MODULE,
709         .ioctl = ubi_cdev_ioctl,
710         .llseek = no_llseek,
711 };
712
713 /* UBI volume character device operations */
714 struct file_operations ubi_vol_cdev_operations = {
715         .owner   = THIS_MODULE,
716         .open    = vol_cdev_open,
717         .release = vol_cdev_release,
718         .llseek  = vol_cdev_llseek,
719         .read    = vol_cdev_read,
720         .write   = vol_cdev_write,
721         .ioctl   = vol_cdev_ioctl,
722 };