cdev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #include <linux/module.h>
  32. #include <linux/stat.h>
  33. #include <linux/ioctl.h>
  34. #include <linux/capability.h>
  35. #include <mtd/ubi-user.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/div64.h>
  38. #include "ubi.h"
  39. /*
  40. * Maximum sequence numbers of UBI and volume character device IOCTLs (direct
  41. * logical eraseblock erase is a debug-only feature).
  42. */
  43. #define UBI_CDEV_IOC_MAX_SEQ 2
  44. #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  45. #define VOL_CDEV_IOC_MAX_SEQ 1
  46. #else
  47. #define VOL_CDEV_IOC_MAX_SEQ 2
  48. #endif
  49. /**
  50. * major_to_device - get UBI device object by character device major number.
  51. * @major: major number
  52. *
  53. * This function returns a pointer to the UBI device object.
  54. */
  55. static struct ubi_device *major_to_device(int major)
  56. {
  57. int i;
  58. for (i = 0; i < ubi_devices_cnt; i++)
  59. if (ubi_devices[i] && ubi_devices[i]->major == major)
  60. return ubi_devices[i];
  61. BUG();
  62. }
  63. /**
  64. * get_exclusive - get exclusive access to an UBI volume.
  65. * @desc: volume descriptor
  66. *
  67. * This function changes UBI volume open mode to "exclusive". Returns previous
  68. * mode value (positive integer) in case of success and a negative error code
  69. * in case of failure.
  70. */
  71. static int get_exclusive(struct ubi_volume_desc *desc)
  72. {
  73. int users, err;
  74. struct ubi_volume *vol = desc->vol;
  75. spin_lock(&vol->ubi->volumes_lock);
  76. users = vol->readers + vol->writers + vol->exclusive;
  77. ubi_assert(users > 0);
  78. if (users > 1) {
  79. dbg_err("%d users for volume %d", users, vol->vol_id);
  80. err = -EBUSY;
  81. } else {
  82. vol->readers = vol->writers = 0;
  83. vol->exclusive = 1;
  84. err = desc->mode;
  85. desc->mode = UBI_EXCLUSIVE;
  86. }
  87. spin_unlock(&vol->ubi->volumes_lock);
  88. return err;
  89. }
  90. /**
  91. * revoke_exclusive - revoke exclusive mode.
  92. * @desc: volume descriptor
  93. * @mode: new mode to switch to
  94. */
  95. static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
  96. {
  97. struct ubi_volume *vol = desc->vol;
  98. spin_lock(&vol->ubi->volumes_lock);
  99. ubi_assert(vol->readers == 0 && vol->writers == 0);
  100. ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
  101. vol->exclusive = 0;
  102. if (mode == UBI_READONLY)
  103. vol->readers = 1;
  104. else if (mode == UBI_READWRITE)
  105. vol->writers = 1;
  106. else
  107. vol->exclusive = 1;
  108. spin_unlock(&vol->ubi->volumes_lock);
  109. desc->mode = mode;
  110. }
  111. static int vol_cdev_open(struct inode *inode, struct file *file)
  112. {
  113. struct ubi_volume_desc *desc;
  114. const struct ubi_device *ubi = major_to_device(imajor(inode));
  115. int vol_id = iminor(inode) - 1;
  116. int mode;
  117. if (file->f_mode & FMODE_WRITE)
  118. mode = UBI_READWRITE;
  119. else
  120. mode = UBI_READONLY;
  121. dbg_msg("open volume %d, mode %d", vol_id, mode);
  122. desc = ubi_open_volume(ubi->ubi_num, vol_id, mode);
  123. if (IS_ERR(desc))
  124. return PTR_ERR(desc);
  125. file->private_data = desc;
  126. return 0;
  127. }
  128. static int vol_cdev_release(struct inode *inode, struct file *file)
  129. {
  130. struct ubi_volume_desc *desc = file->private_data;
  131. struct ubi_volume *vol = desc->vol;
  132. dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
  133. if (vol->updating) {
  134. ubi_warn("update of volume %d not finished, volume is damaged",
  135. vol->vol_id);
  136. vol->updating = 0;
  137. kfree(vol->upd_buf);
  138. }
  139. ubi_close_volume(desc);
  140. return 0;
  141. }
  142. static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
  143. {
  144. struct ubi_volume_desc *desc = file->private_data;
  145. struct ubi_volume *vol = desc->vol;
  146. loff_t new_offset;
  147. if (vol->updating) {
  148. /* Update is in progress, seeking is prohibited */
  149. dbg_err("updating");
  150. return -EBUSY;
  151. }
  152. switch (origin) {
  153. case 0: /* SEEK_SET */
  154. new_offset = offset;
  155. break;
  156. case 1: /* SEEK_CUR */
  157. new_offset = file->f_pos + offset;
  158. break;
  159. case 2: /* SEEK_END */
  160. new_offset = vol->used_bytes + offset;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. if (new_offset < 0 || new_offset > vol->used_bytes) {
  166. dbg_err("bad seek %lld", new_offset);
  167. return -EINVAL;
  168. }
  169. dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
  170. vol->vol_id, offset, origin, new_offset);
  171. file->f_pos = new_offset;
  172. return new_offset;
  173. }
  174. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  175. loff_t *offp)
  176. {
  177. struct ubi_volume_desc *desc = file->private_data;
  178. struct ubi_volume *vol = desc->vol;
  179. struct ubi_device *ubi = vol->ubi;
  180. int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size;
  181. size_t count_save = count;
  182. void *tbuf;
  183. uint64_t tmp;
  184. dbg_msg("read %zd bytes from offset %lld of volume %d",
  185. count, *offp, vol_id);
  186. if (vol->updating) {
  187. dbg_err("updating");
  188. return -EBUSY;
  189. }
  190. if (vol->upd_marker) {
  191. dbg_err("damaged volume, update marker is set");
  192. return -EBADF;
  193. }
  194. if (*offp == vol->used_bytes || count == 0)
  195. return 0;
  196. if (vol->corrupted)
  197. dbg_msg("read from corrupted volume %d", vol_id);
  198. if (*offp + count > vol->used_bytes)
  199. count_save = count = vol->used_bytes - *offp;
  200. tbuf_size = vol->usable_leb_size;
  201. if (count < tbuf_size)
  202. tbuf_size = ALIGN(count, ubi->min_io_size);
  203. tbuf = kmalloc(tbuf_size, GFP_KERNEL);
  204. if (!tbuf)
  205. return -ENOMEM;
  206. len = count > tbuf_size ? tbuf_size : count;
  207. tmp = *offp;
  208. off = do_div(tmp, vol->usable_leb_size);
  209. lnum = tmp;
  210. do {
  211. cond_resched();
  212. if (off + len >= vol->usable_leb_size)
  213. len = vol->usable_leb_size - off;
  214. err = ubi_eba_read_leb(ubi, vol_id, lnum, tbuf, off, len, 0);
  215. if (err)
  216. break;
  217. off += len;
  218. if (off == vol->usable_leb_size) {
  219. lnum += 1;
  220. off -= vol->usable_leb_size;
  221. }
  222. count -= len;
  223. *offp += len;
  224. err = copy_to_user(buf, tbuf, len);
  225. if (err) {
  226. err = -EFAULT;
  227. break;
  228. }
  229. buf += len;
  230. len = count > tbuf_size ? tbuf_size : count;
  231. } while (count);
  232. kfree(tbuf);
  233. return err ? err : count_save - count;
  234. }
  235. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  236. /*
  237. * This function allows to directly write to dynamic UBI volumes, without
  238. * issuing the volume update operation. Available only as a debugging feature.
  239. * Very useful for testing UBI.
  240. */
  241. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  242. size_t count, loff_t *offp)
  243. {
  244. struct ubi_volume_desc *desc = file->private_data;
  245. struct ubi_volume *vol = desc->vol;
  246. struct ubi_device *ubi = vol->ubi;
  247. int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0;
  248. size_t count_save = count;
  249. char *tbuf;
  250. uint64_t tmp;
  251. dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
  252. count, *offp, desc->vol->vol_id);
  253. if (vol->vol_type == UBI_STATIC_VOLUME)
  254. return -EROFS;
  255. tmp = *offp;
  256. off = do_div(tmp, vol->usable_leb_size);
  257. lnum = tmp;
  258. if (off % ubi->min_io_size) {
  259. dbg_err("unaligned position");
  260. return -EINVAL;
  261. }
  262. if (*offp + count > vol->used_bytes)
  263. count_save = count = vol->used_bytes - *offp;
  264. /* We can write only in fractions of the minimum I/O unit */
  265. if (count % ubi->min_io_size) {
  266. dbg_err("unaligned write length");
  267. return -EINVAL;
  268. }
  269. tbuf_size = vol->usable_leb_size;
  270. if (count < tbuf_size)
  271. tbuf_size = ALIGN(count, ubi->min_io_size);
  272. tbuf = kmalloc(tbuf_size, GFP_KERNEL);
  273. if (!tbuf)
  274. return -ENOMEM;
  275. len = count > tbuf_size ? tbuf_size : count;
  276. while (count) {
  277. cond_resched();
  278. if (off + len >= vol->usable_leb_size)
  279. len = vol->usable_leb_size - off;
  280. err = copy_from_user(tbuf, buf, len);
  281. if (err) {
  282. err = -EFAULT;
  283. break;
  284. }
  285. err = ubi_eba_write_leb(ubi, vol_id, lnum, tbuf, off, len,
  286. UBI_UNKNOWN);
  287. if (err)
  288. break;
  289. off += len;
  290. if (off == vol->usable_leb_size) {
  291. lnum += 1;
  292. off -= vol->usable_leb_size;
  293. }
  294. count -= len;
  295. *offp += len;
  296. buf += len;
  297. len = count > tbuf_size ? tbuf_size : count;
  298. }
  299. kfree(tbuf);
  300. return err ? err : count_save - count;
  301. }
  302. #else
  303. #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
  304. #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
  305. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  306. size_t count, loff_t *offp)
  307. {
  308. int err = 0;
  309. struct ubi_volume_desc *desc = file->private_data;
  310. struct ubi_volume *vol = desc->vol;
  311. struct ubi_device *ubi = vol->ubi;
  312. if (!vol->updating)
  313. return vol_cdev_direct_write(file, buf, count, offp);
  314. err = ubi_more_update_data(ubi, vol->vol_id, buf, count);
  315. if (err < 0) {
  316. ubi_err("cannot write %zd bytes of update data", count);
  317. return err;
  318. }
  319. if (err) {
  320. /*
  321. * Update is finished, @err contains number of actually written
  322. * bytes now.
  323. */
  324. count = err;
  325. err = ubi_check_volume(ubi, vol->vol_id);
  326. if (err < 0)
  327. return err;
  328. if (err) {
  329. ubi_warn("volume %d on UBI device %d is corrupted",
  330. vol->vol_id, ubi->ubi_num);
  331. vol->corrupted = 1;
  332. }
  333. vol->checked = 1;
  334. revoke_exclusive(desc, UBI_READWRITE);
  335. }
  336. *offp += count;
  337. return count;
  338. }
  339. static int vol_cdev_ioctl(struct inode *inode, struct file *file,
  340. unsigned int cmd, unsigned long arg)
  341. {
  342. int err = 0;
  343. struct ubi_volume_desc *desc = file->private_data;
  344. struct ubi_volume *vol = desc->vol;
  345. struct ubi_device *ubi = vol->ubi;
  346. void __user *argp = (void __user *)arg;
  347. if (_IOC_NR(cmd) > VOL_CDEV_IOC_MAX_SEQ ||
  348. _IOC_TYPE(cmd) != UBI_VOL_IOC_MAGIC)
  349. return -ENOTTY;
  350. if (_IOC_DIR(cmd) && _IOC_READ)
  351. err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
  352. else if (_IOC_DIR(cmd) && _IOC_WRITE)
  353. err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
  354. if (err)
  355. return -EFAULT;
  356. switch (cmd) {
  357. /* Volume update command */
  358. case UBI_IOCVOLUP:
  359. {
  360. int64_t bytes, rsvd_bytes;
  361. if (!capable(CAP_SYS_RESOURCE)) {
  362. err = -EPERM;
  363. break;
  364. }
  365. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  366. if (err) {
  367. err = -EFAULT;
  368. break;
  369. }
  370. if (desc->mode == UBI_READONLY) {
  371. err = -EROFS;
  372. break;
  373. }
  374. rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
  375. if (bytes < 0 || bytes > rsvd_bytes) {
  376. err = -EINVAL;
  377. break;
  378. }
  379. err = get_exclusive(desc);
  380. if (err < 0)
  381. break;
  382. err = ubi_start_update(ubi, vol->vol_id, bytes);
  383. if (bytes == 0)
  384. revoke_exclusive(desc, UBI_READWRITE);
  385. file->f_pos = 0;
  386. break;
  387. }
  388. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  389. /* Logical eraseblock erasure command */
  390. case UBI_IOCEBER:
  391. {
  392. int32_t lnum;
  393. err = __get_user(lnum, (__user int32_t *)argp);
  394. if (err) {
  395. err = -EFAULT;
  396. break;
  397. }
  398. if (desc->mode == UBI_READONLY) {
  399. err = -EROFS;
  400. break;
  401. }
  402. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  403. err = -EINVAL;
  404. break;
  405. }
  406. if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
  407. err = -EROFS;
  408. break;
  409. }
  410. dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
  411. err = ubi_eba_unmap_leb(ubi, vol->vol_id, lnum);
  412. if (err)
  413. break;
  414. err = ubi_wl_flush(ubi);
  415. break;
  416. }
  417. #endif
  418. default:
  419. err = -ENOTTY;
  420. break;
  421. }
  422. return err;
  423. }
  424. /**
  425. * verify_mkvol_req - verify volume creation request.
  426. * @ubi: UBI device description object
  427. * @req: the request to check
  428. *
  429. * This function zero if the request is correct, and %-EINVAL if not.
  430. */
  431. static int verify_mkvol_req(const struct ubi_device *ubi,
  432. const struct ubi_mkvol_req *req)
  433. {
  434. int n, err = -EINVAL;
  435. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  436. req->name_len < 0)
  437. goto bad;
  438. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  439. req->vol_id != UBI_VOL_NUM_AUTO)
  440. goto bad;
  441. if (req->alignment == 0)
  442. goto bad;
  443. if (req->bytes == 0)
  444. goto bad;
  445. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  446. req->vol_type != UBI_STATIC_VOLUME)
  447. goto bad;
  448. if (req->alignment > ubi->leb_size)
  449. goto bad;
  450. n = req->alignment % ubi->min_io_size;
  451. if (req->alignment != 1 && n)
  452. goto bad;
  453. if (req->name_len > UBI_VOL_NAME_MAX) {
  454. err = -ENAMETOOLONG;
  455. goto bad;
  456. }
  457. return 0;
  458. bad:
  459. dbg_err("bad volume creation request");
  460. ubi_dbg_dump_mkvol_req(req);
  461. return err;
  462. }
  463. /**
  464. * verify_rsvol_req - verify volume re-size request.
  465. * @ubi: UBI device description object
  466. * @req: the request to check
  467. *
  468. * This function returns zero if the request is correct, and %-EINVAL if not.
  469. */
  470. static int verify_rsvol_req(const struct ubi_device *ubi,
  471. const struct ubi_rsvol_req *req)
  472. {
  473. if (req->bytes <= 0)
  474. return -EINVAL;
  475. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  476. return -EINVAL;
  477. return 0;
  478. }
  479. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  480. unsigned int cmd, unsigned long arg)
  481. {
  482. int err = 0;
  483. struct ubi_device *ubi;
  484. struct ubi_volume_desc *desc;
  485. void __user *argp = (void __user *)arg;
  486. if (_IOC_NR(cmd) > UBI_CDEV_IOC_MAX_SEQ ||
  487. _IOC_TYPE(cmd) != UBI_IOC_MAGIC)
  488. return -ENOTTY;
  489. if (_IOC_DIR(cmd) && _IOC_READ)
  490. err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
  491. else if (_IOC_DIR(cmd) && _IOC_WRITE)
  492. err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
  493. if (err)
  494. return -EFAULT;
  495. if (!capable(CAP_SYS_RESOURCE))
  496. return -EPERM;
  497. ubi = major_to_device(imajor(inode));
  498. if (IS_ERR(ubi))
  499. return PTR_ERR(ubi);
  500. switch (cmd) {
  501. /* Create volume command */
  502. case UBI_IOCMKVOL:
  503. {
  504. struct ubi_mkvol_req req;
  505. dbg_msg("create volume");
  506. err = __copy_from_user(&req, argp,
  507. sizeof(struct ubi_mkvol_req));
  508. if (err) {
  509. err = -EFAULT;
  510. break;
  511. }
  512. err = verify_mkvol_req(ubi, &req);
  513. if (err)
  514. break;
  515. req.name[req.name_len] = '\0';
  516. err = ubi_create_volume(ubi, &req);
  517. if (err)
  518. break;
  519. err = __put_user(req.vol_id, (__user int32_t *)argp);
  520. if (err)
  521. err = -EFAULT;
  522. break;
  523. }
  524. /* Remove volume command */
  525. case UBI_IOCRMVOL:
  526. {
  527. int vol_id;
  528. dbg_msg("remove volume");
  529. err = __get_user(vol_id, (__user int32_t *)argp);
  530. if (err) {
  531. err = -EFAULT;
  532. break;
  533. }
  534. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  535. if (IS_ERR(desc)) {
  536. err = PTR_ERR(desc);
  537. break;
  538. }
  539. err = ubi_remove_volume(desc);
  540. if (err)
  541. ubi_close_volume(desc);
  542. break;
  543. }
  544. /* Re-size volume command */
  545. case UBI_IOCRSVOL:
  546. {
  547. int pebs;
  548. uint64_t tmp;
  549. struct ubi_rsvol_req req;
  550. dbg_msg("re-size volume");
  551. err = __copy_from_user(&req, argp,
  552. sizeof(struct ubi_rsvol_req));
  553. if (err) {
  554. err = -EFAULT;
  555. break;
  556. }
  557. err = verify_rsvol_req(ubi, &req);
  558. if (err)
  559. break;
  560. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  561. if (IS_ERR(desc)) {
  562. err = PTR_ERR(desc);
  563. break;
  564. }
  565. tmp = req.bytes;
  566. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  567. pebs += tmp;
  568. err = ubi_resize_volume(desc, pebs);
  569. ubi_close_volume(desc);
  570. break;
  571. }
  572. default:
  573. err = -ENOTTY;
  574. break;
  575. }
  576. return err;
  577. }
  578. /* UBI character device operations */
  579. struct file_operations ubi_cdev_operations = {
  580. .owner = THIS_MODULE,
  581. .ioctl = ubi_cdev_ioctl,
  582. .llseek = no_llseek
  583. };
  584. /* UBI volume character device operations */
  585. struct file_operations ubi_vol_cdev_operations = {
  586. .owner = THIS_MODULE,
  587. .open = vol_cdev_open,
  588. .release = vol_cdev_release,
  589. .llseek = vol_cdev_llseek,
  590. .read = vol_cdev_read,
  591. .write = vol_cdev_write,
  592. .ioctl = vol_cdev_ioctl
  593. };