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