blob: 631983615f1184768866a3cb780fa54facd5e91d [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>
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +030042#include <linux/uaccess.h>
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +020043#include <linux/compat.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020044#include <linux/math64.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040045#include <mtd/ubi-user.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040046#include "ubi.h"
47
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040048/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040049 * get_exclusive - get exclusive access to an UBI volume.
50 * @desc: volume descriptor
51 *
52 * This function changes UBI volume open mode to "exclusive". Returns previous
53 * mode value (positive integer) in case of success and a negative error code
54 * in case of failure.
55 */
56static int get_exclusive(struct ubi_volume_desc *desc)
57{
58 int users, err;
59 struct ubi_volume *vol = desc->vol;
60
61 spin_lock(&vol->ubi->volumes_lock);
62 users = vol->readers + vol->writers + vol->exclusive;
63 ubi_assert(users > 0);
64 if (users > 1) {
65 dbg_err("%d users for volume %d", users, vol->vol_id);
66 err = -EBUSY;
67 } else {
68 vol->readers = vol->writers = 0;
69 vol->exclusive = 1;
70 err = desc->mode;
71 desc->mode = UBI_EXCLUSIVE;
72 }
73 spin_unlock(&vol->ubi->volumes_lock);
74
75 return err;
76}
77
78/**
79 * revoke_exclusive - revoke exclusive mode.
80 * @desc: volume descriptor
81 * @mode: new mode to switch to
82 */
83static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
84{
85 struct ubi_volume *vol = desc->vol;
86
87 spin_lock(&vol->ubi->volumes_lock);
88 ubi_assert(vol->readers == 0 && vol->writers == 0);
89 ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
90 vol->exclusive = 0;
91 if (mode == UBI_READONLY)
92 vol->readers = 1;
93 else if (mode == UBI_READWRITE)
94 vol->writers = 1;
95 else
96 vol->exclusive = 1;
97 spin_unlock(&vol->ubi->volumes_lock);
98
99 desc->mode = mode;
100}
101
102static int vol_cdev_open(struct inode *inode, struct file *file)
103{
104 struct ubi_volume_desc *desc;
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200105 int vol_id = iminor(inode) - 1, mode, ubi_num;
106
107 ubi_num = ubi_major2num(imajor(inode));
Artem Bityutskiy7d200e82008-08-31 19:32:13 +0300108 if (ubi_num < 0)
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200109 return ubi_num;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400110
111 if (file->f_mode & FMODE_WRITE)
112 mode = UBI_READWRITE;
113 else
114 mode = UBI_READONLY;
115
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300116 dbg_gen("open device %d, volume %d, mode %d",
117 ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400118
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200119 desc = ubi_open_volume(ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400120 if (IS_ERR(desc))
121 return PTR_ERR(desc);
122
123 file->private_data = desc;
124 return 0;
125}
126
127static int vol_cdev_release(struct inode *inode, struct file *file)
128{
129 struct ubi_volume_desc *desc = file->private_data;
130 struct ubi_volume *vol = desc->vol;
131
Artem Bityutskiye1cf7e62009-05-07 18:24:14 +0300132 dbg_gen("release device %d, volume %d, mode %d",
133 vol->ubi->ubi_num, vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134
135 if (vol->updating) {
136 ubi_warn("update of volume %d not finished, volume is damaged",
137 vol->vol_id);
Artem Bityutskiye6538792008-01-24 18:48:21 +0200138 ubi_assert(!vol->changing_leb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400139 vol->updating = 0;
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300140 vfree(vol->upd_buf);
Artem Bityutskiye6538792008-01-24 18:48:21 +0200141 } else if (vol->changing_leb) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300142 dbg_gen("only %lld of %lld bytes received for atomic LEB change"
Artem Bityutskiye6538792008-01-24 18:48:21 +0200143 " for volume %d:%d, cancel", vol->upd_received,
144 vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
145 vol->changing_leb = 0;
146 vfree(vol->upd_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400147 }
148
149 ubi_close_volume(desc);
150 return 0;
151}
152
153static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
154{
155 struct ubi_volume_desc *desc = file->private_data;
156 struct ubi_volume *vol = desc->vol;
157 loff_t new_offset;
158
159 if (vol->updating) {
160 /* Update is in progress, seeking is prohibited */
161 dbg_err("updating");
162 return -EBUSY;
163 }
164
165 switch (origin) {
166 case 0: /* SEEK_SET */
167 new_offset = offset;
168 break;
169 case 1: /* SEEK_CUR */
170 new_offset = file->f_pos + offset;
171 break;
172 case 2: /* SEEK_END */
173 new_offset = vol->used_bytes + offset;
174 break;
175 default:
176 return -EINVAL;
177 }
178
179 if (new_offset < 0 || new_offset > vol->used_bytes) {
180 dbg_err("bad seek %lld", new_offset);
181 return -EINVAL;
182 }
183
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300184 dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400185 vol->vol_id, offset, origin, new_offset);
186
187 file->f_pos = new_offset;
188 return new_offset;
189}
190
Corentin Chary1b24bc32009-02-05 22:25:52 +0100191static int vol_cdev_fsync(struct file *file, struct dentry *dentry,
192 int datasync)
193{
194 struct ubi_volume_desc *desc = file->private_data;
195 struct ubi_device *ubi = desc->vol->ubi;
196
197 return ubi_sync(ubi->ubi_num);
198}
199
200
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400201static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
202 loff_t *offp)
203{
204 struct ubi_volume_desc *desc = file->private_data;
205 struct ubi_volume *vol = desc->vol;
206 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200207 int err, lnum, off, len, tbuf_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208 size_t count_save = count;
209 void *tbuf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400210
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300211 dbg_gen("read %zd bytes from offset %lld of volume %d",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200212 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400213
214 if (vol->updating) {
215 dbg_err("updating");
216 return -EBUSY;
217 }
218 if (vol->upd_marker) {
219 dbg_err("damaged volume, update marker is set");
220 return -EBADF;
221 }
222 if (*offp == vol->used_bytes || count == 0)
223 return 0;
224
225 if (vol->corrupted)
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300226 dbg_gen("read from corrupted volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400227
228 if (*offp + count > vol->used_bytes)
229 count_save = count = vol->used_bytes - *offp;
230
231 tbuf_size = vol->usable_leb_size;
232 if (count < tbuf_size)
233 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300234 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400235 if (!tbuf)
236 return -ENOMEM;
237
238 len = count > tbuf_size ? tbuf_size : count;
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200239 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400240
241 do {
242 cond_resched();
243
244 if (off + len >= vol->usable_leb_size)
245 len = vol->usable_leb_size - off;
246
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200247 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400248 if (err)
249 break;
250
251 off += len;
252 if (off == vol->usable_leb_size) {
253 lnum += 1;
254 off -= vol->usable_leb_size;
255 }
256
257 count -= len;
258 *offp += len;
259
260 err = copy_to_user(buf, tbuf, len);
261 if (err) {
262 err = -EFAULT;
263 break;
264 }
265
266 buf += len;
267 len = count > tbuf_size ? tbuf_size : count;
268 } while (count);
269
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300270 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400271 return err ? err : count_save - count;
272}
273
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400274/*
275 * This function allows to directly write to dynamic UBI volumes, without
Sidney Amani766fb952009-01-27 10:11:46 +0100276 * issuing the volume update operation.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400277 */
278static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
279 size_t count, loff_t *offp)
280{
281 struct ubi_volume_desc *desc = file->private_data;
282 struct ubi_volume *vol = desc->vol;
283 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200284 int lnum, off, len, tbuf_size, err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400285 size_t count_save = count;
286 char *tbuf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400287
Sidney Amani766fb952009-01-27 10:11:46 +0100288 if (!vol->direct_writes)
289 return -EPERM;
290
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300291 dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200292 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400293
294 if (vol->vol_type == UBI_STATIC_VOLUME)
295 return -EROFS;
296
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200297 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900298 if (off & (ubi->min_io_size - 1)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400299 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 */
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900307 if (count & (ubi->min_io_size - 1)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400308 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);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300315 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400316 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
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200333 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400334 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
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300350 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400351 return err ? err : count_save - count;
352}
353
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400354static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
355 size_t count, loff_t *offp)
356{
357 int err = 0;
358 struct ubi_volume_desc *desc = file->private_data;
359 struct ubi_volume *vol = desc->vol;
360 struct ubi_device *ubi = vol->ubi;
361
Artem Bityutskiye6538792008-01-24 18:48:21 +0200362 if (!vol->updating && !vol->changing_leb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400363 return vol_cdev_direct_write(file, buf, count, offp);
364
Artem Bityutskiye6538792008-01-24 18:48:21 +0200365 if (vol->updating)
366 err = ubi_more_update_data(ubi, vol, buf, count);
367 else
368 err = ubi_more_leb_change_data(ubi, vol, buf, count);
369
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400370 if (err < 0) {
Artem Bityutskiye6538792008-01-24 18:48:21 +0200371 ubi_err("cannot accept more %zd bytes of data, error %d",
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200372 count, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373 return err;
374 }
375
376 if (err) {
377 /*
Artem Bityutskiye6538792008-01-24 18:48:21 +0200378 * The operation is finished, @err contains number of actually
379 * written bytes.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380 */
381 count = err;
382
Artem Bityutskiye6538792008-01-24 18:48:21 +0200383 if (vol->changing_leb) {
384 revoke_exclusive(desc, UBI_READWRITE);
385 return count;
386 }
387
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388 err = ubi_check_volume(ubi, vol->vol_id);
389 if (err < 0)
390 return err;
391
392 if (err) {
393 ubi_warn("volume %d on UBI device %d is corrupted",
394 vol->vol_id, ubi->ubi_num);
395 vol->corrupted = 1;
396 }
397 vol->checked = 1;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300398 ubi_gluebi_updated(vol);
Dmitry Pervushin0e0ee1c2009-04-29 19:29:38 +0400399 ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400400 revoke_exclusive(desc, UBI_READWRITE);
401 }
402
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400403 return count;
404}
405
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200406static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
407 unsigned long arg)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400408{
409 int err = 0;
410 struct ubi_volume_desc *desc = file->private_data;
411 struct ubi_volume *vol = desc->vol;
412 struct ubi_device *ubi = vol->ubi;
413 void __user *argp = (void __user *)arg;
414
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415 switch (cmd) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400416 /* Volume update command */
417 case UBI_IOCVOLUP:
418 {
419 int64_t bytes, rsvd_bytes;
420
421 if (!capable(CAP_SYS_RESOURCE)) {
422 err = -EPERM;
423 break;
424 }
425
426 err = copy_from_user(&bytes, argp, sizeof(int64_t));
427 if (err) {
428 err = -EFAULT;
429 break;
430 }
431
432 if (desc->mode == UBI_READONLY) {
433 err = -EROFS;
434 break;
435 }
436
Bruce Leonard73789a32008-07-03 10:35:49 +0300437 rsvd_bytes = (long long)vol->reserved_pebs *
438 ubi->leb_size-vol->data_pad;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439 if (bytes < 0 || bytes > rsvd_bytes) {
440 err = -EINVAL;
441 break;
442 }
443
444 err = get_exclusive(desc);
445 if (err < 0)
446 break;
447
Artem Bityutskiy1b68d0e2008-01-24 17:04:01 +0200448 err = ubi_start_update(ubi, vol, bytes);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400449 if (bytes == 0)
450 revoke_exclusive(desc, UBI_READWRITE);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451 break;
452 }
453
Artem Bityutskiye6538792008-01-24 18:48:21 +0200454 /* Atomic logical eraseblock change command */
455 case UBI_IOCEBCH:
456 {
457 struct ubi_leb_change_req req;
458
459 err = copy_from_user(&req, argp,
460 sizeof(struct ubi_leb_change_req));
461 if (err) {
462 err = -EFAULT;
463 break;
464 }
465
466 if (desc->mode == UBI_READONLY ||
467 vol->vol_type == UBI_STATIC_VOLUME) {
468 err = -EROFS;
469 break;
470 }
471
472 /* Validate the request */
473 err = -EINVAL;
474 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
475 req.bytes < 0 || req.lnum >= vol->usable_leb_size)
476 break;
477 if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
478 req.dtype != UBI_UNKNOWN)
479 break;
480
481 err = get_exclusive(desc);
482 if (err < 0)
483 break;
484
485 err = ubi_start_leb_change(ubi, vol, &req);
486 if (req.bytes == 0)
487 revoke_exclusive(desc, UBI_READWRITE);
488 break;
489 }
490
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400491 /* Logical eraseblock erasure command */
492 case UBI_IOCEBER:
493 {
494 int32_t lnum;
495
Christoph Hellwigbf078032007-05-17 16:32:10 +0200496 err = get_user(lnum, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400497 if (err) {
498 err = -EFAULT;
499 break;
500 }
501
Artem Bityutskiye6538792008-01-24 18:48:21 +0200502 if (desc->mode == UBI_READONLY ||
503 vol->vol_type == UBI_STATIC_VOLUME) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400504 err = -EROFS;
505 break;
506 }
507
508 if (lnum < 0 || lnum >= vol->reserved_pebs) {
509 err = -EINVAL;
510 break;
511 }
512
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300513 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200514 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 if (err)
516 break;
517
518 err = ubi_wl_flush(ubi);
519 break;
520 }
Corentin Chary141e6eb2009-01-05 14:44:11 +0100521
522 /* Logical eraseblock map command */
523 case UBI_IOCEBMAP:
524 {
525 struct ubi_map_req req;
526
527 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
528 if (err) {
529 err = -EFAULT;
530 break;
531 }
532 err = ubi_leb_map(desc, req.lnum, req.dtype);
533 break;
534 }
Corentin Charyc3da23b2009-01-05 14:46:19 +0100535
536 /* Logical eraseblock un-map command */
537 case UBI_IOCEBUNMAP:
538 {
539 int32_t lnum;
540
541 err = get_user(lnum, (__user int32_t *)argp);
542 if (err) {
543 err = -EFAULT;
544 break;
545 }
546 err = ubi_leb_unmap(desc, lnum);
547 break;
548 }
Corentin Charya27ce8f2009-01-05 14:48:59 +0100549
550 /* Check if logical eraseblock is mapped command */
551 case UBI_IOCEBISMAP:
552 {
553 int32_t lnum;
554
555 err = get_user(lnum, (__user int32_t *)argp);
556 if (err) {
557 err = -EFAULT;
558 break;
559 }
560 err = ubi_is_mapped(desc, lnum);
561 break;
562 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400563
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300564 /* Set volume property command */
Sidney Amani766fb952009-01-27 10:11:46 +0100565 case UBI_IOCSETPROP:
566 {
567 struct ubi_set_prop_req req;
568
569 err = copy_from_user(&req, argp,
570 sizeof(struct ubi_set_prop_req));
571 if (err) {
572 err = -EFAULT;
573 break;
574 }
575 switch (req.property) {
576 case UBI_PROP_DIRECT_WRITE:
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300577 mutex_lock(&ubi->device_mutex);
Sidney Amani766fb952009-01-27 10:11:46 +0100578 desc->vol->direct_writes = !!req.value;
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300579 mutex_unlock(&ubi->device_mutex);
Sidney Amani766fb952009-01-27 10:11:46 +0100580 break;
581 default:
582 err = -EINVAL;
583 break;
584 }
585 break;
586 }
587
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400588 default:
589 err = -ENOTTY;
590 break;
591 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400592 return err;
593}
594
595/**
596 * verify_mkvol_req - verify volume creation request.
597 * @ubi: UBI device description object
598 * @req: the request to check
599 *
600 * This function zero if the request is correct, and %-EINVAL if not.
601 */
602static int verify_mkvol_req(const struct ubi_device *ubi,
603 const struct ubi_mkvol_req *req)
604{
605 int n, err = -EINVAL;
606
607 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
608 req->name_len < 0)
609 goto bad;
610
611 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
612 req->vol_id != UBI_VOL_NUM_AUTO)
613 goto bad;
614
615 if (req->alignment == 0)
616 goto bad;
617
618 if (req->bytes == 0)
619 goto bad;
620
621 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
622 req->vol_type != UBI_STATIC_VOLUME)
623 goto bad;
624
625 if (req->alignment > ubi->leb_size)
626 goto bad;
627
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900628 n = req->alignment & (ubi->min_io_size - 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400629 if (req->alignment != 1 && n)
630 goto bad;
631
632 if (req->name_len > UBI_VOL_NAME_MAX) {
633 err = -ENAMETOOLONG;
634 goto bad;
635 }
636
Artem Bityutskiya6ea4402008-07-13 21:46:24 +0300637 n = strnlen(req->name, req->name_len + 1);
638 if (n != req->name_len)
639 goto bad;
640
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400641 return 0;
642
643bad:
644 dbg_err("bad volume creation request");
645 ubi_dbg_dump_mkvol_req(req);
646 return err;
647}
648
649/**
650 * verify_rsvol_req - verify volume re-size request.
651 * @ubi: UBI device description object
652 * @req: the request to check
653 *
654 * This function returns zero if the request is correct, and %-EINVAL if not.
655 */
656static int verify_rsvol_req(const struct ubi_device *ubi,
657 const struct ubi_rsvol_req *req)
658{
659 if (req->bytes <= 0)
660 return -EINVAL;
661
662 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
663 return -EINVAL;
664
665 return 0;
666}
667
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300668/**
669 * rename_volumes - rename UBI volumes.
670 * @ubi: UBI device description object
671 * @req: volumes re-name request
672 *
673 * This is a helper function for the volume re-name IOCTL which validates the
674 * the request, opens the volume and calls corresponding volumes management
675 * function. Returns zero in case of success and a negative error code in case
676 * of failure.
677 */
678static int rename_volumes(struct ubi_device *ubi,
679 struct ubi_rnvol_req *req)
680{
681 int i, n, err;
682 struct list_head rename_list;
683 struct ubi_rename_entry *re, *re1;
684
685 if (req->count < 0 || req->count > UBI_MAX_RNVOL)
686 return -EINVAL;
687
688 if (req->count == 0)
689 return 0;
690
691 /* Validate volume IDs and names in the request */
692 for (i = 0; i < req->count; i++) {
693 if (req->ents[i].vol_id < 0 ||
694 req->ents[i].vol_id >= ubi->vtbl_slots)
695 return -EINVAL;
696 if (req->ents[i].name_len < 0)
697 return -EINVAL;
698 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
699 return -ENAMETOOLONG;
700 req->ents[i].name[req->ents[i].name_len] = '\0';
701 n = strlen(req->ents[i].name);
702 if (n != req->ents[i].name_len)
703 err = -EINVAL;
704 }
705
706 /* Make sure volume IDs and names are unique */
707 for (i = 0; i < req->count - 1; i++) {
708 for (n = i + 1; n < req->count; n++) {
709 if (req->ents[i].vol_id == req->ents[n].vol_id) {
710 dbg_err("duplicated volume id %d",
711 req->ents[i].vol_id);
712 return -EINVAL;
713 }
714 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
715 dbg_err("duplicated volume name \"%s\"",
716 req->ents[i].name);
717 return -EINVAL;
718 }
719 }
720 }
721
722 /* Create the re-name list */
723 INIT_LIST_HEAD(&rename_list);
724 for (i = 0; i < req->count; i++) {
725 int vol_id = req->ents[i].vol_id;
726 int name_len = req->ents[i].name_len;
727 const char *name = req->ents[i].name;
728
729 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
730 if (!re) {
731 err = -ENOMEM;
732 goto out_free;
733 }
734
735 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
736 if (IS_ERR(re->desc)) {
737 err = PTR_ERR(re->desc);
738 dbg_err("cannot open volume %d, error %d", vol_id, err);
739 kfree(re);
740 goto out_free;
741 }
742
743 /* Skip this re-naming if the name does not really change */
744 if (re->desc->vol->name_len == name_len &&
745 !memcmp(re->desc->vol->name, name, name_len)) {
746 ubi_close_volume(re->desc);
747 kfree(re);
748 continue;
749 }
750
751 re->new_name_len = name_len;
752 memcpy(re->new_name, name, name_len);
753 list_add_tail(&re->list, &rename_list);
754 dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
755 vol_id, re->desc->vol->name, name);
756 }
757
758 if (list_empty(&rename_list))
759 return 0;
760
761 /* Find out the volumes which have to be removed */
762 list_for_each_entry(re, &rename_list, list) {
763 struct ubi_volume_desc *desc;
764 int no_remove_needed = 0;
765
766 /*
767 * Volume @re->vol_id is going to be re-named to
768 * @re->new_name, while its current name is @name. If a volume
769 * with name @re->new_name currently exists, it has to be
770 * removed, unless it is also re-named in the request (@req).
771 */
772 list_for_each_entry(re1, &rename_list, list) {
773 if (re->new_name_len == re1->desc->vol->name_len &&
774 !memcmp(re->new_name, re1->desc->vol->name,
775 re1->desc->vol->name_len)) {
776 no_remove_needed = 1;
777 break;
778 }
779 }
780
781 if (no_remove_needed)
782 continue;
783
784 /*
785 * It seems we need to remove volume with name @re->new_name,
786 * if it exists.
787 */
Artem Bityutskiyf2863c52008-12-28 12:20:51 +0200788 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
789 UBI_EXCLUSIVE);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300790 if (IS_ERR(desc)) {
791 err = PTR_ERR(desc);
792 if (err == -ENODEV)
793 /* Re-naming into a non-existing volume name */
794 continue;
795
796 /* The volume exists but busy, or an error occurred */
797 dbg_err("cannot open volume \"%s\", error %d",
798 re->new_name, err);
799 goto out_free;
800 }
801
802 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
803 if (!re) {
804 err = -ENOMEM;
805 ubi_close_volume(desc);
806 goto out_free;
807 }
808
809 re->remove = 1;
810 re->desc = desc;
811 list_add(&re->list, &rename_list);
812 dbg_msg("will remove volume %d, name \"%s\"",
813 re->desc->vol->vol_id, re->desc->vol->name);
814 }
815
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300816 mutex_lock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300817 err = ubi_rename_volumes(ubi, &rename_list);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300818 mutex_unlock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300819
820out_free:
821 list_for_each_entry_safe(re, re1, &rename_list, list) {
822 ubi_close_volume(re->desc);
823 list_del(&re->list);
824 kfree(re);
825 }
826 return err;
827}
828
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200829static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
830 unsigned long arg)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400831{
832 int err = 0;
833 struct ubi_device *ubi;
834 struct ubi_volume_desc *desc;
835 void __user *argp = (void __user *)arg;
836
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400837 if (!capable(CAP_SYS_RESOURCE))
838 return -EPERM;
839
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200840 ubi = ubi_get_by_major(imajor(file->f_mapping->host));
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200841 if (!ubi)
842 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400843
844 switch (cmd) {
845 /* Create volume command */
846 case UBI_IOCMKVOL:
847 {
848 struct ubi_mkvol_req req;
849
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300850 dbg_gen("create volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200851 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400852 if (err) {
853 err = -EFAULT;
854 break;
855 }
856
Artem Bityutskiya6ea4402008-07-13 21:46:24 +0300857 req.name[req.name_len] = '\0';
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400858 err = verify_mkvol_req(ubi, &req);
859 if (err)
860 break;
861
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300862 mutex_lock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863 err = ubi_create_volume(ubi, &req);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300864 mutex_unlock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400865 if (err)
866 break;
867
Christoph Hellwigbf078032007-05-17 16:32:10 +0200868 err = put_user(req.vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400869 if (err)
870 err = -EFAULT;
871
872 break;
873 }
874
875 /* Remove volume command */
876 case UBI_IOCRMVOL:
877 {
878 int vol_id;
879
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300880 dbg_gen("remove volume");
Christoph Hellwigbf078032007-05-17 16:32:10 +0200881 err = get_user(vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400882 if (err) {
883 err = -EFAULT;
884 break;
885 }
886
887 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
888 if (IS_ERR(desc)) {
889 err = PTR_ERR(desc);
890 break;
891 }
892
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300893 mutex_lock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300894 err = ubi_remove_volume(desc, 0);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300895 mutex_unlock(&ubi->device_mutex);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200896
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200897 /*
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200898 * The volume is deleted (unless an error occurred), and the
899 * 'struct ubi_volume' object will be freed when
900 * 'ubi_close_volume()' will call 'put_device()'.
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200901 */
902 ubi_close_volume(desc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400903 break;
904 }
905
906 /* Re-size volume command */
907 case UBI_IOCRSVOL:
908 {
909 int pebs;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400910 struct ubi_rsvol_req req;
911
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300912 dbg_gen("re-size volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200913 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400914 if (err) {
915 err = -EFAULT;
916 break;
917 }
918
919 err = verify_rsvol_req(ubi, &req);
920 if (err)
921 break;
922
923 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
924 if (IS_ERR(desc)) {
925 err = PTR_ERR(desc);
926 break;
927 }
928
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200929 pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
930 desc->vol->usable_leb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400931
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300932 mutex_lock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400933 err = ubi_resize_volume(desc, pebs);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300934 mutex_unlock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400935 ubi_close_volume(desc);
936 break;
937 }
938
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300939 /* Re-name volumes command */
940 case UBI_IOCRNVOL:
941 {
942 struct ubi_rnvol_req *req;
943
944 dbg_msg("re-name volumes");
945 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
946 if (!req) {
947 err = -ENOMEM;
948 break;
949 };
950
951 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
952 if (err) {
953 err = -EFAULT;
954 kfree(req);
955 break;
956 }
957
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300958 err = rename_volumes(ubi, req);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300959 kfree(req);
960 break;
961 }
962
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400963 default:
964 err = -ENOTTY;
965 break;
966 }
967
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200968 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400969 return err;
970}
971
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200972static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
973 unsigned long arg)
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200974{
975 int err = 0;
976 void __user *argp = (void __user *)arg;
977
978 if (!capable(CAP_SYS_RESOURCE))
979 return -EPERM;
980
981 switch (cmd) {
982 /* Attach an MTD device command */
983 case UBI_IOCATT:
984 {
985 struct ubi_attach_req req;
986 struct mtd_info *mtd;
987
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300988 dbg_gen("attach MTD device");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200989 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
990 if (err) {
991 err = -EFAULT;
992 break;
993 }
994
995 if (req.mtd_num < 0 ||
996 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
997 err = -EINVAL;
998 break;
999 }
1000
1001 mtd = get_mtd_device(NULL, req.mtd_num);
1002 if (IS_ERR(mtd)) {
1003 err = PTR_ERR(mtd);
1004 break;
1005 }
1006
1007 /*
1008 * Note, further request verification is done by
1009 * 'ubi_attach_mtd_dev()'.
1010 */
1011 mutex_lock(&ubi_devices_mutex);
1012 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
1013 mutex_unlock(&ubi_devices_mutex);
1014 if (err < 0)
1015 put_mtd_device(mtd);
1016 else
1017 /* @err contains UBI device number */
1018 err = put_user(err, (__user int32_t *)argp);
1019
1020 break;
1021 }
1022
1023 /* Detach an MTD device command */
1024 case UBI_IOCDET:
1025 {
1026 int ubi_num;
1027
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001028 dbg_gen("dettach MTD device");
Artem Bityutskiy897a3162007-12-18 18:23:39 +02001029 err = get_user(ubi_num, (__user int32_t *)argp);
1030 if (err) {
1031 err = -EFAULT;
1032 break;
1033 }
1034
1035 mutex_lock(&ubi_devices_mutex);
1036 err = ubi_detach_mtd_dev(ubi_num, 0);
1037 mutex_unlock(&ubi_devices_mutex);
1038 break;
1039 }
1040
1041 default:
1042 err = -ENOTTY;
1043 break;
1044 }
1045
1046 return err;
1047}
1048
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001049#ifdef CONFIG_COMPAT
1050static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1051 unsigned long arg)
1052{
1053 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1054
1055 return vol_cdev_ioctl(file, cmd, translated_arg);
1056}
1057
1058static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1059 unsigned long arg)
1060{
1061 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1062
1063 return ubi_cdev_ioctl(file, cmd, translated_arg);
1064}
1065
1066static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1067 unsigned long arg)
1068{
1069 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1070
1071 return ctrl_cdev_ioctl(file, cmd, translated_arg);
1072}
1073#else
1074#define vol_cdev_compat_ioctl NULL
1075#define ubi_cdev_compat_ioctl NULL
1076#define ctrl_cdev_compat_ioctl NULL
1077#endif
1078
1079/* UBI volume character device operations */
1080const struct file_operations ubi_vol_cdev_operations = {
1081 .owner = THIS_MODULE,
1082 .open = vol_cdev_open,
1083 .release = vol_cdev_release,
1084 .llseek = vol_cdev_llseek,
1085 .read = vol_cdev_read,
1086 .write = vol_cdev_write,
Corentin Chary1b24bc32009-02-05 22:25:52 +01001087 .fsync = vol_cdev_fsync,
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001088 .unlocked_ioctl = vol_cdev_ioctl,
1089 .compat_ioctl = vol_cdev_compat_ioctl,
Artem Bityutskiy9f961b52007-12-16 16:59:31 +02001090};
1091
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001092/* UBI character device operations */
Jan Engelhardt4d187a82009-01-11 23:55:39 +01001093const struct file_operations ubi_cdev_operations = {
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001094 .owner = THIS_MODULE,
1095 .llseek = no_llseek,
1096 .unlocked_ioctl = ubi_cdev_ioctl,
1097 .compat_ioctl = ubi_cdev_compat_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001098};
1099
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001100/* UBI control character device operations */
1101const struct file_operations ubi_ctrl_cdev_operations = {
1102 .owner = THIS_MODULE,
1103 .unlocked_ioctl = ctrl_cdev_ioctl,
1104 .compat_ioctl = ctrl_cdev_compat_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001105};