cdev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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 + vol->metaonly;
  60. ubi_assert(users > 0);
  61. if (users > 1) {
  62. ubi_err(vol->ubi, "%d users for volume %d", users, vol->vol_id);
  63. err = -EBUSY;
  64. } else {
  65. vol->readers = vol->writers = vol->metaonly = 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 && vol->metaonly == 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 if (mode == UBI_METAONLY)
  90. vol->metaonly = 1;
  91. else
  92. vol->exclusive = 1;
  93. spin_unlock(&vol->ubi->volumes_lock);
  94. desc->mode = mode;
  95. }
  96. static int vol_cdev_open(struct inode *inode, struct file *file)
  97. {
  98. struct ubi_volume_desc *desc;
  99. int vol_id = iminor(inode) - 1, mode, ubi_num;
  100. ubi_num = ubi_major2num(imajor(inode));
  101. if (ubi_num < 0)
  102. return ubi_num;
  103. if (file->f_mode & FMODE_WRITE)
  104. mode = UBI_READWRITE;
  105. else
  106. mode = UBI_READONLY;
  107. dbg_gen("open device %d, volume %d, mode %d",
  108. ubi_num, vol_id, mode);
  109. desc = ubi_open_volume(ubi_num, vol_id, mode);
  110. if (IS_ERR(desc))
  111. return PTR_ERR(desc);
  112. file->private_data = desc;
  113. return 0;
  114. }
  115. static int vol_cdev_release(struct inode *inode, struct file *file)
  116. {
  117. struct ubi_volume_desc *desc = file->private_data;
  118. struct ubi_volume *vol = desc->vol;
  119. dbg_gen("release device %d, volume %d, mode %d",
  120. vol->ubi->ubi_num, vol->vol_id, desc->mode);
  121. if (vol->updating) {
  122. ubi_warn(vol->ubi, "update of volume %d not finished, volume is damaged",
  123. vol->vol_id);
  124. ubi_assert(!vol->changing_leb);
  125. vol->updating = 0;
  126. vfree(vol->upd_buf);
  127. } else if (vol->changing_leb) {
  128. dbg_gen("only %lld of %lld bytes received for atomic LEB change for volume %d:%d, cancel",
  129. vol->upd_received, vol->upd_bytes, vol->ubi->ubi_num,
  130. vol->vol_id);
  131. vol->changing_leb = 0;
  132. vfree(vol->upd_buf);
  133. }
  134. ubi_close_volume(desc);
  135. return 0;
  136. }
  137. static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
  138. {
  139. struct ubi_volume_desc *desc = file->private_data;
  140. struct ubi_volume *vol = desc->vol;
  141. if (vol->updating) {
  142. /* Update is in progress, seeking is prohibited */
  143. ubi_err(vol->ubi, "updating");
  144. return -EBUSY;
  145. }
  146. return fixed_size_llseek(file, offset, origin, vol->used_bytes);
  147. }
  148. static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end,
  149. int datasync)
  150. {
  151. struct ubi_volume_desc *desc = file->private_data;
  152. struct ubi_device *ubi = desc->vol->ubi;
  153. struct inode *inode = file_inode(file);
  154. int err;
  155. mutex_lock(&inode->i_mutex);
  156. err = ubi_sync(ubi->ubi_num);
  157. mutex_unlock(&inode->i_mutex);
  158. return err;
  159. }
  160. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  161. loff_t *offp)
  162. {
  163. struct ubi_volume_desc *desc = file->private_data;
  164. struct ubi_volume *vol = desc->vol;
  165. struct ubi_device *ubi = vol->ubi;
  166. int err, lnum, off, len, tbuf_size;
  167. size_t count_save = count;
  168. void *tbuf;
  169. dbg_gen("read %zd bytes from offset %lld of volume %d",
  170. count, *offp, vol->vol_id);
  171. if (vol->updating) {
  172. ubi_err(vol->ubi, "updating");
  173. return -EBUSY;
  174. }
  175. if (vol->upd_marker) {
  176. ubi_err(vol->ubi, "damaged volume, update marker is set");
  177. return -EBADF;
  178. }
  179. if (*offp == vol->used_bytes || count == 0)
  180. return 0;
  181. if (vol->corrupted)
  182. dbg_gen("read from corrupted volume %d", vol->vol_id);
  183. if (*offp + count > vol->used_bytes)
  184. count_save = count = vol->used_bytes - *offp;
  185. tbuf_size = vol->usable_leb_size;
  186. if (count < tbuf_size)
  187. tbuf_size = ALIGN(count, ubi->min_io_size);
  188. tbuf = vmalloc(tbuf_size);
  189. if (!tbuf)
  190. return -ENOMEM;
  191. len = count > tbuf_size ? tbuf_size : count;
  192. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  193. do {
  194. cond_resched();
  195. if (off + len >= vol->usable_leb_size)
  196. len = vol->usable_leb_size - off;
  197. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  198. if (err)
  199. break;
  200. off += len;
  201. if (off == vol->usable_leb_size) {
  202. lnum += 1;
  203. off -= vol->usable_leb_size;
  204. }
  205. count -= len;
  206. *offp += len;
  207. err = copy_to_user(buf, tbuf, len);
  208. if (err) {
  209. err = -EFAULT;
  210. break;
  211. }
  212. buf += len;
  213. len = count > tbuf_size ? tbuf_size : count;
  214. } while (count);
  215. vfree(tbuf);
  216. return err ? err : count_save - count;
  217. }
  218. /*
  219. * This function allows to directly write to dynamic UBI volumes, without
  220. * issuing the volume update operation.
  221. */
  222. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  223. size_t count, loff_t *offp)
  224. {
  225. struct ubi_volume_desc *desc = file->private_data;
  226. struct ubi_volume *vol = desc->vol;
  227. struct ubi_device *ubi = vol->ubi;
  228. int lnum, off, len, tbuf_size, err = 0;
  229. size_t count_save = count;
  230. char *tbuf;
  231. if (!vol->direct_writes)
  232. return -EPERM;
  233. dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
  234. count, *offp, vol->vol_id);
  235. if (vol->vol_type == UBI_STATIC_VOLUME)
  236. return -EROFS;
  237. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  238. if (off & (ubi->min_io_size - 1)) {
  239. ubi_err(ubi, "unaligned position");
  240. return -EINVAL;
  241. }
  242. if (*offp + count > vol->used_bytes)
  243. count_save = count = vol->used_bytes - *offp;
  244. /* We can write only in fractions of the minimum I/O unit */
  245. if (count & (ubi->min_io_size - 1)) {
  246. ubi_err(ubi, "unaligned write length");
  247. return -EINVAL;
  248. }
  249. tbuf_size = vol->usable_leb_size;
  250. if (count < tbuf_size)
  251. tbuf_size = ALIGN(count, ubi->min_io_size);
  252. tbuf = vmalloc(tbuf_size);
  253. if (!tbuf)
  254. return -ENOMEM;
  255. len = count > tbuf_size ? tbuf_size : count;
  256. while (count) {
  257. cond_resched();
  258. if (off + len >= vol->usable_leb_size)
  259. len = vol->usable_leb_size - off;
  260. err = copy_from_user(tbuf, buf, len);
  261. if (err) {
  262. err = -EFAULT;
  263. break;
  264. }
  265. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
  266. if (err)
  267. break;
  268. off += len;
  269. if (off == vol->usable_leb_size) {
  270. lnum += 1;
  271. off -= vol->usable_leb_size;
  272. }
  273. count -= len;
  274. *offp += len;
  275. buf += len;
  276. len = count > tbuf_size ? tbuf_size : count;
  277. }
  278. vfree(tbuf);
  279. return err ? err : count_save - count;
  280. }
  281. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  282. size_t count, loff_t *offp)
  283. {
  284. int err = 0;
  285. struct ubi_volume_desc *desc = file->private_data;
  286. struct ubi_volume *vol = desc->vol;
  287. struct ubi_device *ubi = vol->ubi;
  288. if (!vol->updating && !vol->changing_leb)
  289. return vol_cdev_direct_write(file, buf, count, offp);
  290. if (vol->updating)
  291. err = ubi_more_update_data(ubi, vol, buf, count);
  292. else
  293. err = ubi_more_leb_change_data(ubi, vol, buf, count);
  294. if (err < 0) {
  295. ubi_err(ubi, "cannot accept more %zd bytes of data, error %d",
  296. count, err);
  297. return err;
  298. }
  299. if (err) {
  300. /*
  301. * The operation is finished, @err contains number of actually
  302. * written bytes.
  303. */
  304. count = err;
  305. if (vol->changing_leb) {
  306. revoke_exclusive(desc, UBI_READWRITE);
  307. return count;
  308. }
  309. err = ubi_check_volume(ubi, vol->vol_id);
  310. if (err < 0)
  311. return err;
  312. if (err) {
  313. ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
  314. vol->vol_id, ubi->ubi_num);
  315. vol->corrupted = 1;
  316. }
  317. vol->checked = 1;
  318. ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
  319. revoke_exclusive(desc, UBI_READWRITE);
  320. }
  321. return count;
  322. }
  323. static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
  324. unsigned long arg)
  325. {
  326. int err = 0;
  327. struct ubi_volume_desc *desc = file->private_data;
  328. struct ubi_volume *vol = desc->vol;
  329. struct ubi_device *ubi = vol->ubi;
  330. void __user *argp = (void __user *)arg;
  331. switch (cmd) {
  332. /* Volume update command */
  333. case UBI_IOCVOLUP:
  334. {
  335. int64_t bytes, rsvd_bytes;
  336. if (!capable(CAP_SYS_RESOURCE)) {
  337. err = -EPERM;
  338. break;
  339. }
  340. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  341. if (err) {
  342. err = -EFAULT;
  343. break;
  344. }
  345. if (desc->mode == UBI_READONLY) {
  346. err = -EROFS;
  347. break;
  348. }
  349. rsvd_bytes = (long long)vol->reserved_pebs *
  350. ubi->leb_size-vol->data_pad;
  351. if (bytes < 0 || bytes > rsvd_bytes) {
  352. err = -EINVAL;
  353. break;
  354. }
  355. err = get_exclusive(desc);
  356. if (err < 0)
  357. break;
  358. err = ubi_start_update(ubi, vol, bytes);
  359. if (bytes == 0) {
  360. ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
  361. revoke_exclusive(desc, UBI_READWRITE);
  362. }
  363. break;
  364. }
  365. /* Atomic logical eraseblock change command */
  366. case UBI_IOCEBCH:
  367. {
  368. struct ubi_leb_change_req req;
  369. err = copy_from_user(&req, argp,
  370. sizeof(struct ubi_leb_change_req));
  371. if (err) {
  372. err = -EFAULT;
  373. break;
  374. }
  375. if (desc->mode == UBI_READONLY ||
  376. vol->vol_type == UBI_STATIC_VOLUME) {
  377. err = -EROFS;
  378. break;
  379. }
  380. /* Validate the request */
  381. err = -EINVAL;
  382. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  383. req.bytes < 0 || req.bytes > vol->usable_leb_size)
  384. break;
  385. err = get_exclusive(desc);
  386. if (err < 0)
  387. break;
  388. err = ubi_start_leb_change(ubi, vol, &req);
  389. if (req.bytes == 0)
  390. revoke_exclusive(desc, UBI_READWRITE);
  391. break;
  392. }
  393. /* Logical eraseblock erasure command */
  394. case UBI_IOCEBER:
  395. {
  396. int32_t lnum;
  397. err = get_user(lnum, (__user int32_t *)argp);
  398. if (err) {
  399. err = -EFAULT;
  400. break;
  401. }
  402. if (desc->mode == UBI_READONLY ||
  403. vol->vol_type == UBI_STATIC_VOLUME) {
  404. err = -EROFS;
  405. break;
  406. }
  407. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  408. err = -EINVAL;
  409. break;
  410. }
  411. dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
  412. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  413. if (err)
  414. break;
  415. err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
  416. break;
  417. }
  418. /* Logical eraseblock map command */
  419. case UBI_IOCEBMAP:
  420. {
  421. struct ubi_map_req req;
  422. err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
  423. if (err) {
  424. err = -EFAULT;
  425. break;
  426. }
  427. err = ubi_leb_map(desc, req.lnum);
  428. break;
  429. }
  430. /* Logical eraseblock un-map command */
  431. case UBI_IOCEBUNMAP:
  432. {
  433. int32_t lnum;
  434. err = get_user(lnum, (__user int32_t *)argp);
  435. if (err) {
  436. err = -EFAULT;
  437. break;
  438. }
  439. err = ubi_leb_unmap(desc, lnum);
  440. break;
  441. }
  442. /* Check if logical eraseblock is mapped command */
  443. case UBI_IOCEBISMAP:
  444. {
  445. int32_t lnum;
  446. err = get_user(lnum, (__user int32_t *)argp);
  447. if (err) {
  448. err = -EFAULT;
  449. break;
  450. }
  451. err = ubi_is_mapped(desc, lnum);
  452. break;
  453. }
  454. /* Set volume property command */
  455. case UBI_IOCSETVOLPROP:
  456. {
  457. struct ubi_set_vol_prop_req req;
  458. err = copy_from_user(&req, argp,
  459. sizeof(struct ubi_set_vol_prop_req));
  460. if (err) {
  461. err = -EFAULT;
  462. break;
  463. }
  464. switch (req.property) {
  465. case UBI_VOL_PROP_DIRECT_WRITE:
  466. mutex_lock(&ubi->device_mutex);
  467. desc->vol->direct_writes = !!req.value;
  468. mutex_unlock(&ubi->device_mutex);
  469. break;
  470. default:
  471. err = -EINVAL;
  472. break;
  473. }
  474. break;
  475. }
  476. /* Create a R/O block device on top of the UBI volume */
  477. case UBI_IOCVOLCRBLK:
  478. {
  479. struct ubi_volume_info vi;
  480. ubi_get_volume_info(desc, &vi);
  481. err = ubiblock_create(&vi);
  482. break;
  483. }
  484. /* Remove the R/O block device */
  485. case UBI_IOCVOLRMBLK:
  486. {
  487. struct ubi_volume_info vi;
  488. ubi_get_volume_info(desc, &vi);
  489. err = ubiblock_remove(&vi);
  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(ubi, "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. return -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(ubi, "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(ubi, "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_METAONLY);
  619. if (IS_ERR(re->desc)) {
  620. err = PTR_ERR(re->desc);
  621. ubi_err(ubi, "cannot open volume %d, error %d",
  622. vol_id, err);
  623. kfree(re);
  624. goto out_free;
  625. }
  626. /* Skip this re-naming if the name does not really change */
  627. if (re->desc->vol->name_len == name_len &&
  628. !memcmp(re->desc->vol->name, name, name_len)) {
  629. ubi_close_volume(re->desc);
  630. kfree(re);
  631. continue;
  632. }
  633. re->new_name_len = name_len;
  634. memcpy(re->new_name, name, name_len);
  635. list_add_tail(&re->list, &rename_list);
  636. dbg_gen("will rename volume %d from \"%s\" to \"%s\"",
  637. vol_id, re->desc->vol->name, name);
  638. }
  639. if (list_empty(&rename_list))
  640. return 0;
  641. /* Find out the volumes which have to be removed */
  642. list_for_each_entry(re, &rename_list, list) {
  643. struct ubi_volume_desc *desc;
  644. int no_remove_needed = 0;
  645. /*
  646. * Volume @re->vol_id is going to be re-named to
  647. * @re->new_name, while its current name is @name. If a volume
  648. * with name @re->new_name currently exists, it has to be
  649. * removed, unless it is also re-named in the request (@req).
  650. */
  651. list_for_each_entry(re1, &rename_list, list) {
  652. if (re->new_name_len == re1->desc->vol->name_len &&
  653. !memcmp(re->new_name, re1->desc->vol->name,
  654. re1->desc->vol->name_len)) {
  655. no_remove_needed = 1;
  656. break;
  657. }
  658. }
  659. if (no_remove_needed)
  660. continue;
  661. /*
  662. * It seems we need to remove volume with name @re->new_name,
  663. * if it exists.
  664. */
  665. desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
  666. UBI_EXCLUSIVE);
  667. if (IS_ERR(desc)) {
  668. err = PTR_ERR(desc);
  669. if (err == -ENODEV)
  670. /* Re-naming into a non-existing volume name */
  671. continue;
  672. /* The volume exists but busy, or an error occurred */
  673. ubi_err(ubi, "cannot open volume \"%s\", error %d",
  674. re->new_name, err);
  675. goto out_free;
  676. }
  677. re1 = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  678. if (!re1) {
  679. err = -ENOMEM;
  680. ubi_close_volume(desc);
  681. goto out_free;
  682. }
  683. re1->remove = 1;
  684. re1->desc = desc;
  685. list_add(&re1->list, &rename_list);
  686. dbg_gen("will remove volume %d, name \"%s\"",
  687. re1->desc->vol->vol_id, re1->desc->vol->name);
  688. }
  689. mutex_lock(&ubi->device_mutex);
  690. err = ubi_rename_volumes(ubi, &rename_list);
  691. mutex_unlock(&ubi->device_mutex);
  692. out_free:
  693. list_for_each_entry_safe(re, re1, &rename_list, list) {
  694. ubi_close_volume(re->desc);
  695. list_del(&re->list);
  696. kfree(re);
  697. }
  698. return err;
  699. }
  700. static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
  701. unsigned long arg)
  702. {
  703. int err = 0;
  704. struct ubi_device *ubi;
  705. struct ubi_volume_desc *desc;
  706. void __user *argp = (void __user *)arg;
  707. if (!capable(CAP_SYS_RESOURCE))
  708. return -EPERM;
  709. ubi = ubi_get_by_major(imajor(file->f_mapping->host));
  710. if (!ubi)
  711. return -ENODEV;
  712. switch (cmd) {
  713. /* Create volume command */
  714. case UBI_IOCMKVOL:
  715. {
  716. struct ubi_mkvol_req req;
  717. dbg_gen("create volume");
  718. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  719. if (err) {
  720. err = -EFAULT;
  721. break;
  722. }
  723. err = verify_mkvol_req(ubi, &req);
  724. if (err)
  725. break;
  726. mutex_lock(&ubi->device_mutex);
  727. err = ubi_create_volume(ubi, &req);
  728. mutex_unlock(&ubi->device_mutex);
  729. if (err)
  730. break;
  731. err = put_user(req.vol_id, (__user int32_t *)argp);
  732. if (err)
  733. err = -EFAULT;
  734. break;
  735. }
  736. /* Remove volume command */
  737. case UBI_IOCRMVOL:
  738. {
  739. int vol_id;
  740. dbg_gen("remove volume");
  741. err = get_user(vol_id, (__user int32_t *)argp);
  742. if (err) {
  743. err = -EFAULT;
  744. break;
  745. }
  746. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  747. if (IS_ERR(desc)) {
  748. err = PTR_ERR(desc);
  749. break;
  750. }
  751. mutex_lock(&ubi->device_mutex);
  752. err = ubi_remove_volume(desc, 0);
  753. mutex_unlock(&ubi->device_mutex);
  754. /*
  755. * The volume is deleted (unless an error occurred), and the
  756. * 'struct ubi_volume' object will be freed when
  757. * 'ubi_close_volume()' will call 'put_device()'.
  758. */
  759. ubi_close_volume(desc);
  760. break;
  761. }
  762. /* Re-size volume command */
  763. case UBI_IOCRSVOL:
  764. {
  765. int pebs;
  766. struct ubi_rsvol_req req;
  767. dbg_gen("re-size volume");
  768. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  769. if (err) {
  770. err = -EFAULT;
  771. break;
  772. }
  773. err = verify_rsvol_req(ubi, &req);
  774. if (err)
  775. break;
  776. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  777. if (IS_ERR(desc)) {
  778. err = PTR_ERR(desc);
  779. break;
  780. }
  781. pebs = div_u64(req.bytes + desc->vol->usable_leb_size - 1,
  782. desc->vol->usable_leb_size);
  783. mutex_lock(&ubi->device_mutex);
  784. err = ubi_resize_volume(desc, pebs);
  785. mutex_unlock(&ubi->device_mutex);
  786. ubi_close_volume(desc);
  787. break;
  788. }
  789. /* Re-name volumes command */
  790. case UBI_IOCRNVOL:
  791. {
  792. struct ubi_rnvol_req *req;
  793. dbg_gen("re-name volumes");
  794. req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
  795. if (!req) {
  796. err = -ENOMEM;
  797. break;
  798. }
  799. err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
  800. if (err) {
  801. err = -EFAULT;
  802. kfree(req);
  803. break;
  804. }
  805. err = rename_volumes(ubi, req);
  806. kfree(req);
  807. break;
  808. }
  809. default:
  810. err = -ENOTTY;
  811. break;
  812. }
  813. ubi_put_device(ubi);
  814. return err;
  815. }
  816. static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
  817. unsigned long arg)
  818. {
  819. int err = 0;
  820. void __user *argp = (void __user *)arg;
  821. if (!capable(CAP_SYS_RESOURCE))
  822. return -EPERM;
  823. switch (cmd) {
  824. /* Attach an MTD device command */
  825. case UBI_IOCATT:
  826. {
  827. struct ubi_attach_req req;
  828. struct mtd_info *mtd;
  829. dbg_gen("attach MTD device");
  830. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  831. if (err) {
  832. err = -EFAULT;
  833. break;
  834. }
  835. if (req.mtd_num < 0 ||
  836. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  837. err = -EINVAL;
  838. break;
  839. }
  840. mtd = get_mtd_device(NULL, req.mtd_num);
  841. if (IS_ERR(mtd)) {
  842. err = PTR_ERR(mtd);
  843. break;
  844. }
  845. /*
  846. * Note, further request verification is done by
  847. * 'ubi_attach_mtd_dev()'.
  848. */
  849. mutex_lock(&ubi_devices_mutex);
  850. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
  851. req.max_beb_per1024);
  852. mutex_unlock(&ubi_devices_mutex);
  853. if (err < 0)
  854. put_mtd_device(mtd);
  855. else
  856. /* @err contains UBI device number */
  857. err = put_user(err, (__user int32_t *)argp);
  858. break;
  859. }
  860. /* Detach an MTD device command */
  861. case UBI_IOCDET:
  862. {
  863. int ubi_num;
  864. dbg_gen("detach MTD device");
  865. err = get_user(ubi_num, (__user int32_t *)argp);
  866. if (err) {
  867. err = -EFAULT;
  868. break;
  869. }
  870. mutex_lock(&ubi_devices_mutex);
  871. err = ubi_detach_mtd_dev(ubi_num, 0);
  872. mutex_unlock(&ubi_devices_mutex);
  873. break;
  874. }
  875. default:
  876. err = -ENOTTY;
  877. break;
  878. }
  879. return err;
  880. }
  881. #ifdef CONFIG_COMPAT
  882. static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  883. unsigned long arg)
  884. {
  885. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  886. return vol_cdev_ioctl(file, cmd, translated_arg);
  887. }
  888. static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  889. unsigned long arg)
  890. {
  891. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  892. return ubi_cdev_ioctl(file, cmd, translated_arg);
  893. }
  894. static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  895. unsigned long arg)
  896. {
  897. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  898. return ctrl_cdev_ioctl(file, cmd, translated_arg);
  899. }
  900. #else
  901. #define vol_cdev_compat_ioctl NULL
  902. #define ubi_cdev_compat_ioctl NULL
  903. #define ctrl_cdev_compat_ioctl NULL
  904. #endif
  905. /* UBI volume character device operations */
  906. const struct file_operations ubi_vol_cdev_operations = {
  907. .owner = THIS_MODULE,
  908. .open = vol_cdev_open,
  909. .release = vol_cdev_release,
  910. .llseek = vol_cdev_llseek,
  911. .read = vol_cdev_read,
  912. .write = vol_cdev_write,
  913. .fsync = vol_cdev_fsync,
  914. .unlocked_ioctl = vol_cdev_ioctl,
  915. .compat_ioctl = vol_cdev_compat_ioctl,
  916. };
  917. /* UBI character device operations */
  918. const struct file_operations ubi_cdev_operations = {
  919. .owner = THIS_MODULE,
  920. .llseek = no_llseek,
  921. .unlocked_ioctl = ubi_cdev_ioctl,
  922. .compat_ioctl = ubi_cdev_compat_ioctl,
  923. };
  924. /* UBI control character device operations */
  925. const struct file_operations ubi_ctrl_cdev_operations = {
  926. .owner = THIS_MODULE,
  927. .unlocked_ioctl = ctrl_cdev_ioctl,
  928. .compat_ioctl = ctrl_cdev_compat_ioctl,
  929. .llseek = no_llseek,
  930. };