]>
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> |
f429b2ea | 43 | #include <linux/compat.h> |
3013ee31 | 44 | #include <linux/math64.h> |
801c135c | 45 | #include <mtd/ubi-user.h> |
801c135c AB |
46 | #include "ubi.h" |
47 | ||
801c135c AB |
48 | /** |
49 | * 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 | */ | |
56 | static 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 | */ | |
83 | static 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 | ||
102 | static int vol_cdev_open(struct inode *inode, struct file *file) | |
103 | { | |
104 | struct ubi_volume_desc *desc; | |
e73f4459 AB |
105 | int vol_id = iminor(inode) - 1, mode, ubi_num; |
106 | ||
107 | ubi_num = ubi_major2num(imajor(inode)); | |
7d200e88 | 108 | if (ubi_num < 0) |
e73f4459 | 109 | return ubi_num; |
801c135c AB |
110 | |
111 | if (file->f_mode & FMODE_WRITE) | |
112 | mode = UBI_READWRITE; | |
113 | else | |
114 | mode = UBI_READONLY; | |
115 | ||
c8566350 | 116 | dbg_gen("open volume %d, mode %d", vol_id, mode); |
801c135c | 117 | |
e73f4459 | 118 | desc = ubi_open_volume(ubi_num, vol_id, mode); |
801c135c AB |
119 | if (IS_ERR(desc)) |
120 | return PTR_ERR(desc); | |
121 | ||
122 | file->private_data = desc; | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static 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 | ||
c8566350 | 131 | dbg_gen("release volume %d, mode %d", vol->vol_id, desc->mode); |
801c135c AB |
132 | |
133 | if (vol->updating) { | |
134 | ubi_warn("update of volume %d not finished, volume is damaged", | |
135 | vol->vol_id); | |
e653879c | 136 | ubi_assert(!vol->changing_leb); |
801c135c | 137 | vol->updating = 0; |
92ad8f37 | 138 | vfree(vol->upd_buf); |
e653879c | 139 | } else if (vol->changing_leb) { |
c8566350 | 140 | dbg_gen("only %lld of %lld bytes received for atomic LEB change" |
e653879c AB |
141 | " 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); | |
801c135c AB |
145 | } |
146 | ||
147 | ubi_close_volume(desc); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static 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 | ||
c8566350 | 182 | dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld", |
801c135c AB |
183 | vol->vol_id, offset, origin, new_offset); |
184 | ||
185 | file->f_pos = new_offset; | |
186 | return new_offset; | |
187 | } | |
188 | ||
1b24bc3a CC |
189 | static 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 | ||
801c135c AB |
199 | static 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; | |
ae616e1b | 205 | int err, lnum, off, len, tbuf_size; |
801c135c AB |
206 | size_t count_save = count; |
207 | void *tbuf; | |
801c135c | 208 | |
c8566350 | 209 | dbg_gen("read %zd bytes from offset %lld of volume %d", |
ae616e1b | 210 | count, *offp, vol->vol_id); |
801c135c AB |
211 | |
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) | |
c8566350 | 224 | dbg_gen("read from corrupted volume %d", vol->vol_id); |
801c135c AB |
225 | |
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); | |
92ad8f37 | 232 | tbuf = vmalloc(tbuf_size); |
801c135c AB |
233 | if (!tbuf) |
234 | return -ENOMEM; | |
235 | ||
236 | len = count > tbuf_size ? tbuf_size : count; | |
3013ee31 | 237 | lnum = div_u64_rem(*offp, vol->usable_leb_size, &off); |
801c135c AB |
238 | |
239 | do { | |
240 | cond_resched(); | |
241 | ||
242 | if (off + len >= vol->usable_leb_size) | |
243 | len = vol->usable_leb_size - off; | |
244 | ||
89b96b69 | 245 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
801c135c AB |
246 | 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 | ||
92ad8f37 | 268 | vfree(tbuf); |
801c135c AB |
269 | return err ? err : count_save - count; |
270 | } | |
271 | ||
801c135c AB |
272 | /* |
273 | * This function allows to directly write to dynamic UBI volumes, without | |
766fb95b | 274 | * issuing the volume update operation. |
801c135c AB |
275 | */ |
276 | static 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; | |
ae616e1b | 282 | int lnum, off, len, tbuf_size, err = 0; |
801c135c AB |
283 | size_t count_save = count; |
284 | char *tbuf; | |
801c135c | 285 | |
766fb95b SA |
286 | if (!vol->direct_writes) |
287 | return -EPERM; | |
288 | ||
c8566350 | 289 | dbg_gen("requested: write %zd bytes to offset %lld of volume %u", |
ae616e1b | 290 | count, *offp, vol->vol_id); |
801c135c AB |
291 | |
292 | if (vol->vol_type == UBI_STATIC_VOLUME) | |
293 | return -EROFS; | |
294 | ||
3013ee31 | 295 | lnum = div_u64_rem(*offp, vol->usable_leb_size, &off); |
cadb40cc | 296 | if (off & (ubi->min_io_size - 1)) { |
801c135c AB |
297 | 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 */ | |
cadb40cc | 305 | if (count & (ubi->min_io_size - 1)) { |
801c135c AB |
306 | 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); | |
92ad8f37 | 313 | tbuf = vmalloc(tbuf_size); |
801c135c AB |
314 | 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 | ||
89b96b69 | 331 | err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len, |
801c135c AB |
332 | 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 | ||
92ad8f37 | 348 | vfree(tbuf); |
801c135c AB |
349 | return err ? err : count_save - count; |
350 | } | |
351 | ||
801c135c AB |
352 | static 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 | ||
e653879c | 360 | if (!vol->updating && !vol->changing_leb) |
801c135c AB |
361 | return vol_cdev_direct_write(file, buf, count, offp); |
362 | ||
e653879c AB |
363 | 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 | ||
801c135c | 368 | if (err < 0) { |
e653879c | 369 | ubi_err("cannot accept more %zd bytes of data, error %d", |
01f7b309 | 370 | count, err); |
801c135c AB |
371 | return err; |
372 | } | |
373 | ||
374 | if (err) { | |
375 | /* | |
e653879c AB |
376 | * The operation is finished, @err contains number of actually |
377 | * written bytes. | |
801c135c AB |
378 | */ |
379 | count = err; | |
380 | ||
e653879c AB |
381 | if (vol->changing_leb) { |
382 | revoke_exclusive(desc, UBI_READWRITE); | |
383 | return count; | |
384 | } | |
385 | ||
801c135c AB |
386 | 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; | |
941dfb07 | 396 | ubi_gluebi_updated(vol); |
801c135c AB |
397 | revoke_exclusive(desc, UBI_READWRITE); |
398 | } | |
399 | ||
801c135c AB |
400 | return count; |
401 | } | |
402 | ||
f429b2ea AB |
403 | static long vol_cdev_ioctl(struct file *file, unsigned int cmd, |
404 | unsigned long arg) | |
801c135c AB |
405 | { |
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 | ||
801c135c | 412 | switch (cmd) { |
801c135c AB |
413 | /* 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 | ||
73789a3d BL |
434 | rsvd_bytes = (long long)vol->reserved_pebs * |
435 | ubi->leb_size-vol->data_pad; | |
801c135c AB |
436 | 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 | ||
1b68d0ee | 445 | err = ubi_start_update(ubi, vol, bytes); |
801c135c AB |
446 | if (bytes == 0) |
447 | revoke_exclusive(desc, UBI_READWRITE); | |
801c135c AB |
448 | break; |
449 | } | |
450 | ||
e653879c AB |
451 | /* 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 | ||
801c135c AB |
488 | /* Logical eraseblock erasure command */ |
489 | case UBI_IOCEBER: | |
490 | { | |
491 | int32_t lnum; | |
492 | ||
bf07803a | 493 | err = get_user(lnum, (__user int32_t *)argp); |
801c135c AB |
494 | if (err) { |
495 | err = -EFAULT; | |
496 | break; | |
497 | } | |
498 | ||
e653879c AB |
499 | if (desc->mode == UBI_READONLY || |
500 | vol->vol_type == UBI_STATIC_VOLUME) { | |
801c135c AB |
501 | err = -EROFS; |
502 | break; | |
503 | } | |
504 | ||
505 | if (lnum < 0 || lnum >= vol->reserved_pebs) { | |
506 | err = -EINVAL; | |
507 | break; | |
508 | } | |
509 | ||
c8566350 | 510 | dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); |
89b96b69 | 511 | err = ubi_eba_unmap_leb(ubi, vol, lnum); |
801c135c AB |
512 | if (err) |
513 | break; | |
514 | ||
515 | err = ubi_wl_flush(ubi); | |
516 | break; | |
517 | } | |
141e6ebd CC |
518 | |
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 | } | |
c3da23be CC |
532 | |
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 | } | |
a27ce8f5 CC |
546 | |
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 | } | |
801c135c | 560 | |
766fb95b SA |
561 | /* Set volume property command*/ |
562 | 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: | |
574 | mutex_lock(&ubi->volumes_mutex); | |
575 | desc->vol->direct_writes = !!req.value; | |
576 | mutex_unlock(&ubi->volumes_mutex); | |
577 | break; | |
578 | default: | |
579 | err = -EINVAL; | |
580 | break; | |
581 | } | |
582 | break; | |
583 | } | |
584 | ||
801c135c AB |
585 | default: |
586 | err = -ENOTTY; | |
587 | break; | |
588 | } | |
801c135c AB |
589 | 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 | */ | |
599 | static 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 | ||
cadb40cc | 625 | n = req->alignment & (ubi->min_io_size - 1); |
801c135c AB |
626 | 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 | ||
a6ea4407 AB |
634 | n = strnlen(req->name, req->name_len + 1); |
635 | if (n != req->name_len) | |
636 | goto bad; | |
637 | ||
801c135c AB |
638 | return 0; |
639 | ||
640 | bad: | |
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 | */ | |
653 | static 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 | ||
f40ac9cd AB |
665 | /** |
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 | */ | |
675 | static 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 | */ | |
f2863c54 AB |
785 | desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name, |
786 | UBI_EXCLUSIVE); | |
f40ac9cd AB |
787 | 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 | ||
f40ac9cd | 813 | err = ubi_rename_volumes(ubi, &rename_list); |
f40ac9cd AB |
814 | |
815 | out_free: | |
816 | list_for_each_entry_safe(re, re1, &rename_list, list) { | |
817 | ubi_close_volume(re->desc); | |
818 | list_del(&re->list); | |
819 | kfree(re); | |
820 | } | |
821 | return err; | |
822 | } | |
823 | ||
f429b2ea AB |
824 | static long ubi_cdev_ioctl(struct file *file, unsigned int cmd, |
825 | unsigned long arg) | |
801c135c AB |
826 | { |
827 | int err = 0; | |
828 | struct ubi_device *ubi; | |
829 | struct ubi_volume_desc *desc; | |
830 | void __user *argp = (void __user *)arg; | |
831 | ||
801c135c AB |
832 | if (!capable(CAP_SYS_RESOURCE)) |
833 | return -EPERM; | |
834 | ||
f429b2ea | 835 | ubi = ubi_get_by_major(imajor(file->f_mapping->host)); |
e73f4459 AB |
836 | if (!ubi) |
837 | return -ENODEV; | |
801c135c AB |
838 | |
839 | switch (cmd) { | |
840 | /* Create volume command */ | |
841 | case UBI_IOCMKVOL: | |
842 | { | |
843 | struct ubi_mkvol_req req; | |
844 | ||
c8566350 | 845 | dbg_gen("create volume"); |
897a316c | 846 | err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req)); |
801c135c AB |
847 | if (err) { |
848 | err = -EFAULT; | |
849 | break; | |
850 | } | |
851 | ||
a6ea4407 | 852 | req.name[req.name_len] = '\0'; |
801c135c AB |
853 | err = verify_mkvol_req(ubi, &req); |
854 | if (err) | |
855 | break; | |
856 | ||
40e4d0c1 | 857 | mutex_lock(&ubi->volumes_mutex); |
801c135c | 858 | err = ubi_create_volume(ubi, &req); |
40e4d0c1 | 859 | mutex_unlock(&ubi->volumes_mutex); |
801c135c AB |
860 | if (err) |
861 | break; | |
862 | ||
bf07803a | 863 | err = put_user(req.vol_id, (__user int32_t *)argp); |
801c135c AB |
864 | if (err) |
865 | err = -EFAULT; | |
866 | ||
867 | break; | |
868 | } | |
869 | ||
870 | /* Remove volume command */ | |
871 | case UBI_IOCRMVOL: | |
872 | { | |
873 | int vol_id; | |
874 | ||
c8566350 | 875 | dbg_gen("remove volume"); |
bf07803a | 876 | err = get_user(vol_id, (__user int32_t *)argp); |
801c135c AB |
877 | if (err) { |
878 | err = -EFAULT; | |
879 | break; | |
880 | } | |
881 | ||
882 | desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE); | |
883 | if (IS_ERR(desc)) { | |
884 | err = PTR_ERR(desc); | |
885 | break; | |
886 | } | |
887 | ||
40e4d0c1 | 888 | mutex_lock(&ubi->volumes_mutex); |
f40ac9cd | 889 | err = ubi_remove_volume(desc, 0); |
40e4d0c1 AB |
890 | mutex_unlock(&ubi->volumes_mutex); |
891 | ||
450f872a | 892 | /* |
40e4d0c1 AB |
893 | * The volume is deleted (unless an error occurred), and the |
894 | * 'struct ubi_volume' object will be freed when | |
895 | * 'ubi_close_volume()' will call 'put_device()'. | |
450f872a AB |
896 | */ |
897 | ubi_close_volume(desc); | |
801c135c AB |
898 | break; |
899 | } | |
900 | ||
901 | /* Re-size volume command */ | |
902 | case UBI_IOCRSVOL: | |
903 | { | |
904 | int pebs; | |
801c135c AB |
905 | struct ubi_rsvol_req req; |
906 | ||
c8566350 | 907 | dbg_gen("re-size volume"); |
897a316c | 908 | err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req)); |
801c135c AB |
909 | if (err) { |
910 | err = -EFAULT; | |
911 | break; | |
912 | } | |
913 | ||
914 | err = verify_rsvol_req(ubi, &req); | |
915 | if (err) | |
916 | break; | |
917 | ||
918 | desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE); | |
919 | if (IS_ERR(desc)) { | |
920 | err = PTR_ERR(desc); | |
921 | break; | |
922 | } | |
923 | ||
3013ee31 AB |
924 | pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1, |
925 | desc->vol->usable_leb_size); | |
801c135c | 926 | |
40e4d0c1 | 927 | mutex_lock(&ubi->volumes_mutex); |
801c135c | 928 | err = ubi_resize_volume(desc, pebs); |
40e4d0c1 | 929 | mutex_unlock(&ubi->volumes_mutex); |
801c135c AB |
930 | ubi_close_volume(desc); |
931 | break; | |
932 | } | |
933 | ||
f40ac9cd AB |
934 | /* Re-name volumes command */ |
935 | case UBI_IOCRNVOL: | |
936 | { | |
937 | struct ubi_rnvol_req *req; | |
938 | ||
939 | dbg_msg("re-name volumes"); | |
940 | req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL); | |
941 | if (!req) { | |
942 | err = -ENOMEM; | |
943 | break; | |
944 | }; | |
945 | ||
946 | err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req)); | |
947 | if (err) { | |
948 | err = -EFAULT; | |
949 | kfree(req); | |
950 | break; | |
951 | } | |
952 | ||
383d08e0 | 953 | mutex_lock(&ubi->volumes_mutex); |
f40ac9cd | 954 | err = rename_volumes(ubi, req); |
383d08e0 | 955 | mutex_unlock(&ubi->volumes_mutex); |
f40ac9cd AB |
956 | kfree(req); |
957 | break; | |
958 | } | |
959 | ||
801c135c AB |
960 | default: |
961 | err = -ENOTTY; | |
962 | break; | |
963 | } | |
964 | ||
e73f4459 | 965 | ubi_put_device(ubi); |
801c135c AB |
966 | return err; |
967 | } | |
968 | ||
f429b2ea AB |
969 | static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, |
970 | unsigned long arg) | |
897a316c AB |
971 | { |
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 | ||
c8566350 | 985 | dbg_gen("attach MTD device"); |
897a316c AB |
986 | 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 | ||
c8566350 | 1025 | dbg_gen("dettach MTD device"); |
897a316c AB |
1026 | 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 | ||
f429b2ea AB |
1046 | #ifdef CONFIG_COMPAT |
1047 | static 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 | ||
1055 | static 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 | ||
1063 | static 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 */ | |
1077 | const 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, | |
1b24bc3a | 1084 | .fsync = vol_cdev_fsync, |
f429b2ea AB |
1085 | .unlocked_ioctl = vol_cdev_ioctl, |
1086 | .compat_ioctl = vol_cdev_compat_ioctl, | |
9f961b57 AB |
1087 | }; |
1088 | ||
801c135c | 1089 | /* UBI character device operations */ |
4d187a88 | 1090 | const struct file_operations ubi_cdev_operations = { |
f429b2ea AB |
1091 | .owner = THIS_MODULE, |
1092 | .llseek = no_llseek, | |
1093 | .unlocked_ioctl = ubi_cdev_ioctl, | |
1094 | .compat_ioctl = ubi_cdev_compat_ioctl, | |
801c135c AB |
1095 | }; |
1096 | ||
f429b2ea AB |
1097 | /* UBI control character device operations */ |
1098 | const struct file_operations ubi_ctrl_cdev_operations = { | |
1099 | .owner = THIS_MODULE, | |
1100 | .unlocked_ioctl = ctrl_cdev_ioctl, | |
1101 | .compat_ioctl = ctrl_cdev_compat_ioctl, | |
801c135c | 1102 | }; |