cdev.c 24 KB

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