cdev.c 24 KB

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