gluebi.c 15 KB

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