cdev.c 17 KB

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