cdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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) {
  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) {
  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 = vol->reserved_pebs * (ubi->leb_size-vol->data_pad);
  368. if (bytes < 0 || bytes > rsvd_bytes) {
  369. err = -EINVAL;
  370. break;
  371. }
  372. err = get_exclusive(desc);
  373. if (err < 0)
  374. break;
  375. err = ubi_start_update(ubi, vol, bytes);
  376. if (bytes == 0)
  377. revoke_exclusive(desc, UBI_READWRITE);
  378. break;
  379. }
  380. /* Atomic logical eraseblock change command */
  381. case UBI_IOCEBCH:
  382. {
  383. struct ubi_leb_change_req req;
  384. err = copy_from_user(&req, argp,
  385. sizeof(struct ubi_leb_change_req));
  386. if (err) {
  387. err = -EFAULT;
  388. break;
  389. }
  390. if (desc->mode == UBI_READONLY ||
  391. vol->vol_type == UBI_STATIC_VOLUME) {
  392. err = -EROFS;
  393. break;
  394. }
  395. /* Validate the request */
  396. err = -EINVAL;
  397. if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
  398. req.bytes < 0 || req.lnum >= vol->usable_leb_size)
  399. break;
  400. if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
  401. req.dtype != UBI_UNKNOWN)
  402. break;
  403. err = get_exclusive(desc);
  404. if (err < 0)
  405. break;
  406. err = ubi_start_leb_change(ubi, vol, &req);
  407. if (req.bytes == 0)
  408. revoke_exclusive(desc, UBI_READWRITE);
  409. break;
  410. }
  411. #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO
  412. /* Logical eraseblock erasure command */
  413. case UBI_IOCEBER:
  414. {
  415. int32_t lnum;
  416. err = get_user(lnum, (__user int32_t *)argp);
  417. if (err) {
  418. err = -EFAULT;
  419. break;
  420. }
  421. if (desc->mode == UBI_READONLY ||
  422. vol->vol_type == UBI_STATIC_VOLUME) {
  423. err = -EROFS;
  424. break;
  425. }
  426. if (lnum < 0 || lnum >= vol->reserved_pebs) {
  427. err = -EINVAL;
  428. break;
  429. }
  430. dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
  431. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  432. if (err)
  433. break;
  434. err = ubi_wl_flush(ubi);
  435. break;
  436. }
  437. #endif
  438. default:
  439. err = -ENOTTY;
  440. break;
  441. }
  442. return err;
  443. }
  444. /**
  445. * verify_mkvol_req - verify volume creation request.
  446. * @ubi: UBI device description object
  447. * @req: the request to check
  448. *
  449. * This function zero if the request is correct, and %-EINVAL if not.
  450. */
  451. static int verify_mkvol_req(const struct ubi_device *ubi,
  452. const struct ubi_mkvol_req *req)
  453. {
  454. int n, err = -EINVAL;
  455. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  456. req->name_len < 0)
  457. goto bad;
  458. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  459. req->vol_id != UBI_VOL_NUM_AUTO)
  460. goto bad;
  461. if (req->alignment == 0)
  462. goto bad;
  463. if (req->bytes == 0)
  464. goto bad;
  465. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  466. req->vol_type != UBI_STATIC_VOLUME)
  467. goto bad;
  468. if (req->alignment > ubi->leb_size)
  469. goto bad;
  470. n = req->alignment % ubi->min_io_size;
  471. if (req->alignment != 1 && n)
  472. goto bad;
  473. if (req->name_len > UBI_VOL_NAME_MAX) {
  474. err = -ENAMETOOLONG;
  475. goto bad;
  476. }
  477. return 0;
  478. bad:
  479. dbg_err("bad volume creation request");
  480. ubi_dbg_dump_mkvol_req(req);
  481. return err;
  482. }
  483. /**
  484. * verify_rsvol_req - verify volume re-size request.
  485. * @ubi: UBI device description object
  486. * @req: the request to check
  487. *
  488. * This function returns zero if the request is correct, and %-EINVAL if not.
  489. */
  490. static int verify_rsvol_req(const struct ubi_device *ubi,
  491. const struct ubi_rsvol_req *req)
  492. {
  493. if (req->bytes <= 0)
  494. return -EINVAL;
  495. if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots)
  496. return -EINVAL;
  497. return 0;
  498. }
  499. static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
  500. unsigned int cmd, unsigned long arg)
  501. {
  502. int err = 0;
  503. struct ubi_device *ubi;
  504. struct ubi_volume_desc *desc;
  505. void __user *argp = (void __user *)arg;
  506. if (!capable(CAP_SYS_RESOURCE))
  507. return -EPERM;
  508. ubi = ubi_get_by_major(imajor(inode));
  509. if (!ubi)
  510. return -ENODEV;
  511. switch (cmd) {
  512. /* Create volume command */
  513. case UBI_IOCMKVOL:
  514. {
  515. struct ubi_mkvol_req req;
  516. dbg_msg("create volume");
  517. err = copy_from_user(&req, argp, sizeof(struct ubi_mkvol_req));
  518. if (err) {
  519. err = -EFAULT;
  520. break;
  521. }
  522. err = verify_mkvol_req(ubi, &req);
  523. if (err)
  524. break;
  525. req.name[req.name_len] = '\0';
  526. mutex_lock(&ubi->volumes_mutex);
  527. err = ubi_create_volume(ubi, &req);
  528. mutex_unlock(&ubi->volumes_mutex);
  529. if (err)
  530. break;
  531. err = put_user(req.vol_id, (__user int32_t *)argp);
  532. if (err)
  533. err = -EFAULT;
  534. break;
  535. }
  536. /* Remove volume command */
  537. case UBI_IOCRMVOL:
  538. {
  539. int vol_id;
  540. dbg_msg("remove volume");
  541. err = get_user(vol_id, (__user int32_t *)argp);
  542. if (err) {
  543. err = -EFAULT;
  544. break;
  545. }
  546. desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
  547. if (IS_ERR(desc)) {
  548. err = PTR_ERR(desc);
  549. break;
  550. }
  551. mutex_lock(&ubi->volumes_mutex);
  552. err = ubi_remove_volume(desc);
  553. mutex_unlock(&ubi->volumes_mutex);
  554. /*
  555. * The volume is deleted (unless an error occurred), and the
  556. * 'struct ubi_volume' object will be freed when
  557. * 'ubi_close_volume()' will call 'put_device()'.
  558. */
  559. ubi_close_volume(desc);
  560. break;
  561. }
  562. /* Re-size volume command */
  563. case UBI_IOCRSVOL:
  564. {
  565. int pebs;
  566. uint64_t tmp;
  567. struct ubi_rsvol_req req;
  568. dbg_msg("re-size volume");
  569. err = copy_from_user(&req, argp, sizeof(struct ubi_rsvol_req));
  570. if (err) {
  571. err = -EFAULT;
  572. break;
  573. }
  574. err = verify_rsvol_req(ubi, &req);
  575. if (err)
  576. break;
  577. desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE);
  578. if (IS_ERR(desc)) {
  579. err = PTR_ERR(desc);
  580. break;
  581. }
  582. tmp = req.bytes;
  583. pebs = !!do_div(tmp, desc->vol->usable_leb_size);
  584. pebs += tmp;
  585. mutex_lock(&ubi->volumes_mutex);
  586. err = ubi_resize_volume(desc, pebs);
  587. mutex_unlock(&ubi->volumes_mutex);
  588. ubi_close_volume(desc);
  589. break;
  590. }
  591. default:
  592. err = -ENOTTY;
  593. break;
  594. }
  595. ubi_put_device(ubi);
  596. return err;
  597. }
  598. static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
  599. unsigned int cmd, unsigned long arg)
  600. {
  601. int err = 0;
  602. void __user *argp = (void __user *)arg;
  603. if (!capable(CAP_SYS_RESOURCE))
  604. return -EPERM;
  605. switch (cmd) {
  606. /* Attach an MTD device command */
  607. case UBI_IOCATT:
  608. {
  609. struct ubi_attach_req req;
  610. struct mtd_info *mtd;
  611. dbg_msg("attach MTD device");
  612. err = copy_from_user(&req, argp, sizeof(struct ubi_attach_req));
  613. if (err) {
  614. err = -EFAULT;
  615. break;
  616. }
  617. if (req.mtd_num < 0 ||
  618. (req.ubi_num < 0 && req.ubi_num != UBI_DEV_NUM_AUTO)) {
  619. err = -EINVAL;
  620. break;
  621. }
  622. mtd = get_mtd_device(NULL, req.mtd_num);
  623. if (IS_ERR(mtd)) {
  624. err = PTR_ERR(mtd);
  625. break;
  626. }
  627. /*
  628. * Note, further request verification is done by
  629. * 'ubi_attach_mtd_dev()'.
  630. */
  631. mutex_lock(&ubi_devices_mutex);
  632. err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
  633. mutex_unlock(&ubi_devices_mutex);
  634. if (err < 0)
  635. put_mtd_device(mtd);
  636. else
  637. /* @err contains UBI device number */
  638. err = put_user(err, (__user int32_t *)argp);
  639. break;
  640. }
  641. /* Detach an MTD device command */
  642. case UBI_IOCDET:
  643. {
  644. int ubi_num;
  645. dbg_msg("dettach MTD device");
  646. err = get_user(ubi_num, (__user int32_t *)argp);
  647. if (err) {
  648. err = -EFAULT;
  649. break;
  650. }
  651. mutex_lock(&ubi_devices_mutex);
  652. err = ubi_detach_mtd_dev(ubi_num, 0);
  653. mutex_unlock(&ubi_devices_mutex);
  654. break;
  655. }
  656. default:
  657. err = -ENOTTY;
  658. break;
  659. }
  660. return err;
  661. }
  662. /* UBI control character device operations */
  663. struct file_operations ubi_ctrl_cdev_operations = {
  664. .ioctl = ctrl_cdev_ioctl,
  665. .owner = THIS_MODULE,
  666. };
  667. /* UBI character device operations */
  668. struct file_operations ubi_cdev_operations = {
  669. .owner = THIS_MODULE,
  670. .ioctl = ubi_cdev_ioctl,
  671. .llseek = no_llseek,
  672. };
  673. /* UBI volume character device operations */
  674. struct file_operations ubi_vol_cdev_operations = {
  675. .owner = THIS_MODULE,
  676. .open = vol_cdev_open,
  677. .release = vol_cdev_release,
  678. .llseek = vol_cdev_llseek,
  679. .read = vol_cdev_read,
  680. .write = vol_cdev_write,
  681. .ioctl = vol_cdev_ioctl,
  682. };