cdev.c 24 KB

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