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