cdev.c 25 KB

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