cdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. vfree(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 = vmalloc(tbuf_size);
  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. vfree(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 = vmalloc(tbuf_size);
  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. vfree(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. ubi_gluebi_updated(vol);
  335. revoke_exclusive(desc, UBI_READWRITE);
  336. }
  337. *offp += count;
  338. return count;
  339. }
  340. static int vol_cdev_ioctl(struct inode *inode, struct file *file,
  341. unsigned int cmd, unsigned long arg)
  342. {
  343. int err = 0;
  344. struct ubi_volume_desc *desc = file->private_data;
  345. struct ubi_volume *vol = desc->vol;
  346. struct ubi_device *ubi = vol->ubi;
  347. void __user *argp = (void __user *)arg;
  348. switch (cmd) {
  349. /* Volume update command */
  350. case UBI_IOCVOLUP:
  351. {
  352. int64_t bytes, rsvd_bytes;
  353. if (!capable(CAP_SYS_RESOURCE)) {
  354. err = -EPERM;
  355. break;
  356. }
  357. err = copy_from_user(&bytes, argp, sizeof(int64_t));
  358. if (err) {
  359. err = -EFAULT;
  360. break;
  361. }
  362. if (desc->mode == UBI_READONLY) {
  363. err = -EROFS;
  364. break;
  365. }
  366. rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
  367. if (bytes < 0 || bytes > rsvd_bytes) {
  368. err = -EINVAL;
  369. break;
  370. }
  371. err = get_exclusive(desc);
  372. if (err < 0)
  373. break;
  374. err = ubi_start_update(ubi, vol->vol_id, bytes);
  375. if (bytes == 0)
  376. revoke_exclusive(desc, UBI_READWRITE);
  377. file->f_pos = 0;
  378. break;
  379. }
  380. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  381. /* Logical eraseblock erasure command */
  382. case UBI_IOCEBER:
  383. {
  384. int32_t lnum;
  385. err = get_user(lnum, (__user int32_t *)argp);
  386. if (err) {
  387. err = -EFAULT;
  388. break;
  389. }
  390. if (desc->mode == UBI_READONLY) {
  391. err = -EROFS;
  392. break;
  393. }
  394. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  395. err = -EINVAL;
  396. break;
  397. }
  398. if (vol->vol_type != UBI_DYNAMIC_VOLUME) {
  399. err = -EROFS;
  400. break;
  401. }
  402. dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
  403. err = ubi_eba_unmap_leb(ubi, vol->vol_id, lnum);
  404. if (err)
  405. break;
  406. err = ubi_wl_flush(ubi);
  407. break;
  408. }
  409. #endif
  410. default:
  411. err = -ENOTTY;
  412. break;
  413. }
  414. return err;
  415. }
  416. /**
  417. * verify_mkvol_req - verify volume creation request.
  418. * @ubi: UBI device description object
  419. * @req: the request to check
  420. *
  421. * This function zero if the request is correct, and %-EINVAL if not.
  422. */
  423. static int verify_mkvol_req(const struct ubi_device *ubi,
  424. const struct ubi_mkvol_req *req)
  425. {
  426. int n, err = -EINVAL;
  427. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  428. req->name_len < 0)
  429. goto bad;
  430. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  431. req->vol_id != UBI_VOL_NUM_AUTO)
  432. goto bad;
  433. if (req->alignment == 0)
  434. goto bad;
  435. if (req->bytes == 0)
  436. goto bad;
  437. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  438. req->vol_type != UBI_STATIC_VOLUME)
  439. goto bad;
  440. if (req->alignment > ubi->leb_size)
  441. goto bad;
  442. n = req->alignment % ubi->min_io_size;
  443. if (req->alignment != 1 && n)
  444. goto bad;
  445. if (req->name_len > UBI_VOL_NAME_MAX) {
  446. err = -ENAMETOOLONG;
  447. goto bad;
  448. }
  449. return 0;
  450. bad:
  451. dbg_err("bad volume creation request");
  452. ubi_dbg_dump_mkvol_req(req);
  453. return err;
  454. }
  455. /**
  456. * verify_rsvol_req - verify volume re-size request.
  457. * @ubi: UBI device description object
  458. * @req: the request to check
  459. *
  460. * This function returns zero if the request is correct, and %-EINVAL if not.
  461. */
  462. static int verify_rsvol_req(const struct ubi_device *ubi,
  463. const struct ubi_rsvol_req *req)
  464. {
  465. if (req->bytes <= 0)
  466. return -EINVAL;
  467. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  468. return -EINVAL;
  469. return 0;
  470. }
  471. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  472. unsigned int cmd, unsigned long arg)
  473. {
  474. int err = 0;
  475. struct ubi_device *ubi;
  476. struct ubi_volume_desc *desc;
  477. void __user *argp = (void __user *)arg;
  478. if (!capable(CAP_SYS_RESOURCE))
  479. return -EPERM;
  480. ubi = major_to_device(imajor(inode));
  481. if (IS_ERR(ubi))
  482. return PTR_ERR(ubi);
  483. switch (cmd) {
  484. /* Create volume command */
  485. case UBI_IOCMKVOL:
  486. {
  487. struct ubi_mkvol_req req;
  488. dbg_msg("create volume");
  489. err = copy_from_user(&req, argp,
  490. sizeof(struct ubi_mkvol_req));
  491. if (err) {
  492. err = -EFAULT;
  493. break;
  494. }
  495. err = verify_mkvol_req(ubi, &req);
  496. if (err)
  497. break;
  498. req.name[req.name_len] = '\0';
  499. err = ubi_create_volume(ubi, &req);
  500. if (err)
  501. break;
  502. err = put_user(req.vol_id, (__user int32_t *)argp);
  503. if (err)
  504. err = -EFAULT;
  505. break;
  506. }
  507. /* Remove volume command */
  508. case UBI_IOCRMVOL:
  509. {
  510. int vol_id;
  511. dbg_msg("remove volume");
  512. err = get_user(vol_id, (__user int32_t *)argp);
  513. if (err) {
  514. err = -EFAULT;
  515. break;
  516. }
  517. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  518. if (IS_ERR(desc)) {
  519. err = PTR_ERR(desc);
  520. break;
  521. }
  522. err = ubi_remove_volume(desc);
  523. if (err)
  524. ubi_close_volume(desc);
  525. break;
  526. }
  527. /* Re-size volume command */
  528. case UBI_IOCRSVOL:
  529. {
  530. int pebs;
  531. uint64_t tmp;
  532. struct ubi_rsvol_req req;
  533. dbg_msg("re-size volume");
  534. err = copy_from_user(&req, argp,
  535. sizeof(struct ubi_rsvol_req));
  536. if (err) {
  537. err = -EFAULT;
  538. break;
  539. }
  540. err = verify_rsvol_req(ubi, &req);
  541. if (err)
  542. break;
  543. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  544. if (IS_ERR(desc)) {
  545. err = PTR_ERR(desc);
  546. break;
  547. }
  548. tmp = req.bytes;
  549. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  550. pebs += tmp;
  551. err = ubi_resize_volume(desc, pebs);
  552. ubi_close_volume(desc);
  553. break;
  554. }
  555. default:
  556. err = -ENOTTY;
  557. break;
  558. }
  559. return err;
  560. }
  561. /* UBI character device operations */
  562. struct file_operations ubi_cdev_operations = {
  563. .owner = THIS_MODULE,
  564. .ioctl = ubi_cdev_ioctl,
  565. .llseek = no_llseek,
  566. };
  567. /* UBI volume character device operations */
  568. struct file_operations ubi_vol_cdev_operations = {
  569. .owner = THIS_MODULE,
  570. .open = vol_cdev_open,
  571. .release = vol_cdev_release,
  572. .llseek = vol_cdev_llseek,
  573. .read = vol_cdev_read,
  574. .write = vol_cdev_write,
  575. .ioctl = vol_cdev_ioctl,
  576. };