cdev.c 15 KB

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