Blame view
fs/romfs/storage.c
6.37 KB
da4458bda NOMMU: Make it po... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/* RomFS storage access routines * * Copyright © 2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */ #include <linux/fs.h> #include <linux/mtd/super.h> #include <linux/buffer_head.h> #include "internal.h" #if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK) #error no ROMFS backing store interface configured #endif #ifdef CONFIG_ROMFS_ON_MTD #define ROMFS_MTD_READ(sb, ...) ((sb)->s_mtd->read((sb)->s_mtd, ##__VA_ARGS__)) /* * read data from an romfs image on an MTD device */ static int romfs_mtd_read(struct super_block *sb, unsigned long pos, void *buf, size_t buflen) { size_t rlen; int ret; ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf); return (ret < 0 || rlen != buflen) ? -EIO : 0; } /* * determine the length of a string in a romfs image on an MTD device */ static ssize_t romfs_mtd_strnlen(struct super_block *sb, unsigned long pos, size_t maxlen) { ssize_t n = 0; size_t segment; u_char buf[16], *p; size_t len; int ret; /* scan the string up to 16 bytes at a time */ while (maxlen > 0) { segment = min_t(size_t, maxlen, 16); ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf); if (ret < 0) return ret; p = memchr(buf, 0, len); if (p) return n + (p - buf); maxlen -= len; pos += len; n += len; } return n; } /* * compare a string to one in a romfs image on MTD * - return 1 if matched, 0 if differ, -ve if error */ |
84baf74bf ROMFS: romfs_look... |
70 71 |
static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos, const char *str, size_t size) |
da4458bda NOMMU: Make it po... |
72 |
{ |
84baf74bf ROMFS: romfs_look... |
73 |
u_char buf[17]; |
da4458bda NOMMU: Make it po... |
74 75 |
size_t len, segment; int ret; |
84baf74bf ROMFS: romfs_look... |
76 77 78 |
/* scan the string up to 16 bytes at a time, and attempt to grab the * trailing NUL whilst we're at it */ buf[0] = 0xff; |
da4458bda NOMMU: Make it po... |
79 |
while (size > 0) { |
84baf74bf ROMFS: romfs_look... |
80 |
segment = min_t(size_t, size + 1, 17); |
da4458bda NOMMU: Make it po... |
81 82 83 |
ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf); if (ret < 0) return ret; |
84baf74bf ROMFS: romfs_look... |
84 |
len--; |
da4458bda NOMMU: Make it po... |
85 86 |
if (memcmp(buf, str, len) != 0) return 0; |
84baf74bf ROMFS: romfs_look... |
87 |
buf[0] = buf[len]; |
da4458bda NOMMU: Make it po... |
88 89 90 91 |
size -= len; pos += len; str += len; } |
84baf74bf ROMFS: romfs_look... |
92 93 94 |
/* check the trailing NUL was */ if (buf[0]) return 0; |
da4458bda NOMMU: Make it po... |
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
return 1; } #endif /* CONFIG_ROMFS_ON_MTD */ #ifdef CONFIG_ROMFS_ON_BLOCK /* * read data from an romfs image on a block device */ static int romfs_blk_read(struct super_block *sb, unsigned long pos, void *buf, size_t buflen) { struct buffer_head *bh; unsigned long offset; size_t segment; /* copy the string up to blocksize bytes at a time */ while (buflen > 0) { offset = pos & (ROMBSIZE - 1); segment = min_t(size_t, buflen, ROMBSIZE - offset); bh = sb_bread(sb, pos >> ROMBSBITS); if (!bh) return -EIO; memcpy(buf, bh->b_data + offset, segment); brelse(bh); |
4b2b0b975 ROMFS: Advance de... |
119 |
buf += segment; |
da4458bda NOMMU: Make it po... |
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
buflen -= segment; pos += segment; } return 0; } /* * determine the length of a string in romfs on a block device */ static ssize_t romfs_blk_strnlen(struct super_block *sb, unsigned long pos, size_t limit) { struct buffer_head *bh; unsigned long offset; ssize_t n = 0; size_t segment; u_char *buf, *p; /* scan the string up to blocksize bytes at a time */ while (limit > 0) { offset = pos & (ROMBSIZE - 1); segment = min_t(size_t, limit, ROMBSIZE - offset); bh = sb_bread(sb, pos >> ROMBSBITS); if (!bh) return -EIO; buf = bh->b_data + offset; p = memchr(buf, 0, segment); brelse(bh); if (p) return n + (p - buf); limit -= segment; pos += segment; n += segment; } return n; } /* * compare a string to one in a romfs image on a block device * - return 1 if matched, 0 if differ, -ve if error */ |
84baf74bf ROMFS: romfs_look... |
163 164 |
static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos, const char *str, size_t size) |
da4458bda NOMMU: Make it po... |
165 166 167 168 |
{ struct buffer_head *bh; unsigned long offset; size_t segment; |
84baf74bf ROMFS: romfs_look... |
169 |
bool matched, terminated = false; |
da4458bda NOMMU: Make it po... |
170 |
|
84baf74bf ROMFS: romfs_look... |
171 |
/* compare string up to a block at a time */ |
da4458bda NOMMU: Make it po... |
172 173 174 175 176 177 |
while (size > 0) { offset = pos & (ROMBSIZE - 1); segment = min_t(size_t, size, ROMBSIZE - offset); bh = sb_bread(sb, pos >> ROMBSBITS); if (!bh) return -EIO; |
84baf74bf ROMFS: romfs_look... |
178 |
matched = (memcmp(bh->b_data + offset, str, segment) == 0); |
da4458bda NOMMU: Make it po... |
179 180 181 |
size -= segment; pos += segment; str += segment; |
84baf74bf ROMFS: romfs_look... |
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
if (matched && size == 0 && offset + segment < ROMBSIZE) { if (!bh->b_data[offset + segment]) terminated = true; else matched = false; } brelse(bh); if (!matched) return 0; } if (!terminated) { /* the terminating NUL must be on the first byte of the next * block */ BUG_ON((pos & (ROMBSIZE - 1)) != 0); bh = sb_bread(sb, pos >> ROMBSBITS); if (!bh) return -EIO; matched = !bh->b_data[0]; brelse(bh); if (!matched) return 0; |
da4458bda NOMMU: Make it po... |
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
} return 1; } #endif /* CONFIG_ROMFS_ON_BLOCK */ /* * read data from the romfs image */ int romfs_dev_read(struct super_block *sb, unsigned long pos, void *buf, size_t buflen) { size_t limit; limit = romfs_maxsize(sb); if (pos >= limit) return -EIO; if (buflen > limit - pos) buflen = limit - pos; #ifdef CONFIG_ROMFS_ON_MTD if (sb->s_mtd) return romfs_mtd_read(sb, pos, buf, buflen); #endif #ifdef CONFIG_ROMFS_ON_BLOCK if (sb->s_bdev) return romfs_blk_read(sb, pos, buf, buflen); #endif return -EIO; } /* * determine the length of a string in romfs */ ssize_t romfs_dev_strnlen(struct super_block *sb, unsigned long pos, size_t maxlen) { size_t limit; limit = romfs_maxsize(sb); if (pos >= limit) return -EIO; if (maxlen > limit - pos) maxlen = limit - pos; #ifdef CONFIG_ROMFS_ON_MTD if (sb->s_mtd) |
ef1f7a7e8 ROMFS: fix length... |
251 |
return romfs_mtd_strnlen(sb, pos, maxlen); |
da4458bda NOMMU: Make it po... |
252 253 254 |
#endif #ifdef CONFIG_ROMFS_ON_BLOCK if (sb->s_bdev) |
ef1f7a7e8 ROMFS: fix length... |
255 |
return romfs_blk_strnlen(sb, pos, maxlen); |
da4458bda NOMMU: Make it po... |
256 257 258 259 260 261 |
#endif return -EIO; } /* * compare a string to one in romfs |
84baf74bf ROMFS: romfs_look... |
262 263 |
* - the string to be compared to, str, may not be NUL-terminated; instead the * string is of the specified size |
da4458bda NOMMU: Make it po... |
264 265 |
* - return 1 if matched, 0 if differ, -ve if error */ |
84baf74bf ROMFS: romfs_look... |
266 267 |
int romfs_dev_strcmp(struct super_block *sb, unsigned long pos, const char *str, size_t size) |
da4458bda NOMMU: Make it po... |
268 269 270 271 272 273 274 275 |
{ size_t limit; limit = romfs_maxsize(sb); if (pos >= limit) return -EIO; if (size > ROMFS_MAXFN) return -ENAMETOOLONG; |
84baf74bf ROMFS: romfs_look... |
276 |
if (size + 1 > limit - pos) |
da4458bda NOMMU: Make it po... |
277 278 279 280 |
return -EIO; #ifdef CONFIG_ROMFS_ON_MTD if (sb->s_mtd) |
84baf74bf ROMFS: romfs_look... |
281 |
return romfs_mtd_strcmp(sb, pos, str, size); |
da4458bda NOMMU: Make it po... |
282 283 284 |
#endif #ifdef CONFIG_ROMFS_ON_BLOCK if (sb->s_bdev) |
84baf74bf ROMFS: romfs_look... |
285 |
return romfs_blk_strcmp(sb, pos, str, size); |
da4458bda NOMMU: Make it po... |
286 287 288 |
#endif return -EIO; } |