cdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 <linux/smp_lock.h>
  41. #include <mtd/ubi-user.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/div64.h>
  44. #include "ubi.h"
  45. /**
  46. * get_exclusive - get exclusive access to an UBI volume.
  47. * @desc: volume descriptor
  48. *
  49. * This function changes UBI volume open mode to "exclusive". Returns previous
  50. * mode value (positive integer) in case of success and a negative error code
  51. * in case of failure.
  52. */
  53. static int get_exclusive(struct ubi_volume_desc *desc)
  54. {
  55. int users, err;
  56. struct ubi_volume *vol = desc->vol;
  57. spin_lock(&vol->ubi->volumes_lock);
  58. users = vol->readers + vol->writers + vol->exclusive;
  59. ubi_assert(users > 0);
  60. if (users > 1) {
  61. dbg_err("%d users for volume %d", users, vol->vol_id);
  62. err = -EBUSY;
  63. } else {
  64. vol->readers = vol->writers = 0;
  65. vol->exclusive = 1;
  66. err = desc->mode;
  67. desc->mode = UBI_EXCLUSIVE;
  68. }
  69. spin_unlock(&vol->ubi->volumes_lock);
  70. return err;
  71. }
  72. /**
  73. * revoke_exclusive - revoke exclusive mode.
  74. * @desc: volume descriptor
  75. * @mode: new mode to switch to
  76. */
  77. static void revoke_exclusive(struct ubi_volume_desc *desc, int mode)
  78. {
  79. struct ubi_volume *vol = desc->vol;
  80. spin_lock(&vol->ubi->volumes_lock);
  81. ubi_assert(vol->readers == 0 && vol->writers == 0);
  82. ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE);
  83. vol->exclusive = 0;
  84. if (mode == UBI_READONLY)
  85. vol->readers = 1;
  86. else if (mode == UBI_READWRITE)
  87. vol->writers = 1;
  88. else
  89. vol->exclusive = 1;
  90. spin_unlock(&vol->ubi->volumes_lock);
  91. desc->mode = mode;
  92. }
  93. static int vol_cdev_open(struct inode *inode, struct file *file)
  94. {
  95. struct ubi_volume_desc *desc;
  96. int vol_id = iminor(inode) - 1, mode, ubi_num;
  97. lock_kernel();
  98. ubi_num = ubi_major2num(imajor(inode));
  99. if (ubi_num < 0) {
  100. unlock_kernel();
  101. return ubi_num;
  102. }
  103. if (file->f_mode & FMODE_WRITE)
  104. mode = UBI_READWRITE;
  105. else
  106. mode = UBI_READONLY;
  107. dbg_msg("open volume %d, mode %d", vol_id, mode);
  108. desc = ubi_open_volume(ubi_num, vol_id, mode);
  109. unlock_kernel();
  110. if (IS_ERR(desc))
  111. return PTR_ERR(desc);
  112. file->private_data = desc;
  113. return 0;
  114. }
  115. static int vol_cdev_release(struct inode *inode, struct file *file)
  116. {
  117. struct ubi_volume_desc *desc = file->private_data;
  118. struct ubi_volume *vol = desc->vol;
  119. dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode);
  120. if (vol->updating) {
  121. ubi_warn("update of volume %d not finished, volume is damaged",
  122. vol->vol_id);
  123. ubi_assert(!vol->changing_leb);
  124. vol->updating = 0;
  125. vfree(vol->upd_buf);
  126. } else if (vol->changing_leb) {
  127. dbg_msg("only %lld of %lld bytes received for atomic LEB change"
  128. " for volume %d:%d, cancel", vol->upd_received,
  129. vol->upd_bytes, vol->ubi->ubi_num, vol->vol_id);
  130. vol->changing_leb = 0;
  131. vfree(vol->upd_buf);
  132. }
  133. ubi_close_volume(desc);
  134. return 0;
  135. }
  136. static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
  137. {
  138. struct ubi_volume_desc *desc = file->private_data;
  139. struct ubi_volume *vol = desc->vol;
  140. loff_t new_offset;
  141. if (vol->updating) {
  142. /* Update is in progress, seeking is prohibited */
  143. dbg_err("updating");
  144. return -EBUSY;
  145. }
  146. switch (origin) {
  147. case 0: /* SEEK_SET */
  148. new_offset = offset;
  149. break;
  150. case 1: /* SEEK_CUR */
  151. new_offset = file->f_pos + offset;
  152. break;
  153. case 2: /* SEEK_END */
  154. new_offset = vol->used_bytes + offset;
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. if (new_offset < 0 || new_offset > vol->used_bytes) {
  160. dbg_err("bad seek %lld", new_offset);
  161. return -EINVAL;
  162. }
  163. dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld",
  164. vol->vol_id, offset, origin, new_offset);
  165. file->f_pos = new_offset;
  166. return new_offset;
  167. }
  168. static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
  169. loff_t *offp)
  170. {
  171. struct ubi_volume_desc *desc = file->private_data;
  172. struct ubi_volume *vol = desc->vol;
  173. struct ubi_device *ubi = vol->ubi;
  174. int err, lnum, off, len, tbuf_size;
  175. size_t count_save = count;
  176. void *tbuf;
  177. uint64_t tmp;
  178. dbg_msg("read %zd bytes from offset %lld of volume %d",
  179. count, *offp, vol->vol_id);
  180. if (vol->updating) {
  181. dbg_err("updating");
  182. return -EBUSY;
  183. }
  184. if (vol->upd_marker) {
  185. dbg_err("damaged volume, update marker is set");
  186. return -EBADF;
  187. }
  188. if (*offp == vol->used_bytes || count == 0)
  189. return 0;
  190. if (vol->corrupted)
  191. dbg_msg("read from corrupted volume %d", vol->vol_id);
  192. if (*offp + count > vol->used_bytes)
  193. count_save = count = vol->used_bytes - *offp;
  194. tbuf_size = vol->usable_leb_size;
  195. if (count < tbuf_size)
  196. tbuf_size = ALIGN(count, ubi->min_io_size);
  197. tbuf = vmalloc(tbuf_size);
  198. if (!tbuf)
  199. return -ENOMEM;
  200. len = count > tbuf_size ? tbuf_size : count;
  201. tmp = *offp;
  202. off = do_div(tmp, vol->usable_leb_size);
  203. lnum = tmp;
  204. do {
  205. cond_resched();
  206. if (off + len >= vol->usable_leb_size)
  207. len = vol->usable_leb_size - off;
  208. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  209. if (err)
  210. break;
  211. off += len;
  212. if (off == vol->usable_leb_size) {
  213. lnum += 1;
  214. off -= vol->usable_leb_size;
  215. }
  216. count -= len;
  217. *offp += len;
  218. err = copy_to_user(buf, tbuf, len);
  219. if (err) {
  220. err = -EFAULT;
  221. break;
  222. }
  223. buf += len;
  224. len = count > tbuf_size ? tbuf_size : count;
  225. } while (count);
  226. vfree(tbuf);
  227. return err ? err : count_save - count;
  228. }
  229. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  230. /*
  231. * This function allows to directly write to dynamic UBI volumes, without
  232. * issuing the volume update operation. Available only as a debugging feature.
  233. * Very useful for testing UBI.
  234. */
  235. static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
  236. size_t count, loff_t *offp)
  237. {
  238. struct ubi_volume_desc *desc = file->private_data;
  239. struct ubi_volume *vol = desc->vol;
  240. struct ubi_device *ubi = vol->ubi;
  241. int lnum, off, len, tbuf_size, err = 0;
  242. size_t count_save = count;
  243. char *tbuf;
  244. uint64_t tmp;
  245. dbg_msg("requested: write %zd bytes to offset %lld of volume %u",
  246. count, *offp, vol->vol_id);
  247. if (vol->vol_type == UBI_STATIC_VOLUME)
  248. return -EROFS;
  249. tmp = *offp;
  250. off = do_div(tmp, vol->usable_leb_size);
  251. lnum = tmp;
  252. if (off & (ubi->min_io_size - 1)) {
  253. dbg_err("unaligned position");
  254. return -EINVAL;
  255. }
  256. if (*offp + count > vol->used_bytes)
  257. count_save = count = vol->used_bytes - *offp;
  258. /* We can write only in fractions of the minimum I/O unit */
  259. if (count & (ubi->min_io_size - 1)) {
  260. dbg_err("unaligned write length");
  261. return -EINVAL;
  262. }
  263. tbuf_size = vol->usable_leb_size;
  264. if (count < tbuf_size)
  265. tbuf_size = ALIGN(count, ubi->min_io_size);
  266. tbuf = vmalloc(tbuf_size);
  267. if (!tbuf)
  268. return -ENOMEM;
  269. len = count > tbuf_size ? tbuf_size : count;
  270. while (count) {
  271. cond_resched();
  272. if (off + len >= vol->usable_leb_size)
  273. len = vol->usable_leb_size - off;
  274. err = copy_from_user(tbuf, buf, len);
  275. if (err) {
  276. err = -EFAULT;
  277. break;
  278. }
  279. err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len,
  280. UBI_UNKNOWN);
  281. if (err)
  282. break;
  283. off += len;
  284. if (off == vol->usable_leb_size) {
  285. lnum += 1;
  286. off -= vol->usable_leb_size;
  287. }
  288. count -= len;
  289. *offp += len;
  290. buf += len;
  291. len = count > tbuf_size ? tbuf_size : count;
  292. }
  293. vfree(tbuf);
  294. return err ? err : count_save - count;
  295. }
  296. #else
  297. #define vol_cdev_direct_write(file, buf, count, offp) -EPERM
  298. #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */
  299. static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
  300. size_t count, loff_t *offp)
  301. {
  302. int err = 0;
  303. struct ubi_volume_desc *desc = file->private_data;
  304. struct ubi_volume *vol = desc->vol;
  305. struct ubi_device *ubi = vol->ubi;
  306. if (!vol->updating && !vol->changing_leb)
  307. return vol_cdev_direct_write(file, buf, count, offp);
  308. if (vol->updating)
  309. err = ubi_more_update_data(ubi, vol, buf, count);
  310. else
  311. err = ubi_more_leb_change_data(ubi, vol, buf, count);
  312. if (err < 0) {
  313. ubi_err("cannot accept more %zd bytes of data, error %d",
  314. count, err);
  315. return err;
  316. }
  317. if (err) {
  318. /*
  319. * The operation is finished, @err contains number of actually
  320. * written bytes.
  321. */
  322. count = err;
  323. if (vol->changing_leb) {
  324. revoke_exclusive(desc, UBI_READWRITE);
  325. return count;
  326. }
  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. 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 = (long long)vol->reserved_pebs *
  368. 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, bytes);
  377. if (bytes == 0)
  378. revoke_exclusive(desc, UBI_READWRITE);
  379. break;
  380. }
  381. /* Atomic logical eraseblock change command */
  382. case UBI_IOCEBCH:
  383. {
  384. struct ubi_leb_change_req req;
  385. err = copy_from_user(&req, argp,
  386. sizeof(struct ubi_leb_change_req));
  387. if (err) {
  388. err = -EFAULT;
  389. break;
  390. }
  391. if (desc->mode == UBI_READONLY ||
  392. vol->vol_type == UBI_STATIC_VOLUME) {
  393. err = -EROFS;
  394. break;
  395. }
  396. /* Validate the request */
  397. err = -EINVAL;
  398. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  399. req.bytes < 0 || req.lnum >= vol->usable_leb_size)
  400. break;
  401. if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
  402. req.dtype != UBI_UNKNOWN)
  403. break;
  404. err = get_exclusive(desc);
  405. if (err < 0)
  406. break;
  407. err = ubi_start_leb_change(ubi, vol, &req);
  408. if (req.bytes == 0)
  409. revoke_exclusive(desc, UBI_READWRITE);
  410. break;
  411. }
  412. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  413. /* Logical eraseblock erasure command */
  414. case UBI_IOCEBER:
  415. {
  416. int32_t lnum;
  417. err = get_user(lnum, (__user int32_t *)argp);
  418. if (err) {
  419. err = -EFAULT;
  420. break;
  421. }
  422. if (desc->mode == UBI_READONLY ||
  423. vol->vol_type == UBI_STATIC_VOLUME) {
  424. err = -EROFS;
  425. break;
  426. }
  427. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  428. err = -EINVAL;
  429. break;
  430. }
  431. dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
  432. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  433. if (err)
  434. break;
  435. err = ubi_wl_flush(ubi);
  436. break;
  437. }
  438. #endif
  439. default:
  440. err = -ENOTTY;
  441. break;
  442. }
  443. return err;
  444. }
  445. /**
  446. * verify_mkvol_req - verify volume creation request.
  447. * @ubi: UBI device description object
  448. * @req: the request to check
  449. *
  450. * This function zero if the request is correct, and %-EINVAL if not.
  451. */
  452. static int verify_mkvol_req(const struct ubi_device *ubi,
  453. const struct ubi_mkvol_req *req)
  454. {
  455. int n, err = -EINVAL;
  456. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  457. req->name_len < 0)
  458. goto bad;
  459. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  460. req->vol_id != UBI_VOL_NUM_AUTO)
  461. goto bad;
  462. if (req->alignment == 0)
  463. goto bad;
  464. if (req->bytes == 0)
  465. goto bad;
  466. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  467. req->vol_type != UBI_STATIC_VOLUME)
  468. goto bad;
  469. if (req->alignment > ubi->leb_size)
  470. goto bad;
  471. n = req->alignment & (ubi->min_io_size - 1);
  472. if (req->alignment != 1 && n)
  473. goto bad;
  474. if (req->name_len > UBI_VOL_NAME_MAX) {
  475. err = -ENAMETOOLONG;
  476. goto bad;
  477. }
  478. n = strnlen(req->name, req->name_len + 1);
  479. if (n != req->name_len)
  480. goto bad;
  481. return 0;
  482. bad:
  483. dbg_err("bad volume creation request");
  484. ubi_dbg_dump_mkvol_req(req);
  485. return err;
  486. }
  487. /**
  488. * verify_rsvol_req - verify volume re-size request.
  489. * @ubi: UBI device description object
  490. * @req: the request to check
  491. *
  492. * This function returns zero if the request is correct, and %-EINVAL if not.
  493. */
  494. static int verify_rsvol_req(const struct ubi_device *ubi,
  495. const struct ubi_rsvol_req *req)
  496. {
  497. if (req->bytes <= 0)
  498. return -EINVAL;
  499. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  500. return -EINVAL;
  501. return 0;
  502. }
  503. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  504. unsigned int cmd, unsigned long arg)
  505. {
  506. int err = 0;
  507. struct ubi_device *ubi;
  508. struct ubi_volume_desc *desc;
  509. void __user *argp = (void __user *)arg;
  510. if (!capable(CAP_SYS_RESOURCE))
  511. return -EPERM;
  512. ubi = ubi_get_by_major(imajor(inode));
  513. if (!ubi)
  514. return -ENODEV;
  515. switch (cmd) {
  516. /* Create volume command */
  517. case UBI_IOCMKVOL:
  518. {
  519. struct ubi_mkvol_req req;
  520. dbg_msg("create volume");
  521. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  522. if (err) {
  523. err = -EFAULT;
  524. break;
  525. }
  526. req.name[req.name_len] = '\0';
  527. err = verify_mkvol_req(ubi, &req);
  528. if (err)
  529. break;
  530. mutex_lock(&ubi->volumes_mutex);
  531. err = ubi_create_volume(ubi, &req);
  532. mutex_unlock(&ubi->volumes_mutex);
  533. if (err)
  534. break;
  535. err = put_user(req.vol_id, (__user int32_t *)argp);
  536. if (err)
  537. err = -EFAULT;
  538. break;
  539. }
  540. /* Remove volume command */
  541. case UBI_IOCRMVOL:
  542. {
  543. int vol_id;
  544. dbg_msg("remove volume");
  545. err = get_user(vol_id, (__user int32_t *)argp);
  546. if (err) {
  547. err = -EFAULT;
  548. break;
  549. }
  550. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  551. if (IS_ERR(desc)) {
  552. err = PTR_ERR(desc);
  553. break;
  554. }
  555. mutex_lock(&ubi->volumes_mutex);
  556. err = ubi_remove_volume(desc);
  557. mutex_unlock(&ubi->volumes_mutex);
  558. /*
  559. * The volume is deleted (unless an error occurred), and the
  560. * 'struct ubi_volume' object will be freed when
  561. * 'ubi_close_volume()' will call 'put_device()'.
  562. */
  563. ubi_close_volume(desc);
  564. break;
  565. }
  566. /* Re-size volume command */
  567. case UBI_IOCRSVOL:
  568. {
  569. int pebs;
  570. uint64_t tmp;
  571. struct ubi_rsvol_req req;
  572. dbg_msg("re-size volume");
  573. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  574. if (err) {
  575. err = -EFAULT;
  576. break;
  577. }
  578. err = verify_rsvol_req(ubi, &req);
  579. if (err)
  580. break;
  581. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  582. if (IS_ERR(desc)) {
  583. err = PTR_ERR(desc);
  584. break;
  585. }
  586. tmp = req.bytes;
  587. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  588. pebs += tmp;
  589. mutex_lock(&ubi->volumes_mutex);
  590. err = ubi_resize_volume(desc, pebs);
  591. mutex_unlock(&ubi->volumes_mutex);
  592. ubi_close_volume(desc);
  593. break;
  594. }
  595. default:
  596. err = -ENOTTY;
  597. break;
  598. }
  599. ubi_put_device(ubi);
  600. return err;
  601. }
  602. static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
  603. unsigned int cmd, unsigned long arg)
  604. {
  605. int err = 0;
  606. void __user *argp = (void __user *)arg;
  607. if (!capable(CAP_SYS_RESOURCE))
  608. return -EPERM;
  609. switch (cmd) {
  610. /* Attach an MTD device command */
  611. case UBI_IOCATT:
  612. {
  613. struct ubi_attach_req req;
  614. struct mtd_info *mtd;
  615. dbg_msg("attach MTD device");
  616. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  617. if (err) {
  618. err = -EFAULT;
  619. break;
  620. }
  621. if (req.mtd_num < 0 ||
  622. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  623. err = -EINVAL;
  624. break;
  625. }
  626. mtd = get_mtd_device(NULL, req.mtd_num);
  627. if (IS_ERR(mtd)) {
  628. err = PTR_ERR(mtd);
  629. break;
  630. }
  631. /*
  632. * Note, further request verification is done by
  633. * 'ubi_attach_mtd_dev()'.
  634. */
  635. mutex_lock(&ubi_devices_mutex);
  636. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
  637. mutex_unlock(&ubi_devices_mutex);
  638. if (err < 0)
  639. put_mtd_device(mtd);
  640. else
  641. /* @err contains UBI device number */
  642. err = put_user(err, (__user int32_t *)argp);
  643. break;
  644. }
  645. /* Detach an MTD device command */
  646. case UBI_IOCDET:
  647. {
  648. int ubi_num;
  649. dbg_msg("dettach MTD device");
  650. err = get_user(ubi_num, (__user int32_t *)argp);
  651. if (err) {
  652. err = -EFAULT;
  653. break;
  654. }
  655. mutex_lock(&ubi_devices_mutex);
  656. err = ubi_detach_mtd_dev(ubi_num, 0);
  657. mutex_unlock(&ubi_devices_mutex);
  658. break;
  659. }
  660. default:
  661. err = -ENOTTY;
  662. break;
  663. }
  664. return err;
  665. }
  666. /* UBI control character device operations */
  667. struct file_operations ubi_ctrl_cdev_operations = {
  668. .ioctl = ctrl_cdev_ioctl,
  669. .owner = THIS_MODULE,
  670. };
  671. /* UBI character device operations */
  672. struct file_operations ubi_cdev_operations = {
  673. .owner = THIS_MODULE,
  674. .ioctl = ubi_cdev_ioctl,
  675. .llseek = no_llseek,
  676. };
  677. /* UBI volume character device operations */
  678. struct file_operations ubi_vol_cdev_operations = {
  679. .owner = THIS_MODULE,
  680. .open = vol_cdev_open,
  681. .release = vol_cdev_release,
  682. .llseek = vol_cdev_llseek,
  683. .read = vol_cdev_read,
  684. .write = vol_cdev_write,
  685. .ioctl = vol_cdev_ioctl,
  686. };