blob: a7aa123afaf6ec94558b10ff0a86e7879ba1d580 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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.
Artem Bityutskiy9f961b52007-12-16 16:59:31 +020031 *
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.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040036 */
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
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040047/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040048 * get_exclusive - get exclusive access to an UBI volume.
49 * @desc: volume descriptor
50 *
51 * This function changes UBI volume open mode to "exclusive". Returns previous
52 * mode value (positive integer) in case of success and a negative error code
53 * in case of failure.
54 */
55static int get_exclusive(struct ubi_volume_desc *desc)
56{
57 int users, err;
58 struct ubi_volume *vol = desc->vol;
59
60 spin_lock(&vol->ubi->volumes_lock);
61 users = vol->readers + vol->writers + vol->exclusive;
62 ubi_assert(users > 0);
63 if (users > 1) {
64 dbg_err("%d users for volume %d", users, vol->vol_id);
65 err = -EBUSY;
66 } else {
67 vol->readers = vol->writers = 0;
68 vol->exclusive = 1;
69 err = desc->mode;
70 desc->mode = UBI_EXCLUSIVE;
71 }
72 spin_unlock(&vol->ubi->volumes_lock);
73
74 return err;
75}
76
77/**
78 * revoke_exclusive - revoke exclusive mode.
79 * @desc: volume descriptor
80 * @mode: new mode to switch to
81 */
82static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
83{
84 struct ubi_volume *vol = desc->vol;
85
86 spin_lock(&vol->ubi->volumes_lock);
87 ubi_assert(vol->readers == 0 && vol->writers == 0);
88 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
89 vol->exclusive = 0;
90 if (mode == UBI_READONLY)
91 vol->readers = 1;
92 else if (mode == UBI_READWRITE)
93 vol->writers = 1;
94 else
95 vol->exclusive = 1;
96 spin_unlock(&vol->ubi->volumes_lock);
97
98 desc->mode = mode;
99}
100
101static int vol_cdev_open(struct inode *inode, struct file *file)
102{
103 struct ubi_volume_desc *desc;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200104 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;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400109
110 if (file->f_mode & FMODE_WRITE)
111 mode = UBI_READWRITE;
112 else
113 mode = UBI_READONLY;
114
115 dbg_msg("open volume %d, mode %d", vol_id, mode);
116
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200117 desc = ubi_open_volume(ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400118 if (IS_ERR(desc))
119 return PTR_ERR(desc);
120
121 file->private_data = desc;
122 return 0;
123}
124
125static int vol_cdev_release(struct inode *inode, struct file *file)
126{
127 struct ubi_volume_desc *desc = file->private_data;
128 struct ubi_volume *vol = desc->vol;
129
130 dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
131
132 if (vol->updating) {
133 ubi_warn("update of volume %d not finished, volume is damaged",
134 vol->vol_id);
135 vol->updating = 0;
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300136 vfree(vol->upd_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400137 }
138
139 ubi_close_volume(desc);
140 return 0;
141}
142
143static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
144{
145 struct ubi_volume_desc *desc = file->private_data;
146 struct ubi_volume *vol = desc->vol;
147 loff_t new_offset;
148
149 if (vol->updating) {
150 /* Update is in progress, seeking is prohibited */
151 dbg_err("updating");
152 return -EBUSY;
153 }
154
155 switch (origin) {
156 case 0: /* SEEK_SET */
157 new_offset = offset;
158 break;
159 case 1: /* SEEK_CUR */
160 new_offset = file->f_pos + offset;
161 break;
162 case 2: /* SEEK_END */
163 new_offset = vol->used_bytes + offset;
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 if (new_offset < 0 || new_offset > vol->used_bytes) {
170 dbg_err("bad seek %lld", new_offset);
171 return -EINVAL;
172 }
173
174 dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
175 vol->vol_id, offset, origin, new_offset);
176
177 file->f_pos = new_offset;
178 return new_offset;
179}
180
181static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
182 loff_t *offp)
183{
184 struct ubi_volume_desc *desc = file->private_data;
185 struct ubi_volume *vol = desc->vol;
186 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200187 int err, lnum, off, len, tbuf_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400188 size_t count_save = count;
189 void *tbuf;
190 uint64_t tmp;
191
192 dbg_msg("read %zd bytes from offset %lld of volume %d",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200193 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194
195 if (vol->updating) {
196 dbg_err("updating");
197 return -EBUSY;
198 }
199 if (vol->upd_marker) {
200 dbg_err("damaged volume, update marker is set");
201 return -EBADF;
202 }
203 if (*offp == vol->used_bytes || count == 0)
204 return 0;
205
206 if (vol->corrupted)
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200207 dbg_msg("read from corrupted volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208
209 if (*offp + count > vol->used_bytes)
210 count_save = count = vol->used_bytes - *offp;
211
212 tbuf_size = vol->usable_leb_size;
213 if (count < tbuf_size)
214 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300215 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 if (!tbuf)
217 return -ENOMEM;
218
219 len = count > tbuf_size ? tbuf_size : count;
220
221 tmp = *offp;
222 off = do_div(tmp, vol->usable_leb_size);
223 lnum = tmp;
224
225 do {
226 cond_resched();
227
228 if (off + len >= vol->usable_leb_size)
229 len = vol->usable_leb_size - off;
230
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200231 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400232 if (err)
233 break;
234
235 off += len;
236 if (off == vol->usable_leb_size) {
237 lnum += 1;
238 off -= vol->usable_leb_size;
239 }
240
241 count -= len;
242 *offp += len;
243
244 err = copy_to_user(buf, tbuf, len);
245 if (err) {
246 err = -EFAULT;
247 break;
248 }
249
250 buf += len;
251 len = count > tbuf_size ? tbuf_size : count;
252 } while (count);
253
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300254 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255 return err ? err : count_save - count;
256}
257
258#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
259
260/*
261 * This function allows to directly write to dynamic UBI volumes, without
262 * issuing the volume update operation. Available only as a debugging feature.
263 * Very useful for testing UBI.
264 */
265static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
266 size_t count, loff_t *offp)
267{
268 struct ubi_volume_desc *desc = file->private_data;
269 struct ubi_volume *vol = desc->vol;
270 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200271 int lnum, off, len, tbuf_size, err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272 size_t count_save = count;
273 char *tbuf;
274 uint64_t tmp;
275
276 dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200277 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400278
279 if (vol->vol_type == UBI_STATIC_VOLUME)
280 return -EROFS;
281
282 tmp = *offp;
283 off = do_div(tmp, vol->usable_leb_size);
284 lnum = tmp;
285
286 if (off % ubi->min_io_size) {
287 dbg_err("unaligned position");
288 return -EINVAL;
289 }
290
291 if (*offp + count > vol->used_bytes)
292 count_save = count = vol->used_bytes - *offp;
293
294 /* We can write only in fractions of the minimum I/O unit */
295 if (count % ubi->min_io_size) {
296 dbg_err("unaligned write length");
297 return -EINVAL;
298 }
299
300 tbuf_size = vol->usable_leb_size;
301 if (count < tbuf_size)
302 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300303 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400304 if (!tbuf)
305 return -ENOMEM;
306
307 len = count > tbuf_size ? tbuf_size : count;
308
309 while (count) {
310 cond_resched();
311
312 if (off + len >= vol->usable_leb_size)
313 len = vol->usable_leb_size - off;
314
315 err = copy_from_user(tbuf, buf, len);
316 if (err) {
317 err = -EFAULT;
318 break;
319 }
320
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200321 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400322 UBI_UNKNOWN);
323 if (err)
324 break;
325
326 off += len;
327 if (off == vol->usable_leb_size) {
328 lnum += 1;
329 off -= vol->usable_leb_size;
330 }
331
332 count -= len;
333 *offp += len;
334 buf += len;
335 len = count > tbuf_size ? tbuf_size : count;
336 }
337
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300338 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400339 return err ? err : count_save - count;
340}
341
342#else
343#define vol_cdev_direct_write(file, buf, count, offp) -EPERM
344#endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
345
346static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
347 size_t count, loff_t *offp)
348{
349 int err = 0;
350 struct ubi_volume_desc *desc = file->private_data;
351 struct ubi_volume *vol = desc->vol;
352 struct ubi_device *ubi = vol->ubi;
353
354 if (!vol->updating)
355 return vol_cdev_direct_write(file, buf, count, offp);
356
357 err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
358 if (err < 0) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200359 ubi_err("cannot write %zd bytes of update data, error %d",
360 count, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361 return err;
362 }
363
364 if (err) {
365 /*
366 * Update is finished, @err contains number of actually written
367 * bytes now.
368 */
369 count = err;
370
371 err = ubi_check_volume(ubi, vol->vol_id);
372 if (err < 0)
373 return err;
374
375 if (err) {
376 ubi_warn("volume %d on UBI device %d is corrupted",
377 vol->vol_id, ubi->ubi_num);
378 vol->corrupted = 1;
379 }
380 vol->checked = 1;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300381 ubi_gluebi_updated(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400382 revoke_exclusive(desc, UBI_READWRITE);
383 }
384
385 *offp += count;
386 return count;
387}
388
389static int vol_cdev_ioctl(struct inode *inode, struct file *file,
390 unsigned int cmd, unsigned long arg)
391{
392 int err = 0;
393 struct ubi_volume_desc *desc = file->private_data;
394 struct ubi_volume *vol = desc->vol;
395 struct ubi_device *ubi = vol->ubi;
396 void __user *argp = (void __user *)arg;
397
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 switch (cmd) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400399 /* Volume update command */
400 case UBI_IOCVOLUP:
401 {
402 int64_t bytes, rsvd_bytes;
403
404 if (!capable(CAP_SYS_RESOURCE)) {
405 err = -EPERM;
406 break;
407 }
408
409 err = copy_from_user(&bytes, argp, sizeof(int64_t));
410 if (err) {
411 err = -EFAULT;
412 break;
413 }
414
415 if (desc->mode == UBI_READONLY) {
416 err = -EROFS;
417 break;
418 }
419
420 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
421 if (bytes < 0 || bytes > rsvd_bytes) {
422 err = -EINVAL;
423 break;
424 }
425
426 err = get_exclusive(desc);
427 if (err < 0)
428 break;
429
430 err = ubi_start_update(ubi, vol->vol_id, bytes);
431 if (bytes == 0)
432 revoke_exclusive(desc, UBI_READWRITE);
433
434 file->f_pos = 0;
435 break;
436 }
437
438#ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
439 /* Logical eraseblock erasure command */
440 case UBI_IOCEBER:
441 {
442 int32_t lnum;
443
Christoph Hellwigbf078032007-05-17 16:32:10 +0200444 err = get_user(lnum, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400445 if (err) {
446 err = -EFAULT;
447 break;
448 }
449
450 if (desc->mode == UBI_READONLY) {
451 err = -EROFS;
452 break;
453 }
454
455 if (lnum < 0 || lnum >= vol->reserved_pebs) {
456 err = -EINVAL;
457 break;
458 }
459
460 if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
461 err = -EROFS;
462 break;
463 }
464
465 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200466 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400467 if (err)
468 break;
469
470 err = ubi_wl_flush(ubi);
471 break;
472 }
473#endif
474
475 default:
476 err = -ENOTTY;
477 break;
478 }
479
480 return err;
481}
482
483/**
484 * verify_mkvol_req - verify volume creation request.
485 * @ubi: UBI device description object
486 * @req: the request to check
487 *
488 * This function zero if the request is correct, and %-EINVAL if not.
489 */
490static int verify_mkvol_req(const struct ubi_device *ubi,
491 const struct ubi_mkvol_req *req)
492{
493 int n, err = -EINVAL;
494
495 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
496 req->name_len < 0)
497 goto bad;
498
499 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
500 req->vol_id != UBI_VOL_NUM_AUTO)
501 goto bad;
502
503 if (req->alignment == 0)
504 goto bad;
505
506 if (req->bytes == 0)
507 goto bad;
508
509 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
510 req->vol_type != UBI_STATIC_VOLUME)
511 goto bad;
512
513 if (req->alignment > ubi->leb_size)
514 goto bad;
515
516 n = req->alignment % ubi->min_io_size;
517 if (req->alignment != 1 && n)
518 goto bad;
519
520 if (req->name_len > UBI_VOL_NAME_MAX) {
521 err = -ENAMETOOLONG;
522 goto bad;
523 }
524
525 return 0;
526
527bad:
528 dbg_err("bad volume creation request");
529 ubi_dbg_dump_mkvol_req(req);
530 return err;
531}
532
533/**
534 * verify_rsvol_req - verify volume re-size request.
535 * @ubi: UBI device description object
536 * @req: the request to check
537 *
538 * This function returns zero if the request is correct, and %-EINVAL if not.
539 */
540static int verify_rsvol_req(const struct ubi_device *ubi,
541 const struct ubi_rsvol_req *req)
542{
543 if (req->bytes <= 0)
544 return -EINVAL;
545
546 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
547 return -EINVAL;
548
549 return 0;
550}
551
552static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
553 unsigned int cmd, unsigned long arg)
554{
555 int err = 0;
556 struct ubi_device *ubi;
557 struct ubi_volume_desc *desc;
558 void __user *argp = (void __user *)arg;
559
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560 if (!capable(CAP_SYS_RESOURCE))
561 return -EPERM;
562
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200563 ubi = ubi_get_by_major(imajor(inode));
564 if (!ubi)
565 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400566
567 switch (cmd) {
568 /* Create volume command */
569 case UBI_IOCMKVOL:
570 {
571 struct ubi_mkvol_req req;
572
573 dbg_msg("create volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200574 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400575 if (err) {
576 err = -EFAULT;
577 break;
578 }
579
580 err = verify_mkvol_req(ubi, &req);
581 if (err)
582 break;
583
584 req.name[req.name_len] = '\0';
585
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200586 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400587 err = ubi_create_volume(ubi, &req);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200588 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400589 if (err)
590 break;
591
Christoph Hellwigbf078032007-05-17 16:32:10 +0200592 err = put_user(req.vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400593 if (err)
594 err = -EFAULT;
595
596 break;
597 }
598
599 /* Remove volume command */
600 case UBI_IOCRMVOL:
601 {
602 int vol_id;
603
604 dbg_msg("remove volume");
Christoph Hellwigbf078032007-05-17 16:32:10 +0200605 err = get_user(vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400606 if (err) {
607 err = -EFAULT;
608 break;
609 }
610
611 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
612 if (IS_ERR(desc)) {
613 err = PTR_ERR(desc);
614 break;
615 }
616
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200617 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400618 err = ubi_remove_volume(desc);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200619 mutex_unlock(&ubi->volumes_mutex);
620
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200621 /*
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200622 * The volume is deleted (unless an error occurred), and the
623 * 'struct ubi_volume' object will be freed when
624 * 'ubi_close_volume()' will call 'put_device()'.
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200625 */
626 ubi_close_volume(desc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400627 break;
628 }
629
630 /* Re-size volume command */
631 case UBI_IOCRSVOL:
632 {
633 int pebs;
634 uint64_t tmp;
635 struct ubi_rsvol_req req;
636
637 dbg_msg("re-size volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200638 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400639 if (err) {
640 err = -EFAULT;
641 break;
642 }
643
644 err = verify_rsvol_req(ubi, &req);
645 if (err)
646 break;
647
648 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
649 if (IS_ERR(desc)) {
650 err = PTR_ERR(desc);
651 break;
652 }
653
654 tmp = req.bytes;
655 pebs = !!do_div(tmp, desc->vol->usable_leb_size);
656 pebs += tmp;
657
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200658 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400659 err = ubi_resize_volume(desc, pebs);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200660 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400661 ubi_close_volume(desc);
662 break;
663 }
664
665 default:
666 err = -ENOTTY;
667 break;
668 }
669
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200670 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400671 return err;
672}
673
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200674static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
675 unsigned int cmd, unsigned long arg)
676{
677 int err = 0;
678 void __user *argp = (void __user *)arg;
679
680 if (!capable(CAP_SYS_RESOURCE))
681 return -EPERM;
682
683 switch (cmd) {
684 /* Attach an MTD device command */
685 case UBI_IOCATT:
686 {
687 struct ubi_attach_req req;
688 struct mtd_info *mtd;
689
690 dbg_msg("attach MTD device");
691 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
692 if (err) {
693 err = -EFAULT;
694 break;
695 }
696
697 if (req.mtd_num < 0 ||
698 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
699 err = -EINVAL;
700 break;
701 }
702
703 mtd = get_mtd_device(NULL, req.mtd_num);
704 if (IS_ERR(mtd)) {
705 err = PTR_ERR(mtd);
706 break;
707 }
708
709 /*
710 * Note, further request verification is done by
711 * 'ubi_attach_mtd_dev()'.
712 */
713 mutex_lock(&ubi_devices_mutex);
714 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
715 mutex_unlock(&ubi_devices_mutex);
716 if (err < 0)
717 put_mtd_device(mtd);
718 else
719 /* @err contains UBI device number */
720 err = put_user(err, (__user int32_t *)argp);
721
722 break;
723 }
724
725 /* Detach an MTD device command */
726 case UBI_IOCDET:
727 {
728 int ubi_num;
729
730 dbg_msg("dettach MTD device");
731 err = get_user(ubi_num, (__user int32_t *)argp);
732 if (err) {
733 err = -EFAULT;
734 break;
735 }
736
737 mutex_lock(&ubi_devices_mutex);
738 err = ubi_detach_mtd_dev(ubi_num, 0);
739 mutex_unlock(&ubi_devices_mutex);
740 break;
741 }
742
743 default:
744 err = -ENOTTY;
745 break;
746 }
747
748 return err;
749}
750
Artem Bityutskiy9f961b52007-12-16 16:59:31 +0200751/* UBI control character device operations */
752struct file_operations ubi_ctrl_cdev_operations = {
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200753 .ioctl = ctrl_cdev_ioctl,
Artem Bityutskiy9f961b52007-12-16 16:59:31 +0200754 .owner = THIS_MODULE,
755};
756
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400757/* UBI character device operations */
758struct file_operations ubi_cdev_operations = {
759 .owner = THIS_MODULE,
760 .ioctl = ubi_cdev_ioctl,
Artem Bityutskiyf800f092007-05-06 16:45:43 +0300761 .llseek = no_llseek,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400762};
763
764/* UBI volume character device operations */
765struct file_operations ubi_vol_cdev_operations = {
766 .owner = THIS_MODULE,
767 .open = vol_cdev_open,
768 .release = vol_cdev_release,
769 .llseek = vol_cdev_llseek,
770 .read = vol_cdev_read,
771 .write = vol_cdev_write,
Artem Bityutskiyf800f092007-05-06 16:45:43 +0300772 .ioctl = vol_cdev_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400773};