storage.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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, ...) 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_strcmp(struct super_block *sb, unsigned long pos,
  62. const char *str, size_t size)
  63. {
  64. u_char buf[17];
  65. size_t len, segment;
  66. int ret;
  67. /* scan the string up to 16 bytes at a time, and attempt to grab the
  68. * trailing NUL whilst we're at it */
  69. buf[0] = 0xff;
  70. while (size > 0) {
  71. segment = min_t(size_t, size + 1, 17);
  72. ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
  73. if (ret < 0)
  74. return ret;
  75. len--;
  76. if (memcmp(buf, str, len) != 0)
  77. return 0;
  78. buf[0] = buf[len];
  79. size -= len;
  80. pos += len;
  81. str += len;
  82. }
  83. /* check the trailing NUL was */
  84. if (buf[0])
  85. return 0;
  86. return 1;
  87. }
  88. #endif /* CONFIG_ROMFS_ON_MTD */
  89. #ifdef CONFIG_ROMFS_ON_BLOCK
  90. /*
  91. * read data from an romfs image on a block device
  92. */
  93. static int romfs_blk_read(struct super_block *sb, unsigned long pos,
  94. void *buf, size_t buflen)
  95. {
  96. struct buffer_head *bh;
  97. unsigned long offset;
  98. size_t segment;
  99. /* copy the string up to blocksize bytes at a time */
  100. while (buflen > 0) {
  101. offset = pos & (ROMBSIZE - 1);
  102. segment = min_t(size_t, buflen, ROMBSIZE - offset);
  103. bh = sb_bread(sb, pos >> ROMBSBITS);
  104. if (!bh)
  105. return -EIO;
  106. memcpy(buf, bh->b_data + offset, segment);
  107. brelse(bh);
  108. buf += segment;
  109. buflen -= segment;
  110. pos += segment;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * determine the length of a string in romfs on a block device
  116. */
  117. static ssize_t romfs_blk_strnlen(struct super_block *sb,
  118. unsigned long pos, size_t limit)
  119. {
  120. struct buffer_head *bh;
  121. unsigned long offset;
  122. ssize_t n = 0;
  123. size_t segment;
  124. u_char *buf, *p;
  125. /* scan the string up to blocksize bytes at a time */
  126. while (limit > 0) {
  127. offset = pos & (ROMBSIZE - 1);
  128. segment = min_t(size_t, limit, ROMBSIZE - offset);
  129. bh = sb_bread(sb, pos >> ROMBSBITS);
  130. if (!bh)
  131. return -EIO;
  132. buf = bh->b_data + offset;
  133. p = memchr(buf, 0, segment);
  134. brelse(bh);
  135. if (p)
  136. return n + (p - buf);
  137. limit -= segment;
  138. pos += segment;
  139. n += segment;
  140. }
  141. return n;
  142. }
  143. /*
  144. * compare a string to one in a romfs image on a block device
  145. * - return 1 if matched, 0 if differ, -ve if error
  146. */
  147. static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos,
  148. const char *str, size_t size)
  149. {
  150. struct buffer_head *bh;
  151. unsigned long offset;
  152. size_t segment;
  153. bool matched, terminated = false;
  154. /* compare string up to a block at a time */
  155. while (size > 0) {
  156. offset = pos & (ROMBSIZE - 1);
  157. segment = min_t(size_t, size, ROMBSIZE - offset);
  158. bh = sb_bread(sb, pos >> ROMBSBITS);
  159. if (!bh)
  160. return -EIO;
  161. matched = (memcmp(bh->b_data + offset, str, segment) == 0);
  162. size -= segment;
  163. pos += segment;
  164. str += segment;
  165. if (matched && size == 0 && offset + segment < ROMBSIZE) {
  166. if (!bh->b_data[offset + segment])
  167. terminated = true;
  168. else
  169. matched = false;
  170. }
  171. brelse(bh);
  172. if (!matched)
  173. return 0;
  174. }
  175. if (!terminated) {
  176. /* the terminating NUL must be on the first byte of the next
  177. * block */
  178. BUG_ON((pos & (ROMBSIZE - 1)) != 0);
  179. bh = sb_bread(sb, pos >> ROMBSBITS);
  180. if (!bh)
  181. return -EIO;
  182. matched = !bh->b_data[0];
  183. brelse(bh);
  184. if (!matched)
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. #endif /* CONFIG_ROMFS_ON_BLOCK */
  190. /*
  191. * read data from the romfs image
  192. */
  193. int romfs_dev_read(struct super_block *sb, unsigned long pos,
  194. void *buf, size_t buflen)
  195. {
  196. size_t limit;
  197. limit = romfs_maxsize(sb);
  198. if (pos >= limit)
  199. return -EIO;
  200. if (buflen > limit - pos)
  201. buflen = limit - pos;
  202. #ifdef CONFIG_ROMFS_ON_MTD
  203. if (sb->s_mtd)
  204. return romfs_mtd_read(sb, pos, buf, buflen);
  205. #endif
  206. #ifdef CONFIG_ROMFS_ON_BLOCK
  207. if (sb->s_bdev)
  208. return romfs_blk_read(sb, pos, buf, buflen);
  209. #endif
  210. return -EIO;
  211. }
  212. /*
  213. * determine the length of a string in romfs
  214. */
  215. ssize_t romfs_dev_strnlen(struct super_block *sb,
  216. unsigned long pos, size_t maxlen)
  217. {
  218. size_t limit;
  219. limit = romfs_maxsize(sb);
  220. if (pos >= limit)
  221. return -EIO;
  222. if (maxlen > limit - pos)
  223. maxlen = limit - pos;
  224. #ifdef CONFIG_ROMFS_ON_MTD
  225. if (sb->s_mtd)
  226. return romfs_mtd_strnlen(sb, pos, maxlen);
  227. #endif
  228. #ifdef CONFIG_ROMFS_ON_BLOCK
  229. if (sb->s_bdev)
  230. return romfs_blk_strnlen(sb, pos, maxlen);
  231. #endif
  232. return -EIO;
  233. }
  234. /*
  235. * compare a string to one in romfs
  236. * - the string to be compared to, str, may not be NUL-terminated; instead the
  237. * string is of the specified size
  238. * - return 1 if matched, 0 if differ, -ve if error
  239. */
  240. int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
  241. const char *str, size_t size)
  242. {
  243. size_t limit;
  244. limit = romfs_maxsize(sb);
  245. if (pos >= limit)
  246. return -EIO;
  247. if (size > ROMFS_MAXFN)
  248. return -ENAMETOOLONG;
  249. if (size + 1 > limit - pos)
  250. return -EIO;
  251. #ifdef CONFIG_ROMFS_ON_MTD
  252. if (sb->s_mtd)
  253. return romfs_mtd_strcmp(sb, pos, str, size);
  254. #endif
  255. #ifdef CONFIG_ROMFS_ON_BLOCK
  256. if (sb->s_bdev)
  257. return romfs_blk_strcmp(sb, pos, str, size);
  258. #endif
  259. return -EIO;
  260. }