UBI: add ioctl for unmap operation
[pandora-kernel.git] / 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 <linux/uaccess.h>
43 #include <mtd/ubi-user.h>
44 #include <asm/div64.h>
45 #include "ubi.h"
46
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  */
55 static 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  */
82 static 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
101 static int vol_cdev_open(struct inode *inode, struct file *file)
102 {
103         struct ubi_volume_desc *desc;
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;
109
110         if (file->f_mode & FMODE_WRITE)
111                 mode = UBI_READWRITE;
112         else
113                 mode = UBI_READONLY;
114
115         dbg_gen("open volume %d, mode %d", vol_id, mode);
116         
117         const struct ubi_device *ubi = ubi_get_by_major(imajor(inode));
118         if(ubi->ubi_num != ubi_num)
119                 printk("ubiblk: ubi_num not equal!\n");
120         
121
122         desc = ubi_open_volume(ubi_num, vol_id, mode);
123         if (IS_ERR(desc))
124                 return PTR_ERR(desc);
125
126         file->private_data = desc;
127         return 0;
128 }
129
130 static int vol_cdev_release(struct inode *inode, struct file *file)
131 {
132         struct ubi_volume_desc *desc = file->private_data;
133         struct ubi_volume *vol = desc->vol;
134
135         dbg_gen("release volume %d, mode %d", vol->vol_id, desc->mode);
136
137         if (vol->updating) {
138                 ubi_warn("update of volume %d not finished, volume is damaged",
139                          vol->vol_id);
140                 ubi_assert(!vol->changing_leb);
141                 vol->updating = 0;
142                 vfree(vol->upd_buf);
143         } else if (vol->changing_leb) {
144                 dbg_gen("only %lld of %lld bytes received for atomic LEB change"
145                         " for volume %d:%d, cancel", vol->upd_received,
146                         vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
147                 vol->changing_leb = 0;
148                 vfree(vol->upd_buf);
149         }
150
151         ubi_close_volume(desc);
152         return 0;
153 }
154
155 static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
156 {
157         struct ubi_volume_desc *desc = file->private_data;
158         struct ubi_volume *vol = desc->vol;
159         loff_t new_offset;
160
161         if (vol->updating) {
162                  /* Update is in progress, seeking is prohibited */
163                 dbg_err("updating");
164                 return -EBUSY;
165         }
166
167         switch (origin) {
168         case 0: /* SEEK_SET */
169                 new_offset = offset;
170                 break;
171         case 1: /* SEEK_CUR */
172                 new_offset = file->f_pos + offset;
173                 break;
174         case 2: /* SEEK_END */
175                 new_offset = vol->used_bytes + offset;
176                 break;
177         default:
178                 return -EINVAL;
179         }
180
181         if (new_offset < 0 || new_offset > vol->used_bytes) {
182                 dbg_err("bad seek %lld", new_offset);
183                 return -EINVAL;
184         }
185
186         dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
187                 vol->vol_id, offset, origin, new_offset);
188
189         file->f_pos = new_offset;
190         return new_offset;
191 }
192
193 static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
194                              loff_t *offp)
195 {
196         struct ubi_volume_desc *desc = file->private_data;
197         struct ubi_volume *vol = desc->vol;
198         struct ubi_device *ubi = vol->ubi;
199         int err, lnum, off, len,  tbuf_size;
200         size_t count_save = count;
201         void *tbuf;
202         uint64_t tmp;
203
204         dbg_gen("read %zd bytes from offset %lld of volume %d",
205                 count, *offp, vol->vol_id);
206
207         if (vol->updating) {
208                 dbg_err("updating");
209                 return -EBUSY;
210         }
211         if (vol->upd_marker) {
212                 dbg_err("damaged volume, update marker is set");
213                 return -EBADF;
214         }
215         if (*offp == vol->used_bytes || count == 0)
216                 return 0;
217
218         if (vol->corrupted)
219                 dbg_gen("read from corrupted volume %d", vol->vol_id);
220
221         if (*offp + count > vol->used_bytes)
222                 count_save = count = vol->used_bytes - *offp;
223
224         tbuf_size = vol->usable_leb_size;
225         if (count < tbuf_size)
226                 tbuf_size = ALIGN(count, ubi->min_io_size);
227         tbuf = vmalloc(tbuf_size);
228         if (!tbuf)
229                 return -ENOMEM;
230
231         len = count > tbuf_size ? tbuf_size : count;
232
233         tmp = *offp;
234         off = do_div(tmp, vol->usable_leb_size);
235         lnum = tmp;
236
237         do {
238                 cond_resched();
239
240                 if (off + len >= vol->usable_leb_size)
241                         len = vol->usable_leb_size - off;
242
243                 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
244                 if (err)
245                         break;
246
247                 off += len;
248                 if (off == vol->usable_leb_size) {
249                         lnum += 1;
250                         off -= vol->usable_leb_size;
251                 }
252
253                 count -= len;
254                 *offp += len;
255
256                 err = copy_to_user(buf, tbuf, len);
257                 if (err) {
258                         err = -EFAULT;
259                         break;
260                 }
261
262                 buf += len;
263                 len = count > tbuf_size ? tbuf_size : count;
264         } while (count);
265
266         vfree(tbuf);
267         return err ? err : count_save - count;
268 }
269
270 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
271
272 /*
273  * This function allows to directly write to dynamic UBI volumes, without
274  * issuing the volume update operation. Available only as a debugging feature.
275  * Very useful for testing UBI.
276  */
277 static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
278                                      size_t count, loff_t *offp)
279 {
280         struct ubi_volume_desc *desc = file->private_data;
281         struct ubi_volume *vol = desc->vol;
282         struct ubi_device *ubi = vol->ubi;
283         int lnum, off, len, tbuf_size, err = 0;
284         size_t count_save = count;
285         char *tbuf;
286         uint64_t tmp;
287
288         dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
289                 count, *offp, vol->vol_id);
290
291         if (vol->vol_type == UBI_STATIC_VOLUME)
292                 return -EROFS;
293
294         tmp = *offp;
295         off = do_div(tmp, vol->usable_leb_size);
296         lnum = tmp;
297
298         if (off & (ubi->min_io_size - 1)) {
299                 dbg_err("unaligned position");
300                 return -EINVAL;
301         }
302
303         if (*offp + count > vol->used_bytes)
304                 count_save = count = vol->used_bytes - *offp;
305
306         /* We can write only in fractions of the minimum I/O unit */
307         if (count & (ubi->min_io_size - 1)) {
308                 dbg_err("unaligned write length");
309                 return -EINVAL;
310         }
311
312         tbuf_size = vol->usable_leb_size;
313         if (count < tbuf_size)
314                 tbuf_size = ALIGN(count, ubi->min_io_size);
315         tbuf = vmalloc(tbuf_size);
316         if (!tbuf)
317                 return -ENOMEM;
318
319         len = count > tbuf_size ? tbuf_size : count;
320
321         while (count) {
322                 cond_resched();
323
324                 if (off + len >= vol->usable_leb_size)
325                         len = vol->usable_leb_size - off;
326
327                 err = copy_from_user(tbuf, buf, len);
328                 if (err) {
329                         err = -EFAULT;
330                         break;
331                 }
332
333                 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
334                                         UBI_UNKNOWN);
335                 if (err)
336                         break;
337
338                 off += len;
339                 if (off == vol->usable_leb_size) {
340                         lnum += 1;
341                         off -= vol->usable_leb_size;
342                 }
343
344                 count -= len;
345                 *offp += len;
346                 buf += len;
347                 len = count > tbuf_size ? tbuf_size : count;
348         }
349
350         vfree(tbuf);
351         return err ? err : count_save - count;
352 }
353
354 #else
355 #define vol_cdev_direct_write(file, buf, count, offp) (-EPERM)
356 #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
357
358 static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
359                               size_t count, loff_t *offp)
360 {
361         int err = 0;
362         struct ubi_volume_desc *desc = file->private_data;
363         struct ubi_volume *vol = desc->vol;
364         struct ubi_device *ubi = vol->ubi;
365
366         if (!vol->updating && !vol->changing_leb)
367                 return vol_cdev_direct_write(file, buf, count, offp);
368
369         if (vol->updating)
370                 err = ubi_more_update_data(ubi, vol, buf, count);
371         else
372                 err = ubi_more_leb_change_data(ubi, vol, buf, count);
373
374         if (err < 0) {
375                 ubi_err("cannot accept more %zd bytes of data, error %d",
376                         count, err);
377                 return err;
378         }
379
380         if (err) {
381                 /*
382                  * The operation is finished, @err contains number of actually
383                  * written bytes.
384                  */
385                 count = err;
386
387                 if (vol->changing_leb) {
388                         revoke_exclusive(desc, UBI_READWRITE);
389                         return count;
390                 }
391
392                 err = ubi_check_volume(ubi, vol->vol_id);
393                 if (err < 0)
394                         return err;
395
396                 if (err) {
397                         ubi_warn("volume %d on UBI device %d is corrupted",
398                                  vol->vol_id, ubi->ubi_num);
399                         vol->corrupted = 1;
400                 }
401                 vol->checked = 1;
402                 ubi_gluebi_updated(vol);
403                 revoke_exclusive(desc, UBI_READWRITE);
404         }
405
406         return count;
407 }
408
409 static int vol_cdev_ioctl(struct inode *inode, struct file *file,
410                           unsigned int cmd, unsigned long arg)
411 {
412         int err = 0;
413         struct ubi_volume_desc *desc = file->private_data;
414         struct ubi_volume *vol = desc->vol;
415         struct ubi_device *ubi = vol->ubi;
416         void __user *argp = (void __user *)arg;
417
418         switch (cmd) {
419         /* Volume update command */
420         case UBI_IOCVOLUP:
421         {
422                 int64_t bytes, rsvd_bytes;
423
424                 if (!capable(CAP_SYS_RESOURCE)) {
425                         err = -EPERM;
426                         break;
427                 }
428
429                 err = copy_from_user(&bytes, argp, sizeof(int64_t));
430                 if (err) {
431                         err = -EFAULT;
432                         break;
433                 }
434
435                 if (desc->mode == UBI_READONLY) {
436                         err = -EROFS;
437                         break;
438                 }
439
440                 rsvd_bytes = (long long)vol->reserved_pebs *
441                                         ubi->leb_size-vol->data_pad;
442                 if (bytes < 0 || bytes > rsvd_bytes) {
443                         err = -EINVAL;
444                         break;
445                 }
446
447                 err = get_exclusive(desc);
448                 if (err < 0)
449                         break;
450
451                 err = ubi_start_update(ubi, vol, bytes);
452                 if (bytes == 0)
453                         revoke_exclusive(desc, UBI_READWRITE);
454                 break;
455         }
456
457         /* Atomic logical eraseblock change command */
458         case UBI_IOCEBCH:
459         {
460                 struct ubi_leb_change_req req;
461
462                 err = copy_from_user(&req, argp,
463                                      sizeof(struct ubi_leb_change_req));
464                 if (err) {
465                         err = -EFAULT;
466                         break;
467                 }
468
469                 if (desc->mode == UBI_READONLY ||
470                     vol->vol_type == UBI_STATIC_VOLUME) {
471                         err = -EROFS;
472                         break;
473                 }
474
475                 /* Validate the request */
476                 err = -EINVAL;
477                 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
478                     req.bytes < 0 || req.lnum >= vol->usable_leb_size)
479                         break;
480                 if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
481                     req.dtype != UBI_UNKNOWN)
482                         break;
483
484                 err = get_exclusive(desc);
485                 if (err < 0)
486                         break;
487
488                 err = ubi_start_leb_change(ubi, vol, &req);
489                 if (req.bytes == 0)
490                         revoke_exclusive(desc, UBI_READWRITE);
491                 break;
492         }
493
494 #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
495         /* Logical eraseblock erasure command */
496         case UBI_IOCEBER:
497         {
498                 int32_t lnum;
499
500                 err = get_user(lnum, (__user int32_t *)argp);
501                 if (err) {
502                         err = -EFAULT;
503                         break;
504                 }
505
506                 if (desc->mode == UBI_READONLY ||
507                     vol->vol_type == UBI_STATIC_VOLUME) {
508                         err = -EROFS;
509                         break;
510                 }
511
512                 if (lnum < 0 || lnum >= vol->reserved_pebs) {
513                         err = -EINVAL;
514                         break;
515                 }
516
517                 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
518                 err = ubi_eba_unmap_leb(ubi, vol, lnum);
519                 if (err)
520                         break;
521
522                 err = ubi_wl_flush(ubi);
523                 break;
524         }
525
526         /* Logical eraseblock map command */
527         case UBI_IOCEBMAP:
528         {
529                 struct ubi_map_req req;
530
531                 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
532                 if (err) {
533                         err = -EFAULT;
534                         break;
535                 }
536                 err = ubi_leb_map(desc, req.lnum, req.dtype);
537                 break;
538         }
539
540         /* Logical eraseblock un-map command */
541         case UBI_IOCEBUNMAP:
542         {
543                 int32_t lnum;
544
545                 err = get_user(lnum, (__user int32_t *)argp);
546                 if (err) {
547                         err = -EFAULT;
548                         break;
549                 }
550                 err = ubi_leb_unmap(desc, lnum);
551                 break;
552         }
553 #endif
554
555         default:
556                 err = -ENOTTY;
557                 break;
558         }
559         return err;
560 }
561
562 /**
563  * verify_mkvol_req - verify volume creation request.
564  * @ubi: UBI device description object
565  * @req: the request to check
566  *
567  * This function zero if the request is correct, and %-EINVAL if not.
568  */
569 static int verify_mkvol_req(const struct ubi_device *ubi,
570                             const struct ubi_mkvol_req *req)
571 {
572         int n, err = -EINVAL;
573
574         if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
575             req->name_len < 0)
576                 goto bad;
577
578         if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
579             req->vol_id != UBI_VOL_NUM_AUTO)
580                 goto bad;
581
582         if (req->alignment == 0)
583                 goto bad;
584
585         if (req->bytes == 0)
586                 goto bad;
587
588         if (req->vol_type != UBI_DYNAMIC_VOLUME &&
589             req->vol_type != UBI_STATIC_VOLUME)
590                 goto bad;
591
592         if (req->alignment > ubi->leb_size)
593                 goto bad;
594
595         n = req->alignment & (ubi->min_io_size - 1);
596         if (req->alignment != 1 && n)
597                 goto bad;
598
599         if (req->name_len > UBI_VOL_NAME_MAX) {
600                 err = -ENAMETOOLONG;
601                 goto bad;
602         }
603
604         n = strnlen(req->name, req->name_len + 1);
605         if (n != req->name_len)
606                 goto bad;
607
608         return 0;
609
610 bad:
611         dbg_err("bad volume creation request");
612         ubi_dbg_dump_mkvol_req(req);
613         return err;
614 }
615
616 /**
617  * verify_rsvol_req - verify volume re-size request.
618  * @ubi: UBI device description object
619  * @req: the request to check
620  *
621  * This function returns zero if the request is correct, and %-EINVAL if not.
622  */
623 static int verify_rsvol_req(const struct ubi_device *ubi,
624                             const struct ubi_rsvol_req *req)
625 {
626         if (req->bytes <= 0)
627                 return -EINVAL;
628
629         if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
630                 return -EINVAL;
631
632         return 0;
633 }
634
635 /**
636  * rename_volumes - rename UBI volumes.
637  * @ubi: UBI device description object
638  * @req: volumes re-name request
639  *
640  * This is a helper function for the volume re-name IOCTL which validates the
641  * the request, opens the volume and calls corresponding volumes management
642  * function. Returns zero in case of success and a negative error code in case
643  * of failure.
644  */
645 static int rename_volumes(struct ubi_device *ubi,
646                           struct ubi_rnvol_req *req)
647 {
648         int i, n, err;
649         struct list_head rename_list;
650         struct ubi_rename_entry *re, *re1;
651
652         if (req->count < 0 || req->count > UBI_MAX_RNVOL)
653                 return -EINVAL;
654
655         if (req->count == 0)
656                 return 0;
657
658         /* Validate volume IDs and names in the request */
659         for (i = 0; i < req->count; i++) {
660                 if (req->ents[i].vol_id < 0 ||
661                     req->ents[i].vol_id >= ubi->vtbl_slots)
662                         return -EINVAL;
663                 if (req->ents[i].name_len < 0)
664                         return -EINVAL;
665                 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
666                         return -ENAMETOOLONG;
667                 req->ents[i].name[req->ents[i].name_len] = '\0';
668                 n = strlen(req->ents[i].name);
669                 if (n != req->ents[i].name_len)
670                         err = -EINVAL;
671         }
672
673         /* Make sure volume IDs and names are unique */
674         for (i = 0; i < req->count - 1; i++) {
675                 for (n = i + 1; n < req->count; n++) {
676                         if (req->ents[i].vol_id == req->ents[n].vol_id) {
677                                 dbg_err("duplicated volume id %d",
678                                         req->ents[i].vol_id);
679                                 return -EINVAL;
680                         }
681                         if (!strcmp(req->ents[i].name, req->ents[n].name)) {
682                                 dbg_err("duplicated volume name \"%s\"",
683                                         req->ents[i].name);
684                                 return -EINVAL;
685                         }
686                 }
687         }
688
689         /* Create the re-name list */
690         INIT_LIST_HEAD(&rename_list);
691         for (i = 0; i < req->count; i++) {
692                 int vol_id = req->ents[i].vol_id;
693                 int name_len = req->ents[i].name_len;
694                 const char *name = req->ents[i].name;
695
696                 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
697                 if (!re) {
698                         err = -ENOMEM;
699                         goto out_free;
700                 }
701
702                 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
703                 if (IS_ERR(re->desc)) {
704                         err = PTR_ERR(re->desc);
705                         dbg_err("cannot open volume %d, error %d", vol_id, err);
706                         kfree(re);
707                         goto out_free;
708                 }
709
710                 /* Skip this re-naming if the name does not really change */
711                 if (re->desc->vol->name_len == name_len &&
712                     !memcmp(re->desc->vol->name, name, name_len)) {
713                         ubi_close_volume(re->desc);
714                         kfree(re);
715                         continue;
716                 }
717
718                 re->new_name_len = name_len;
719                 memcpy(re->new_name, name, name_len);
720                 list_add_tail(&re->list, &rename_list);
721                 dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
722                         vol_id, re->desc->vol->name, name);
723         }
724
725         if (list_empty(&rename_list))
726                 return 0;
727
728         /* Find out the volumes which have to be removed */
729         list_for_each_entry(re, &rename_list, list) {
730                 struct ubi_volume_desc *desc;
731                 int no_remove_needed = 0;
732
733                 /*
734                  * Volume @re->vol_id is going to be re-named to
735                  * @re->new_name, while its current name is @name. If a volume
736                  * with name @re->new_name currently exists, it has to be
737                  * removed, unless it is also re-named in the request (@req).
738                  */
739                 list_for_each_entry(re1, &rename_list, list) {
740                         if (re->new_name_len == re1->desc->vol->name_len &&
741                             !memcmp(re->new_name, re1->desc->vol->name,
742                                     re1->desc->vol->name_len)) {
743                                 no_remove_needed = 1;
744                                 break;
745                         }
746                 }
747
748                 if (no_remove_needed)
749                         continue;
750
751                 /*
752                  * It seems we need to remove volume with name @re->new_name,
753                  * if it exists.
754                  */
755                 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
756                                           UBI_EXCLUSIVE);
757                 if (IS_ERR(desc)) {
758                         err = PTR_ERR(desc);
759                         if (err == -ENODEV)
760                                 /* Re-naming into a non-existing volume name */
761                                 continue;
762
763                         /* The volume exists but busy, or an error occurred */
764                         dbg_err("cannot open volume \"%s\", error %d",
765                                 re->new_name, err);
766                         goto out_free;
767                 }
768
769                 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
770                 if (!re) {
771                         err = -ENOMEM;
772                         ubi_close_volume(desc);
773                         goto out_free;
774                 }
775
776                 re->remove = 1;
777                 re->desc = desc;
778                 list_add(&re->list, &rename_list);
779                 dbg_msg("will remove volume %d, name \"%s\"",
780                         re->desc->vol->vol_id, re->desc->vol->name);
781         }
782
783         mutex_lock(&ubi->volumes_mutex);
784         err = ubi_rename_volumes(ubi, &rename_list);
785         mutex_unlock(&ubi->volumes_mutex);
786
787 out_free:
788         list_for_each_entry_safe(re, re1, &rename_list, list) {
789                 ubi_close_volume(re->desc);
790                 list_del(&re->list);
791                 kfree(re);
792         }
793         return err;
794 }
795
796 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
797                           unsigned int cmd, unsigned long arg)
798 {
799         int err = 0;
800         struct ubi_device *ubi;
801         struct ubi_volume_desc *desc;
802         void __user *argp = (void __user *)arg;
803
804         if (!capable(CAP_SYS_RESOURCE))
805                 return -EPERM;
806
807         ubi = ubi_get_by_major(imajor(inode));
808         if (!ubi)
809                 return -ENODEV;
810
811         switch (cmd) {
812         /* Create volume command */
813         case UBI_IOCMKVOL:
814         {
815                 struct ubi_mkvol_req req;
816
817                 dbg_gen("create volume");
818                 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
819                 if (err) {
820                         err = -EFAULT;
821                         break;
822                 }
823
824                 req.name[req.name_len] = '\0';
825                 err = verify_mkvol_req(ubi, &req);
826                 if (err)
827                         break;
828
829                 mutex_lock(&ubi->volumes_mutex);
830                 err = ubi_create_volume(ubi, &req);
831                 mutex_unlock(&ubi->volumes_mutex);
832                 if (err)
833                         break;
834
835                 err = put_user(req.vol_id, (__user int32_t *)argp);
836                 if (err)
837                         err = -EFAULT;
838
839                 break;
840         }
841
842         /* Remove volume command */
843         case UBI_IOCRMVOL:
844         {
845                 int vol_id;
846
847                 dbg_gen("remove volume");
848                 err = get_user(vol_id, (__user int32_t *)argp);
849                 if (err) {
850                         err = -EFAULT;
851                         break;
852                 }
853
854                 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
855                 if (IS_ERR(desc)) {
856                         err = PTR_ERR(desc);
857                         break;
858                 }
859
860                 mutex_lock(&ubi->volumes_mutex);
861                 err = ubi_remove_volume(desc, 0);
862                 mutex_unlock(&ubi->volumes_mutex);
863
864                 /*
865                  * The volume is deleted (unless an error occurred), and the
866                  * 'struct ubi_volume' object will be freed when
867                  * 'ubi_close_volume()' will call 'put_device()'.
868                  */
869                 ubi_close_volume(desc);
870                 break;
871         }
872
873         /* Re-size volume command */
874         case UBI_IOCRSVOL:
875         {
876                 int pebs;
877                 uint64_t tmp;
878                 struct ubi_rsvol_req req;
879
880                 dbg_gen("re-size volume");
881                 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
882                 if (err) {
883                         err = -EFAULT;
884                         break;
885                 }
886
887                 err = verify_rsvol_req(ubi, &req);
888                 if (err)
889                         break;
890
891                 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
892                 if (IS_ERR(desc)) {
893                         err = PTR_ERR(desc);
894                         break;
895                 }
896
897                 tmp = req.bytes;
898                 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
899                 pebs += tmp;
900
901                 mutex_lock(&ubi->volumes_mutex);
902                 err = ubi_resize_volume(desc, pebs);
903                 mutex_unlock(&ubi->volumes_mutex);
904                 ubi_close_volume(desc);
905                 break;
906         }
907
908         /* Re-name volumes command */
909         case UBI_IOCRNVOL:
910         {
911                 struct ubi_rnvol_req *req;
912
913                 dbg_msg("re-name volumes");
914                 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
915                 if (!req) {
916                         err = -ENOMEM;
917                         break;
918                 };
919
920                 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
921                 if (err) {
922                         err = -EFAULT;
923                         kfree(req);
924                         break;
925                 }
926
927                 mutex_lock(&ubi->mult_mutex);
928                 err = rename_volumes(ubi, req);
929                 mutex_unlock(&ubi->mult_mutex);
930                 kfree(req);
931                 break;
932         }
933
934         default:
935                 err = -ENOTTY;
936                 break;
937         }
938
939         ubi_put_device(ubi);
940         return err;
941 }
942
943 static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
944                            unsigned int cmd, unsigned long arg)
945 {
946         int err = 0;
947         void __user *argp = (void __user *)arg;
948
949         if (!capable(CAP_SYS_RESOURCE))
950                 return -EPERM;
951
952         switch (cmd) {
953         /* Attach an MTD device command */
954         case UBI_IOCATT:
955         {
956                 struct ubi_attach_req req;
957                 struct mtd_info *mtd;
958
959                 dbg_gen("attach MTD device");
960                 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
961                 if (err) {
962                         err = -EFAULT;
963                         break;
964                 }
965
966                 if (req.mtd_num < 0 ||
967                     (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
968                         err = -EINVAL;
969                         break;
970                 }
971
972                 mtd = get_mtd_device(NULL, req.mtd_num);
973                 if (IS_ERR(mtd)) {
974                         err = PTR_ERR(mtd);
975                         break;
976                 }
977
978                 /*
979                  * Note, further request verification is done by
980                  * 'ubi_attach_mtd_dev()'.
981                  */
982                 mutex_lock(&ubi_devices_mutex);
983                 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
984                 mutex_unlock(&ubi_devices_mutex);
985                 if (err < 0)
986                         put_mtd_device(mtd);
987                 else
988                         /* @err contains UBI device number */
989                         err = put_user(err, (__user int32_t *)argp);
990
991                 break;
992         }
993
994         /* Detach an MTD device command */
995         case UBI_IOCDET:
996         {
997                 int ubi_num;
998
999                 dbg_gen("dettach MTD device");
1000                 err = get_user(ubi_num, (__user int32_t *)argp);
1001                 if (err) {
1002                         err = -EFAULT;
1003                         break;
1004                 }
1005
1006                 mutex_lock(&ubi_devices_mutex);
1007                 err = ubi_detach_mtd_dev(ubi_num, 0);
1008                 mutex_unlock(&ubi_devices_mutex);
1009                 break;
1010         }
1011
1012         default:
1013                 err = -ENOTTY;
1014                 break;
1015         }
1016
1017         return err;
1018 }
1019
1020 /* UBI control character device operations */
1021 struct file_operations ubi_ctrl_cdev_operations = {
1022         .ioctl = ctrl_cdev_ioctl,
1023         .owner = THIS_MODULE,
1024 };
1025
1026 /* UBI character device operations */
1027 struct file_operations ubi_cdev_operations = {
1028         .owner = THIS_MODULE,
1029         .ioctl = ubi_cdev_ioctl,
1030         .llseek = no_llseek,
1031 };
1032
1033 /* UBI volume character device operations */
1034 struct file_operations ubi_vol_cdev_operations = {
1035         .owner   = THIS_MODULE,
1036         .open    = vol_cdev_open,
1037         .release = vol_cdev_release,
1038         .llseek  = vol_cdev_llseek,
1039         .read    = vol_cdev_read,
1040         .write   = vol_cdev_write,
1041         .ioctl   = vol_cdev_ioctl,
1042 };