cdev.c 23 KB

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