cdev.c 23 KB

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