cdev.c 25 KB

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