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