blob: 1024c106c8999aa2633690b431f40279e5289125 [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 Bityutskiyc8566352008-07-16 17:40:22 +0300116 dbg_gen("open volume %d, mode %d", vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400117
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200118 desc = ubi_open_volume(ubi_num, vol_id, mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119 if (IS_ERR(desc))
120 return PTR_ERR(desc);
121
122 file->private_data = desc;
123 return 0;
124}
125
126static int vol_cdev_release(struct inode *inode, struct file *file)
127{
128 struct ubi_volume_desc *desc = file->private_data;
129 struct ubi_volume *vol = desc->vol;
130
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300131 dbg_gen("release volume %d, mode %d", vol->vol_id, desc->mode);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400132
133 if (vol->updating) {
134 ubi_warn("update of volume %d not finished, volume is damaged",
135 vol->vol_id);
Artem Bityutskiye6538792008-01-24 18:48:21 +0200136 ubi_assert(!vol->changing_leb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400137 vol->updating = 0;
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300138 vfree(vol->upd_buf);
Artem Bityutskiye6538792008-01-24 18:48:21 +0200139 } else if (vol->changing_leb) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300140 dbg_gen("only %lld of %lld bytes received for atomic LEB change"
Artem Bityutskiye6538792008-01-24 18:48:21 +0200141 " for volume %d:%d, cancel", vol->upd_received,
142 vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
143 vol->changing_leb = 0;
144 vfree(vol->upd_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400145 }
146
147 ubi_close_volume(desc);
148 return 0;
149}
150
151static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
152{
153 struct ubi_volume_desc *desc = file->private_data;
154 struct ubi_volume *vol = desc->vol;
155 loff_t new_offset;
156
157 if (vol->updating) {
158 /* Update is in progress, seeking is prohibited */
159 dbg_err("updating");
160 return -EBUSY;
161 }
162
163 switch (origin) {
164 case 0: /* SEEK_SET */
165 new_offset = offset;
166 break;
167 case 1: /* SEEK_CUR */
168 new_offset = file->f_pos + offset;
169 break;
170 case 2: /* SEEK_END */
171 new_offset = vol->used_bytes + offset;
172 break;
173 default:
174 return -EINVAL;
175 }
176
177 if (new_offset < 0 || new_offset > vol->used_bytes) {
178 dbg_err("bad seek %lld", new_offset);
179 return -EINVAL;
180 }
181
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300182 dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400183 vol->vol_id, offset, origin, new_offset);
184
185 file->f_pos = new_offset;
186 return new_offset;
187}
188
Corentin Chary1b24bc32009-02-05 22:25:52 +0100189static int vol_cdev_fsync(struct file *file, struct dentry *dentry,
190 int datasync)
191{
192 struct ubi_volume_desc *desc = file->private_data;
193 struct ubi_device *ubi = desc->vol->ubi;
194
195 return ubi_sync(ubi->ubi_num);
196}
197
198
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400199static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
200 loff_t *offp)
201{
202 struct ubi_volume_desc *desc = file->private_data;
203 struct ubi_volume *vol = desc->vol;
204 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200205 int err, lnum, off, len, tbuf_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400206 size_t count_save = count;
207 void *tbuf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300209 dbg_gen("read %zd bytes from offset %lld of volume %d",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200210 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400211
212 if (vol->updating) {
213 dbg_err("updating");
214 return -EBUSY;
215 }
216 if (vol->upd_marker) {
217 dbg_err("damaged volume, update marker is set");
218 return -EBADF;
219 }
220 if (*offp == vol->used_bytes || count == 0)
221 return 0;
222
223 if (vol->corrupted)
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300224 dbg_gen("read from corrupted volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400225
226 if (*offp + count > vol->used_bytes)
227 count_save = count = vol->used_bytes - *offp;
228
229 tbuf_size = vol->usable_leb_size;
230 if (count < tbuf_size)
231 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300232 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400233 if (!tbuf)
234 return -ENOMEM;
235
236 len = count > tbuf_size ? tbuf_size : count;
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200237 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400238
239 do {
240 cond_resched();
241
242 if (off + len >= vol->usable_leb_size)
243 len = vol->usable_leb_size - off;
244
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200245 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400246 if (err)
247 break;
248
249 off += len;
250 if (off == vol->usable_leb_size) {
251 lnum += 1;
252 off -= vol->usable_leb_size;
253 }
254
255 count -= len;
256 *offp += len;
257
258 err = copy_to_user(buf, tbuf, len);
259 if (err) {
260 err = -EFAULT;
261 break;
262 }
263
264 buf += len;
265 len = count > tbuf_size ? tbuf_size : count;
266 } while (count);
267
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300268 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400269 return err ? err : count_save - count;
270}
271
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272/*
273 * This function allows to directly write to dynamic UBI volumes, without
Sidney Amani766fb952009-01-27 10:11:46 +0100274 * issuing the volume update operation.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400275 */
276static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
277 size_t count, loff_t *offp)
278{
279 struct ubi_volume_desc *desc = file->private_data;
280 struct ubi_volume *vol = desc->vol;
281 struct ubi_device *ubi = vol->ubi;
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200282 int lnum, off, len, tbuf_size, err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400283 size_t count_save = count;
284 char *tbuf;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400285
Sidney Amani766fb952009-01-27 10:11:46 +0100286 if (!vol->direct_writes)
287 return -EPERM;
288
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300289 dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
Artem Bityutskiyae616e12008-01-16 12:15:47 +0200290 count, *offp, vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291
292 if (vol->vol_type == UBI_STATIC_VOLUME)
293 return -EROFS;
294
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200295 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900296 if (off & (ubi->min_io_size - 1)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400297 dbg_err("unaligned position");
298 return -EINVAL;
299 }
300
301 if (*offp + count > vol->used_bytes)
302 count_save = count = vol->used_bytes - *offp;
303
304 /* We can write only in fractions of the minimum I/O unit */
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900305 if (count & (ubi->min_io_size - 1)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400306 dbg_err("unaligned write length");
307 return -EINVAL;
308 }
309
310 tbuf_size = vol->usable_leb_size;
311 if (count < tbuf_size)
312 tbuf_size = ALIGN(count, ubi->min_io_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300313 tbuf = vmalloc(tbuf_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400314 if (!tbuf)
315 return -ENOMEM;
316
317 len = count > tbuf_size ? tbuf_size : count;
318
319 while (count) {
320 cond_resched();
321
322 if (off + len >= vol->usable_leb_size)
323 len = vol->usable_leb_size - off;
324
325 err = copy_from_user(tbuf, buf, len);
326 if (err) {
327 err = -EFAULT;
328 break;
329 }
330
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200331 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332 UBI_UNKNOWN);
333 if (err)
334 break;
335
336 off += len;
337 if (off == vol->usable_leb_size) {
338 lnum += 1;
339 off -= vol->usable_leb_size;
340 }
341
342 count -= len;
343 *offp += len;
344 buf += len;
345 len = count > tbuf_size ? tbuf_size : count;
346 }
347
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300348 vfree(tbuf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400349 return err ? err : count_save - count;
350}
351
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400352static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
353 size_t count, loff_t *offp)
354{
355 int err = 0;
356 struct ubi_volume_desc *desc = file->private_data;
357 struct ubi_volume *vol = desc->vol;
358 struct ubi_device *ubi = vol->ubi;
359
Artem Bityutskiye6538792008-01-24 18:48:21 +0200360 if (!vol->updating && !vol->changing_leb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361 return vol_cdev_direct_write(file, buf, count, offp);
362
Artem Bityutskiye6538792008-01-24 18:48:21 +0200363 if (vol->updating)
364 err = ubi_more_update_data(ubi, vol, buf, count);
365 else
366 err = ubi_more_leb_change_data(ubi, vol, buf, count);
367
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400368 if (err < 0) {
Artem Bityutskiye6538792008-01-24 18:48:21 +0200369 ubi_err("cannot accept more %zd bytes of data, error %d",
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200370 count, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400371 return err;
372 }
373
374 if (err) {
375 /*
Artem Bityutskiye6538792008-01-24 18:48:21 +0200376 * The operation is finished, @err contains number of actually
377 * written bytes.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400378 */
379 count = err;
380
Artem Bityutskiye6538792008-01-24 18:48:21 +0200381 if (vol->changing_leb) {
382 revoke_exclusive(desc, UBI_READWRITE);
383 return count;
384 }
385
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386 err = ubi_check_volume(ubi, vol->vol_id);
387 if (err < 0)
388 return err;
389
390 if (err) {
391 ubi_warn("volume %d on UBI device %d is corrupted",
392 vol->vol_id, ubi->ubi_num);
393 vol->corrupted = 1;
394 }
395 vol->checked = 1;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300396 ubi_gluebi_updated(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400397 revoke_exclusive(desc, UBI_READWRITE);
398 }
399
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400400 return count;
401}
402
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200403static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
404 unsigned long arg)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400405{
406 int err = 0;
407 struct ubi_volume_desc *desc = file->private_data;
408 struct ubi_volume *vol = desc->vol;
409 struct ubi_device *ubi = vol->ubi;
410 void __user *argp = (void __user *)arg;
411
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400412 switch (cmd) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400413 /* Volume update command */
414 case UBI_IOCVOLUP:
415 {
416 int64_t bytes, rsvd_bytes;
417
418 if (!capable(CAP_SYS_RESOURCE)) {
419 err = -EPERM;
420 break;
421 }
422
423 err = copy_from_user(&bytes, argp, sizeof(int64_t));
424 if (err) {
425 err = -EFAULT;
426 break;
427 }
428
429 if (desc->mode == UBI_READONLY) {
430 err = -EROFS;
431 break;
432 }
433
Bruce Leonard73789a32008-07-03 10:35:49 +0300434 rsvd_bytes = (long long)vol->reserved_pebs *
435 ubi->leb_size-vol->data_pad;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436 if (bytes < 0 || bytes > rsvd_bytes) {
437 err = -EINVAL;
438 break;
439 }
440
441 err = get_exclusive(desc);
442 if (err < 0)
443 break;
444
Artem Bityutskiy1b68d0e2008-01-24 17:04:01 +0200445 err = ubi_start_update(ubi, vol, bytes);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400446 if (bytes == 0)
447 revoke_exclusive(desc, UBI_READWRITE);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400448 break;
449 }
450
Artem Bityutskiye6538792008-01-24 18:48:21 +0200451 /* Atomic logical eraseblock change command */
452 case UBI_IOCEBCH:
453 {
454 struct ubi_leb_change_req req;
455
456 err = copy_from_user(&req, argp,
457 sizeof(struct ubi_leb_change_req));
458 if (err) {
459 err = -EFAULT;
460 break;
461 }
462
463 if (desc->mode == UBI_READONLY ||
464 vol->vol_type == UBI_STATIC_VOLUME) {
465 err = -EROFS;
466 break;
467 }
468
469 /* Validate the request */
470 err = -EINVAL;
471 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
472 req.bytes < 0 || req.lnum >= vol->usable_leb_size)
473 break;
474 if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
475 req.dtype != UBI_UNKNOWN)
476 break;
477
478 err = get_exclusive(desc);
479 if (err < 0)
480 break;
481
482 err = ubi_start_leb_change(ubi, vol, &req);
483 if (req.bytes == 0)
484 revoke_exclusive(desc, UBI_READWRITE);
485 break;
486 }
487
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 /* Logical eraseblock erasure command */
489 case UBI_IOCEBER:
490 {
491 int32_t lnum;
492
Christoph Hellwigbf078032007-05-17 16:32:10 +0200493 err = get_user(lnum, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400494 if (err) {
495 err = -EFAULT;
496 break;
497 }
498
Artem Bityutskiye6538792008-01-24 18:48:21 +0200499 if (desc->mode == UBI_READONLY ||
500 vol->vol_type == UBI_STATIC_VOLUME) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400501 err = -EROFS;
502 break;
503 }
504
505 if (lnum < 0 || lnum >= vol->reserved_pebs) {
506 err = -EINVAL;
507 break;
508 }
509
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300510 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200511 err = ubi_eba_unmap_leb(ubi, vol, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400512 if (err)
513 break;
514
515 err = ubi_wl_flush(ubi);
516 break;
517 }
Corentin Chary141e6eb2009-01-05 14:44:11 +0100518
519 /* Logical eraseblock map command */
520 case UBI_IOCEBMAP:
521 {
522 struct ubi_map_req req;
523
524 err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
525 if (err) {
526 err = -EFAULT;
527 break;
528 }
529 err = ubi_leb_map(desc, req.lnum, req.dtype);
530 break;
531 }
Corentin Charyc3da23b2009-01-05 14:46:19 +0100532
533 /* Logical eraseblock un-map command */
534 case UBI_IOCEBUNMAP:
535 {
536 int32_t lnum;
537
538 err = get_user(lnum, (__user int32_t *)argp);
539 if (err) {
540 err = -EFAULT;
541 break;
542 }
543 err = ubi_leb_unmap(desc, lnum);
544 break;
545 }
Corentin Charya27ce8f2009-01-05 14:48:59 +0100546
547 /* Check if logical eraseblock is mapped command */
548 case UBI_IOCEBISMAP:
549 {
550 int32_t lnum;
551
552 err = get_user(lnum, (__user int32_t *)argp);
553 if (err) {
554 err = -EFAULT;
555 break;
556 }
557 err = ubi_is_mapped(desc, lnum);
558 break;
559 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300561 /* Set volume property command */
Sidney Amani766fb952009-01-27 10:11:46 +0100562 case UBI_IOCSETPROP:
563 {
564 struct ubi_set_prop_req req;
565
566 err = copy_from_user(&req, argp,
567 sizeof(struct ubi_set_prop_req));
568 if (err) {
569 err = -EFAULT;
570 break;
571 }
572 switch (req.property) {
573 case UBI_PROP_DIRECT_WRITE:
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300574 mutex_lock(&ubi->device_mutex);
Sidney Amani766fb952009-01-27 10:11:46 +0100575 desc->vol->direct_writes = !!req.value;
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300576 mutex_unlock(&ubi->device_mutex);
Sidney Amani766fb952009-01-27 10:11:46 +0100577 break;
578 default:
579 err = -EINVAL;
580 break;
581 }
582 break;
583 }
584
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400585 default:
586 err = -ENOTTY;
587 break;
588 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400589 return err;
590}
591
592/**
593 * verify_mkvol_req - verify volume creation request.
594 * @ubi: UBI device description object
595 * @req: the request to check
596 *
597 * This function zero if the request is correct, and %-EINVAL if not.
598 */
599static int verify_mkvol_req(const struct ubi_device *ubi,
600 const struct ubi_mkvol_req *req)
601{
602 int n, err = -EINVAL;
603
604 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
605 req->name_len < 0)
606 goto bad;
607
608 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
609 req->vol_id != UBI_VOL_NUM_AUTO)
610 goto bad;
611
612 if (req->alignment == 0)
613 goto bad;
614
615 if (req->bytes == 0)
616 goto bad;
617
618 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
619 req->vol_type != UBI_STATIC_VOLUME)
620 goto bad;
621
622 if (req->alignment > ubi->leb_size)
623 goto bad;
624
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900625 n = req->alignment & (ubi->min_io_size - 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400626 if (req->alignment != 1 && n)
627 goto bad;
628
629 if (req->name_len > UBI_VOL_NAME_MAX) {
630 err = -ENAMETOOLONG;
631 goto bad;
632 }
633
Artem Bityutskiya6ea4402008-07-13 21:46:24 +0300634 n = strnlen(req->name, req->name_len + 1);
635 if (n != req->name_len)
636 goto bad;
637
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400638 return 0;
639
640bad:
641 dbg_err("bad volume creation request");
642 ubi_dbg_dump_mkvol_req(req);
643 return err;
644}
645
646/**
647 * verify_rsvol_req - verify volume re-size request.
648 * @ubi: UBI device description object
649 * @req: the request to check
650 *
651 * This function returns zero if the request is correct, and %-EINVAL if not.
652 */
653static int verify_rsvol_req(const struct ubi_device *ubi,
654 const struct ubi_rsvol_req *req)
655{
656 if (req->bytes <= 0)
657 return -EINVAL;
658
659 if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
660 return -EINVAL;
661
662 return 0;
663}
664
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300665/**
666 * rename_volumes - rename UBI volumes.
667 * @ubi: UBI device description object
668 * @req: volumes re-name request
669 *
670 * This is a helper function for the volume re-name IOCTL which validates the
671 * the request, opens the volume and calls corresponding volumes management
672 * function. Returns zero in case of success and a negative error code in case
673 * of failure.
674 */
675static int rename_volumes(struct ubi_device *ubi,
676 struct ubi_rnvol_req *req)
677{
678 int i, n, err;
679 struct list_head rename_list;
680 struct ubi_rename_entry *re, *re1;
681
682 if (req->count < 0 || req->count > UBI_MAX_RNVOL)
683 return -EINVAL;
684
685 if (req->count == 0)
686 return 0;
687
688 /* Validate volume IDs and names in the request */
689 for (i = 0; i < req->count; i++) {
690 if (req->ents[i].vol_id < 0 ||
691 req->ents[i].vol_id >= ubi->vtbl_slots)
692 return -EINVAL;
693 if (req->ents[i].name_len < 0)
694 return -EINVAL;
695 if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
696 return -ENAMETOOLONG;
697 req->ents[i].name[req->ents[i].name_len] = '\0';
698 n = strlen(req->ents[i].name);
699 if (n != req->ents[i].name_len)
700 err = -EINVAL;
701 }
702
703 /* Make sure volume IDs and names are unique */
704 for (i = 0; i < req->count - 1; i++) {
705 for (n = i + 1; n < req->count; n++) {
706 if (req->ents[i].vol_id == req->ents[n].vol_id) {
707 dbg_err("duplicated volume id %d",
708 req->ents[i].vol_id);
709 return -EINVAL;
710 }
711 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
712 dbg_err("duplicated volume name \"%s\"",
713 req->ents[i].name);
714 return -EINVAL;
715 }
716 }
717 }
718
719 /* Create the re-name list */
720 INIT_LIST_HEAD(&rename_list);
721 for (i = 0; i < req->count; i++) {
722 int vol_id = req->ents[i].vol_id;
723 int name_len = req->ents[i].name_len;
724 const char *name = req->ents[i].name;
725
726 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
727 if (!re) {
728 err = -ENOMEM;
729 goto out_free;
730 }
731
732 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
733 if (IS_ERR(re->desc)) {
734 err = PTR_ERR(re->desc);
735 dbg_err("cannot open volume %d, error %d", vol_id, err);
736 kfree(re);
737 goto out_free;
738 }
739
740 /* Skip this re-naming if the name does not really change */
741 if (re->desc->vol->name_len == name_len &&
742 !memcmp(re->desc->vol->name, name, name_len)) {
743 ubi_close_volume(re->desc);
744 kfree(re);
745 continue;
746 }
747
748 re->new_name_len = name_len;
749 memcpy(re->new_name, name, name_len);
750 list_add_tail(&re->list, &rename_list);
751 dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
752 vol_id, re->desc->vol->name, name);
753 }
754
755 if (list_empty(&rename_list))
756 return 0;
757
758 /* Find out the volumes which have to be removed */
759 list_for_each_entry(re, &rename_list, list) {
760 struct ubi_volume_desc *desc;
761 int no_remove_needed = 0;
762
763 /*
764 * Volume @re->vol_id is going to be re-named to
765 * @re->new_name, while its current name is @name. If a volume
766 * with name @re->new_name currently exists, it has to be
767 * removed, unless it is also re-named in the request (@req).
768 */
769 list_for_each_entry(re1, &rename_list, list) {
770 if (re->new_name_len == re1->desc->vol->name_len &&
771 !memcmp(re->new_name, re1->desc->vol->name,
772 re1->desc->vol->name_len)) {
773 no_remove_needed = 1;
774 break;
775 }
776 }
777
778 if (no_remove_needed)
779 continue;
780
781 /*
782 * It seems we need to remove volume with name @re->new_name,
783 * if it exists.
784 */
Artem Bityutskiyf2863c52008-12-28 12:20:51 +0200785 desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
786 UBI_EXCLUSIVE);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300787 if (IS_ERR(desc)) {
788 err = PTR_ERR(desc);
789 if (err == -ENODEV)
790 /* Re-naming into a non-existing volume name */
791 continue;
792
793 /* The volume exists but busy, or an error occurred */
794 dbg_err("cannot open volume \"%s\", error %d",
795 re->new_name, err);
796 goto out_free;
797 }
798
799 re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
800 if (!re) {
801 err = -ENOMEM;
802 ubi_close_volume(desc);
803 goto out_free;
804 }
805
806 re->remove = 1;
807 re->desc = desc;
808 list_add(&re->list, &rename_list);
809 dbg_msg("will remove volume %d, name \"%s\"",
810 re->desc->vol->vol_id, re->desc->vol->name);
811 }
812
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300813 mutex_lock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300814 err = ubi_rename_volumes(ubi, &rename_list);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300815 mutex_unlock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300816
817out_free:
818 list_for_each_entry_safe(re, re1, &rename_list, list) {
819 ubi_close_volume(re->desc);
820 list_del(&re->list);
821 kfree(re);
822 }
823 return err;
824}
825
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200826static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
827 unsigned long arg)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400828{
829 int err = 0;
830 struct ubi_device *ubi;
831 struct ubi_volume_desc *desc;
832 void __user *argp = (void __user *)arg;
833
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400834 if (!capable(CAP_SYS_RESOURCE))
835 return -EPERM;
836
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200837 ubi = ubi_get_by_major(imajor(file->f_mapping->host));
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200838 if (!ubi)
839 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400840
841 switch (cmd) {
842 /* Create volume command */
843 case UBI_IOCMKVOL:
844 {
845 struct ubi_mkvol_req req;
846
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300847 dbg_gen("create volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200848 err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400849 if (err) {
850 err = -EFAULT;
851 break;
852 }
853
Artem Bityutskiya6ea4402008-07-13 21:46:24 +0300854 req.name[req.name_len] = '\0';
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400855 err = verify_mkvol_req(ubi, &req);
856 if (err)
857 break;
858
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300859 mutex_lock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400860 err = ubi_create_volume(ubi, &req);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300861 mutex_unlock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400862 if (err)
863 break;
864
Christoph Hellwigbf078032007-05-17 16:32:10 +0200865 err = put_user(req.vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400866 if (err)
867 err = -EFAULT;
868
869 break;
870 }
871
872 /* Remove volume command */
873 case UBI_IOCRMVOL:
874 {
875 int vol_id;
876
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300877 dbg_gen("remove volume");
Christoph Hellwigbf078032007-05-17 16:32:10 +0200878 err = get_user(vol_id, (__user int32_t *)argp);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400879 if (err) {
880 err = -EFAULT;
881 break;
882 }
883
884 desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
885 if (IS_ERR(desc)) {
886 err = PTR_ERR(desc);
887 break;
888 }
889
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300890 mutex_lock(&ubi->device_mutex);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300891 err = ubi_remove_volume(desc, 0);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300892 mutex_unlock(&ubi->device_mutex);
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200893
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200894 /*
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200895 * The volume is deleted (unless an error occurred), and the
896 * 'struct ubi_volume' object will be freed when
897 * 'ubi_close_volume()' will call 'put_device()'.
Artem Bityutskiy450f8722007-12-17 13:09:09 +0200898 */
899 ubi_close_volume(desc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400900 break;
901 }
902
903 /* Re-size volume command */
904 case UBI_IOCRSVOL:
905 {
906 int pebs;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400907 struct ubi_rsvol_req req;
908
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300909 dbg_gen("re-size volume");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200910 err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400911 if (err) {
912 err = -EFAULT;
913 break;
914 }
915
916 err = verify_rsvol_req(ubi, &req);
917 if (err)
918 break;
919
920 desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
921 if (IS_ERR(desc)) {
922 err = PTR_ERR(desc);
923 break;
924 }
925
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200926 pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
927 desc->vol->usable_leb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400928
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300929 mutex_lock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400930 err = ubi_resize_volume(desc, pebs);
Artem Bityutskiyf089c0b2009-05-07 11:46:49 +0300931 mutex_unlock(&ubi->device_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400932 ubi_close_volume(desc);
933 break;
934 }
935
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300936 /* Re-name volumes command */
937 case UBI_IOCRNVOL:
938 {
939 struct ubi_rnvol_req *req;
940
941 dbg_msg("re-name volumes");
942 req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
943 if (!req) {
944 err = -ENOMEM;
945 break;
946 };
947
948 err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
949 if (err) {
950 err = -EFAULT;
951 kfree(req);
952 break;
953 }
954
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300955 err = rename_volumes(ubi, req);
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300956 kfree(req);
957 break;
958 }
959
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400960 default:
961 err = -ENOTTY;
962 break;
963 }
964
Artem Bityutskiye73f4452007-12-17 17:37:26 +0200965 ubi_put_device(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400966 return err;
967}
968
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +0200969static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
970 unsigned long arg)
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200971{
972 int err = 0;
973 void __user *argp = (void __user *)arg;
974
975 if (!capable(CAP_SYS_RESOURCE))
976 return -EPERM;
977
978 switch (cmd) {
979 /* Attach an MTD device command */
980 case UBI_IOCATT:
981 {
982 struct ubi_attach_req req;
983 struct mtd_info *mtd;
984
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300985 dbg_gen("attach MTD device");
Artem Bityutskiy897a3162007-12-18 18:23:39 +0200986 err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
987 if (err) {
988 err = -EFAULT;
989 break;
990 }
991
992 if (req.mtd_num < 0 ||
993 (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
994 err = -EINVAL;
995 break;
996 }
997
998 mtd = get_mtd_device(NULL, req.mtd_num);
999 if (IS_ERR(mtd)) {
1000 err = PTR_ERR(mtd);
1001 break;
1002 }
1003
1004 /*
1005 * Note, further request verification is done by
1006 * 'ubi_attach_mtd_dev()'.
1007 */
1008 mutex_lock(&ubi_devices_mutex);
1009 err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
1010 mutex_unlock(&ubi_devices_mutex);
1011 if (err < 0)
1012 put_mtd_device(mtd);
1013 else
1014 /* @err contains UBI device number */
1015 err = put_user(err, (__user int32_t *)argp);
1016
1017 break;
1018 }
1019
1020 /* Detach an MTD device command */
1021 case UBI_IOCDET:
1022 {
1023 int ubi_num;
1024
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001025 dbg_gen("dettach MTD device");
Artem Bityutskiy897a3162007-12-18 18:23:39 +02001026 err = get_user(ubi_num, (__user int32_t *)argp);
1027 if (err) {
1028 err = -EFAULT;
1029 break;
1030 }
1031
1032 mutex_lock(&ubi_devices_mutex);
1033 err = ubi_detach_mtd_dev(ubi_num, 0);
1034 mutex_unlock(&ubi_devices_mutex);
1035 break;
1036 }
1037
1038 default:
1039 err = -ENOTTY;
1040 break;
1041 }
1042
1043 return err;
1044}
1045
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001046#ifdef CONFIG_COMPAT
1047static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1048 unsigned long arg)
1049{
1050 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1051
1052 return vol_cdev_ioctl(file, cmd, translated_arg);
1053}
1054
1055static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1056 unsigned long arg)
1057{
1058 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1059
1060 return ubi_cdev_ioctl(file, cmd, translated_arg);
1061}
1062
1063static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
1064 unsigned long arg)
1065{
1066 unsigned long translated_arg = (unsigned long)compat_ptr(arg);
1067
1068 return ctrl_cdev_ioctl(file, cmd, translated_arg);
1069}
1070#else
1071#define vol_cdev_compat_ioctl NULL
1072#define ubi_cdev_compat_ioctl NULL
1073#define ctrl_cdev_compat_ioctl NULL
1074#endif
1075
1076/* UBI volume character device operations */
1077const struct file_operations ubi_vol_cdev_operations = {
1078 .owner = THIS_MODULE,
1079 .open = vol_cdev_open,
1080 .release = vol_cdev_release,
1081 .llseek = vol_cdev_llseek,
1082 .read = vol_cdev_read,
1083 .write = vol_cdev_write,
Corentin Chary1b24bc32009-02-05 22:25:52 +01001084 .fsync = vol_cdev_fsync,
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001085 .unlocked_ioctl = vol_cdev_ioctl,
1086 .compat_ioctl = vol_cdev_compat_ioctl,
Artem Bityutskiy9f961b52007-12-16 16:59:31 +02001087};
1088
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001089/* UBI character device operations */
Jan Engelhardt4d187a82009-01-11 23:55:39 +01001090const struct file_operations ubi_cdev_operations = {
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001091 .owner = THIS_MODULE,
1092 .llseek = no_llseek,
1093 .unlocked_ioctl = ubi_cdev_ioctl,
1094 .compat_ioctl = ubi_cdev_compat_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001095};
1096
Artem Bityutskiyf429b2e2009-01-16 18:06:55 +02001097/* UBI control character device operations */
1098const struct file_operations ubi_ctrl_cdev_operations = {
1099 .owner = THIS_MODULE,
1100 .unlocked_ioctl = ctrl_cdev_ioctl,
1101 .compat_ioctl = ctrl_cdev_compat_ioctl,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001102};