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> | |
5a0e3ad6 | 40 | #include <linux/slab.h> |
801c135c AB |
41 | #include <linux/ioctl.h> |
42 | #include <linux/capability.h> | |
9c9ec147 | 43 | #include <linux/uaccess.h> |
f429b2ea | 44 | #include <linux/compat.h> |
3013ee31 | 45 | #include <linux/math64.h> |
801c135c | 46 | #include <mtd/ubi-user.h> |
801c135c AB |
47 | #include "ubi.h" |
48 | ||
801c135c AB |
49 | /** |
50 | * get_exclusive - get exclusive access to an UBI volume. | |
51 | * @desc: volume descriptor | |
52 | * | |
53 | * This function changes UBI volume open mode to "exclusive". Returns previous | |
54 | * mode value (positive integer) in case of success and a negative error code | |
55 | * in case of failure. | |
56 | */ | |
57 | static int get_exclusive(struct ubi_volume_desc *desc) | |
58 | { | |
59 | int users, err; | |
60 | struct ubi_volume *vol = desc->vol; | |
61 | ||
62 | spin_lock(&vol->ubi->volumes_lock); | |
63 | users = vol->readers + vol->writers + vol->exclusive; | |
64 | ubi_assert(users > 0); | |
65 | if (users > 1) { | |
66 | dbg_err("%d users for volume %d", users, vol->vol_id); | |
67 | err = -EBUSY; | |
68 | } else { | |
69 | vol->readers = vol->writers = 0; | |
70 | vol->exclusive = 1; | |
71 | err = desc->mode; | |
72 | desc->mode = UBI_EXCLUSIVE; | |
73 | } | |
74 | spin_unlock(&vol->ubi->volumes_lock); | |
75 | ||
76 | return err; | |
77 | } | |
78 | ||
79 | /** | |
80 | * revoke_exclusive - revoke exclusive mode. | |
81 | * @desc: volume descriptor | |
82 | * @mode: new mode to switch to | |
83 | */ | |
84 | static void revoke_exclusive(struct ubi_volume_desc *desc, int mode) | |
85 | { | |
86 | struct ubi_volume *vol = desc->vol; | |
87 | ||
88 | spin_lock(&vol->ubi->volumes_lock); | |
89 | ubi_assert(vol->readers == 0 && vol->writers == 0); | |
90 | ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE); | |
91 | vol->exclusive = 0; | |
92 | if (mode == UBI_READONLY) | |
93 | vol->readers = 1; | |
94 | else if (mode == UBI_READWRITE) | |
95 | vol->writers = 1; | |
96 | else | |
97 | vol->exclusive = 1; | |
98 | spin_unlock(&vol->ubi->volumes_lock); | |
99 | ||
100 | desc->mode = mode; | |
101 | } | |
102 | ||
103 | static int vol_cdev_open(struct inode *inode, struct file *file) | |
104 | { | |
105 | struct ubi_volume_desc *desc; | |
e73f4459 AB |
106 | int vol_id = iminor(inode) - 1, mode, ubi_num; |
107 | ||
108 | ubi_num = ubi_major2num(imajor(inode)); | |
7d200e88 | 109 | if (ubi_num < 0) |
e73f4459 | 110 | return ubi_num; |
801c135c AB |
111 | |
112 | if (file->f_mode & FMODE_WRITE) | |
113 | mode = UBI_READWRITE; | |
114 | else | |
115 | mode = UBI_READONLY; | |
116 | ||
e1cf7e6d AB |
117 | dbg_gen("open device %d, volume %d, mode %d", |
118 | ubi_num, vol_id, mode); | |
801c135c | 119 | |
e73f4459 | 120 | desc = ubi_open_volume(ubi_num, vol_id, mode); |
801c135c AB |
121 | if (IS_ERR(desc)) |
122 | return PTR_ERR(desc); | |
123 | ||
124 | file->private_data = desc; | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static int vol_cdev_release(struct inode *inode, struct file *file) | |
129 | { | |
130 | struct ubi_volume_desc *desc = file->private_data; | |
131 | struct ubi_volume *vol = desc->vol; | |
132 | ||
e1cf7e6d AB |
133 | dbg_gen("release device %d, volume %d, mode %d", |
134 | vol->ubi->ubi_num, vol->vol_id, desc->mode); | |
801c135c AB |
135 | |
136 | if (vol->updating) { | |
137 | ubi_warn("update of volume %d not finished, volume is damaged", | |
138 | vol->vol_id); | |
e653879c | 139 | ubi_assert(!vol->changing_leb); |
801c135c | 140 | vol->updating = 0; |
92ad8f37 | 141 | vfree(vol->upd_buf); |
e653879c | 142 | } else if (vol->changing_leb) { |
c8566350 | 143 | dbg_gen("only %lld of %lld bytes received for atomic LEB change" |
e653879c AB |
144 | " for volume %d:%d, cancel", vol->upd_received, |
145 | vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id); | |
146 | vol->changing_leb = 0; | |
147 | vfree(vol->upd_buf); | |
801c135c AB |
148 | } |
149 | ||
150 | ubi_close_volume(desc); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin) | |
155 | { | |
156 | struct ubi_volume_desc *desc = file->private_data; | |
157 | struct ubi_volume *vol = desc->vol; | |
158 | loff_t new_offset; | |
159 | ||
160 | if (vol->updating) { | |
161 | /* Update is in progress, seeking is prohibited */ | |
162 | dbg_err("updating"); | |
163 | return -EBUSY; | |
164 | } | |
165 | ||
166 | switch (origin) { | |
167 | case 0: /* SEEK_SET */ | |
168 | new_offset = offset; | |
169 | break; | |
170 | case 1: /* SEEK_CUR */ | |
171 | new_offset = file->f_pos + offset; | |
172 | break; | |
173 | case 2: /* SEEK_END */ | |
174 | new_offset = vol->used_bytes + offset; | |
175 | break; | |
176 | default: | |
177 | return -EINVAL; | |
178 | } | |
179 | ||
180 | if (new_offset < 0 || new_offset > vol->used_bytes) { | |
181 | dbg_err("bad seek %lld", new_offset); | |
182 | return -EINVAL; | |
183 | } | |
184 | ||
c8566350 | 185 | dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld", |
801c135c AB |
186 | vol->vol_id, offset, origin, new_offset); |
187 | ||
188 | file->f_pos = new_offset; | |
189 | return new_offset; | |
190 | } | |
191 | ||
1b24bc3a CC |
192 | static int vol_cdev_fsync(struct file *file, struct dentry *dentry, |
193 | int datasync) | |
194 | { | |
195 | struct ubi_volume_desc *desc = file->private_data; | |
196 | struct ubi_device *ubi = desc->vol->ubi; | |
197 | ||
198 | return ubi_sync(ubi->ubi_num); | |
199 | } | |
200 | ||
201 | ||
801c135c AB |
202 | static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count, |
203 | loff_t *offp) | |
204 | { | |
205 | struct ubi_volume_desc *desc = file->private_data; | |
206 | struct ubi_volume *vol = desc->vol; | |
207 | struct ubi_device *ubi = vol->ubi; | |
ae616e1b | 208 | int err, lnum, off, len, tbuf_size; |
801c135c AB |
209 | size_t count_save = count; |
210 | void *tbuf; | |
801c135c | 211 | |
c8566350 | 212 | dbg_gen("read %zd bytes from offset %lld of volume %d", |
ae616e1b | 213 | count, *offp, vol->vol_id); |
801c135c AB |
214 | |
215 | if (vol->updating) { | |
216 | dbg_err("updating"); | |
217 | return -EBUSY; | |
218 | } | |
219 | if (vol->upd_marker) { | |
220 | dbg_err("damaged volume, update marker is set"); | |
221 | return -EBADF; | |
222 | } | |
223 | if (*offp == vol->used_bytes || count == 0) | |
224 | return 0; | |
225 | ||
226 | if (vol->corrupted) | |
c8566350 | 227 | dbg_gen("read from corrupted volume %d", vol->vol_id); |
801c135c AB |
228 | |
229 | if (*offp + count > vol->used_bytes) | |
230 | count_save = count = vol->used_bytes - *offp; | |
231 | ||
232 | tbuf_size = vol->usable_leb_size; | |
233 | if (count < tbuf_size) | |
234 | tbuf_size = ALIGN(count, ubi->min_io_size); | |
92ad8f37 | 235 | tbuf = vmalloc(tbuf_size); |
801c135c AB |
236 | if (!tbuf) |
237 | return -ENOMEM; | |
238 | ||
239 | len = count > tbuf_size ? tbuf_size : count; | |
3013ee31 | 240 | lnum = div_u64_rem(*offp, vol->usable_leb_size, &off); |
801c135c AB |
241 | |
242 | do { | |
243 | cond_resched(); | |
244 | ||
245 | if (off + len >= vol->usable_leb_size) | |
246 | len = vol->usable_leb_size - off; | |
247 | ||
89b96b69 | 248 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
801c135c AB |
249 | if (err) |
250 | break; | |
251 | ||
252 | off += len; | |
253 | if (off == vol->usable_leb_size) { | |
254 | lnum += 1; | |
255 | off -= vol->usable_leb_size; | |
256 | } | |
257 | ||
258 | count -= len; | |
259 | *offp += len; | |
260 | ||
261 | err = copy_to_user(buf, tbuf, len); | |
262 | if (err) { | |
263 | err = -EFAULT; | |
264 | break; | |
265 | } | |
266 | ||
267 | buf += len; | |
268 | len = count > tbuf_size ? tbuf_size : count; | |
269 | } while (count); | |
270 | ||
92ad8f37 | 271 | vfree(tbuf); |
801c135c AB |
272 | return err ? err : count_save - count; |
273 | } | |
274 | ||
801c135c AB |
275 | /* |
276 | * This function allows to directly write to dynamic UBI volumes, without | |
766fb95b | 277 | * issuing the volume update operation. |
801c135c AB |
278 | */ |
279 | static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf, | |
280 | size_t count, loff_t *offp) | |
281 | { | |
282 | struct ubi_volume_desc *desc = file->private_data; | |
283 | struct ubi_volume *vol = desc->vol; | |
284 | struct ubi_device *ubi = vol->ubi; | |
ae616e1b | 285 | int lnum, off, len, tbuf_size, err = 0; |
801c135c AB |
286 | size_t count_save = count; |
287 | char *tbuf; | |
801c135c | 288 | |
766fb95b SA |
289 | if (!vol->direct_writes) |
290 | return -EPERM; | |
291 | ||
c8566350 | 292 | dbg_gen("requested: write %zd bytes to offset %lld of volume %u", |
ae616e1b | 293 | count, *offp, vol->vol_id); |
801c135c AB |
294 | |
295 | if (vol->vol_type == UBI_STATIC_VOLUME) | |
296 | return -EROFS; | |
297 | ||
3013ee31 | 298 | lnum = div_u64_rem(*offp, vol->usable_leb_size, &off); |
cadb40cc | 299 | if (off & (ubi->min_io_size - 1)) { |
801c135c AB |
300 | dbg_err("unaligned position"); |
301 | return -EINVAL; | |
302 | } | |
303 | ||
304 | if (*offp + count > vol->used_bytes) | |
305 | count_save = count = vol->used_bytes - *offp; | |
306 | ||
307 | /* We can write only in fractions of the minimum I/O unit */ | |
cadb40cc | 308 | if (count & (ubi->min_io_size - 1)) { |
801c135c AB |
309 | dbg_err("unaligned write length"); |
310 | return -EINVAL; | |
311 | } | |
312 | ||
313 | tbuf_size = vol->usable_leb_size; | |
314 | if (count < tbuf_size) | |
315 | tbuf_size = ALIGN(count, ubi->min_io_size); | |
92ad8f37 | 316 | tbuf = vmalloc(tbuf_size); |
801c135c AB |
317 | if (!tbuf) |
318 | return -ENOMEM; | |
319 | ||
320 | len = count > tbuf_size ? tbuf_size : count; | |
321 | ||
322 | while (count) { | |
323 | cond_resched(); | |
324 | ||
325 | if (off + len >= vol->usable_leb_size) | |
326 | len = vol->usable_leb_size - off; | |
327 | ||
328 | err = copy_from_user(tbuf, buf, len); | |
329 | if (err) { | |
330 | err = -EFAULT; | |
331 | break; | |
332 | } | |
333 | ||
89b96b69 | 334 | err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len, |
801c135c AB |
335 | UBI_UNKNOWN); |
336 | if (err) | |
337 | break; | |
338 | ||
339 | off += len; | |
340 | if (off == vol->usable_leb_size) { | |
341 | lnum += 1; | |
342 | off -= vol->usable_leb_size; | |
343 | } | |
344 | ||
345 | count -= len; | |
346 | *offp += len; | |
347 | buf += len; | |
348 | len = count > tbuf_size ? tbuf_size : count; | |
349 | } | |
350 | ||
92ad8f37 | 351 | vfree(tbuf); |
801c135c AB |
352 | return err ? err : count_save - count; |
353 | } | |
354 | ||
801c135c AB |
355 | static ssize_t vol_cdev_write(struct file *file, const char __user *buf, |
356 | size_t count, loff_t *offp) | |
357 | { | |
358 | int err = 0; | |
359 | struct ubi_volume_desc *desc = file->private_data; | |
360 | struct ubi_volume *vol = desc->vol; | |
361 | struct ubi_device *ubi = vol->ubi; | |
362 | ||
e653879c | 363 | if (!vol->updating && !vol->changing_leb) |
801c135c AB |
364 | return vol_cdev_direct_write(file, buf, count, offp); |
365 | ||
e653879c AB |
366 | if (vol->updating) |
367 | err = ubi_more_update_data(ubi, vol, buf, count); | |
368 | else | |
369 | err = ubi_more_leb_change_data(ubi, vol, buf, count); | |
370 | ||
801c135c | 371 | if (err < 0) { |
e653879c | 372 | ubi_err("cannot accept more %zd bytes of data, error %d", |
01f7b309 | 373 | count, err); |
801c135c AB |
374 | return err; |
375 | } | |
376 | ||
377 | if (err) { | |
378 | /* | |
e653879c AB |
379 | * The operation is finished, @err contains number of actually |
380 | * written bytes. | |
801c135c AB |
381 | */ |
382 | count = err; | |
383 | ||
e653879c AB |
384 | if (vol->changing_leb) { |
385 | revoke_exclusive(desc, UBI_READWRITE); | |
386 | return count; | |
387 | } | |
388 | ||
801c135c AB |
389 | err = ubi_check_volume(ubi, vol->vol_id); |
390 | if (err < 0) | |
391 | return err; | |
392 | ||
393 | if (err) { | |
394 | ubi_warn("volume %d on UBI device %d is corrupted", | |
395 | vol->vol_id, ubi->ubi_num); | |
396 | vol->corrupted = 1; | |
397 | } | |
398 | vol->checked = 1; | |
0e0ee1cc | 399 | ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED); |
801c135c AB |
400 | revoke_exclusive(desc, UBI_READWRITE); |
401 | } | |
402 | ||
801c135c AB |
403 | return count; |
404 | } | |
405 | ||
f429b2ea AB |
406 | static long vol_cdev_ioctl(struct file *file, unsigned int cmd, |
407 | unsigned long arg) | |
801c135c AB |
408 | { |
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 | ||
801c135c | 415 | switch (cmd) { |
801c135c AB |
416 | /* 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 | ||
73789a3d BL |
437 | rsvd_bytes = (long long)vol->reserved_pebs * |
438 | ubi->leb_size-vol->data_pad; | |
801c135c AB |
439 | 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 | ||
1b68d0ee | 448 | err = ubi_start_update(ubi, vol, bytes); |
801c135c AB |
449 | if (bytes == 0) |
450 | revoke_exclusive(desc, UBI_READWRITE); | |
801c135c AB |
451 | break; |
452 | } | |
453 | ||
e653879c AB |
454 | /* 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 | ||
801c135c AB |
491 | /* Logical eraseblock erasure command */ |
492 | case UBI_IOCEBER: | |
493 | { | |
494 | int32_t lnum; | |
495 | ||
bf07803a | 496 | err = get_user(lnum, (__user int32_t *)argp); |
801c135c AB |
497 | if (err) { |
498 | err = -EFAULT; | |
499 | break; | |
500 | } | |
501 | ||
e653879c AB |
502 | if (desc->mode == UBI_READONLY || |
503 | vol->vol_type == UBI_STATIC_VOLUME) { | |
801c135c AB |
504 | err = -EROFS; |
505 | break; | |
506 | } | |
507 | ||
508 | if (lnum < 0 || lnum >= vol->reserved_pebs) { | |
509 | err = -EINVAL; | |
510 | break; | |
511 | } | |
512 | ||
c8566350 | 513 | dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); |
89b96b69 | 514 | err = ubi_eba_unmap_leb(ubi, vol, lnum); |
801c135c AB |
515 | if (err) |
516 | break; | |
517 | ||
518 | err = ubi_wl_flush(ubi); | |
519 | break; | |
520 | } | |
141e6ebd CC |
521 | |
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 | } | |
c3da23be CC |
535 | |
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 | } | |
a27ce8f5 CC |
549 | |
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 | } | |
801c135c | 563 | |
f089c0b2 | 564 | /* Set volume property command */ |
766fb95b SA |
565 | 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: | |
f089c0b2 | 577 | mutex_lock(&ubi->device_mutex); |
766fb95b | 578 | desc->vol->direct_writes = !!req.value; |
f089c0b2 | 579 | mutex_unlock(&ubi->device_mutex); |
766fb95b SA |
580 | break; |
581 | default: | |
582 | err = -EINVAL; | |
583 | break; | |
584 | } | |
585 | break; | |
586 | } | |
587 | ||
801c135c AB |
588 | default: |
589 | err = -ENOTTY; | |
590 | break; | |
591 | } | |
801c135c AB |
592 | 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 | */ | |
602 | static 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 | ||
cadb40cc | 628 | n = req->alignment & (ubi->min_io_size - 1); |
801c135c AB |
629 | 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 | ||
a6ea4407 AB |
637 | n = strnlen(req->name, req->name_len + 1); |
638 | if (n != req->name_len) | |
639 | goto bad; | |
640 | ||
801c135c AB |
641 | return 0; |
642 | ||
643 | bad: | |
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 | */ | |
656 | static 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 | ||
f40ac9cd AB |
668 | /** |
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 | */ | |
678 | static 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 | */ | |
f2863c54 AB |
788 | desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name, |
789 | UBI_EXCLUSIVE); | |
f40ac9cd AB |
790 | 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 | ||
f089c0b2 | 816 | mutex_lock(&ubi->device_mutex); |
f40ac9cd | 817 | err = ubi_rename_volumes(ubi, &rename_list); |
f089c0b2 | 818 | mutex_unlock(&ubi->device_mutex); |
f40ac9cd AB |
819 | |
820 | out_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 | ||
f429b2ea AB |
829 | static long ubi_cdev_ioctl(struct file *file, unsigned int cmd, |
830 | unsigned long arg) | |
801c135c AB |
831 | { |
832 | int err = 0; | |
833 | struct ubi_device *ubi; | |
834 | struct ubi_volume_desc *desc; | |
835 | void __user *argp = (void __user *)arg; | |
836 | ||
801c135c AB |
837 | if (!capable(CAP_SYS_RESOURCE)) |
838 | return -EPERM; | |
839 | ||
f429b2ea | 840 | ubi = ubi_get_by_major(imajor(file->f_mapping->host)); |
e73f4459 AB |
841 | if (!ubi) |
842 | return -ENODEV; | |
801c135c AB |
843 | |
844 | switch (cmd) { | |
845 | /* Create volume command */ | |
846 | case UBI_IOCMKVOL: | |
847 | { | |
848 | struct ubi_mkvol_req req; | |
849 | ||
c8566350 | 850 | dbg_gen("create volume"); |
897a316c | 851 | err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req)); |
801c135c AB |
852 | if (err) { |
853 | err = -EFAULT; | |
854 | break; | |
855 | } | |
856 | ||
857 | err = verify_mkvol_req(ubi, &req); | |
858 | if (err) | |
859 | break; | |
860 | ||
f089c0b2 | 861 | mutex_lock(&ubi->device_mutex); |
801c135c | 862 | err = ubi_create_volume(ubi, &req); |
f089c0b2 | 863 | mutex_unlock(&ubi->device_mutex); |
801c135c AB |
864 | if (err) |
865 | break; | |
866 | ||
bf07803a | 867 | err = put_user(req.vol_id, (__user int32_t *)argp); |
801c135c AB |
868 | if (err) |
869 | err = -EFAULT; | |
870 | ||
871 | break; | |
872 | } | |
873 | ||
874 | /* Remove volume command */ | |
875 | case UBI_IOCRMVOL: | |
876 | { | |
877 | int vol_id; | |
878 | ||
c8566350 | 879 | dbg_gen("remove volume"); |
bf07803a | 880 | err = get_user(vol_id, (__user int32_t *)argp); |
801c135c AB |
881 | if (err) { |
882 | err = -EFAULT; | |
883 | break; | |
884 | } | |
885 | ||
886 | desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE); | |
887 | if (IS_ERR(desc)) { | |
888 | err = PTR_ERR(desc); | |
889 | break; | |
890 | } | |
891 | ||
f089c0b2 | 892 | mutex_lock(&ubi->device_mutex); |
f40ac9cd | 893 | err = ubi_remove_volume(desc, 0); |
f089c0b2 | 894 | mutex_unlock(&ubi->device_mutex); |
40e4d0c1 | 895 | |
450f872a | 896 | /* |
40e4d0c1 AB |
897 | * The volume is deleted (unless an error occurred), and the |
898 | * 'struct ubi_volume' object will be freed when | |
899 | * 'ubi_close_volume()' will call 'put_device()'. | |
450f872a AB |
900 | */ |
901 | ubi_close_volume(desc); | |
801c135c AB |
902 | break; |
903 | } | |
904 | ||
905 | /* Re-size volume command */ | |
906 | case UBI_IOCRSVOL: | |
907 | { | |
908 | int pebs; | |
801c135c AB |
909 | struct ubi_rsvol_req req; |
910 | ||
c8566350 | 911 | dbg_gen("re-size volume"); |
897a316c | 912 | err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req)); |
801c135c AB |
913 | if (err) { |
914 | err = -EFAULT; | |
915 | break; | |
916 | } | |
917 | ||
918 | err = verify_rsvol_req(ubi, &req); | |
919 | if (err) | |
920 | break; | |
921 | ||
922 | desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE); | |
923 | if (IS_ERR(desc)) { | |
924 | err = PTR_ERR(desc); | |
925 | break; | |
926 | } | |
927 | ||
3013ee31 AB |
928 | pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1, |
929 | desc->vol->usable_leb_size); | |
801c135c | 930 | |
f089c0b2 | 931 | mutex_lock(&ubi->device_mutex); |
801c135c | 932 | err = ubi_resize_volume(desc, pebs); |
f089c0b2 | 933 | mutex_unlock(&ubi->device_mutex); |
801c135c AB |
934 | ubi_close_volume(desc); |
935 | break; | |
936 | } | |
937 | ||
f40ac9cd AB |
938 | /* Re-name volumes command */ |
939 | case UBI_IOCRNVOL: | |
940 | { | |
941 | struct ubi_rnvol_req *req; | |
942 | ||
943 | dbg_msg("re-name volumes"); | |
944 | req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL); | |
945 | if (!req) { | |
946 | err = -ENOMEM; | |
947 | break; | |
948 | }; | |
949 | ||
950 | err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req)); | |
951 | if (err) { | |
952 | err = -EFAULT; | |
953 | kfree(req); | |
954 | break; | |
955 | } | |
956 | ||
f40ac9cd | 957 | err = rename_volumes(ubi, req); |
f40ac9cd AB |
958 | kfree(req); |
959 | break; | |
960 | } | |
961 | ||
801c135c AB |
962 | default: |
963 | err = -ENOTTY; | |
964 | break; | |
965 | } | |
966 | ||
e73f4459 | 967 | ubi_put_device(ubi); |
801c135c AB |
968 | return err; |
969 | } | |
970 | ||
f429b2ea AB |
971 | static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, |
972 | unsigned long arg) | |
897a316c AB |
973 | { |
974 | int err = 0; | |
975 | void __user *argp = (void __user *)arg; | |
976 | ||
977 | if (!capable(CAP_SYS_RESOURCE)) | |
978 | return -EPERM; | |
979 | ||
980 | switch (cmd) { | |
981 | /* Attach an MTD device command */ | |
982 | case UBI_IOCATT: | |
983 | { | |
984 | struct ubi_attach_req req; | |
985 | struct mtd_info *mtd; | |
986 | ||
c8566350 | 987 | dbg_gen("attach MTD device"); |
897a316c AB |
988 | err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req)); |
989 | if (err) { | |
990 | err = -EFAULT; | |
991 | break; | |
992 | } | |
993 | ||
994 | if (req.mtd_num < 0 || | |
995 | (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) { | |
996 | err = -EINVAL; | |
997 | break; | |
998 | } | |
999 | ||
1000 | mtd = get_mtd_device(NULL, req.mtd_num); | |
1001 | if (IS_ERR(mtd)) { | |
1002 | err = PTR_ERR(mtd); | |
1003 | break; | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * Note, further request verification is done by | |
1008 | * 'ubi_attach_mtd_dev()'. | |
1009 | */ | |
1010 | mutex_lock(&ubi_devices_mutex); | |
1011 | err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset); | |
1012 | mutex_unlock(&ubi_devices_mutex); | |
1013 | if (err < 0) | |
1014 | put_mtd_device(mtd); | |
1015 | else | |
1016 | /* @err contains UBI device number */ | |
1017 | err = put_user(err, (__user int32_t *)argp); | |
1018 | ||
1019 | break; | |
1020 | } | |
1021 | ||
1022 | /* Detach an MTD device command */ | |
1023 | case UBI_IOCDET: | |
1024 | { | |
1025 | int ubi_num; | |
1026 | ||
c8566350 | 1027 | dbg_gen("dettach MTD device"); |
897a316c AB |
1028 | err = get_user(ubi_num, (__user int32_t *)argp); |
1029 | if (err) { | |
1030 | err = -EFAULT; | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | mutex_lock(&ubi_devices_mutex); | |
1035 | err = ubi_detach_mtd_dev(ubi_num, 0); | |
1036 | mutex_unlock(&ubi_devices_mutex); | |
1037 | break; | |
1038 | } | |
1039 | ||
1040 | default: | |
1041 | err = -ENOTTY; | |
1042 | break; | |
1043 | } | |
1044 | ||
1045 | return err; | |
1046 | } | |
1047 | ||
f429b2ea AB |
1048 | #ifdef CONFIG_COMPAT |
1049 | static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd, | |
1050 | unsigned long arg) | |
1051 | { | |
1052 | unsigned long translated_arg = (unsigned long)compat_ptr(arg); | |
1053 | ||
1054 | return vol_cdev_ioctl(file, cmd, translated_arg); | |
1055 | } | |
1056 | ||
1057 | static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd, | |
1058 | unsigned long arg) | |
1059 | { | |
1060 | unsigned long translated_arg = (unsigned long)compat_ptr(arg); | |
1061 | ||
1062 | return ubi_cdev_ioctl(file, cmd, translated_arg); | |
1063 | } | |
1064 | ||
1065 | static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd, | |
1066 | unsigned long arg) | |
1067 | { | |
1068 | unsigned long translated_arg = (unsigned long)compat_ptr(arg); | |
1069 | ||
1070 | return ctrl_cdev_ioctl(file, cmd, translated_arg); | |
1071 | } | |
1072 | #else | |
1073 | #define vol_cdev_compat_ioctl NULL | |
1074 | #define ubi_cdev_compat_ioctl NULL | |
1075 | #define ctrl_cdev_compat_ioctl NULL | |
1076 | #endif | |
1077 | ||
1078 | /* UBI volume character device operations */ | |
1079 | const struct file_operations ubi_vol_cdev_operations = { | |
1080 | .owner = THIS_MODULE, | |
1081 | .open = vol_cdev_open, | |
1082 | .release = vol_cdev_release, | |
1083 | .llseek = vol_cdev_llseek, | |
1084 | .read = vol_cdev_read, | |
1085 | .write = vol_cdev_write, | |
1b24bc3a | 1086 | .fsync = vol_cdev_fsync, |
f429b2ea AB |
1087 | .unlocked_ioctl = vol_cdev_ioctl, |
1088 | .compat_ioctl = vol_cdev_compat_ioctl, | |
9f961b57 AB |
1089 | }; |
1090 | ||
801c135c | 1091 | /* UBI character device operations */ |
4d187a88 | 1092 | const struct file_operations ubi_cdev_operations = { |
f429b2ea AB |
1093 | .owner = THIS_MODULE, |
1094 | .llseek = no_llseek, | |
1095 | .unlocked_ioctl = ubi_cdev_ioctl, | |
1096 | .compat_ioctl = ubi_cdev_compat_ioctl, | |
801c135c AB |
1097 | }; |
1098 | ||
f429b2ea AB |
1099 | /* UBI control character device operations */ |
1100 | const struct file_operations ubi_ctrl_cdev_operations = { | |
1101 | .owner = THIS_MODULE, | |
1102 | .unlocked_ioctl = ctrl_cdev_ioctl, | |
1103 | .compat_ioctl = ctrl_cdev_compat_ioctl, | |
801c135c | 1104 | }; |