cdev.c 18 KB

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