gluebi.c 13 KB

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