cdev.c 25 KB

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