cdev.c 25 KB

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