cdev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /*
  21. * This file includes implementation of UBI character device operations.
  22. *
  23. * There are two kinds of character devices in UBI: UBI character devices and
  24. * UBI volume character devices. UBI character devices allow users to
  25. * manipulate whole volumes: create, remove, and re-size them. Volume character
  26. * devices provide volume I/O capabilities.
  27. *
  28. * Major and minor numbers are assigned dynamically to both UBI and volume
  29. * character devices.
  30. *
  31. * Well, there is the third kind of character devices - the UBI control
  32. * character device, which allows to manipulate by UBI devices - create and
  33. * delete them. In other words, it is used for attaching and detaching MTD
  34. * devices.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/stat.h>
  38. #include <linux/slab.h>
  39. #include <linux/ioctl.h>
  40. #include <linux/capability.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/compat.h>
  43. #include <linux/math64.h>
  44. #include <mtd/ubi-user.h>
  45. #include "ubi.h"
  46. /**
  47. * get_exclusive - get exclusive access to an UBI volume.
  48. * @desc: volume descriptor
  49. *
  50. * This function changes UBI volume open mode to "exclusive". Returns previous
  51. * mode value (positive integer) in case of success and a negative error code
  52. * in case of failure.
  53. */
  54. static int get_exclusive(struct ubi_volume_desc *desc)
  55. {
  56. int users, err;
  57. struct ubi_volume *vol = desc->vol;
  58. spin_lock(&vol->ubi->volumes_lock);
  59. users = vol->readers + vol->writers + vol->exclusive;
  60. ubi_assert(users > 0);
  61. if (users > 1) {
  62. ubi_err("%d users for volume %d", users, vol->vol_id);
  63. err = -EBUSY;
  64. } else {
  65. vol->readers = vol->writers = 0;
  66. vol->exclusive = 1;
  67. err = desc->mode;
  68. desc->mode = UBI_EXCLUSIVE;
  69. }
  70. spin_unlock(&vol->ubi->volumes_lock);
  71. return err;
  72. }
  73. /**
  74. * revoke_exclusive - revoke exclusive mode.
  75. * @desc: volume descriptor
  76. * @mode: new mode to switch to
  77. */
  78. static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
  79. {
  80. struct ubi_volume *vol = desc->vol;
  81. spin_lock(&vol->ubi->volumes_lock);
  82. ubi_assert(vol->readers == 0 && vol->writers == 0);
  83. ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
  84. vol->exclusive = 0;
  85. if (mode == UBI_READONLY)
  86. vol->readers = 1;
  87. else if (mode == UBI_READWRITE)
  88. vol->writers = 1;
  89. else
  90. vol->exclusive = 1;
  91. spin_unlock(&vol->ubi->volumes_lock);
  92. desc->mode = mode;
  93. }
  94. static int vol_cdev_open(struct inode *inode, struct file *file)
  95. {
  96. struct ubi_volume_desc *desc;
  97. int vol_id = iminor(inode) - 1, mode, ubi_num;
  98. ubi_num = ubi_major2num(imajor(inode));
  99. if (ubi_num < 0)
  100. return ubi_num;
  101. if (file->f_mode & FMODE_WRITE)
  102. mode = UBI_READWRITE;
  103. else
  104. mode = UBI_READONLY;
  105. dbg_gen("open device %d, volume %d, mode %d",
  106. ubi_num, vol_id, mode);
  107. desc = ubi_open_volume(ubi_num, vol_id, mode);
  108. if (IS_ERR(desc))
  109. return PTR_ERR(desc);
  110. file->private_data = desc;
  111. return 0;
  112. }
  113. static int vol_cdev_release(struct inode *inode, struct file *file)
  114. {
  115. struct ubi_volume_desc *desc = file->private_data;
  116. struct ubi_volume *vol = desc->vol;
  117. dbg_gen("release device %d, volume %d, mode %d",
  118. vol->ubi->ubi_num, vol->vol_id, desc->mode);
  119. if (vol->updating) {
  120. ubi_warn("update of volume %d not finished, volume is damaged",
  121. vol->vol_id);
  122. ubi_assert(!vol->changing_leb);
  123. vol->updating = 0;
  124. vfree(vol->upd_buf);
  125. } else if (vol->changing_leb) {
  126. dbg_gen("only %lld of %lld bytes received for atomic LEB change"
  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. ubi_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. ubi_err("bad seek %lld", new_offset);
  160. return -EINVAL;
  161. }
  162. dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
  163. vol->vol_id, offset, origin, new_offset);
  164. file->f_pos = new_offset;
  165. return new_offset;
  166. }
  167. static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  168. {
  169. struct ubi_volume_desc *desc = file->private_data;
  170. struct ubi_device *ubi = desc->vol->ubi;
  171. struct inode *inode = file->f_path.dentry->d_inode;
  172. int err;
  173. mutex_lock(&inode->i_mutex);
  174. err = ubi_sync(ubi->ubi_num);
  175. mutex_unlock(&inode->i_mutex);
  176. return err;
  177. }
  178. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  179. loff_t *offp)
  180. {
  181. struct ubi_volume_desc *desc = file->private_data;
  182. struct ubi_volume *vol = desc->vol;
  183. struct ubi_device *ubi = vol->ubi;
  184. int err, lnum, off, len, tbuf_size;
  185. size_t count_save = count;
  186. void *tbuf;
  187. dbg_gen("read %zd bytes from offset %lld of volume %d",
  188. count, *offp, vol->vol_id);
  189. if (vol->updating) {
  190. ubi_err("updating");
  191. return -EBUSY;
  192. }
  193. if (vol->upd_marker) {
  194. ubi_err("damaged volume, update marker is set");
  195. return -EBADF;
  196. }
  197. if (*offp == vol->used_bytes || count == 0)
  198. return 0;
  199. if (vol->corrupted)
  200. dbg_gen("read from corrupted volume %d", vol->vol_id);
  201. if (*offp + count > vol->used_bytes)
  202. count_save = count = vol->used_bytes - *offp;
  203. tbuf_size = vol->usable_leb_size;
  204. if (count < tbuf_size)
  205. tbuf_size = ALIGN(count, ubi->min_io_size);
  206. tbuf = vmalloc(tbuf_size);
  207. if (!tbuf)
  208. return -ENOMEM;
  209. len = count > tbuf_size ? tbuf_size : count;
  210. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  211. do {
  212. cond_resched();
  213. if (off + len >= vol->usable_leb_size)
  214. len = vol->usable_leb_size - off;
  215. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  216. if (err)
  217. break;
  218. off += len;
  219. if (off == vol->usable_leb_size) {
  220. lnum += 1;
  221. off -= vol->usable_leb_size;
  222. }
  223. count -= len;
  224. *offp += len;
  225. err = copy_to_user(buf, tbuf, len);
  226. if (err) {
  227. err = -EFAULT;
  228. break;
  229. }
  230. buf += len;
  231. len = count > tbuf_size ? tbuf_size : count;
  232. } while (count);
  233. vfree(tbuf);
  234. return err ? err : count_save - count;
  235. }
  236. /*
  237. * This function allows to directly write to dynamic UBI volumes, without
  238. * issuing the volume update operation.
  239. */
  240. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  241. size_t count, loff_t *offp)
  242. {
  243. struct ubi_volume_desc *desc = file->private_data;
  244. struct ubi_volume *vol = desc->vol;
  245. struct ubi_device *ubi = vol->ubi;
  246. int lnum, off, len, tbuf_size, err = 0;
  247. size_t count_save = count;
  248. char *tbuf;
  249. if (!vol->direct_writes)
  250. return -EPERM;
  251. dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
  252. count, *offp, vol->vol_id);
  253. if (vol->vol_type == UBI_STATIC_VOLUME)
  254. return -EROFS;
  255. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  256. if (off & (ubi->min_io_size - 1)) {
  257. ubi_err("unaligned position");
  258. return -EINVAL;
  259. }
  260. if (*offp + count > vol->used_bytes)
  261. count_save = count = vol->used_bytes - *offp;
  262. /* We can write only in fractions of the minimum I/O unit */
  263. if (count & (ubi->min_io_size - 1)) {
  264. ubi_err("unaligned write length");
  265. return -EINVAL;
  266. }
  267. tbuf_size = vol->usable_leb_size;
  268. if (count < tbuf_size)
  269. tbuf_size = ALIGN(count, ubi->min_io_size);
  270. tbuf = vmalloc(tbuf_size);
  271. if (!tbuf)
  272. return -ENOMEM;
  273. len = count > tbuf_size ? tbuf_size : count;
  274. while (count) {
  275. cond_resched();
  276. if (off + len >= vol->usable_leb_size)
  277. len = vol->usable_leb_size - off;
  278. err = copy_from_user(tbuf, buf, len);
  279. if (err) {
  280. err = -EFAULT;
  281. break;
  282. }
  283. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
  284. if (err)
  285. break;
  286. off += len;
  287. if (off == vol->usable_leb_size) {
  288. lnum += 1;
  289. off -= vol->usable_leb_size;
  290. }
  291. count -= len;
  292. *offp += len;
  293. buf += len;
  294. len = count > tbuf_size ? tbuf_size : count;
  295. }
  296. vfree(tbuf);
  297. return err ? err : count_save - count;
  298. }
  299. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  300. size_t count, loff_t *offp)
  301. {
  302. int err = 0;
  303. struct ubi_volume_desc *desc = file->private_data;
  304. struct ubi_volume *vol = desc->vol;
  305. struct ubi_device *ubi = vol->ubi;
  306. if (!vol->updating && !vol->changing_leb)
  307. return vol_cdev_direct_write(file, buf, count, offp);
  308. if (vol->updating)
  309. err = ubi_more_update_data(ubi, vol, buf, count);
  310. else
  311. err = ubi_more_leb_change_data(ubi, vol, buf, count);
  312. if (err < 0) {
  313. ubi_err("cannot accept more %zd bytes of data, error %d",
  314. count, err);
  315. return err;
  316. }
  317. if (err) {
  318. /*
  319. * The operation is finished, @err contains number of actually
  320. * written bytes.
  321. */
  322. count = err;
  323. if (vol->changing_leb) {
  324. revoke_exclusive(desc, UBI_READWRITE);
  325. return count;
  326. }
  327. err = ubi_check_volume(ubi, vol->vol_id);
  328. if (err < 0)
  329. return err;
  330. if (err) {
  331. ubi_warn("volume %d on UBI device %d is corrupted",
  332. vol->vol_id, ubi->ubi_num);
  333. vol->corrupted = 1;
  334. }
  335. vol->checked = 1;
  336. ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
  337. revoke_exclusive(desc, UBI_READWRITE);
  338. }
  339. return count;
  340. }
  341. static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
  342. unsigned long arg)
  343. {
  344. int err = 0;
  345. struct ubi_volume_desc *desc = file->private_data;
  346. struct ubi_volume *vol = desc->vol;
  347. struct ubi_device *ubi = vol->ubi;
  348. void __user *argp = (void __user *)arg;
  349. switch (cmd) {
  350. /* Volume update command */
  351. case UBI_IOCVOLUP:
  352. {
  353. int64_t bytes, rsvd_bytes;
  354. if (!capable(CAP_SYS_RESOURCE)) {
  355. err = -EPERM;
  356. break;
  357. }
  358. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  359. if (err) {
  360. err = -EFAULT;
  361. break;
  362. }
  363. if (desc->mode == UBI_READONLY) {
  364. err = -EROFS;
  365. break;
  366. }
  367. rsvd_bytes = (long long)vol->reserved_pebs *
  368. ubi->leb_size-vol->data_pad;
  369. if (bytes < 0 || bytes > rsvd_bytes) {
  370. err = -EINVAL;
  371. break;
  372. }
  373. err = get_exclusive(desc);
  374. if (err < 0)
  375. break;
  376. err = ubi_start_update(ubi, vol, bytes);
  377. if (bytes == 0)
  378. revoke_exclusive(desc, UBI_READWRITE);
  379. break;
  380. }
  381. /* Atomic logical eraseblock change command */
  382. case UBI_IOCEBCH:
  383. {
  384. struct ubi_leb_change_req req;
  385. err = copy_from_user(&req, argp,
  386. sizeof(struct ubi_leb_change_req));
  387. if (err) {
  388. err = -EFAULT;
  389. break;
  390. }
  391. if (desc->mode == UBI_READONLY ||
  392. vol->vol_type == UBI_STATIC_VOLUME) {
  393. err = -EROFS;
  394. break;
  395. }
  396. /* Validate the request */
  397. err = -EINVAL;
  398. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  399. req.bytes < 0 || req.lnum >= vol->usable_leb_size)
  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, UBI_ALL, UBI_ALL);
  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);
  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_IOCSETVOLPROP:
  472. {
  473. struct ubi_set_vol_prop_req req;
  474. err = copy_from_user(&req, argp,
  475. sizeof(struct ubi_set_vol_prop_req));
  476. if (err) {
  477. err = -EFAULT;
  478. break;
  479. }
  480. switch (req.property) {
  481. case UBI_VOL_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[0] || !req->name_len)
  528. goto bad;
  529. if (req->name_len > UBI_VOL_NAME_MAX) {
  530. err = -ENAMETOOLONG;
  531. goto bad;
  532. }
  533. n = strnlen(req->name, req->name_len + 1);
  534. if (n != req->name_len)
  535. goto bad;
  536. return 0;
  537. bad:
  538. ubi_err("bad volume creation request");
  539. ubi_dump_mkvol_req(req);
  540. return err;
  541. }
  542. /**
  543. * verify_rsvol_req - verify volume re-size request.
  544. * @ubi: UBI device description object
  545. * @req: the request to check
  546. *
  547. * This function returns zero if the request is correct, and %-EINVAL if not.
  548. */
  549. static int verify_rsvol_req(const struct ubi_device *ubi,
  550. const struct ubi_rsvol_req *req)
  551. {
  552. if (req->bytes <= 0)
  553. return -EINVAL;
  554. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  555. return -EINVAL;
  556. return 0;
  557. }
  558. /**
  559. * rename_volumes - rename UBI volumes.
  560. * @ubi: UBI device description object
  561. * @req: volumes re-name request
  562. *
  563. * This is a helper function for the volume re-name IOCTL which validates the
  564. * the request, opens the volume and calls corresponding volumes management
  565. * function. Returns zero in case of success and a negative error code in case
  566. * of failure.
  567. */
  568. static int rename_volumes(struct ubi_device *ubi,
  569. struct ubi_rnvol_req *req)
  570. {
  571. int i, n, err;
  572. struct list_head rename_list;
  573. struct ubi_rename_entry *re, *re1;
  574. if (req->count < 0 || req->count > UBI_MAX_RNVOL)
  575. return -EINVAL;
  576. if (req->count == 0)
  577. return 0;
  578. /* Validate volume IDs and names in the request */
  579. for (i = 0; i < req->count; i++) {
  580. if (req->ents[i].vol_id < 0 ||
  581. req->ents[i].vol_id >= ubi->vtbl_slots)
  582. return -EINVAL;
  583. if (req->ents[i].name_len < 0)
  584. return -EINVAL;
  585. if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
  586. return -ENAMETOOLONG;
  587. req->ents[i].name[req->ents[i].name_len] = '\0';
  588. n = strlen(req->ents[i].name);
  589. if (n != req->ents[i].name_len)
  590. err = -EINVAL;
  591. }
  592. /* Make sure volume IDs and names are unique */
  593. for (i = 0; i < req->count - 1; i++) {
  594. for (n = i + 1; n < req->count; n++) {
  595. if (req->ents[i].vol_id == req->ents[n].vol_id) {
  596. ubi_err("duplicated volume id %d",
  597. req->ents[i].vol_id);
  598. return -EINVAL;
  599. }
  600. if (!strcmp(req->ents[i].name, req->ents[n].name)) {
  601. ubi_err("duplicated volume name \"%s\"",
  602. req->ents[i].name);
  603. return -EINVAL;
  604. }
  605. }
  606. }
  607. /* Create the re-name list */
  608. INIT_LIST_HEAD(&rename_list);
  609. for (i = 0; i < req->count; i++) {
  610. int vol_id = req->ents[i].vol_id;
  611. int name_len = req->ents[i].name_len;
  612. const char *name = req->ents[i].name;
  613. re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  614. if (!re) {
  615. err = -ENOMEM;
  616. goto out_free;
  617. }
  618. re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  619. if (IS_ERR(re->desc)) {
  620. err = PTR_ERR(re->desc);
  621. ubi_err("cannot open volume %d, error %d", vol_id, err);
  622. kfree(re);
  623. goto out_free;
  624. }
  625. /* Skip this re-naming if the name does not really change */
  626. if (re->desc->vol->name_len == name_len &&
  627. !memcmp(re->desc->vol->name, name, name_len)) {
  628. ubi_close_volume(re->desc);
  629. kfree(re);
  630. continue;
  631. }
  632. re->new_name_len = name_len;
  633. memcpy(re->new_name, name, name_len);
  634. list_add_tail(&re->list, &rename_list);
  635. dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
  636. vol_id, re->desc->vol->name, name);
  637. }
  638. if (list_empty(&rename_list))
  639. return 0;
  640. /* Find out the volumes which have to be removed */
  641. list_for_each_entry(re, &rename_list, list) {
  642. struct ubi_volume_desc *desc;
  643. int no_remove_needed = 0;
  644. /*
  645. * Volume @re->vol_id is going to be re-named to
  646. * @re->new_name, while its current name is @name. If a volume
  647. * with name @re->new_name currently exists, it has to be
  648. * removed, unless it is also re-named in the request (@req).
  649. */
  650. list_for_each_entry(re1, &rename_list, list) {
  651. if (re->new_name_len == re1->desc->vol->name_len &&
  652. !memcmp(re->new_name, re1->desc->vol->name,
  653. re1->desc->vol->name_len)) {
  654. no_remove_needed = 1;
  655. break;
  656. }
  657. }
  658. if (no_remove_needed)
  659. continue;
  660. /*
  661. * It seems we need to remove volume with name @re->new_name,
  662. * if it exists.
  663. */
  664. desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
  665. UBI_EXCLUSIVE);
  666. if (IS_ERR(desc)) {
  667. err = PTR_ERR(desc);
  668. if (err == -ENODEV)
  669. /* Re-naming into a non-existing volume name */
  670. continue;
  671. /* The volume exists but busy, or an error occurred */
  672. ubi_err("cannot open volume \"%s\", error %d",
  673. re->new_name, err);
  674. goto out_free;
  675. }
  676. re1 = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  677. if (!re1) {
  678. err = -ENOMEM;
  679. ubi_close_volume(desc);
  680. goto out_free;
  681. }
  682. re1->remove = 1;
  683. re1->desc = desc;
  684. list_add(&re1->list, &rename_list);
  685. dbg_msg("will remove volume %d, name \"%s\"",
  686. re1->desc->vol->vol_id, re1->desc->vol->name);
  687. }
  688. mutex_lock(&ubi->device_mutex);
  689. err = ubi_rename_volumes(ubi, &rename_list);
  690. mutex_unlock(&ubi->device_mutex);
  691. out_free:
  692. list_for_each_entry_safe(re, re1, &rename_list, list) {
  693. ubi_close_volume(re->desc);
  694. list_del(&re->list);
  695. kfree(re);
  696. }
  697. return err;
  698. }
  699. static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
  700. unsigned long arg)
  701. {
  702. int err = 0;
  703. struct ubi_device *ubi;
  704. struct ubi_volume_desc *desc;
  705. void __user *argp = (void __user *)arg;
  706. if (!capable(CAP_SYS_RESOURCE))
  707. return -EPERM;
  708. ubi = ubi_get_by_major(imajor(file->f_mapping->host));
  709. if (!ubi)
  710. return -ENODEV;
  711. switch (cmd) {
  712. /* Create volume command */
  713. case UBI_IOCMKVOL:
  714. {
  715. struct ubi_mkvol_req req;
  716. dbg_gen("create volume");
  717. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  718. if (err) {
  719. err = -EFAULT;
  720. break;
  721. }
  722. err = verify_mkvol_req(ubi, &req);
  723. if (err)
  724. break;
  725. mutex_lock(&ubi->device_mutex);
  726. err = ubi_create_volume(ubi, &req);
  727. mutex_unlock(&ubi->device_mutex);
  728. if (err)
  729. break;
  730. err = put_user(req.vol_id, (__user int32_t *)argp);
  731. if (err)
  732. err = -EFAULT;
  733. break;
  734. }
  735. /* Remove volume command */
  736. case UBI_IOCRMVOL:
  737. {
  738. int vol_id;
  739. dbg_gen("remove volume");
  740. err = get_user(vol_id, (__user int32_t *)argp);
  741. if (err) {
  742. err = -EFAULT;
  743. break;
  744. }
  745. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  746. if (IS_ERR(desc)) {
  747. err = PTR_ERR(desc);
  748. break;
  749. }
  750. mutex_lock(&ubi->device_mutex);
  751. err = ubi_remove_volume(desc, 0);
  752. mutex_unlock(&ubi->device_mutex);
  753. /*
  754. * The volume is deleted (unless an error occurred), and the
  755. * 'struct ubi_volume' object will be freed when
  756. * 'ubi_close_volume()' will call 'put_device()'.
  757. */
  758. ubi_close_volume(desc);
  759. break;
  760. }
  761. /* Re-size volume command */
  762. case UBI_IOCRSVOL:
  763. {
  764. int pebs;
  765. struct ubi_rsvol_req req;
  766. dbg_gen("re-size volume");
  767. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  768. if (err) {
  769. err = -EFAULT;
  770. break;
  771. }
  772. err = verify_rsvol_req(ubi, &req);
  773. if (err)
  774. break;
  775. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  776. if (IS_ERR(desc)) {
  777. err = PTR_ERR(desc);
  778. break;
  779. }
  780. pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
  781. desc->vol->usable_leb_size);
  782. mutex_lock(&ubi->device_mutex);
  783. err = ubi_resize_volume(desc, pebs);
  784. mutex_unlock(&ubi->device_mutex);
  785. ubi_close_volume(desc);
  786. break;
  787. }
  788. /* Re-name volumes command */
  789. case UBI_IOCRNVOL:
  790. {
  791. struct ubi_rnvol_req *req;
  792. dbg_msg("re-name volumes");
  793. req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
  794. if (!req) {
  795. err = -ENOMEM;
  796. break;
  797. };
  798. err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
  799. if (err) {
  800. err = -EFAULT;
  801. kfree(req);
  802. break;
  803. }
  804. err = rename_volumes(ubi, req);
  805. kfree(req);
  806. break;
  807. }
  808. default:
  809. err = -ENOTTY;
  810. break;
  811. }
  812. ubi_put_device(ubi);
  813. return err;
  814. }
  815. static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
  816. unsigned long arg)
  817. {
  818. int err = 0;
  819. void __user *argp = (void __user *)arg;
  820. if (!capable(CAP_SYS_RESOURCE))
  821. return -EPERM;
  822. switch (cmd) {
  823. /* Attach an MTD device command */
  824. case UBI_IOCATT:
  825. {
  826. struct ubi_attach_req req;
  827. struct mtd_info *mtd;
  828. dbg_gen("attach MTD device");
  829. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  830. if (err) {
  831. err = -EFAULT;
  832. break;
  833. }
  834. if (req.mtd_num < 0 ||
  835. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  836. err = -EINVAL;
  837. break;
  838. }
  839. mtd = get_mtd_device(NULL, req.mtd_num);
  840. if (IS_ERR(mtd)) {
  841. err = PTR_ERR(mtd);
  842. break;
  843. }
  844. /*
  845. * Note, further request verification is done by
  846. * 'ubi_attach_mtd_dev()'.
  847. */
  848. mutex_lock(&ubi_devices_mutex);
  849. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
  850. mutex_unlock(&ubi_devices_mutex);
  851. if (err < 0)
  852. put_mtd_device(mtd);
  853. else
  854. /* @err contains UBI device number */
  855. err = put_user(err, (__user int32_t *)argp);
  856. break;
  857. }
  858. /* Detach an MTD device command */
  859. case UBI_IOCDET:
  860. {
  861. int ubi_num;
  862. dbg_gen("dettach MTD device");
  863. err = get_user(ubi_num, (__user int32_t *)argp);
  864. if (err) {
  865. err = -EFAULT;
  866. break;
  867. }
  868. mutex_lock(&ubi_devices_mutex);
  869. err = ubi_detach_mtd_dev(ubi_num, 0);
  870. mutex_unlock(&ubi_devices_mutex);
  871. break;
  872. }
  873. default:
  874. err = -ENOTTY;
  875. break;
  876. }
  877. return err;
  878. }
  879. #ifdef CONFIG_COMPAT
  880. static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  881. unsigned long arg)
  882. {
  883. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  884. return vol_cdev_ioctl(file, cmd, translated_arg);
  885. }
  886. static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  887. unsigned long arg)
  888. {
  889. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  890. return ubi_cdev_ioctl(file, cmd, translated_arg);
  891. }
  892. static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  893. unsigned long arg)
  894. {
  895. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  896. return ctrl_cdev_ioctl(file, cmd, translated_arg);
  897. }
  898. #else
  899. #define vol_cdev_compat_ioctl NULL
  900. #define ubi_cdev_compat_ioctl NULL
  901. #define ctrl_cdev_compat_ioctl NULL
  902. #endif
  903. /* UBI volume character device operations */
  904. const struct file_operations ubi_vol_cdev_operations = {
  905. .owner = THIS_MODULE,
  906. .open = vol_cdev_open,
  907. .release = vol_cdev_release,
  908. .llseek = vol_cdev_llseek,
  909. .read = vol_cdev_read,
  910. .write = vol_cdev_write,
  911. .fsync = vol_cdev_fsync,
  912. .unlocked_ioctl = vol_cdev_ioctl,
  913. .compat_ioctl = vol_cdev_compat_ioctl,
  914. };
  915. /* UBI character device operations */
  916. const struct file_operations ubi_cdev_operations = {
  917. .owner = THIS_MODULE,
  918. .llseek = no_llseek,
  919. .unlocked_ioctl = ubi_cdev_ioctl,
  920. .compat_ioctl = ubi_cdev_compat_ioctl,
  921. };
  922. /* UBI control character device operations */
  923. const struct file_operations ubi_ctrl_cdev_operations = {
  924. .owner = THIS_MODULE,
  925. .unlocked_ioctl = ctrl_cdev_ioctl,
  926. .compat_ioctl = ctrl_cdev_compat_ioctl,
  927. .llseek = no_llseek,
  928. };