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