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 for volume %d:%d, cancel",
  127. vol->upd_received, vol->upd_bytes, vol->ubi->ubi_num,
  128. 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. if (vol->updating) {
  140. /* Update is in progress, seeking is prohibited */
  141. ubi_err("updating");
  142. return -EBUSY;
  143. }
  144. return fixed_size_llseek(file, offset, origin, vol->used_bytes);
  145. }
  146. static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end,
  147. int datasync)
  148. {
  149. struct ubi_volume_desc *desc = file->private_data;
  150. struct ubi_device *ubi = desc->vol->ubi;
  151. struct inode *inode = file_inode(file);
  152. int err;
  153. mutex_lock(&inode->i_mutex);
  154. err = ubi_sync(ubi->ubi_num);
  155. mutex_unlock(&inode->i_mutex);
  156. return err;
  157. }
  158. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  159. loff_t *offp)
  160. {
  161. struct ubi_volume_desc *desc = file->private_data;
  162. struct ubi_volume *vol = desc->vol;
  163. struct ubi_device *ubi = vol->ubi;
  164. int err, lnum, off, len, tbuf_size;
  165. size_t count_save = count;
  166. void *tbuf;
  167. dbg_gen("read %zd bytes from offset %lld of volume %d",
  168. count, *offp, vol->vol_id);
  169. if (vol->updating) {
  170. ubi_err("updating");
  171. return -EBUSY;
  172. }
  173. if (vol->upd_marker) {
  174. ubi_err("damaged volume, update marker is set");
  175. return -EBADF;
  176. }
  177. if (*offp == vol->used_bytes || count == 0)
  178. return 0;
  179. if (vol->corrupted)
  180. dbg_gen("read from corrupted volume %d", vol->vol_id);
  181. if (*offp + count > vol->used_bytes)
  182. count_save = count = vol->used_bytes - *offp;
  183. tbuf_size = vol->usable_leb_size;
  184. if (count < tbuf_size)
  185. tbuf_size = ALIGN(count, ubi->min_io_size);
  186. tbuf = vmalloc(tbuf_size);
  187. if (!tbuf)
  188. return -ENOMEM;
  189. len = count > tbuf_size ? tbuf_size : count;
  190. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  191. do {
  192. cond_resched();
  193. if (off + len >= vol->usable_leb_size)
  194. len = vol->usable_leb_size - off;
  195. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  196. if (err)
  197. break;
  198. off += len;
  199. if (off == vol->usable_leb_size) {
  200. lnum += 1;
  201. off -= vol->usable_leb_size;
  202. }
  203. count -= len;
  204. *offp += len;
  205. err = copy_to_user(buf, tbuf, len);
  206. if (err) {
  207. err = -EFAULT;
  208. break;
  209. }
  210. buf += len;
  211. len = count > tbuf_size ? tbuf_size : count;
  212. } while (count);
  213. vfree(tbuf);
  214. return err ? err : count_save - count;
  215. }
  216. /*
  217. * This function allows to directly write to dynamic UBI volumes, without
  218. * issuing the volume update operation.
  219. */
  220. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  221. size_t count, loff_t *offp)
  222. {
  223. struct ubi_volume_desc *desc = file->private_data;
  224. struct ubi_volume *vol = desc->vol;
  225. struct ubi_device *ubi = vol->ubi;
  226. int lnum, off, len, tbuf_size, err = 0;
  227. size_t count_save = count;
  228. char *tbuf;
  229. if (!vol->direct_writes)
  230. return -EPERM;
  231. dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
  232. count, *offp, vol->vol_id);
  233. if (vol->vol_type == UBI_STATIC_VOLUME)
  234. return -EROFS;
  235. lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
  236. if (off & (ubi->min_io_size - 1)) {
  237. ubi_err("unaligned position");
  238. return -EINVAL;
  239. }
  240. if (*offp + count > vol->used_bytes)
  241. count_save = count = vol->used_bytes - *offp;
  242. /* We can write only in fractions of the minimum I/O unit */
  243. if (count & (ubi->min_io_size - 1)) {
  244. ubi_err("unaligned write length");
  245. return -EINVAL;
  246. }
  247. tbuf_size = vol->usable_leb_size;
  248. if (count < tbuf_size)
  249. tbuf_size = ALIGN(count, ubi->min_io_size);
  250. tbuf = vmalloc(tbuf_size);
  251. if (!tbuf)
  252. return -ENOMEM;
  253. len = count > tbuf_size ? tbuf_size : count;
  254. while (count) {
  255. cond_resched();
  256. if (off + len >= vol->usable_leb_size)
  257. len = vol->usable_leb_size - off;
  258. err = copy_from_user(tbuf, buf, len);
  259. if (err) {
  260. err = -EFAULT;
  261. break;
  262. }
  263. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
  264. if (err)
  265. break;
  266. off += len;
  267. if (off == vol->usable_leb_size) {
  268. lnum += 1;
  269. off -= vol->usable_leb_size;
  270. }
  271. count -= len;
  272. *offp += len;
  273. buf += len;
  274. len = count > tbuf_size ? tbuf_size : count;
  275. }
  276. vfree(tbuf);
  277. return err ? err : count_save - count;
  278. }
  279. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  280. size_t count, loff_t *offp)
  281. {
  282. int err = 0;
  283. struct ubi_volume_desc *desc = file->private_data;
  284. struct ubi_volume *vol = desc->vol;
  285. struct ubi_device *ubi = vol->ubi;
  286. if (!vol->updating && !vol->changing_leb)
  287. return vol_cdev_direct_write(file, buf, count, offp);
  288. if (vol->updating)
  289. err = ubi_more_update_data(ubi, vol, buf, count);
  290. else
  291. err = ubi_more_leb_change_data(ubi, vol, buf, count);
  292. if (err < 0) {
  293. ubi_err("cannot accept more %zd bytes of data, error %d",
  294. count, err);
  295. return err;
  296. }
  297. if (err) {
  298. /*
  299. * The operation is finished, @err contains number of actually
  300. * written bytes.
  301. */
  302. count = err;
  303. if (vol->changing_leb) {
  304. revoke_exclusive(desc, UBI_READWRITE);
  305. return count;
  306. }
  307. err = ubi_check_volume(ubi, vol->vol_id);
  308. if (err < 0)
  309. return err;
  310. if (err) {
  311. ubi_warn("volume %d on UBI device %d is corrupted",
  312. vol->vol_id, ubi->ubi_num);
  313. vol->corrupted = 1;
  314. }
  315. vol->checked = 1;
  316. ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
  317. revoke_exclusive(desc, UBI_READWRITE);
  318. }
  319. return count;
  320. }
  321. static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
  322. unsigned long arg)
  323. {
  324. int err = 0;
  325. struct ubi_volume_desc *desc = file->private_data;
  326. struct ubi_volume *vol = desc->vol;
  327. struct ubi_device *ubi = vol->ubi;
  328. void __user *argp = (void __user *)arg;
  329. switch (cmd) {
  330. /* Volume update command */
  331. case UBI_IOCVOLUP:
  332. {
  333. int64_t bytes, rsvd_bytes;
  334. if (!capable(CAP_SYS_RESOURCE)) {
  335. err = -EPERM;
  336. break;
  337. }
  338. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  339. if (err) {
  340. err = -EFAULT;
  341. break;
  342. }
  343. if (desc->mode == UBI_READONLY) {
  344. err = -EROFS;
  345. break;
  346. }
  347. rsvd_bytes = (long long)vol->reserved_pebs *
  348. ubi->leb_size-vol->data_pad;
  349. if (bytes < 0 || bytes > rsvd_bytes) {
  350. err = -EINVAL;
  351. break;
  352. }
  353. err = get_exclusive(desc);
  354. if (err < 0)
  355. break;
  356. err = ubi_start_update(ubi, vol, bytes);
  357. if (bytes == 0) {
  358. ubi_volume_notify(ubi, vol, UBI_VOLUME_UPDATED);
  359. revoke_exclusive(desc, UBI_READWRITE);
  360. }
  361. break;
  362. }
  363. /* Atomic logical eraseblock change command */
  364. case UBI_IOCEBCH:
  365. {
  366. struct ubi_leb_change_req req;
  367. err = copy_from_user(&req, argp,
  368. sizeof(struct ubi_leb_change_req));
  369. if (err) {
  370. err = -EFAULT;
  371. break;
  372. }
  373. if (desc->mode == UBI_READONLY ||
  374. vol->vol_type == UBI_STATIC_VOLUME) {
  375. err = -EROFS;
  376. break;
  377. }
  378. /* Validate the request */
  379. err = -EINVAL;
  380. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  381. req.bytes < 0 || req.lnum >= vol->usable_leb_size)
  382. break;
  383. err = get_exclusive(desc);
  384. if (err < 0)
  385. break;
  386. err = ubi_start_leb_change(ubi, vol, &req);
  387. if (req.bytes == 0)
  388. revoke_exclusive(desc, UBI_READWRITE);
  389. break;
  390. }
  391. /* Logical eraseblock erasure command */
  392. case UBI_IOCEBER:
  393. {
  394. int32_t lnum;
  395. err = get_user(lnum, (__user int32_t *)argp);
  396. if (err) {
  397. err = -EFAULT;
  398. break;
  399. }
  400. if (desc->mode == UBI_READONLY ||
  401. vol->vol_type == UBI_STATIC_VOLUME) {
  402. err = -EROFS;
  403. break;
  404. }
  405. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  406. err = -EINVAL;
  407. break;
  408. }
  409. dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
  410. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  411. if (err)
  412. break;
  413. err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
  414. break;
  415. }
  416. /* Logical eraseblock map command */
  417. case UBI_IOCEBMAP:
  418. {
  419. struct ubi_map_req req;
  420. err = copy_from_user(&req, argp, sizeof(struct ubi_map_req));
  421. if (err) {
  422. err = -EFAULT;
  423. break;
  424. }
  425. err = ubi_leb_map(desc, req.lnum);
  426. break;
  427. }
  428. /* Logical eraseblock un-map command */
  429. case UBI_IOCEBUNMAP:
  430. {
  431. int32_t lnum;
  432. err = get_user(lnum, (__user int32_t *)argp);
  433. if (err) {
  434. err = -EFAULT;
  435. break;
  436. }
  437. err = ubi_leb_unmap(desc, lnum);
  438. break;
  439. }
  440. /* Check if logical eraseblock is mapped command */
  441. case UBI_IOCEBISMAP:
  442. {
  443. int32_t lnum;
  444. err = get_user(lnum, (__user int32_t *)argp);
  445. if (err) {
  446. err = -EFAULT;
  447. break;
  448. }
  449. err = ubi_is_mapped(desc, lnum);
  450. break;
  451. }
  452. /* Set volume property command */
  453. case UBI_IOCSETVOLPROP:
  454. {
  455. struct ubi_set_vol_prop_req req;
  456. err = copy_from_user(&req, argp,
  457. sizeof(struct ubi_set_vol_prop_req));
  458. if (err) {
  459. err = -EFAULT;
  460. break;
  461. }
  462. switch (req.property) {
  463. case UBI_VOL_PROP_DIRECT_WRITE:
  464. mutex_lock(&ubi->device_mutex);
  465. desc->vol->direct_writes = !!req.value;
  466. mutex_unlock(&ubi->device_mutex);
  467. break;
  468. default:
  469. err = -EINVAL;
  470. break;
  471. }
  472. break;
  473. }
  474. /* Create a R/O block device on top of the UBI volume */
  475. case UBI_IOCVOLCRBLK:
  476. {
  477. struct ubi_volume_info vi;
  478. ubi_get_volume_info(desc, &vi);
  479. err = ubiblock_create(&vi);
  480. break;
  481. }
  482. /* Remove the R/O block device */
  483. case UBI_IOCVOLRMBLK:
  484. {
  485. struct ubi_volume_info vi;
  486. ubi_get_volume_info(desc, &vi);
  487. err = ubiblock_remove(&vi);
  488. break;
  489. }
  490. default:
  491. err = -ENOTTY;
  492. break;
  493. }
  494. return err;
  495. }
  496. /**
  497. * verify_mkvol_req - verify volume creation request.
  498. * @ubi: UBI device description object
  499. * @req: the request to check
  500. *
  501. * This function zero if the request is correct, and %-EINVAL if not.
  502. */
  503. static int verify_mkvol_req(const struct ubi_device *ubi,
  504. const struct ubi_mkvol_req *req)
  505. {
  506. int n, err = -EINVAL;
  507. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  508. req->name_len < 0)
  509. goto bad;
  510. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  511. req->vol_id != UBI_VOL_NUM_AUTO)
  512. goto bad;
  513. if (req->alignment == 0)
  514. goto bad;
  515. if (req->bytes == 0)
  516. goto bad;
  517. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  518. req->vol_type != UBI_STATIC_VOLUME)
  519. goto bad;
  520. if (req->alignment > ubi->leb_size)
  521. goto bad;
  522. n = req->alignment & (ubi->min_io_size - 1);
  523. if (req->alignment != 1 && n)
  524. goto bad;
  525. if (!req->name[0] || !req->name_len)
  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. ubi_err("bad volume creation request");
  537. ubi_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. return -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. ubi_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. ubi_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_READWRITE);
  617. if (IS_ERR(re->desc)) {
  618. err = PTR_ERR(re->desc);
  619. ubi_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_gen("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. ubi_err("cannot open volume \"%s\", error %d",
  671. re->new_name, err);
  672. goto out_free;
  673. }
  674. re1 = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  675. if (!re1) {
  676. err = -ENOMEM;
  677. ubi_close_volume(desc);
  678. goto out_free;
  679. }
  680. re1->remove = 1;
  681. re1->desc = desc;
  682. list_add(&re1->list, &rename_list);
  683. dbg_gen("will remove volume %d, name \"%s\"",
  684. re1->desc->vol->vol_id, re1->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_gen("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. req.max_beb_per1024);
  849. mutex_unlock(&ubi_devices_mutex);
  850. if (err < 0)
  851. put_mtd_device(mtd);
  852. else
  853. /* @err contains UBI device number */
  854. err = put_user(err, (__user int32_t *)argp);
  855. break;
  856. }
  857. /* Detach an MTD device command */
  858. case UBI_IOCDET:
  859. {
  860. int ubi_num;
  861. dbg_gen("detach MTD device");
  862. err = get_user(ubi_num, (__user int32_t *)argp);
  863. if (err) {
  864. err = -EFAULT;
  865. break;
  866. }
  867. mutex_lock(&ubi_devices_mutex);
  868. err = ubi_detach_mtd_dev(ubi_num, 0);
  869. mutex_unlock(&ubi_devices_mutex);
  870. break;
  871. }
  872. default:
  873. err = -ENOTTY;
  874. break;
  875. }
  876. return err;
  877. }
  878. #ifdef CONFIG_COMPAT
  879. static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  880. unsigned long arg)
  881. {
  882. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  883. return vol_cdev_ioctl(file, cmd, translated_arg);
  884. }
  885. static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  886. unsigned long arg)
  887. {
  888. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  889. return ubi_cdev_ioctl(file, cmd, translated_arg);
  890. }
  891. static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
  892. unsigned long arg)
  893. {
  894. unsigned long translated_arg = (unsigned long)compat_ptr(arg);
  895. return ctrl_cdev_ioctl(file, cmd, translated_arg);
  896. }
  897. #else
  898. #define vol_cdev_compat_ioctl NULL
  899. #define ubi_cdev_compat_ioctl NULL
  900. #define ctrl_cdev_compat_ioctl NULL
  901. #endif
  902. /* UBI volume character device operations */
  903. const struct file_operations ubi_vol_cdev_operations = {
  904. .owner = THIS_MODULE,
  905. .open = vol_cdev_open,
  906. .release = vol_cdev_release,
  907. .llseek = vol_cdev_llseek,
  908. .read = vol_cdev_read,
  909. .write = vol_cdev_write,
  910. .fsync = vol_cdev_fsync,
  911. .unlocked_ioctl = vol_cdev_ioctl,
  912. .compat_ioctl = vol_cdev_compat_ioctl,
  913. };
  914. /* UBI character device operations */
  915. const struct file_operations ubi_cdev_operations = {
  916. .owner = THIS_MODULE,
  917. .llseek = no_llseek,
  918. .unlocked_ioctl = ubi_cdev_ioctl,
  919. .compat_ioctl = ubi_cdev_compat_ioctl,
  920. };
  921. /* UBI control character device operations */
  922. const struct file_operations ubi_ctrl_cdev_operations = {
  923. .owner = THIS_MODULE,
  924. .unlocked_ioctl = ctrl_cdev_ioctl,
  925. .compat_ioctl = ctrl_cdev_compat_ioctl,
  926. .llseek = no_llseek,
  927. };