cdev.c 25 KB

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