cdev.c 25 KB

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