cdev.c 15 KB

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