UBI: allow all ioctls
[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         /* Logical eraseblock erasure command */
495         case UBI_IOCEBER:
496         {
497                 int32_t lnum;
498
499                 err = get_user(lnum, (__user int32_t *)argp);
500                 if (err) {
501                         err = -EFAULT;
502                         break;
503                 }
504
505                 if (desc->mode == UBI_READONLY ||
506                     vol->vol_type == UBI_STATIC_VOLUME) {
507                         err = -EROFS;
508                         break;
509                 }
510
511                 if (lnum < 0 || lnum >= vol->reserved_pebs) {
512                         err = -EINVAL;
513                         break;
514                 }
515
516                 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
517                 err = ubi_eba_unmap_leb(ubi, vol, lnum);
518                 if (err)
519                         break;
520
521                 err = ubi_wl_flush(ubi);
522                 break;
523         }
524
525         /* Logical eraseblock map command */
526         case UBI_IOCEBMAP:
527         {
528                 struct ubi_map_req req;
529
530                 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
531                 if (err) {
532                         err = -EFAULT;
533                         break;
534                 }
535                 err = ubi_leb_map(desc, req.lnum, req.dtype);
536                 break;
537         }
538
539         /* Logical eraseblock un-map command */
540         case UBI_IOCEBUNMAP:
541         {
542                 int32_t lnum;
543
544                 err = get_user(lnum, (__user int32_t *)argp);
545                 if (err) {
546                         err = -EFAULT;
547                         break;
548                 }
549                 err = ubi_leb_unmap(desc, lnum);
550                 break;
551         }
552
553         /* Check if logical eraseblock is mapped command */
554         case UBI_IOCEBISMAP:
555         {
556                 int32_t lnum;
557
558                 err = get_user(lnum, (__user int32_t *)argp);
559                 if (err) {
560                         err = -EFAULT;
561                         break;
562                 }
563                 err = ubi_is_mapped(desc, lnum);
564                 break;
565         }
566
567         default:
568                 err = -ENOTTY;
569                 break;
570         }
571         return err;
572 }
573
574 /**
575  * verify_mkvol_req - verify volume creation request.
576  * @ubi: UBI device description object
577  * @req: the request to check
578  *
579  * This function zero if the request is correct, and %-EINVAL if not.
580  */
581 static int verify_mkvol_req(const struct ubi_device *ubi,
582                             const struct ubi_mkvol_req *req)
583 {
584         int n, err = -EINVAL;
585
586         if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
587             req->name_len < 0)
588                 goto bad;
589
590         if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
591             req->vol_id != UBI_VOL_NUM_AUTO)
592                 goto bad;
593
594         if (req->alignment == 0)
595                 goto bad;
596
597         if (req->bytes == 0)
598                 goto bad;
599
600         if (req->vol_type != UBI_DYNAMIC_VOLUME &&
601             req->vol_type != UBI_STATIC_VOLUME)
602                 goto bad;
603
604         if (req->alignment > ubi->leb_size)
605                 goto bad;
606
607         n = req->alignment & (ubi->min_io_size - 1);
608         if (req->alignment != 1 && n)
609                 goto bad;
610
611         if (req->name_len > UBI_VOL_NAME_MAX) {
612                 err = -ENAMETOOLONG;
613                 goto bad;
614         }
615
616         n = strnlen(req->name, req->name_len + 1);
617         if (n != req->name_len)
618                 goto bad;
619
620         return 0;
621
622 bad:
623         dbg_err("bad volume creation request");
624         ubi_dbg_dump_mkvol_req(req);
625         return err;
626 }
627
628 /**
629  * verify_rsvol_req - verify volume re-size request.
630  * @ubi: UBI device description object
631  * @req: the request to check
632  *
633  * This function returns zero if the request is correct, and %-EINVAL if not.
634  */
635 static int verify_rsvol_req(const struct ubi_device *ubi,
636                             const struct ubi_rsvol_req *req)
637 {
638         if (req->bytes <= 0)
639                 return -EINVAL;
640
641         if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
642                 return -EINVAL;
643
644         return 0;
645 }
646
647 /**
648  * rename_volumes - rename UBI volumes.
649  * @ubi: UBI device description object
650  * @req: volumes re-name request
651  *
652  * This is a helper function for the volume re-name IOCTL which validates the
653  * the request, opens the volume and calls corresponding volumes management
654  * function. Returns zero in case of success and a negative error code in case
655  * of failure.
656  */
657 static int rename_volumes(struct ubi_device *ubi,
658                           struct ubi_rnvol_req *req)
659 {
660         int i, n, err;
661         struct list_head rename_list;
662         struct ubi_rename_entry *re, *re1;
663
664         if (req->count < 0 || req->count > UBI_MAX_RNVOL)
665                 return -EINVAL;
666
667         if (req->count == 0)
668                 return 0;
669
670         /* Validate volume IDs and names in the request */
671         for (i = 0; i < req->count; i++) {
672                 if (req->ents[i].vol_id < 0 ||
673                     req->ents[i].vol_id >= ubi->vtbl_slots)
674                         return -EINVAL;
675                 if (req->ents[i].name_len < 0)
676                         return -EINVAL;
677                 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
678                         return -ENAMETOOLONG;
679                 req->ents[i].name[req->ents[i].name_len] = '\0';
680                 n = strlen(req->ents[i].name);
681                 if (n != req->ents[i].name_len)
682                         err = -EINVAL;
683         }
684
685         /* Make sure volume IDs and names are unique */
686         for (i = 0; i < req->count - 1; i++) {
687                 for (n = i + 1; n < req->count; n++) {
688                         if (req->ents[i].vol_id == req->ents[n].vol_id) {
689                                 dbg_err("duplicated volume id %d",
690                                         req->ents[i].vol_id);
691                                 return -EINVAL;
692                         }
693                         if (!strcmp(req->ents[i].name, req->ents[n].name)) {
694                                 dbg_err("duplicated volume name \"%s\"",
695                                         req->ents[i].name);
696                                 return -EINVAL;
697                         }
698                 }
699         }
700
701         /* Create the re-name list */
702         INIT_LIST_HEAD(&rename_list);
703         for (i = 0; i < req->count; i++) {
704                 int vol_id = req->ents[i].vol_id;
705                 int name_len = req->ents[i].name_len;
706                 const char *name = req->ents[i].name;
707
708                 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
709                 if (!re) {
710                         err = -ENOMEM;
711                         goto out_free;
712                 }
713
714                 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
715                 if (IS_ERR(re->desc)) {
716                         err = PTR_ERR(re->desc);
717                         dbg_err("cannot open volume %d, error %d", vol_id, err);
718                         kfree(re);
719                         goto out_free;
720                 }
721
722                 /* Skip this re-naming if the name does not really change */
723                 if (re->desc->vol->name_len == name_len &&
724                     !memcmp(re->desc->vol->name, name, name_len)) {
725                         ubi_close_volume(re->desc);
726                         kfree(re);
727                         continue;
728                 }
729
730                 re->new_name_len = name_len;
731                 memcpy(re->new_name, name, name_len);
732                 list_add_tail(&re->list, &rename_list);
733                 dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
734                         vol_id, re->desc->vol->name, name);
735         }
736
737         if (list_empty(&rename_list))
738                 return 0;
739
740         /* Find out the volumes which have to be removed */
741         list_for_each_entry(re, &rename_list, list) {
742                 struct ubi_volume_desc *desc;
743                 int no_remove_needed = 0;
744
745                 /*
746                  * Volume @re->vol_id is going to be re-named to
747                  * @re->new_name, while its current name is @name. If a volume
748                  * with name @re->new_name currently exists, it has to be
749                  * removed, unless it is also re-named in the request (@req).
750                  */
751                 list_for_each_entry(re1, &rename_list, list) {
752                         if (re->new_name_len == re1->desc->vol->name_len &&
753                             !memcmp(re->new_name, re1->desc->vol->name,
754                                     re1->desc->vol->name_len)) {
755                                 no_remove_needed = 1;
756                                 break;
757                         }
758                 }
759
760                 if (no_remove_needed)
761                         continue;
762
763                 /*
764                  * It seems we need to remove volume with name @re->new_name,
765                  * if it exists.
766                  */
767                 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
768                                           UBI_EXCLUSIVE);
769                 if (IS_ERR(desc)) {
770                         err = PTR_ERR(desc);
771                         if (err == -ENODEV)
772                                 /* Re-naming into a non-existing volume name */
773                                 continue;
774
775                         /* The volume exists but busy, or an error occurred */
776                         dbg_err("cannot open volume \"%s\", error %d",
777                                 re->new_name, err);
778                         goto out_free;
779                 }
780
781                 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
782                 if (!re) {
783                         err = -ENOMEM;
784                         ubi_close_volume(desc);
785                         goto out_free;
786                 }
787
788                 re->remove = 1;
789                 re->desc = desc;
790                 list_add(&re->list, &rename_list);
791                 dbg_msg("will remove volume %d, name \"%s\"",
792                         re->desc->vol->vol_id, re->desc->vol->name);
793         }
794
795         mutex_lock(&ubi->volumes_mutex);
796         err = ubi_rename_volumes(ubi, &rename_list);
797         mutex_unlock(&ubi->volumes_mutex);
798
799 out_free:
800         list_for_each_entry_safe(re, re1, &rename_list, list) {
801                 ubi_close_volume(re->desc);
802                 list_del(&re->list);
803                 kfree(re);
804         }
805         return err;
806 }
807
808 static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
809                           unsigned int cmd, unsigned long arg)
810 {
811         int err = 0;
812         struct ubi_device *ubi;
813         struct ubi_volume_desc *desc;
814         void __user *argp = (void __user *)arg;
815
816         if (!capable(CAP_SYS_RESOURCE))
817                 return -EPERM;
818
819         ubi = ubi_get_by_major(imajor(inode));
820         if (!ubi)
821                 return -ENODEV;
822
823         switch (cmd) {
824         /* Create volume command */
825         case UBI_IOCMKVOL:
826         {
827                 struct ubi_mkvol_req req;
828
829                 dbg_gen("create volume");
830                 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
831                 if (err) {
832                         err = -EFAULT;
833                         break;
834                 }
835
836                 req.name[req.name_len] = '\0';
837                 err = verify_mkvol_req(ubi, &req);
838                 if (err)
839                         break;
840
841                 mutex_lock(&ubi->volumes_mutex);
842                 err = ubi_create_volume(ubi, &req);
843                 mutex_unlock(&ubi->volumes_mutex);
844                 if (err)
845                         break;
846
847                 err = put_user(req.vol_id, (__user int32_t *)argp);
848                 if (err)
849                         err = -EFAULT;
850
851                 break;
852         }
853
854         /* Remove volume command */
855         case UBI_IOCRMVOL:
856         {
857                 int vol_id;
858
859                 dbg_gen("remove volume");
860                 err = get_user(vol_id, (__user int32_t *)argp);
861                 if (err) {
862                         err = -EFAULT;
863                         break;
864                 }
865
866                 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
867                 if (IS_ERR(desc)) {
868                         err = PTR_ERR(desc);
869                         break;
870                 }
871
872                 mutex_lock(&ubi->volumes_mutex);
873                 err = ubi_remove_volume(desc, 0);
874                 mutex_unlock(&ubi->volumes_mutex);
875
876                 /*
877                  * The volume is deleted (unless an error occurred), and the
878                  * 'struct ubi_volume' object will be freed when
879                  * 'ubi_close_volume()' will call 'put_device()'.
880                  */
881                 ubi_close_volume(desc);
882                 break;
883         }
884
885         /* Re-size volume command */
886         case UBI_IOCRSVOL:
887         {
888                 int pebs;
889                 uint64_t tmp;
890                 struct ubi_rsvol_req req;
891
892                 dbg_gen("re-size volume");
893                 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
894                 if (err) {
895                         err = -EFAULT;
896                         break;
897                 }
898
899                 err = verify_rsvol_req(ubi, &req);
900                 if (err)
901                         break;
902
903                 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
904                 if (IS_ERR(desc)) {
905                         err = PTR_ERR(desc);
906                         break;
907                 }
908
909                 tmp = req.bytes;
910                 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
911                 pebs += tmp;
912
913                 mutex_lock(&ubi->volumes_mutex);
914                 err = ubi_resize_volume(desc, pebs);
915                 mutex_unlock(&ubi->volumes_mutex);
916                 ubi_close_volume(desc);
917                 break;
918         }
919
920         /* Re-name volumes command */
921         case UBI_IOCRNVOL:
922         {
923                 struct ubi_rnvol_req *req;
924
925                 dbg_msg("re-name volumes");
926                 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
927                 if (!req) {
928                         err = -ENOMEM;
929                         break;
930                 };
931
932                 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
933                 if (err) {
934                         err = -EFAULT;
935                         kfree(req);
936                         break;
937                 }
938
939                 mutex_lock(&ubi->mult_mutex);
940                 err = rename_volumes(ubi, req);
941                 mutex_unlock(&ubi->mult_mutex);
942                 kfree(req);
943                 break;
944         }
945
946         default:
947                 err = -ENOTTY;
948                 break;
949         }
950
951         ubi_put_device(ubi);
952         return err;
953 }
954
955 static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
956                            unsigned int cmd, unsigned long arg)
957 {
958         int err = 0;
959         void __user *argp = (void __user *)arg;
960
961         if (!capable(CAP_SYS_RESOURCE))
962                 return -EPERM;
963
964         switch (cmd) {
965         /* Attach an MTD device command */
966         case UBI_IOCATT:
967         {
968                 struct ubi_attach_req req;
969                 struct mtd_info *mtd;
970
971                 dbg_gen("attach MTD device");
972                 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
973                 if (err) {
974                         err = -EFAULT;
975                         break;
976                 }
977
978                 if (req.mtd_num < 0 ||
979                     (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
980                         err = -EINVAL;
981                         break;
982                 }
983
984                 mtd = get_mtd_device(NULL, req.mtd_num);
985                 if (IS_ERR(mtd)) {
986                         err = PTR_ERR(mtd);
987                         break;
988                 }
989
990                 /*
991                  * Note, further request verification is done by
992                  * 'ubi_attach_mtd_dev()'.
993                  */
994                 mutex_lock(&ubi_devices_mutex);
995                 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
996                 mutex_unlock(&ubi_devices_mutex);
997                 if (err < 0)
998                         put_mtd_device(mtd);
999                 else
1000                         /* @err contains UBI device number */
1001                         err = put_user(err, (__user int32_t *)argp);
1002
1003                 break;
1004         }
1005
1006         /* Detach an MTD device command */
1007         case UBI_IOCDET:
1008         {
1009                 int ubi_num;
1010
1011                 dbg_gen("dettach MTD device");
1012                 err = get_user(ubi_num, (__user int32_t *)argp);
1013                 if (err) {
1014                         err = -EFAULT;
1015                         break;
1016                 }
1017
1018                 mutex_lock(&ubi_devices_mutex);
1019                 err = ubi_detach_mtd_dev(ubi_num, 0);
1020                 mutex_unlock(&ubi_devices_mutex);
1021                 break;
1022         }
1023
1024         default:
1025                 err = -ENOTTY;
1026                 break;
1027         }
1028
1029         return err;
1030 }
1031
1032 /* UBI control character device operations */
1033 struct file_operations ubi_ctrl_cdev_operations = {
1034         .ioctl = ctrl_cdev_ioctl,
1035         .owner = THIS_MODULE,
1036 };
1037
1038 /* UBI character device operations */
1039 struct file_operations ubi_cdev_operations = {
1040         .owner = THIS_MODULE,
1041         .ioctl = ubi_cdev_ioctl,
1042         .llseek = no_llseek,
1043 };
1044
1045 /* UBI volume character device operations */
1046 struct file_operations ubi_vol_cdev_operations = {
1047         .owner   = THIS_MODULE,
1048         .open    = vol_cdev_open,
1049         .release = vol_cdev_release,
1050         .llseek  = vol_cdev_llseek,
1051         .read    = vol_cdev_read,
1052         .write   = vol_cdev_write,
1053         .ioctl   = vol_cdev_ioctl,
1054 };