cdev.c 25 KB

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