gluebi.c 14 KB

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