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