cdev.c 25 KB

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