cdev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. ubi_num = ubi_major2num(imajor(inode));
  98. if (ubi_num < 0)
  99. return ubi_num;
  100. if (file->f_mode & FMODE_WRITE)
  101. mode = UBI_READWRITE;
  102. else
  103. mode = UBI_READONLY;
  104. dbg_gen("open volume %d, mode %d", vol_id, mode);
  105. desc = ubi_open_volume(ubi_num, vol_id, mode);
  106. if (IS_ERR(desc))
  107. return PTR_ERR(desc);
  108. file->private_data = desc;
  109. return 0;
  110. }
  111. static int vol_cdev_release(struct inode *inode, struct file *file)
  112. {
  113. struct ubi_volume_desc *desc = file->private_data;
  114. struct ubi_volume *vol = desc->vol;
  115. dbg_gen("release volume %d, mode %d", vol->vol_id, desc->mode);
  116. if (vol->updating) {
  117. ubi_warn("update of volume %d not finished, volume is damaged",
  118. vol->vol_id);
  119. ubi_assert(!vol->changing_leb);
  120. vol->updating = 0;
  121. vfree(vol->upd_buf);
  122. } else if (vol->changing_leb) {
  123. dbg_gen("only %lld of %lld bytes received for atomic LEB change"
  124. " for volume %d:%d, cancel", vol->upd_received,
  125. vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
  126. vol->changing_leb = 0;
  127. vfree(vol->upd_buf);
  128. }
  129. ubi_close_volume(desc);
  130. return 0;
  131. }
  132. static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
  133. {
  134. struct ubi_volume_desc *desc = file->private_data;
  135. struct ubi_volume *vol = desc->vol;
  136. loff_t new_offset;
  137. if (vol->updating) {
  138. /* Update is in progress, seeking is prohibited */
  139. dbg_err("updating");
  140. return -EBUSY;
  141. }
  142. switch (origin) {
  143. case 0: /* SEEK_SET */
  144. new_offset = offset;
  145. break;
  146. case 1: /* SEEK_CUR */
  147. new_offset = file->f_pos + offset;
  148. break;
  149. case 2: /* SEEK_END */
  150. new_offset = vol->used_bytes + offset;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. if (new_offset < 0 || new_offset > vol->used_bytes) {
  156. dbg_err("bad seek %lld", new_offset);
  157. return -EINVAL;
  158. }
  159. dbg_gen("seek volume %d, offset %lld, origin %d, new offset %lld",
  160. vol->vol_id, offset, origin, new_offset);
  161. file->f_pos = new_offset;
  162. return new_offset;
  163. }
  164. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  165. loff_t *offp)
  166. {
  167. struct ubi_volume_desc *desc = file->private_data;
  168. struct ubi_volume *vol = desc->vol;
  169. struct ubi_device *ubi = vol->ubi;
  170. int err, lnum, off, len, tbuf_size;
  171. size_t count_save = count;
  172. void *tbuf;
  173. uint64_t tmp;
  174. dbg_gen("read %zd bytes from offset %lld of volume %d",
  175. count, *offp, vol->vol_id);
  176. if (vol->updating) {
  177. dbg_err("updating");
  178. return -EBUSY;
  179. }
  180. if (vol->upd_marker) {
  181. dbg_err("damaged volume, update marker is set");
  182. return -EBADF;
  183. }
  184. if (*offp == vol->used_bytes || count == 0)
  185. return 0;
  186. if (vol->corrupted)
  187. dbg_gen("read from corrupted volume %d", vol->vol_id);
  188. if (*offp + count > vol->used_bytes)
  189. count_save = count = vol->used_bytes - *offp;
  190. tbuf_size = vol->usable_leb_size;
  191. if (count < tbuf_size)
  192. tbuf_size = ALIGN(count, ubi->min_io_size);
  193. tbuf = vmalloc(tbuf_size);
  194. if (!tbuf)
  195. return -ENOMEM;
  196. len = count > tbuf_size ? tbuf_size : count;
  197. tmp = *offp;
  198. off = do_div(tmp, vol->usable_leb_size);
  199. lnum = tmp;
  200. do {
  201. cond_resched();
  202. if (off + len >= vol->usable_leb_size)
  203. len = vol->usable_leb_size - off;
  204. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  205. if (err)
  206. break;
  207. off += len;
  208. if (off == vol->usable_leb_size) {
  209. lnum += 1;
  210. off -= vol->usable_leb_size;
  211. }
  212. count -= len;
  213. *offp += len;
  214. err = copy_to_user(buf, tbuf, len);
  215. if (err) {
  216. err = -EFAULT;
  217. break;
  218. }
  219. buf += len;
  220. len = count > tbuf_size ? tbuf_size : count;
  221. } while (count);
  222. vfree(tbuf);
  223. return err ? err : count_save - count;
  224. }
  225. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  226. /*
  227. * This function allows to directly write to dynamic UBI volumes, without
  228. * issuing the volume update operation. Available only as a debugging feature.
  229. * Very useful for testing UBI.
  230. */
  231. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  232. size_t count, loff_t *offp)
  233. {
  234. struct ubi_volume_desc *desc = file->private_data;
  235. struct ubi_volume *vol = desc->vol;
  236. struct ubi_device *ubi = vol->ubi;
  237. int lnum, off, len, tbuf_size, err = 0;
  238. size_t count_save = count;
  239. char *tbuf;
  240. uint64_t tmp;
  241. dbg_gen("requested: write %zd bytes to offset %lld of volume %u",
  242. count, *offp, vol->vol_id);
  243. if (vol->vol_type == UBI_STATIC_VOLUME)
  244. return -EROFS;
  245. tmp = *offp;
  246. off = do_div(tmp, vol->usable_leb_size);
  247. lnum = tmp;
  248. if (off & (ubi->min_io_size - 1)) {
  249. dbg_err("unaligned position");
  250. return -EINVAL;
  251. }
  252. if (*offp + count > vol->used_bytes)
  253. count_save = count = vol->used_bytes - *offp;
  254. /* We can write only in fractions of the minimum I/O unit */
  255. if (count & (ubi->min_io_size - 1)) {
  256. dbg_err("unaligned write length");
  257. return -EINVAL;
  258. }
  259. tbuf_size = vol->usable_leb_size;
  260. if (count < tbuf_size)
  261. tbuf_size = ALIGN(count, ubi->min_io_size);
  262. tbuf = vmalloc(tbuf_size);
  263. if (!tbuf)
  264. return -ENOMEM;
  265. len = count > tbuf_size ? tbuf_size : count;
  266. while (count) {
  267. cond_resched();
  268. if (off + len >= vol->usable_leb_size)
  269. len = vol->usable_leb_size - off;
  270. err = copy_from_user(tbuf, buf, len);
  271. if (err) {
  272. err = -EFAULT;
  273. break;
  274. }
  275. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
  276. UBI_UNKNOWN);
  277. if (err)
  278. break;
  279. off += len;
  280. if (off == vol->usable_leb_size) {
  281. lnum += 1;
  282. off -= vol->usable_leb_size;
  283. }
  284. count -= len;
  285. *offp += len;
  286. buf += len;
  287. len = count > tbuf_size ? tbuf_size : count;
  288. }
  289. vfree(tbuf);
  290. return err ? err : count_save - count;
  291. }
  292. #else
  293. #define vol_cdev_direct_write(file, buf, count, offp) (-EPERM)
  294. #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
  295. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  296. size_t count, loff_t *offp)
  297. {
  298. int err = 0;
  299. struct ubi_volume_desc *desc = file->private_data;
  300. struct ubi_volume *vol = desc->vol;
  301. struct ubi_device *ubi = vol->ubi;
  302. if (!vol->updating && !vol->changing_leb)
  303. return vol_cdev_direct_write(file, buf, count, offp);
  304. if (vol->updating)
  305. err = ubi_more_update_data(ubi, vol, buf, count);
  306. else
  307. err = ubi_more_leb_change_data(ubi, vol, buf, count);
  308. if (err < 0) {
  309. ubi_err("cannot accept more %zd bytes of data, error %d",
  310. count, err);
  311. return err;
  312. }
  313. if (err) {
  314. /*
  315. * The operation is finished, @err contains number of actually
  316. * written bytes.
  317. */
  318. count = err;
  319. if (vol->changing_leb) {
  320. revoke_exclusive(desc, UBI_READWRITE);
  321. return count;
  322. }
  323. err = ubi_check_volume(ubi, vol->vol_id);
  324. if (err < 0)
  325. return err;
  326. if (err) {
  327. ubi_warn("volume %d on UBI device %d is corrupted",
  328. vol->vol_id, ubi->ubi_num);
  329. vol->corrupted = 1;
  330. }
  331. vol->checked = 1;
  332. ubi_gluebi_updated(vol);
  333. revoke_exclusive(desc, UBI_READWRITE);
  334. }
  335. return count;
  336. }
  337. static int vol_cdev_ioctl(struct inode *inode, struct file *file,
  338. unsigned int cmd, unsigned long arg)
  339. {
  340. int err = 0;
  341. struct ubi_volume_desc *desc = file->private_data;
  342. struct ubi_volume *vol = desc->vol;
  343. struct ubi_device *ubi = vol->ubi;
  344. void __user *argp = (void __user *)arg;
  345. switch (cmd) {
  346. /* Volume update command */
  347. case UBI_IOCVOLUP:
  348. {
  349. int64_t bytes, rsvd_bytes;
  350. if (!capable(CAP_SYS_RESOURCE)) {
  351. err = -EPERM;
  352. break;
  353. }
  354. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  355. if (err) {
  356. err = -EFAULT;
  357. break;
  358. }
  359. if (desc->mode == UBI_READONLY) {
  360. err = -EROFS;
  361. break;
  362. }
  363. rsvd_bytes = (long long)vol->reserved_pebs *
  364. ubi->leb_size-vol->data_pad;
  365. if (bytes < 0 || bytes > rsvd_bytes) {
  366. err = -EINVAL;
  367. break;
  368. }
  369. err = get_exclusive(desc);
  370. if (err < 0)
  371. break;
  372. err = ubi_start_update(ubi, vol, bytes);
  373. if (bytes == 0)
  374. revoke_exclusive(desc, UBI_READWRITE);
  375. break;
  376. }
  377. /* Atomic logical eraseblock change command */
  378. case UBI_IOCEBCH:
  379. {
  380. struct ubi_leb_change_req req;
  381. err = copy_from_user(&req, argp,
  382. sizeof(struct ubi_leb_change_req));
  383. if (err) {
  384. err = -EFAULT;
  385. break;
  386. }
  387. if (desc->mode == UBI_READONLY ||
  388. vol->vol_type == UBI_STATIC_VOLUME) {
  389. err = -EROFS;
  390. break;
  391. }
  392. /* Validate the request */
  393. err = -EINVAL;
  394. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  395. req.bytes < 0 || req.lnum >= vol->usable_leb_size)
  396. break;
  397. if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
  398. req.dtype != UBI_UNKNOWN)
  399. break;
  400. err = get_exclusive(desc);
  401. if (err < 0)
  402. break;
  403. err = ubi_start_leb_change(ubi, vol, &req);
  404. if (req.bytes == 0)
  405. revoke_exclusive(desc, UBI_READWRITE);
  406. break;
  407. }
  408. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  409. /* Logical eraseblock erasure command */
  410. case UBI_IOCEBER:
  411. {
  412. int32_t lnum;
  413. err = get_user(lnum, (__user int32_t *)argp);
  414. if (err) {
  415. err = -EFAULT;
  416. break;
  417. }
  418. if (desc->mode == UBI_READONLY ||
  419. vol->vol_type == UBI_STATIC_VOLUME) {
  420. err = -EROFS;
  421. break;
  422. }
  423. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  424. err = -EINVAL;
  425. break;
  426. }
  427. dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
  428. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  429. if (err)
  430. break;
  431. err = ubi_wl_flush(ubi);
  432. break;
  433. }
  434. #endif
  435. default:
  436. err = -ENOTTY;
  437. break;
  438. }
  439. return err;
  440. }
  441. /**
  442. * verify_mkvol_req - verify volume creation request.
  443. * @ubi: UBI device description object
  444. * @req: the request to check
  445. *
  446. * This function zero if the request is correct, and %-EINVAL if not.
  447. */
  448. static int verify_mkvol_req(const struct ubi_device *ubi,
  449. const struct ubi_mkvol_req *req)
  450. {
  451. int n, err = -EINVAL;
  452. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  453. req->name_len < 0)
  454. goto bad;
  455. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  456. req->vol_id != UBI_VOL_NUM_AUTO)
  457. goto bad;
  458. if (req->alignment == 0)
  459. goto bad;
  460. if (req->bytes == 0)
  461. goto bad;
  462. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  463. req->vol_type != UBI_STATIC_VOLUME)
  464. goto bad;
  465. if (req->alignment > ubi->leb_size)
  466. goto bad;
  467. n = req->alignment & (ubi->min_io_size - 1);
  468. if (req->alignment != 1 && n)
  469. goto bad;
  470. if (req->name_len > UBI_VOL_NAME_MAX) {
  471. err = -ENAMETOOLONG;
  472. goto bad;
  473. }
  474. n = strnlen(req->name, req->name_len + 1);
  475. if (n != req->name_len)
  476. goto bad;
  477. return 0;
  478. bad:
  479. dbg_err("bad volume creation request");
  480. ubi_dbg_dump_mkvol_req(req);
  481. return err;
  482. }
  483. /**
  484. * verify_rsvol_req - verify volume re-size request.
  485. * @ubi: UBI device description object
  486. * @req: the request to check
  487. *
  488. * This function returns zero if the request is correct, and %-EINVAL if not.
  489. */
  490. static int verify_rsvol_req(const struct ubi_device *ubi,
  491. const struct ubi_rsvol_req *req)
  492. {
  493. if (req->bytes <= 0)
  494. return -EINVAL;
  495. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  496. return -EINVAL;
  497. return 0;
  498. }
  499. /**
  500. * rename_volumes - rename UBI volumes.
  501. * @ubi: UBI device description object
  502. * @req: volumes re-name request
  503. *
  504. * This is a helper function for the volume re-name IOCTL which validates the
  505. * the request, opens the volume and calls corresponding volumes management
  506. * function. Returns zero in case of success and a negative error code in case
  507. * of failure.
  508. */
  509. static int rename_volumes(struct ubi_device *ubi,
  510. struct ubi_rnvol_req *req)
  511. {
  512. int i, n, err;
  513. struct list_head rename_list;
  514. struct ubi_rename_entry *re, *re1;
  515. if (req->count < 0 || req->count > UBI_MAX_RNVOL)
  516. return -EINVAL;
  517. if (req->count == 0)
  518. return 0;
  519. /* Validate volume IDs and names in the request */
  520. for (i = 0; i < req->count; i++) {
  521. if (req->ents[i].vol_id < 0 ||
  522. req->ents[i].vol_id >= ubi->vtbl_slots)
  523. return -EINVAL;
  524. if (req->ents[i].name_len < 0)
  525. return -EINVAL;
  526. if (req->ents[i].name_len > UBI_VOL_NAME_MAX)
  527. return -ENAMETOOLONG;
  528. req->ents[i].name[req->ents[i].name_len] = '\0';
  529. n = strlen(req->ents[i].name);
  530. if (n != req->ents[i].name_len)
  531. err = -EINVAL;
  532. }
  533. /* Make sure volume IDs and names are unique */
  534. for (i = 0; i < req->count - 1; i++) {
  535. for (n = i + 1; n < req->count; n++) {
  536. if (req->ents[i].vol_id == req->ents[n].vol_id) {
  537. dbg_err("duplicated volume id %d",
  538. req->ents[i].vol_id);
  539. return -EINVAL;
  540. }
  541. if (!strcmp(req->ents[i].name, req->ents[n].name)) {
  542. dbg_err("duplicated volume name \"%s\"",
  543. req->ents[i].name);
  544. return -EINVAL;
  545. }
  546. }
  547. }
  548. /* Create the re-name list */
  549. INIT_LIST_HEAD(&rename_list);
  550. for (i = 0; i < req->count; i++) {
  551. int vol_id = req->ents[i].vol_id;
  552. int name_len = req->ents[i].name_len;
  553. const char *name = req->ents[i].name;
  554. re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  555. if (!re) {
  556. err = -ENOMEM;
  557. goto out_free;
  558. }
  559. re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  560. if (IS_ERR(re->desc)) {
  561. err = PTR_ERR(re->desc);
  562. dbg_err("cannot open volume %d, error %d", vol_id, err);
  563. kfree(re);
  564. goto out_free;
  565. }
  566. /* Skip this re-naming if the name does not really change */
  567. if (re->desc->vol->name_len == name_len &&
  568. !memcmp(re->desc->vol->name, name, name_len)) {
  569. ubi_close_volume(re->desc);
  570. kfree(re);
  571. continue;
  572. }
  573. re->new_name_len = name_len;
  574. memcpy(re->new_name, name, name_len);
  575. list_add_tail(&re->list, &rename_list);
  576. dbg_msg("will rename volume %d from \"%s\" to \"%s\"",
  577. vol_id, re->desc->vol->name, name);
  578. }
  579. if (list_empty(&rename_list))
  580. return 0;
  581. /* Find out the volumes which have to be removed */
  582. list_for_each_entry(re, &rename_list, list) {
  583. struct ubi_volume_desc *desc;
  584. int no_remove_needed = 0;
  585. /*
  586. * Volume @re->vol_id is going to be re-named to
  587. * @re->new_name, while its current name is @name. If a volume
  588. * with name @re->new_name currently exists, it has to be
  589. * removed, unless it is also re-named in the request (@req).
  590. */
  591. list_for_each_entry(re1, &rename_list, list) {
  592. if (re->new_name_len == re1->desc->vol->name_len &&
  593. !memcmp(re->new_name, re1->desc->vol->name,
  594. re1->desc->vol->name_len)) {
  595. no_remove_needed = 1;
  596. break;
  597. }
  598. }
  599. if (no_remove_needed)
  600. continue;
  601. /*
  602. * It seems we need to remove volume with name @re->new_name,
  603. * if it exists.
  604. */
  605. desc = ubi_open_volume_nm(ubi->ubi_num, re->new_name,
  606. UBI_EXCLUSIVE);
  607. if (IS_ERR(desc)) {
  608. err = PTR_ERR(desc);
  609. if (err == -ENODEV)
  610. /* Re-naming into a non-existing volume name */
  611. continue;
  612. /* The volume exists but busy, or an error occurred */
  613. dbg_err("cannot open volume \"%s\", error %d",
  614. re->new_name, err);
  615. goto out_free;
  616. }
  617. re = kzalloc(sizeof(struct ubi_rename_entry), GFP_KERNEL);
  618. if (!re) {
  619. err = -ENOMEM;
  620. ubi_close_volume(desc);
  621. goto out_free;
  622. }
  623. re->remove = 1;
  624. re->desc = desc;
  625. list_add(&re->list, &rename_list);
  626. dbg_msg("will remove volume %d, name \"%s\"",
  627. re->desc->vol->vol_id, re->desc->vol->name);
  628. }
  629. mutex_lock(&ubi->volumes_mutex);
  630. err = ubi_rename_volumes(ubi, &rename_list);
  631. mutex_unlock(&ubi->volumes_mutex);
  632. out_free:
  633. list_for_each_entry_safe(re, re1, &rename_list, list) {
  634. ubi_close_volume(re->desc);
  635. list_del(&re->list);
  636. kfree(re);
  637. }
  638. return err;
  639. }
  640. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  641. unsigned int cmd, unsigned long arg)
  642. {
  643. int err = 0;
  644. struct ubi_device *ubi;
  645. struct ubi_volume_desc *desc;
  646. void __user *argp = (void __user *)arg;
  647. if (!capable(CAP_SYS_RESOURCE))
  648. return -EPERM;
  649. ubi = ubi_get_by_major(imajor(inode));
  650. if (!ubi)
  651. return -ENODEV;
  652. switch (cmd) {
  653. /* Create volume command */
  654. case UBI_IOCMKVOL:
  655. {
  656. struct ubi_mkvol_req req;
  657. dbg_gen("create volume");
  658. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  659. if (err) {
  660. err = -EFAULT;
  661. break;
  662. }
  663. req.name[req.name_len] = '\0';
  664. err = verify_mkvol_req(ubi, &req);
  665. if (err)
  666. break;
  667. mutex_lock(&ubi->volumes_mutex);
  668. err = ubi_create_volume(ubi, &req);
  669. mutex_unlock(&ubi->volumes_mutex);
  670. if (err)
  671. break;
  672. err = put_user(req.vol_id, (__user int32_t *)argp);
  673. if (err)
  674. err = -EFAULT;
  675. break;
  676. }
  677. /* Remove volume command */
  678. case UBI_IOCRMVOL:
  679. {
  680. int vol_id;
  681. dbg_gen("remove volume");
  682. err = get_user(vol_id, (__user int32_t *)argp);
  683. if (err) {
  684. err = -EFAULT;
  685. break;
  686. }
  687. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  688. if (IS_ERR(desc)) {
  689. err = PTR_ERR(desc);
  690. break;
  691. }
  692. mutex_lock(&ubi->volumes_mutex);
  693. err = ubi_remove_volume(desc, 0);
  694. mutex_unlock(&ubi->volumes_mutex);
  695. /*
  696. * The volume is deleted (unless an error occurred), and the
  697. * 'struct ubi_volume' object will be freed when
  698. * 'ubi_close_volume()' will call 'put_device()'.
  699. */
  700. ubi_close_volume(desc);
  701. break;
  702. }
  703. /* Re-size volume command */
  704. case UBI_IOCRSVOL:
  705. {
  706. int pebs;
  707. uint64_t tmp;
  708. struct ubi_rsvol_req req;
  709. dbg_gen("re-size volume");
  710. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  711. if (err) {
  712. err = -EFAULT;
  713. break;
  714. }
  715. err = verify_rsvol_req(ubi, &req);
  716. if (err)
  717. break;
  718. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  719. if (IS_ERR(desc)) {
  720. err = PTR_ERR(desc);
  721. break;
  722. }
  723. tmp = req.bytes;
  724. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  725. pebs += tmp;
  726. mutex_lock(&ubi->volumes_mutex);
  727. err = ubi_resize_volume(desc, pebs);
  728. mutex_unlock(&ubi->volumes_mutex);
  729. ubi_close_volume(desc);
  730. break;
  731. }
  732. /* Re-name volumes command */
  733. case UBI_IOCRNVOL:
  734. {
  735. struct ubi_rnvol_req *req;
  736. dbg_msg("re-name volumes");
  737. req = kmalloc(sizeof(struct ubi_rnvol_req), GFP_KERNEL);
  738. if (!req) {
  739. err = -ENOMEM;
  740. break;
  741. };
  742. err = copy_from_user(req, argp, sizeof(struct ubi_rnvol_req));
  743. if (err) {
  744. err = -EFAULT;
  745. kfree(req);
  746. break;
  747. }
  748. mutex_lock(&ubi->mult_mutex);
  749. err = rename_volumes(ubi, req);
  750. mutex_unlock(&ubi->mult_mutex);
  751. kfree(req);
  752. break;
  753. }
  754. default:
  755. err = -ENOTTY;
  756. break;
  757. }
  758. ubi_put_device(ubi);
  759. return err;
  760. }
  761. static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
  762. unsigned int cmd, unsigned long arg)
  763. {
  764. int err = 0;
  765. void __user *argp = (void __user *)arg;
  766. if (!capable(CAP_SYS_RESOURCE))
  767. return -EPERM;
  768. switch (cmd) {
  769. /* Attach an MTD device command */
  770. case UBI_IOCATT:
  771. {
  772. struct ubi_attach_req req;
  773. struct mtd_info *mtd;
  774. dbg_gen("attach MTD device");
  775. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  776. if (err) {
  777. err = -EFAULT;
  778. break;
  779. }
  780. if (req.mtd_num < 0 ||
  781. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  782. err = -EINVAL;
  783. break;
  784. }
  785. mtd = get_mtd_device(NULL, req.mtd_num);
  786. if (IS_ERR(mtd)) {
  787. err = PTR_ERR(mtd);
  788. break;
  789. }
  790. /*
  791. * Note, further request verification is done by
  792. * 'ubi_attach_mtd_dev()'.
  793. */
  794. mutex_lock(&ubi_devices_mutex);
  795. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
  796. mutex_unlock(&ubi_devices_mutex);
  797. if (err < 0)
  798. put_mtd_device(mtd);
  799. else
  800. /* @err contains UBI device number */
  801. err = put_user(err, (__user int32_t *)argp);
  802. break;
  803. }
  804. /* Detach an MTD device command */
  805. case UBI_IOCDET:
  806. {
  807. int ubi_num;
  808. dbg_gen("dettach MTD device");
  809. err = get_user(ubi_num, (__user int32_t *)argp);
  810. if (err) {
  811. err = -EFAULT;
  812. break;
  813. }
  814. mutex_lock(&ubi_devices_mutex);
  815. err = ubi_detach_mtd_dev(ubi_num, 0);
  816. mutex_unlock(&ubi_devices_mutex);
  817. break;
  818. }
  819. default:
  820. err = -ENOTTY;
  821. break;
  822. }
  823. return err;
  824. }
  825. /* UBI control character device operations */
  826. struct file_operations ubi_ctrl_cdev_operations = {
  827. .ioctl = ctrl_cdev_ioctl,
  828. .owner = THIS_MODULE,
  829. };
  830. /* UBI character device operations */
  831. struct file_operations ubi_cdev_operations = {
  832. .owner = THIS_MODULE,
  833. .ioctl = ubi_cdev_ioctl,
  834. .llseek = no_llseek,
  835. };
  836. /* UBI volume character device operations */
  837. struct file_operations ubi_vol_cdev_operations = {
  838. .owner = THIS_MODULE,
  839. .open = vol_cdev_open,
  840. .release = vol_cdev_release,
  841. .llseek = vol_cdev_llseek,
  842. .read = vol_cdev_read,
  843. .write = vol_cdev_write,
  844. .ioctl = vol_cdev_ioctl,
  845. };