gluebi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 (Битюцкий Артём), Joern Engel
  19. */
  20. /*
  21. * This is a small driver which implements fake MTD devices on top of UBI
  22. * volumes. This sounds strange, but it is in fact quite useful to make
  23. * MTD-oriented software (including all the legacy software) work on top of
  24. * UBI.
  25. *
  26. * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
  27. * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The
  28. * eraseblock size is equivalent to the logical eraseblock size of the volume.
  29. */
  30. #include <linux/err.h>
  31. #include <linux/list.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include <linux/math64.h>
  35. #include <linux/module.h>
  36. #include <linux/mutex.h>
  37. #include <linux/mtd/ubi.h>
  38. #include <linux/mtd/mtd.h>
  39. #include "ubi-media.h"
  40. #define err_msg(fmt, ...) \
  41. pr_err("gluebi (pid %d): %s: " fmt "\n", \
  42. current->pid, __func__, ##__VA_ARGS__)
  43. /**
  44. * struct gluebi_device - a gluebi device description data structure.
  45. * @mtd: emulated MTD device description object
  46. * @refcnt: gluebi device reference count
  47. * @desc: UBI volume descriptor
  48. * @ubi_num: UBI device number this gluebi device works on
  49. * @vol_id: ID of UBI volume this gluebi device works on
  50. * @list: link in a list of gluebi devices
  51. */
  52. struct gluebi_device {
  53. struct mtd_info mtd;
  54. int refcnt;
  55. struct ubi_volume_desc *desc;
  56. int ubi_num;
  57. int vol_id;
  58. struct list_head list;
  59. };
  60. /* List of all gluebi devices */
  61. static LIST_HEAD(gluebi_devices);
  62. static DEFINE_MUTEX(devices_mutex);
  63. /**
  64. * find_gluebi_nolock - find a gluebi device.
  65. * @ubi_num: UBI device number
  66. * @vol_id: volume ID
  67. *
  68. * This function seraches for gluebi device corresponding to UBI device
  69. * @ubi_num and UBI volume @vol_id. Returns the gluebi device description
  70. * object in case of success and %NULL in case of failure. The caller has to
  71. * have the &devices_mutex locked.
  72. */
  73. static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
  74. {
  75. struct gluebi_device *gluebi;
  76. list_for_each_entry(gluebi, &gluebi_devices, list)
  77. if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
  78. return gluebi;
  79. return NULL;
  80. }
  81. /**
  82. * gluebi_get_device - get MTD device reference.
  83. * @mtd: the MTD device description object
  84. *
  85. * This function is called every time the MTD device is being opened and
  86. * implements the MTD get_device() operation. Returns zero in case of success
  87. * and a negative error code in case of failure.
  88. */
  89. static int gluebi_get_device(struct mtd_info *mtd)
  90. {
  91. struct gluebi_device *gluebi;
  92. int ubi_mode = UBI_READONLY;
  93. if (!try_module_get(THIS_MODULE))
  94. return -ENODEV;
  95. if (mtd->flags & MTD_WRITEABLE)
  96. ubi_mode = UBI_READWRITE;
  97. gluebi = container_of(mtd, struct gluebi_device, mtd);
  98. mutex_lock(&devices_mutex);
  99. if (gluebi->refcnt > 0) {
  100. /*
  101. * The MTD device is already referenced and this is just one
  102. * more reference. MTD allows many users to open the same
  103. * volume simultaneously and do not distinguish between
  104. * readers/writers/exclusive/meta openers as UBI does. So we do
  105. * not open the UBI volume again - just increase the reference
  106. * counter and return.
  107. */
  108. gluebi->refcnt += 1;
  109. mutex_unlock(&devices_mutex);
  110. return 0;
  111. }
  112. /*
  113. * This is the first reference to this UBI volume via the MTD device
  114. * interface. Open the corresponding volume in read-write mode.
  115. */
  116. gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
  117. ubi_mode);
  118. if (IS_ERR(gluebi->desc)) {
  119. mutex_unlock(&devices_mutex);
  120. module_put(THIS_MODULE);
  121. return PTR_ERR(gluebi->desc);
  122. }
  123. gluebi->refcnt += 1;
  124. mutex_unlock(&devices_mutex);
  125. return 0;
  126. }
  127. /**
  128. * gluebi_put_device - put MTD device reference.
  129. * @mtd: the MTD device description object
  130. *
  131. * This function is called every time the MTD device is being put. Returns
  132. * zero in case of success and a negative error code in case of failure.
  133. */
  134. static void gluebi_put_device(struct mtd_info *mtd)
  135. {
  136. struct gluebi_device *gluebi;
  137. gluebi = container_of(mtd, struct gluebi_device, mtd);
  138. mutex_lock(&devices_mutex);
  139. gluebi->refcnt -= 1;
  140. if (gluebi->refcnt == 0)
  141. ubi_close_volume(gluebi->desc);
  142. module_put(THIS_MODULE);
  143. mutex_unlock(&devices_mutex);
  144. }
  145. /**
  146. * gluebi_read - read operation of emulated MTD devices.
  147. * @mtd: MTD device description object
  148. * @from: absolute offset from where to read
  149. * @len: how many bytes to read
  150. * @retlen: count of read bytes is returned here
  151. * @buf: buffer to store the read data
  152. *
  153. * This function returns zero in case of success and a negative error code in
  154. * case of failure.
  155. */
  156. static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
  157. size_t *retlen, unsigned char *buf)
  158. {
  159. int err = 0, lnum, offs, bytes_left;
  160. struct gluebi_device *gluebi;
  161. gluebi = container_of(mtd, struct gluebi_device, mtd);
  162. lnum = div_u64_rem(from, mtd->erasesize, &offs);
  163. bytes_left = len;
  164. while (bytes_left) {
  165. size_t to_read = mtd->erasesize - offs;
  166. if (to_read > bytes_left)
  167. to_read = bytes_left;
  168. err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
  169. if (err)
  170. break;
  171. lnum += 1;
  172. offs = 0;
  173. bytes_left -= to_read;
  174. buf += to_read;
  175. }
  176. *retlen = len - bytes_left;
  177. return err;
  178. }
  179. /**
  180. * gluebi_write - write operation of emulated MTD devices.
  181. * @mtd: MTD device description object
  182. * @to: absolute offset where to write
  183. * @len: how many bytes to write
  184. * @retlen: count of written bytes is returned here
  185. * @buf: buffer with data to write
  186. *
  187. * This function returns zero in case of success and a negative error code in
  188. * case of failure.
  189. */
  190. static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
  191. size_t *retlen, const u_char *buf)
  192. {
  193. int err = 0, lnum, offs, bytes_left;
  194. struct gluebi_device *gluebi;
  195. gluebi = container_of(mtd, struct gluebi_device, mtd);
  196. lnum = div_u64_rem(to, mtd->erasesize, &offs);
  197. if (len % mtd->writesize || offs % mtd->writesize)
  198. return -EINVAL;
  199. bytes_left = len;
  200. while (bytes_left) {
  201. size_t to_write = mtd->erasesize - offs;
  202. if (to_write > bytes_left)
  203. to_write = bytes_left;
  204. err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
  205. if (err)
  206. break;
  207. lnum += 1;
  208. offs = 0;
  209. bytes_left -= to_write;
  210. buf += to_write;
  211. }
  212. *retlen = len - bytes_left;
  213. return err;
  214. }
  215. /**
  216. * gluebi_erase - erase operation of emulated MTD devices.
  217. * @mtd: the MTD device description object
  218. * @instr: the erase operation description
  219. *
  220. * This function calls the erase callback when finishes. Returns zero in case
  221. * of success and a negative error code in case of failure.
  222. */
  223. static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
  224. {
  225. int err, i, lnum, count;
  226. struct gluebi_device *gluebi;
  227. if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
  228. return -EINVAL;
  229. lnum = mtd_div_by_eb(instr->addr, mtd);
  230. count = mtd_div_by_eb(instr->len, mtd);
  231. gluebi = container_of(mtd, struct gluebi_device, mtd);
  232. for (i = 0; i < count - 1; i++) {
  233. err = ubi_leb_unmap(gluebi->desc, lnum + i);
  234. if (err)
  235. goto out_err;
  236. }
  237. /*
  238. * MTD erase operations are synchronous, so we have to make sure the
  239. * physical eraseblock is wiped out.
  240. *
  241. * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
  242. * will wait for the end of operations
  243. */
  244. err = ubi_leb_erase(gluebi->desc, lnum + i);
  245. if (err)
  246. goto out_err;
  247. instr->state = MTD_ERASE_DONE;
  248. mtd_erase_callback(instr);
  249. return 0;
  250. out_err:
  251. instr->state = MTD_ERASE_FAILED;
  252. instr->fail_addr = (long long)lnum * mtd->erasesize;
  253. return err;
  254. }
  255. /**
  256. * gluebi_create - create a gluebi device for an UBI volume.
  257. * @di: UBI device description object
  258. * @vi: UBI volume description object
  259. *
  260. * This function is called when a new UBI volume is created in order to create
  261. * corresponding fake MTD device. Returns zero in case of success and a
  262. * negative error code in case of failure.
  263. */
  264. static int gluebi_create(struct ubi_device_info *di,
  265. struct ubi_volume_info *vi)
  266. {
  267. struct gluebi_device *gluebi, *g;
  268. struct mtd_info *mtd;
  269. gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
  270. if (!gluebi)
  271. return -ENOMEM;
  272. mtd = &gluebi->mtd;
  273. mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
  274. if (!mtd->name) {
  275. kfree(gluebi);
  276. return -ENOMEM;
  277. }
  278. gluebi->vol_id = vi->vol_id;
  279. gluebi->ubi_num = vi->ubi_num;
  280. mtd->type = MTD_UBIVOLUME;
  281. if (!di->ro_mode)
  282. mtd->flags = MTD_WRITEABLE;
  283. mtd->owner = THIS_MODULE;
  284. mtd->writesize = di->min_io_size;
  285. mtd->erasesize = vi->usable_leb_size;
  286. mtd->_read = gluebi_read;
  287. mtd->_write = gluebi_write;
  288. mtd->_erase = gluebi_erase;
  289. mtd->_get_device = gluebi_get_device;
  290. mtd->_put_device = gluebi_put_device;
  291. /*
  292. * In case of dynamic a volume, MTD device size is just volume size. In
  293. * case of a static volume the size is equivalent to the amount of data
  294. * bytes.
  295. */
  296. if (vi->vol_type == UBI_DYNAMIC_VOLUME)
  297. mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
  298. else
  299. mtd->size = vi->used_bytes;
  300. /* Just a sanity check - make sure this gluebi device does not exist */
  301. mutex_lock(&devices_mutex);
  302. g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
  303. if (g)
  304. err_msg("gluebi MTD device %d form UBI device %d volume %d already exists",
  305. g->mtd.index, vi->ubi_num, vi->vol_id);
  306. mutex_unlock(&devices_mutex);
  307. if (mtd_device_register(mtd, NULL, 0)) {
  308. err_msg("cannot add MTD device");
  309. kfree(mtd->name);
  310. kfree(gluebi);
  311. return -ENFILE;
  312. }
  313. mutex_lock(&devices_mutex);
  314. list_add_tail(&gluebi->list, &gluebi_devices);
  315. mutex_unlock(&devices_mutex);
  316. return 0;
  317. }
  318. /**
  319. * gluebi_remove - remove a gluebi device.
  320. * @vi: UBI volume description object
  321. *
  322. * This function is called when an UBI volume is removed and it removes
  323. * corresponding fake MTD device. Returns zero in case of success and a
  324. * negative error code in case of failure.
  325. */
  326. static int gluebi_remove(struct ubi_volume_info *vi)
  327. {
  328. int err = 0;
  329. struct mtd_info *mtd;
  330. struct gluebi_device *gluebi;
  331. mutex_lock(&devices_mutex);
  332. gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
  333. if (!gluebi) {
  334. err_msg("got remove notification for unknown UBI device %d volume %d",
  335. vi->ubi_num, vi->vol_id);
  336. err = -ENOENT;
  337. } else if (gluebi->refcnt)
  338. err = -EBUSY;
  339. else
  340. list_del(&gluebi->list);
  341. mutex_unlock(&devices_mutex);
  342. if (err)
  343. return err;
  344. mtd = &gluebi->mtd;
  345. err = mtd_device_unregister(mtd);
  346. if (err) {
  347. err_msg("cannot remove fake MTD device %d, UBI device %d, volume %d, error %d",
  348. mtd->index, gluebi->ubi_num, gluebi->vol_id, err);
  349. mutex_lock(&devices_mutex);
  350. list_add_tail(&gluebi->list, &gluebi_devices);
  351. mutex_unlock(&devices_mutex);
  352. return err;
  353. }
  354. kfree(mtd->name);
  355. kfree(gluebi);
  356. return 0;
  357. }
  358. /**
  359. * gluebi_updated - UBI volume was updated notifier.
  360. * @vi: volume info structure
  361. *
  362. * This function is called every time an UBI volume is updated. It does nothing
  363. * if te volume @vol is dynamic, and changes MTD device size if the
  364. * volume is static. This is needed because static volumes cannot be read past
  365. * data they contain. This function returns zero in case of success and a
  366. * negative error code in case of error.
  367. */
  368. static int gluebi_updated(struct ubi_volume_info *vi)
  369. {
  370. struct gluebi_device *gluebi;
  371. mutex_lock(&devices_mutex);
  372. gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
  373. if (!gluebi) {
  374. mutex_unlock(&devices_mutex);
  375. err_msg("got update notification for unknown UBI device %d volume %d",
  376. vi->ubi_num, vi->vol_id);
  377. return -ENOENT;
  378. }
  379. if (vi->vol_type == UBI_STATIC_VOLUME)
  380. gluebi->mtd.size = vi->used_bytes;
  381. mutex_unlock(&devices_mutex);
  382. return 0;
  383. }
  384. /**
  385. * gluebi_resized - UBI volume was re-sized notifier.
  386. * @vi: volume info structure
  387. *
  388. * This function is called every time an UBI volume is re-size. It changes the
  389. * corresponding fake MTD device size. This function returns zero in case of
  390. * success and a negative error code in case of error.
  391. */
  392. static int gluebi_resized(struct ubi_volume_info *vi)
  393. {
  394. struct gluebi_device *gluebi;
  395. mutex_lock(&devices_mutex);
  396. gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
  397. if (!gluebi) {
  398. mutex_unlock(&devices_mutex);
  399. err_msg("got update notification for unknown UBI device %d volume %d",
  400. vi->ubi_num, vi->vol_id);
  401. return -ENOENT;
  402. }
  403. gluebi->mtd.size = vi->used_bytes;
  404. mutex_unlock(&devices_mutex);
  405. return 0;
  406. }
  407. /**
  408. * gluebi_notify - UBI notification handler.
  409. * @nb: registered notifier block
  410. * @l: notification type
  411. * @ptr: pointer to the &struct ubi_notification object
  412. */
  413. static int gluebi_notify(struct notifier_block *nb, unsigned long l,
  414. void *ns_ptr)
  415. {
  416. struct ubi_notification *nt = ns_ptr;
  417. switch (l) {
  418. case UBI_VOLUME_ADDED:
  419. gluebi_create(&nt->di, &nt->vi);
  420. break;
  421. case UBI_VOLUME_REMOVED:
  422. gluebi_remove(&nt->vi);
  423. break;
  424. case UBI_VOLUME_RESIZED:
  425. gluebi_resized(&nt->vi);
  426. break;
  427. case UBI_VOLUME_UPDATED:
  428. gluebi_updated(&nt->vi);
  429. break;
  430. default:
  431. break;
  432. }
  433. return NOTIFY_OK;
  434. }
  435. static struct notifier_block gluebi_notifier = {
  436. .notifier_call = gluebi_notify,
  437. };
  438. static int __init ubi_gluebi_init(void)
  439. {
  440. return ubi_register_volume_notifier(&gluebi_notifier, 0);
  441. }
  442. static void __exit ubi_gluebi_exit(void)
  443. {
  444. struct gluebi_device *gluebi, *g;
  445. list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
  446. int err;
  447. struct mtd_info *mtd = &gluebi->mtd;
  448. err = mtd_device_unregister(mtd);
  449. if (err)
  450. err_msg("error %d while removing gluebi MTD device %d, UBI device %d, volume %d - ignoring",
  451. err, mtd->index, gluebi->ubi_num,
  452. gluebi->vol_id);
  453. kfree(mtd->name);
  454. kfree(gluebi);
  455. }
  456. ubi_unregister_volume_notifier(&gluebi_notifier);
  457. }
  458. module_init(ubi_gluebi_init);
  459. module_exit(ubi_gluebi_exit);
  460. MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
  461. MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
  462. MODULE_LICENSE("GPL");