storage.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* RomFS storage access routines
  2. *
  3. * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mtd/super.h>
  13. #include <linux/buffer_head.h>
  14. #include "internal.h"
  15. #if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK)
  16. #error no ROMFS backing store interface configured
  17. #endif
  18. #ifdef CONFIG_ROMFS_ON_MTD
  19. #define ROMFS_MTD_READ(sb, ...) ((sb)->s_mtd->read((sb)->s_mtd, ##__VA_ARGS__))
  20. /*
  21. * read data from an romfs image on an MTD device
  22. */
  23. static int romfs_mtd_read(struct super_block *sb, unsigned long pos,
  24. void *buf, size_t buflen)
  25. {
  26. size_t rlen;
  27. int ret;
  28. ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf);
  29. return (ret < 0 || rlen != buflen) ? -EIO : 0;
  30. }
  31. /*
  32. * determine the length of a string in a romfs image on an MTD device
  33. */
  34. static ssize_t romfs_mtd_strnlen(struct super_block *sb,
  35. unsigned long pos, size_t maxlen)
  36. {
  37. ssize_t n = 0;
  38. size_t segment;
  39. u_char buf[16], *p;
  40. size_t len;
  41. int ret;
  42. /* scan the string up to 16 bytes at a time */
  43. while (maxlen > 0) {
  44. segment = min_t(size_t, maxlen, 16);
  45. ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
  46. if (ret < 0)
  47. return ret;
  48. p = memchr(buf, 0, len);
  49. if (p)
  50. return n + (p - buf);
  51. maxlen -= len;
  52. pos += len;
  53. n += len;
  54. }
  55. return n;
  56. }
  57. /*
  58. * compare a string to one in a romfs image on MTD
  59. * - return 1 if matched, 0 if differ, -ve if error
  60. */
  61. static int romfs_mtd_strncmp(struct super_block *sb, unsigned long pos,
  62. const char *str, size_t size)
  63. {
  64. u_char buf[16];
  65. size_t len, segment;
  66. int ret;
  67. /* scan the string up to 16 bytes at a time */
  68. while (size > 0) {
  69. segment = min_t(size_t, size, 16);
  70. ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
  71. if (ret < 0)
  72. return ret;
  73. if (memcmp(buf, str, len) != 0)
  74. return 0;
  75. size -= len;
  76. pos += len;
  77. str += len;
  78. }
  79. return 1;
  80. }
  81. #endif /* CONFIG_ROMFS_ON_MTD */
  82. #ifdef CONFIG_ROMFS_ON_BLOCK
  83. /*
  84. * read data from an romfs image on a block device
  85. */
  86. static int romfs_blk_read(struct super_block *sb, unsigned long pos,
  87. void *buf, size_t buflen)
  88. {
  89. struct buffer_head *bh;
  90. unsigned long offset;
  91. size_t segment;
  92. /* copy the string up to blocksize bytes at a time */
  93. while (buflen > 0) {
  94. offset = pos & (ROMBSIZE - 1);
  95. segment = min_t(size_t, buflen, ROMBSIZE - offset);
  96. bh = sb_bread(sb, pos >> ROMBSBITS);
  97. if (!bh)
  98. return -EIO;
  99. memcpy(buf, bh->b_data + offset, segment);
  100. brelse(bh);
  101. buflen -= segment;
  102. pos += segment;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * determine the length of a string in romfs on a block device
  108. */
  109. static ssize_t romfs_blk_strnlen(struct super_block *sb,
  110. unsigned long pos, size_t limit)
  111. {
  112. struct buffer_head *bh;
  113. unsigned long offset;
  114. ssize_t n = 0;
  115. size_t segment;
  116. u_char *buf, *p;
  117. /* scan the string up to blocksize bytes at a time */
  118. while (limit > 0) {
  119. offset = pos & (ROMBSIZE - 1);
  120. segment = min_t(size_t, limit, ROMBSIZE - offset);
  121. bh = sb_bread(sb, pos >> ROMBSBITS);
  122. if (!bh)
  123. return -EIO;
  124. buf = bh->b_data + offset;
  125. p = memchr(buf, 0, segment);
  126. brelse(bh);
  127. if (p)
  128. return n + (p - buf);
  129. limit -= segment;
  130. pos += segment;
  131. n += segment;
  132. }
  133. return n;
  134. }
  135. /*
  136. * compare a string to one in a romfs image on a block device
  137. * - return 1 if matched, 0 if differ, -ve if error
  138. */
  139. static int romfs_blk_strncmp(struct super_block *sb, unsigned long pos,
  140. const char *str, size_t size)
  141. {
  142. struct buffer_head *bh;
  143. unsigned long offset;
  144. size_t segment;
  145. bool x;
  146. /* scan the string up to 16 bytes at a time */
  147. while (size > 0) {
  148. offset = pos & (ROMBSIZE - 1);
  149. segment = min_t(size_t, size, ROMBSIZE - offset);
  150. bh = sb_bread(sb, pos >> ROMBSBITS);
  151. if (!bh)
  152. return -EIO;
  153. x = (memcmp(bh->b_data + offset, str, segment) != 0);
  154. brelse(bh);
  155. if (x)
  156. return 0;
  157. size -= segment;
  158. pos += segment;
  159. str += segment;
  160. }
  161. return 1;
  162. }
  163. #endif /* CONFIG_ROMFS_ON_BLOCK */
  164. /*
  165. * read data from the romfs image
  166. */
  167. int romfs_dev_read(struct super_block *sb, unsigned long pos,
  168. void *buf, size_t buflen)
  169. {
  170. size_t limit;
  171. limit = romfs_maxsize(sb);
  172. if (pos >= limit)
  173. return -EIO;
  174. if (buflen > limit - pos)
  175. buflen = limit - pos;
  176. #ifdef CONFIG_ROMFS_ON_MTD
  177. if (sb->s_mtd)
  178. return romfs_mtd_read(sb, pos, buf, buflen);
  179. #endif
  180. #ifdef CONFIG_ROMFS_ON_BLOCK
  181. if (sb->s_bdev)
  182. return romfs_blk_read(sb, pos, buf, buflen);
  183. #endif
  184. return -EIO;
  185. }
  186. /*
  187. * determine the length of a string in romfs
  188. */
  189. ssize_t romfs_dev_strnlen(struct super_block *sb,
  190. unsigned long pos, size_t maxlen)
  191. {
  192. size_t limit;
  193. limit = romfs_maxsize(sb);
  194. if (pos >= limit)
  195. return -EIO;
  196. if (maxlen > limit - pos)
  197. maxlen = limit - pos;
  198. #ifdef CONFIG_ROMFS_ON_MTD
  199. if (sb->s_mtd)
  200. return romfs_mtd_strnlen(sb, pos, limit);
  201. #endif
  202. #ifdef CONFIG_ROMFS_ON_BLOCK
  203. if (sb->s_bdev)
  204. return romfs_blk_strnlen(sb, pos, limit);
  205. #endif
  206. return -EIO;
  207. }
  208. /*
  209. * compare a string to one in romfs
  210. * - return 1 if matched, 0 if differ, -ve if error
  211. */
  212. int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
  213. const char *str, size_t size)
  214. {
  215. size_t limit;
  216. limit = romfs_maxsize(sb);
  217. if (pos >= limit)
  218. return -EIO;
  219. if (size > ROMFS_MAXFN)
  220. return -ENAMETOOLONG;
  221. if (size > limit - pos)
  222. return -EIO;
  223. #ifdef CONFIG_ROMFS_ON_MTD
  224. if (sb->s_mtd)
  225. return romfs_mtd_strncmp(sb, pos, str, size);
  226. #endif
  227. #ifdef CONFIG_ROMFS_ON_BLOCK
  228. if (sb->s_bdev)
  229. return romfs_blk_strncmp(sb, pos, str, size);
  230. #endif
  231. return -EIO;
  232. }