cdev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /*
  21. * This file includes implementation of UBI character device operations.
  22. *
  23. * There are two kinds of character devices in UBI: UBI character devices and
  24. * UBI volume character devices. UBI character devices allow users to
  25. * manipulate whole volumes: create, remove, and re-size them. Volume character
  26. * devices provide volume I/O capabilities.
  27. *
  28. * Major and minor numbers are assigned dynamically to both UBI and volume
  29. * character devices.
  30. *
  31. * Well, there is the third kind of character devices - the UBI control
  32. * character device, which allows to manipulate by UBI devices - create and
  33. * delete them. In other words, it is used for attaching and detaching MTD
  34. * devices.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/stat.h>
  38. #include <linux/ioctl.h>
  39. #include <linux/capability.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/smp_lock.h>
  42. #include <mtd/ubi-user.h>
  43. #include <asm/div64.h>
  44. #include "ubi.h"
  45. /**
  46. * get_exclusive - get exclusive access to an UBI volume.
  47. * @desc: volume descriptor
  48. *
  49. * This function changes UBI volume open mode to "exclusive". Returns previous
  50. * mode value (positive integer) in case of success and a negative error code
  51. * in case of failure.
  52. */
  53. static int get_exclusive(struct ubi_volume_desc *desc)
  54. {
  55. int users, err;
  56. struct ubi_volume *vol = desc->vol;
  57. spin_lock(&vol->ubi->volumes_lock);
  58. users = vol->readers + vol->writers + vol->exclusive;
  59. ubi_assert(users > 0);
  60. if (users > 1) {
  61. dbg_err("%d users for volume %d", users, vol->vol_id);
  62. err = -EBUSY;
  63. } else {
  64. vol->readers = vol->writers = 0;
  65. vol->exclusive = 1;
  66. err = desc->mode;
  67. desc->mode = UBI_EXCLUSIVE;
  68. }
  69. spin_unlock(&vol->ubi->volumes_lock);
  70. return err;
  71. }
  72. /**
  73. * revoke_exclusive - revoke exclusive mode.
  74. * @desc: volume descriptor
  75. * @mode: new mode to switch to
  76. */
  77. static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
  78. {
  79. struct ubi_volume *vol = desc->vol;
  80. spin_lock(&vol->ubi->volumes_lock);
  81. ubi_assert(vol->readers == 0 && vol->writers == 0);
  82. ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
  83. vol->exclusive = 0;
  84. if (mode == UBI_READONLY)
  85. vol->readers = 1;
  86. else if (mode == UBI_READWRITE)
  87. vol->writers = 1;
  88. else
  89. vol->exclusive = 1;
  90. spin_unlock(&vol->ubi->volumes_lock);
  91. desc->mode = mode;
  92. }
  93. static int vol_cdev_open(struct inode *inode, struct file *file)
  94. {
  95. struct ubi_volume_desc *desc;
  96. int vol_id = iminor(inode) - 1, mode, ubi_num;
  97. lock_kernel();
  98. ubi_num = ubi_major2num(imajor(inode));
  99. if (ubi_num < 0) {
  100. unlock_kernel();
  101. return ubi_num;
  102. }
  103. if (file->f_mode & FMODE_WRITE)
  104. mode = UBI_READWRITE;
  105. else
  106. mode = UBI_READONLY;
  107. dbg_gen("open volume %d, mode %d", vol_id, mode);
  108. desc = ubi_open_volume(ubi_num, vol_id, mode);
  109. unlock_kernel();
  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 volume %d, mode %d", vol->vol_id, desc->mode);
  120. if (vol->updating) {
  121. ubi_warn("update of volume %d not finished, volume is damaged",
  122. vol->vol_id);
  123. ubi_assert(!vol->changing_leb);
  124. vol->updating = 0;
  125. vfree(vol->upd_buf);
  126. } else if (vol->changing_leb) {
  127. dbg_gen("only %lld of %lld bytes received for atomic LEB change"
  128. " for volume %d:%d, cancel", vol->upd_received,
  129. vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
  130. vol->changing_leb = 0;
  131. vfree(vol->upd_buf);
  132. }
  133. ubi_close_volume(desc);
  134. return 0;
  135. }
  136. static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
  137. {
  138. struct ubi_volume_desc *desc = file->private_data;
  139. struct ubi_volume *vol = desc->vol;
  140. loff_t new_offset;
  141. if (vol->updating) {
  142. /* Update is in progress, seeking is prohibited */
  143. dbg_err("updating");
  144. return -EBUSY;
  145. }
  146. switch (origin) {
  147. case 0: /* SEEK_SET */
  148. new_offset = offset;
  149. break;
  150. case 1: /* SEEK_CUR */
  151. new_offset = file->f_pos + offset;
  152. break;
  153. case 2: /* SEEK_END */
  154. new_offset = vol->used_bytes + offset;
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. if (new_offset < 0 || new_offset > vol->used_bytes) {
  160. dbg_err("bad seek %lld", new_offset);
  161. return -EINVAL;
  162. }
  163. dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
  164. vol->vol_id, offset, origin, new_offset);
  165. file->f_pos = new_offset;
  166. return new_offset;
  167. }
  168. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  169. loff_t *offp)
  170. {
  171. struct ubi_volume_desc *desc = file->private_data;
  172. struct ubi_volume *vol = desc->vol;
  173. struct ubi_device *ubi = vol->ubi;
  174. int err, lnum, off, len, tbuf_size;
  175. size_t count_save = count;
  176. void *tbuf;
  177. uint64_t tmp;
  178. dbg_gen("read %zd bytes from offset %lld of volume %d",
  179. count, *offp, vol->vol_id);
  180. if (vol->updating) {
  181. dbg_err("updating");
  182. return -EBUSY;
  183. }
  184. if (vol->upd_marker) {
  185. dbg_err("damaged volume, update marker is set");
  186. return -EBADF;
  187. }
  188. if (*offp == vol->used_bytes || count == 0)
  189. return 0;
  190. if (vol->corrupted)
  191. dbg_gen("read from corrupted volume %d", vol->vol_id);
  192. if (*offp + count > vol->used_bytes)
  193. count_save = count = vol->used_bytes - *offp;
  194. tbuf_size = vol->usable_leb_size;
  195. if (count < tbuf_size)
  196. tbuf_size = ALIGN(count, ubi->min_io_size);
  197. tbuf = vmalloc(tbuf_size);
  198. if (!tbuf)
  199. return -ENOMEM;
  200. len = count > tbuf_size ? tbuf_size : count;
  201. tmp = *offp;
  202. off = do_div(tmp, vol->usable_leb_size);
  203. lnum = tmp;
  204. do {
  205. cond_resched();
  206. if (off + len >= vol->usable_leb_size)
  207. len = vol->usable_leb_size - off;
  208. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  209. if (err)
  210. break;
  211. off += len;
  212. if (off == vol->usable_leb_size) {
  213. lnum += 1;
  214. off -= vol->usable_leb_size;
  215. }
  216. count -= len;
  217. *offp += len;
  218. err = copy_to_user(buf, tbuf, len);
  219. if (err) {
  220. err = -EFAULT;
  221. break;
  222. }
  223. buf += len;
  224. len = count > tbuf_size ? tbuf_size : count;
  225. } while (count);
  226. vfree(tbuf);
  227. return err ? err : count_save - count;
  228. }
  229. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  230. /*
  231. * This function allows to directly write to dynamic UBI volumes, without
  232. * issuing the volume update operation. Available only as a debugging feature.
  233. * Very useful for testing UBI.
  234. */
  235. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  236. size_t count, loff_t *offp)
  237. {
  238. struct ubi_volume_desc *desc = file->private_data;
  239. struct ubi_volume *vol = desc->vol;
  240. struct ubi_device *ubi = vol->ubi;
  241. int lnum, off, len, tbuf_size, err = 0;
  242. size_t count_save = count;
  243. char *tbuf;
  244. uint64_t tmp;
  245. dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
  246. count, *offp, vol->vol_id);
  247. if (vol->vol_type == UBI_STATIC_VOLUME)
  248. return -EROFS;
  249. tmp = *offp;
  250. off = do_div(tmp, vol->usable_leb_size);
  251. lnum = tmp;
  252. if (off & (ubi->min_io_size - 1)) {
  253. dbg_err("unaligned position");
  254. return -EINVAL;
  255. }
  256. if (*offp + count > vol->used_bytes)
  257. count_save = count = vol->used_bytes - *offp;
  258. /* We can write only in fractions of the minimum I/O unit */
  259. if (count & (ubi->min_io_size - 1)) {
  260. dbg_err("unaligned write length");
  261. return -EINVAL;
  262. }
  263. tbuf_size = vol->usable_leb_size;
  264. if (count < tbuf_size)
  265. tbuf_size = ALIGN(count, ubi->min_io_size);
  266. tbuf = vmalloc(tbuf_size);
  267. if (!tbuf)
  268. return -ENOMEM;
  269. len = count > tbuf_size ? tbuf_size : count;
  270. while (count) {
  271. cond_resched();
  272. if (off + len >= vol->usable_leb_size)
  273. len = vol->usable_leb_size - off;
  274. err = copy_from_user(tbuf, buf, len);
  275. if (err) {
  276. err = -EFAULT;
  277. break;
  278. }
  279. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
  280. UBI_UNKNOWN);
  281. if (err)
  282. break;
  283. off += len;
  284. if (off == vol->usable_leb_size) {
  285. lnum += 1;
  286. off -= vol->usable_leb_size;
  287. }
  288. count -= len;
  289. *offp += len;
  290. buf += len;
  291. len = count > tbuf_size ? tbuf_size : count;
  292. }
  293. vfree(tbuf);
  294. return err ? err : count_save - count;
  295. }
  296. #else
  297. #define vol_cdev_direct_write(file, buf, count, offp) (-EPERM)
  298. #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
  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_gluebi_updated(vol);
  337. revoke_exclusive(desc, UBI_READWRITE);
  338. }
  339. return count;
  340. }
  341. static int vol_cdev_ioctl(struct inode *inode, struct file *file,
  342. unsigned int cmd, 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. if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
  402. req.dtype != UBI_UNKNOWN)
  403. break;
  404. err = get_exclusive(desc);
  405. if (err < 0)
  406. break;
  407. err = ubi_start_leb_change(ubi, vol, &req);
  408. if (req.bytes == 0)
  409. revoke_exclusive(desc, UBI_READWRITE);
  410. break;
  411. }
  412. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  413. /* Logical eraseblock erasure command */
  414. case UBI_IOCEBER:
  415. {
  416. int32_t lnum;
  417. err = get_user(lnum, (__user int32_t *)argp);
  418. if (err) {
  419. err = -EFAULT;
  420. break;
  421. }
  422. if (desc->mode == UBI_READONLY ||
  423. vol->vol_type == UBI_STATIC_VOLUME) {
  424. err = -EROFS;
  425. break;
  426. }
  427. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  428. err = -EINVAL;
  429. break;
  430. }
  431. dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
  432. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  433. if (err)
  434. break;
  435. err = ubi_wl_flush(ubi);
  436. break;
  437. }
  438. #endif
  439. default:
  440. err = -ENOTTY;
  441. break;
  442. }
  443. return err;
  444. }
  445. /**
  446. * verify_mkvol_req - verify volume creation request.
  447. * @ubi: UBI device description object
  448. * @req: the request to check
  449. *
  450. * This function zero if the request is correct, and %-EINVAL if not.
  451. */
  452. static int verify_mkvol_req(const struct ubi_device *ubi,
  453. const struct ubi_mkvol_req *req)
  454. {
  455. int n, err = -EINVAL;
  456. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  457. req->name_len < 0)
  458. goto bad;
  459. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  460. req->vol_id != UBI_VOL_NUM_AUTO)
  461. goto bad;
  462. if (req->alignment == 0)
  463. goto bad;
  464. if (req->bytes == 0)
  465. goto bad;
  466. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  467. req->vol_type != UBI_STATIC_VOLUME)
  468. goto bad;
  469. if (req->alignment > ubi->leb_size)
  470. goto bad;
  471. n = req->alignment & (ubi->min_io_size - 1);
  472. if (req->alignment != 1 && n)
  473. goto bad;
  474. if (req->name_len > UBI_VOL_NAME_MAX) {
  475. err = -ENAMETOOLONG;
  476. goto bad;
  477. }
  478. n = strnlen(req->name, req->name_len + 1);
  479. if (n != req->name_len)
  480. goto bad;
  481. return 0;
  482. bad:
  483. dbg_err("bad volume creation request");
  484. ubi_dbg_dump_mkvol_req(req);
  485. return err;
  486. }
  487. /**
  488. * verify_rsvol_req - verify volume re-size request.
  489. * @ubi: UBI device description object
  490. * @req: the request to check
  491. *
  492. * This function returns zero if the request is correct, and %-EINVAL if not.
  493. */
  494. static int verify_rsvol_req(const struct ubi_device *ubi,
  495. const struct ubi_rsvol_req *req)
  496. {
  497. if (req->bytes <= 0)
  498. return -EINVAL;
  499. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  500. return -EINVAL;
  501. return 0;
  502. }
  503. /**
  504. * rename_volumes - rename UBI volumes.
  505. * @ubi: UBI device description object
  506. * @req: volumes re-name request
  507. *
  508. * This is a helper function for the volume re-name IOCTL which validates the
  509. * the request, opens the volume and calls corresponding volumes management
  510. * function. Returns zero in case of success and a negative error code in case
  511. * of failure.
  512. */
  513. static int rename_volumes(struct ubi_device *ubi,
  514. struct ubi_rnvol_req *req)
  515. {
  516. int i, n, err;
  517. struct list_head rename_list;
  518. struct ubi_rename_entry *re, *re1;
  519. if (req->count < 0 || req->count > UBI_MAX_RNVOL)
  520. return -EINVAL;
  521. if (req->count == 0)
  522. return 0;
  523. /* Validate volume IDs and names in the request */
  524. for (i = 0; i < req->count; i++) {
  525. if (req->ents[i].vol_id < 0 ||
  526. req->ents[i].vol_id >= ubi->vtbl_slots)
  527. return -EINVAL;
  528. if (req->ents[i].name_len < 0)
  529. return -EINVAL;
  530. if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
  531. return -ENAMETOOLONG;
  532. req->ents[i].name[req->ents[i].name_len] = '\0';
  533. n = strlen(req->ents[i].name);
  534. if (n != req->ents[i].name_len)
  535. err = -EINVAL;
  536. }
  537. /* Make sure volume IDs and names are unique */
  538. for (i = 0; i < req->count - 1; i++) {
  539. for (n = i + 1; n < req->count; n++) {
  540. if (req->ents[i].vol_id == req->ents[n].vol_id) {
  541. dbg_err("duplicated volume id %d",
  542. req->ents[i].vol_id);
  543. return -EINVAL;
  544. }
  545. if (!strcmp(req->ents[i].name, req->ents[n].name)) {
  546. dbg_err("duplicated volume name \"%s\"",
  547. req->ents[i].name);
  548. return -EINVAL;
  549. }
  550. }
  551. }
  552. /* Create the re-name list */
  553. INIT_LIST_HEAD(&rename_list);
  554. for (i = 0; i < req->count; i++) {
  555. int vol_id = req->ents[i].vol_id;
  556. int name_len = req->ents[i].name_len;
  557. const char *name = req->ents[i].name;
  558. re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  559. if (!re) {
  560. err = -ENOMEM;
  561. goto out_free;
  562. }
  563. re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  564. if (IS_ERR(re->desc)) {
  565. err = PTR_ERR(re->desc);
  566. dbg_err("cannot open volume %d, error %d", vol_id, err);
  567. kfree(re);
  568. goto out_free;
  569. }
  570. /* Skip this re-naming if the name does not really change */
  571. if (re->desc->vol->name_len == name_len &&
  572. !memcmp(re->desc->vol->name, name, name_len)) {
  573. ubi_close_volume(re->desc);
  574. kfree(re);
  575. continue;
  576. }
  577. re->new_name_len = name_len;
  578. memcpy(re->new_name, name, name_len);
  579. list_add_tail(&re->list, &rename_list);
  580. dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
  581. vol_id, re->desc->vol->name, name);
  582. }
  583. if (list_empty(&rename_list))
  584. return 0;
  585. /* Find out the volumes which have to be removed */
  586. list_for_each_entry(re, &rename_list, list) {
  587. struct ubi_volume_desc *desc;
  588. int no_remove_needed = 0;
  589. /*
  590. * Volume @re->vol_id is going to be re-named to
  591. * @re->new_name, while its current name is @name. If a volume
  592. * with name @re->new_name currently exists, it has to be
  593. * removed, unless it is also re-named in the request (@req).
  594. */
  595. list_for_each_entry(re1, &rename_list, list) {
  596. if (re->new_name_len == re1->desc->vol->name_len &&
  597. !memcmp(re->new_name, re1->desc->vol->name,
  598. re1->desc->vol->name_len)) {
  599. no_remove_needed = 1;
  600. break;
  601. }
  602. }
  603. if (no_remove_needed)
  604. continue;
  605. /*
  606. * It seems we need to remove volume with name @re->new_name,
  607. * if it exists.
  608. */
  609. desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name, UBI_EXCLUSIVE);
  610. if (IS_ERR(desc)) {
  611. err = PTR_ERR(desc);
  612. if (err == -ENODEV)
  613. /* Re-naming into a non-existing volume name */
  614. continue;
  615. /* The volume exists but busy, or an error occurred */
  616. dbg_err("cannot open volume \"%s\", error %d",
  617. re->new_name, err);
  618. goto out_free;
  619. }
  620. re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  621. if (!re) {
  622. err = -ENOMEM;
  623. ubi_close_volume(desc);
  624. goto out_free;
  625. }
  626. re->remove = 1;
  627. re->desc = desc;
  628. list_add(&re->list, &rename_list);
  629. dbg_msg("will remove volume %d, name \"%s\"",
  630. re->desc->vol->vol_id, re->desc->vol->name);
  631. }
  632. mutex_lock(&ubi->volumes_mutex);
  633. err = ubi_rename_volumes(ubi, &rename_list);
  634. mutex_unlock(&ubi->volumes_mutex);
  635. out_free:
  636. list_for_each_entry_safe(re, re1, &rename_list, list) {
  637. ubi_close_volume(re->desc);
  638. list_del(&re->list);
  639. kfree(re);
  640. }
  641. return err;
  642. }
  643. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  644. unsigned int cmd, unsigned long arg)
  645. {
  646. int err = 0;
  647. struct ubi_device *ubi;
  648. struct ubi_volume_desc *desc;
  649. void __user *argp = (void __user *)arg;
  650. if (!capable(CAP_SYS_RESOURCE))
  651. return -EPERM;
  652. ubi = ubi_get_by_major(imajor(inode));
  653. if (!ubi)
  654. return -ENODEV;
  655. switch (cmd) {
  656. /* Create volume command */
  657. case UBI_IOCMKVOL:
  658. {
  659. struct ubi_mkvol_req req;
  660. dbg_gen("create volume");
  661. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  662. if (err) {
  663. err = -EFAULT;
  664. break;
  665. }
  666. req.name[req.name_len] = '\0';
  667. err = verify_mkvol_req(ubi, &req);
  668. if (err)
  669. break;
  670. mutex_lock(&ubi->volumes_mutex);
  671. err = ubi_create_volume(ubi, &req);
  672. mutex_unlock(&ubi->volumes_mutex);
  673. if (err)
  674. break;
  675. err = put_user(req.vol_id, (__user int32_t *)argp);
  676. if (err)
  677. err = -EFAULT;
  678. break;
  679. }
  680. /* Remove volume command */
  681. case UBI_IOCRMVOL:
  682. {
  683. int vol_id;
  684. dbg_gen("remove volume");
  685. err = get_user(vol_id, (__user int32_t *)argp);
  686. if (err) {
  687. err = -EFAULT;
  688. break;
  689. }
  690. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  691. if (IS_ERR(desc)) {
  692. err = PTR_ERR(desc);
  693. break;
  694. }
  695. mutex_lock(&ubi->volumes_mutex);
  696. err = ubi_remove_volume(desc, 0);
  697. mutex_unlock(&ubi->volumes_mutex);
  698. /*
  699. * The volume is deleted (unless an error occurred), and the
  700. * 'struct ubi_volume' object will be freed when
  701. * 'ubi_close_volume()' will call 'put_device()'.
  702. */
  703. ubi_close_volume(desc);
  704. break;
  705. }
  706. /* Re-size volume command */
  707. case UBI_IOCRSVOL:
  708. {
  709. int pebs;
  710. uint64_t tmp;
  711. struct ubi_rsvol_req req;
  712. dbg_gen("re-size volume");
  713. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  714. if (err) {
  715. err = -EFAULT;
  716. break;
  717. }
  718. err = verify_rsvol_req(ubi, &req);
  719. if (err)
  720. break;
  721. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  722. if (IS_ERR(desc)) {
  723. err = PTR_ERR(desc);
  724. break;
  725. }
  726. tmp = req.bytes;
  727. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  728. pebs += tmp;
  729. mutex_lock(&ubi->volumes_mutex);
  730. err = ubi_resize_volume(desc, pebs);
  731. mutex_unlock(&ubi->volumes_mutex);
  732. ubi_close_volume(desc);
  733. break;
  734. }
  735. /* Re-name volumes command */
  736. case UBI_IOCRNVOL:
  737. {
  738. struct ubi_rnvol_req *req;
  739. dbg_msg("re-name volumes");
  740. req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
  741. if (!req) {
  742. err = -ENOMEM;
  743. break;
  744. };
  745. err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
  746. if (err) {
  747. err = -EFAULT;
  748. kfree(req);
  749. break;
  750. }
  751. mutex_lock(&ubi->mult_mutex);
  752. err = rename_volumes(ubi, req);
  753. mutex_unlock(&ubi->mult_mutex);
  754. kfree(req);
  755. break;
  756. }
  757. default:
  758. err = -ENOTTY;
  759. break;
  760. }
  761. ubi_put_device(ubi);
  762. return err;
  763. }
  764. static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
  765. unsigned int cmd, unsigned long arg)
  766. {
  767. int err = 0;
  768. void __user *argp = (void __user *)arg;
  769. if (!capable(CAP_SYS_RESOURCE))
  770. return -EPERM;
  771. switch (cmd) {
  772. /* Attach an MTD device command */
  773. case UBI_IOCATT:
  774. {
  775. struct ubi_attach_req req;
  776. struct mtd_info *mtd;
  777. dbg_gen("attach MTD device");
  778. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  779. if (err) {
  780. err = -EFAULT;
  781. break;
  782. }
  783. if (req.mtd_num < 0 ||
  784. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  785. err = -EINVAL;
  786. break;
  787. }
  788. mtd = get_mtd_device(NULL, req.mtd_num);
  789. if (IS_ERR(mtd)) {
  790. err = PTR_ERR(mtd);
  791. break;
  792. }
  793. /*
  794. * Note, further request verification is done by
  795. * 'ubi_attach_mtd_dev()'.
  796. */
  797. mutex_lock(&ubi_devices_mutex);
  798. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
  799. mutex_unlock(&ubi_devices_mutex);
  800. if (err < 0)
  801. put_mtd_device(mtd);
  802. else
  803. /* @err contains UBI device number */
  804. err = put_user(err, (__user int32_t *)argp);
  805. break;
  806. }
  807. /* Detach an MTD device command */
  808. case UBI_IOCDET:
  809. {
  810. int ubi_num;
  811. dbg_gen("dettach MTD device");
  812. err = get_user(ubi_num, (__user int32_t *)argp);
  813. if (err) {
  814. err = -EFAULT;
  815. break;
  816. }
  817. mutex_lock(&ubi_devices_mutex);
  818. err = ubi_detach_mtd_dev(ubi_num, 0);
  819. mutex_unlock(&ubi_devices_mutex);
  820. break;
  821. }
  822. default:
  823. err = -ENOTTY;
  824. break;
  825. }
  826. return err;
  827. }
  828. /* UBI control character device operations */
  829. struct file_operations ubi_ctrl_cdev_operations = {
  830. .ioctl = ctrl_cdev_ioctl,
  831. .owner = THIS_MODULE,
  832. };
  833. /* UBI character device operations */
  834. struct file_operations ubi_cdev_operations = {
  835. .owner = THIS_MODULE,
  836. .ioctl = ubi_cdev_ioctl,
  837. .llseek = no_llseek,
  838. };
  839. /* UBI volume character device operations */
  840. struct file_operations ubi_vol_cdev_operations = {
  841. .owner = THIS_MODULE,
  842. .open = vol_cdev_open,
  843. .release = vol_cdev_release,
  844. .llseek = vol_cdev_llseek,
  845. .read = vol_cdev_read,
  846. .write = vol_cdev_write,
  847. .ioctl = vol_cdev_ioctl,
  848. };