MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / um / drivers / cow_user.c
blobd3fad248636f1fc9870eb8103ce24e0f8084aa53
1 #include <stddef.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <byteswap.h>
6 #include <sys/time.h>
7 #include <sys/param.h>
8 #include <sys/user.h>
9 #include <netinet/in.h>
11 #include "os.h"
13 #include "cow.h"
14 #include "cow_sys.h"
16 #define PATH_LEN_V1 256
18 struct cow_header_v1 {
19 int magic;
20 int version;
21 char backing_file[PATH_LEN_V1];
22 time_t mtime;
23 __u64 size;
24 int sectorsize;
27 #define PATH_LEN_V2 MAXPATHLEN
29 struct cow_header_v2 {
30 __u32 magic;
31 __u32 version;
32 char backing_file[PATH_LEN_V2];
33 time_t mtime;
34 __u64 size;
35 int sectorsize;
38 /* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
39 * case other systems have different values for MAXPATHLEN
41 #define PATH_LEN_V3 4096
43 /* Changes from V2 -
44 * PATH_LEN_V3 as described above
45 * Explicitly specify field bit lengths for systems with different
46 * lengths for the usual C types. Not sure whether char or
47 * time_t should be changed, this can be changed later without
48 * breaking compatibility
49 * Add alignment field so that different alignments can be used for the
50 * bitmap and data
51 * Add cow_format field to allow for the possibility of different ways
52 * of specifying the COW blocks. For now, the only value is 0,
53 * for the traditional COW bitmap.
54 * Move the backing_file field to the end of the header. This allows
55 * for the possibility of expanding it into the padding required
56 * by the bitmap alignment.
57 * The bitmap and data portions of the file will be aligned as specified
58 * by the alignment field. This is to allow COW files to be
59 * put on devices with restrictions on access alignments, such as
60 * /dev/raw, with a 512 byte alignment restriction. This also
61 * allows the data to be more aligned more strictly than on
62 * sector boundaries. This is needed for ubd-mmap, which needs
63 * the data to be page aligned.
64 * Fixed (finally!) the rounding bug
67 struct cow_header_v3 {
68 __u32 magic;
69 __u32 version;
70 time_t mtime;
71 __u64 size;
72 __u32 sectorsize;
73 __u32 alignment;
74 __u32 cow_format;
75 char backing_file[PATH_LEN_V3];
78 /* COW format definitions - for now, we have only the usual COW bitmap */
79 #define COW_BITMAP 0
81 union cow_header {
82 struct cow_header_v1 v1;
83 struct cow_header_v2 v2;
84 struct cow_header_v3 v3;
87 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
88 #define COW_VERSION 3
90 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
91 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
93 void cow_sizes(int version, __u64 size, int sectorsize, int align,
94 int bitmap_offset, unsigned long *bitmap_len_out,
95 int *data_offset_out)
97 if(version < 3){
98 *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
100 *data_offset_out = bitmap_offset + *bitmap_len_out;
101 *data_offset_out = (*data_offset_out + sectorsize - 1) /
102 sectorsize;
103 *data_offset_out *= sectorsize;
105 else {
106 *bitmap_len_out = DIV_ROUND(size, sectorsize);
107 *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
109 *data_offset_out = bitmap_offset + *bitmap_len_out;
110 *data_offset_out = ROUND_UP(*data_offset_out, align);
114 static int absolutize(char *to, int size, char *from)
116 char save_cwd[256], *slash;
117 int remaining;
119 if(getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
120 cow_printf("absolutize : unable to get cwd - errno = %d\n",
121 errno);
122 return(-1);
124 slash = strrchr(from, '/');
125 if(slash != NULL){
126 *slash = '\0';
127 if(chdir(from)){
128 *slash = '/';
129 cow_printf("absolutize : Can't cd to '%s' - "
130 "errno = %d\n", from, errno);
131 return(-1);
133 *slash = '/';
134 if(getcwd(to, size) == NULL){
135 cow_printf("absolutize : unable to get cwd of '%s' - "
136 "errno = %d\n", from, errno);
137 return(-1);
139 remaining = size - strlen(to);
140 if(strlen(slash) + 1 > remaining){
141 cow_printf("absolutize : unable to fit '%s' into %d "
142 "chars\n", from, size);
143 return(-1);
145 strcat(to, slash);
147 else {
148 if(strlen(save_cwd) + 1 + strlen(from) + 1 > size){
149 cow_printf("absolutize : unable to fit '%s' into %d "
150 "chars\n", from, size);
151 return(-1);
153 strcpy(to, save_cwd);
154 strcat(to, "/");
155 strcat(to, from);
157 chdir(save_cwd);
158 return(0);
161 int write_cow_header(char *cow_file, int fd, char *backing_file,
162 int sectorsize, int alignment, long long *size)
164 struct cow_header_v3 *header;
165 unsigned long modtime;
166 int err;
168 err = cow_seek_file(fd, 0);
169 if(err < 0){
170 cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
171 goto out;
174 err = -ENOMEM;
175 header = cow_malloc(sizeof(*header));
176 if(header == NULL){
177 cow_printf("Failed to allocate COW V3 header\n");
178 goto out;
180 header->magic = htonl(COW_MAGIC);
181 header->version = htonl(COW_VERSION);
183 err = -EINVAL;
184 if(strlen(backing_file) > sizeof(header->backing_file) - 1){
185 cow_printf("Backing file name \"%s\" is too long - names are "
186 "limited to %d characters\n", backing_file,
187 sizeof(header->backing_file) - 1);
188 goto out_free;
191 if(absolutize(header->backing_file, sizeof(header->backing_file),
192 backing_file))
193 goto out_free;
195 err = os_file_modtime(header->backing_file, &modtime);
196 if(err < 0){
197 cow_printf("Backing file '%s' mtime request failed, "
198 "err = %d\n", header->backing_file, -err);
199 goto out_free;
202 err = cow_file_size(header->backing_file, size);
203 if(err < 0){
204 cow_printf("Couldn't get size of backing file '%s', "
205 "err = %d\n", header->backing_file, -err);
206 goto out_free;
209 header->mtime = htonl(modtime);
210 header->size = htonll(*size);
211 header->sectorsize = htonl(sectorsize);
212 header->alignment = htonl(alignment);
213 header->cow_format = COW_BITMAP;
215 err = os_write_file(fd, header, sizeof(*header));
216 if(err != sizeof(*header)){
217 cow_printf("Write of header to new COW file '%s' failed, "
218 "err = %d\n", cow_file, -err);
219 goto out_free;
221 err = 0;
222 out_free:
223 cow_free(header);
224 out:
225 return(err);
228 int file_reader(__u64 offset, char *buf, int len, void *arg)
230 int fd = *((int *) arg);
232 return(pread(fd, buf, len, offset));
235 /* XXX Need to sanity-check the values read from the header */
237 int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
238 __u32 *version_out, char **backing_file_out,
239 time_t *mtime_out, __u64 *size_out,
240 int *sectorsize_out, __u32 *align_out,
241 int *bitmap_offset_out)
243 union cow_header *header;
244 char *file;
245 int err, n;
246 unsigned long version, magic;
248 header = cow_malloc(sizeof(*header));
249 if(header == NULL){
250 cow_printf("read_cow_header - Failed to allocate header\n");
251 return(-ENOMEM);
253 err = -EINVAL;
254 n = (*reader)(0, (char *) header, sizeof(*header), arg);
255 if(n < offsetof(typeof(header->v1), backing_file)){
256 cow_printf("read_cow_header - short header\n");
257 goto out;
260 magic = header->v1.magic;
261 if(magic == COW_MAGIC) {
262 version = header->v1.version;
264 else if(magic == ntohl(COW_MAGIC)){
265 version = ntohl(header->v1.version);
267 /* No error printed because the non-COW case comes through here */
268 else goto out;
270 *version_out = version;
272 if(version == 1){
273 if(n < sizeof(header->v1)){
274 cow_printf("read_cow_header - failed to read V1 "
275 "header\n");
276 goto out;
278 *mtime_out = header->v1.mtime;
279 *size_out = header->v1.size;
280 *sectorsize_out = header->v1.sectorsize;
281 *bitmap_offset_out = sizeof(header->v1);
282 *align_out = *sectorsize_out;
283 file = header->v1.backing_file;
285 else if(version == 2){
286 if(n < sizeof(header->v2)){
287 cow_printf("read_cow_header - failed to read V2 "
288 "header\n");
289 goto out;
291 *mtime_out = ntohl(header->v2.mtime);
292 *size_out = ntohll(header->v2.size);
293 *sectorsize_out = ntohl(header->v2.sectorsize);
294 *bitmap_offset_out = sizeof(header->v2);
295 *align_out = *sectorsize_out;
296 file = header->v2.backing_file;
298 else if(version == 3){
299 if(n < sizeof(header->v3)){
300 cow_printf("read_cow_header - failed to read V2 "
301 "header\n");
302 goto out;
304 *mtime_out = ntohl(header->v3.mtime);
305 *size_out = ntohll(header->v3.size);
306 *sectorsize_out = ntohl(header->v3.sectorsize);
307 *align_out = ntohl(header->v3.alignment);
308 *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
309 file = header->v3.backing_file;
311 else {
312 cow_printf("read_cow_header - invalid COW version\n");
313 goto out;
315 err = -ENOMEM;
316 *backing_file_out = cow_strdup(file);
317 if(*backing_file_out == NULL){
318 cow_printf("read_cow_header - failed to allocate backing "
319 "file\n");
320 goto out;
322 err = 0;
323 out:
324 cow_free(header);
325 return(err);
328 int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
329 int alignment, int *bitmap_offset_out,
330 unsigned long *bitmap_len_out, int *data_offset_out)
332 __u64 size, offset;
333 char zero = 0;
334 int err;
336 err = write_cow_header(cow_file, fd, backing_file, sectorsize,
337 alignment, &size);
338 if(err)
339 goto out;
341 *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
342 cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
343 bitmap_len_out, data_offset_out);
345 offset = *data_offset_out + size - sizeof(zero);
346 err = cow_seek_file(fd, offset);
347 if(err < 0){
348 cow_printf("cow bitmap lseek failed : err = %d\n", -err);
349 goto out;
352 /* does not really matter how much we write it is just to set EOF
353 * this also sets the entire COW bitmap
354 * to zero without having to allocate it
356 err = cow_write_file(fd, &zero, sizeof(zero));
357 if(err != sizeof(zero)){
358 cow_printf("Write of bitmap to new COW file '%s' failed, "
359 "err = %d\n", cow_file, -err);
360 err = -EINVAL;
361 goto out;
364 return(0);
366 out:
367 return(err);
371 * ---------------------------------------------------------------------------
372 * Local variables:
373 * c-file-style: "linux"
374 * End: