cdev.c 22 KB

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