xref: /openbmc/linux/arch/um/drivers/cow_user.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
2cb8fa61cSJeff Dike /*
3cb8fa61cSJeff Dike  * Copyright (C) 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
4cb8fa61cSJeff Dike  */
5cb8fa61cSJeff Dike 
6cb8fa61cSJeff Dike /*
7cb8fa61cSJeff Dike  * _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
81da177e4SLinus Torvalds  * that.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <unistd.h>
11cb8fa61cSJeff Dike #include <errno.h>
12cb8fa61cSJeff Dike #include <string.h>
13cb8fa61cSJeff Dike #include <arpa/inet.h>
14d824d063SAl Viro #include <endian.h>
151da177e4SLinus Torvalds #include "cow.h"
161da177e4SLinus Torvalds #include "cow_sys.h"
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #define PATH_LEN_V1 256
191da177e4SLinus Torvalds 
20*853bc0abSArnd Bergmann /* unsigned time_t works until year 2106 */
21f2ea3940SPaolo 'Blaisorblade' Giarrusso typedef __u32 time32_t;
221da177e4SLinus Torvalds 
23f2ea3940SPaolo 'Blaisorblade' Giarrusso struct cow_header_v1 {
24f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__s32 magic;
25f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__s32 version;
26f2ea3940SPaolo 'Blaisorblade' Giarrusso 	char backing_file[PATH_LEN_V1];
27f2ea3940SPaolo 'Blaisorblade' Giarrusso 	time32_t mtime;
28f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u64 size;
29f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__s32 sectorsize;
30f2ea3940SPaolo 'Blaisorblade' Giarrusso } __attribute__((packed));
31f2ea3940SPaolo 'Blaisorblade' Giarrusso 
32cb8fa61cSJeff Dike /*
33cb8fa61cSJeff Dike  * Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
34f2ea3940SPaolo 'Blaisorblade' Giarrusso  * case other systems have different values for MAXPATHLEN.
35f2ea3940SPaolo 'Blaisorblade' Giarrusso  *
36f2ea3940SPaolo 'Blaisorblade' Giarrusso  * The same must hold for V2 - we want file format compatibility, not anything
37f2ea3940SPaolo 'Blaisorblade' Giarrusso  * else.
38f2ea3940SPaolo 'Blaisorblade' Giarrusso  */
39f2ea3940SPaolo 'Blaisorblade' Giarrusso #define PATH_LEN_V3 4096
40f2ea3940SPaolo 'Blaisorblade' Giarrusso #define PATH_LEN_V2 PATH_LEN_V3
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds struct cow_header_v2 {
431da177e4SLinus Torvalds 	__u32 magic;
441da177e4SLinus Torvalds 	__u32 version;
451da177e4SLinus Torvalds 	char backing_file[PATH_LEN_V2];
46f2ea3940SPaolo 'Blaisorblade' Giarrusso 	time32_t mtime;
471da177e4SLinus Torvalds 	__u64 size;
48f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__s32 sectorsize;
49f2ea3940SPaolo 'Blaisorblade' Giarrusso } __attribute__((packed));
501da177e4SLinus Torvalds 
51cb8fa61cSJeff Dike /*
52cb8fa61cSJeff Dike  * Changes from V2 -
531da177e4SLinus Torvalds  *	PATH_LEN_V3 as described above
541da177e4SLinus Torvalds  *	Explicitly specify field bit lengths for systems with different
551da177e4SLinus Torvalds  *		lengths for the usual C types.  Not sure whether char or
561da177e4SLinus Torvalds  *		time_t should be changed, this can be changed later without
571da177e4SLinus Torvalds  *		breaking compatibility
581da177e4SLinus Torvalds  *	Add alignment field so that different alignments can be used for the
591da177e4SLinus Torvalds  *		bitmap and data
601da177e4SLinus Torvalds  * 	Add cow_format field to allow for the possibility of different ways
611da177e4SLinus Torvalds  *		of specifying the COW blocks.  For now, the only value is 0,
621da177e4SLinus Torvalds  * 		for the traditional COW bitmap.
631da177e4SLinus Torvalds  *	Move the backing_file field to the end of the header.  This allows
641da177e4SLinus Torvalds  *		for the possibility of expanding it into the padding required
651da177e4SLinus Torvalds  *		by the bitmap alignment.
661da177e4SLinus Torvalds  * 	The bitmap and data portions of the file will be aligned as specified
671da177e4SLinus Torvalds  * 		by the alignment field.  This is to allow COW files to be
681da177e4SLinus Torvalds  *		put on devices with restrictions on access alignments, such as
691da177e4SLinus Torvalds  *		/dev/raw, with a 512 byte alignment restriction.  This also
701da177e4SLinus Torvalds  *		allows the data to be more aligned more strictly than on
711da177e4SLinus Torvalds  *		sector boundaries.  This is needed for ubd-mmap, which needs
721da177e4SLinus Torvalds  *		the data to be page aligned.
731da177e4SLinus Torvalds  *	Fixed (finally!) the rounding bug
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds 
76cb8fa61cSJeff Dike /*
77cb8fa61cSJeff Dike  * Until Dec2005, __attribute__((packed)) was left out from the below
78f2ea3940SPaolo 'Blaisorblade' Giarrusso  * definition, leading on 64-bit systems to 4 bytes of padding after mtime, to
79f2ea3940SPaolo 'Blaisorblade' Giarrusso  * align size to 8-byte alignment.  This shifted all fields above (no padding
80f2ea3940SPaolo 'Blaisorblade' Giarrusso  * was present on 32-bit, no other padding was added).
81f2ea3940SPaolo 'Blaisorblade' Giarrusso  *
82f2ea3940SPaolo 'Blaisorblade' Giarrusso  * However, this _can be detected_: it means that cow_format (always 0 until
83f2ea3940SPaolo 'Blaisorblade' Giarrusso  * now) is shifted onto the first 4 bytes of backing_file, where it is otherwise
84f2ea3940SPaolo 'Blaisorblade' Giarrusso  * impossible to find 4 zeros. -bb */
85f2ea3940SPaolo 'Blaisorblade' Giarrusso 
861da177e4SLinus Torvalds struct cow_header_v3 {
871da177e4SLinus Torvalds 	__u32 magic;
881da177e4SLinus Torvalds 	__u32 version;
891da177e4SLinus Torvalds 	__u32 mtime;
901da177e4SLinus Torvalds 	__u64 size;
911da177e4SLinus Torvalds 	__u32 sectorsize;
921da177e4SLinus Torvalds 	__u32 alignment;
931da177e4SLinus Torvalds 	__u32 cow_format;
941da177e4SLinus Torvalds 	char backing_file[PATH_LEN_V3];
95cda402b2SPaolo 'Blaisorblade' Giarrusso } __attribute__((packed));
961da177e4SLinus Torvalds 
97f2ea3940SPaolo 'Blaisorblade' Giarrusso /* This is the broken layout used by some 64-bit binaries. */
98f2ea3940SPaolo 'Blaisorblade' Giarrusso struct cow_header_v3_broken {
99f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u32 magic;
100f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u32 version;
101f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__s64 mtime;
102f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u64 size;
103f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u32 sectorsize;
104f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u32 alignment;
105f2ea3940SPaolo 'Blaisorblade' Giarrusso 	__u32 cow_format;
106f2ea3940SPaolo 'Blaisorblade' Giarrusso 	char backing_file[PATH_LEN_V3];
107b15fb6b1SPaolo 'Blaisorblade' Giarrusso };
108f2ea3940SPaolo 'Blaisorblade' Giarrusso 
1091da177e4SLinus Torvalds /* COW format definitions - for now, we have only the usual COW bitmap */
1101da177e4SLinus Torvalds #define COW_BITMAP 0
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds union cow_header {
1131da177e4SLinus Torvalds 	struct cow_header_v1 v1;
1141da177e4SLinus Torvalds 	struct cow_header_v2 v2;
1151da177e4SLinus Torvalds 	struct cow_header_v3 v3;
116f2ea3940SPaolo 'Blaisorblade' Giarrusso 	struct cow_header_v3_broken v3_b;
1171da177e4SLinus Torvalds };
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define COW_MAGIC 0x4f4f4f4d  /* MOOO */
1201da177e4SLinus Torvalds #define COW_VERSION 3
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
1231da177e4SLinus Torvalds #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
1241da177e4SLinus Torvalds 
cow_sizes(int version,__u64 size,int sectorsize,int align,int bitmap_offset,unsigned long * bitmap_len_out,int * data_offset_out)1251da177e4SLinus Torvalds void cow_sizes(int version, __u64 size, int sectorsize, int align,
1261da177e4SLinus Torvalds 	       int bitmap_offset, unsigned long *bitmap_len_out,
1271da177e4SLinus Torvalds 	       int *data_offset_out)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	if (version < 3) {
1301da177e4SLinus Torvalds 		*bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		*data_offset_out = bitmap_offset + *bitmap_len_out;
1331da177e4SLinus Torvalds 		*data_offset_out = (*data_offset_out + sectorsize - 1) /
1341da177e4SLinus Torvalds 			sectorsize;
1351da177e4SLinus Torvalds 		*data_offset_out *= sectorsize;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	else {
1381da177e4SLinus Torvalds 		*bitmap_len_out = DIV_ROUND(size, sectorsize);
1391da177e4SLinus Torvalds 		*bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 		*data_offset_out = bitmap_offset + *bitmap_len_out;
1421da177e4SLinus Torvalds 		*data_offset_out = ROUND_UP(*data_offset_out, align);
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
absolutize(char * to,int size,char * from)1461da177e4SLinus Torvalds static int absolutize(char *to, int size, char *from)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	char save_cwd[256], *slash;
1491da177e4SLinus Torvalds 	int remaining;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
1521da177e4SLinus Torvalds 		cow_printf("absolutize : unable to get cwd - errno = %d\n",
1531da177e4SLinus Torvalds 			   errno);
154cb8fa61cSJeff Dike 		return -1;
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 	slash = strrchr(from, '/');
1571da177e4SLinus Torvalds 	if (slash != NULL) {
1581da177e4SLinus Torvalds 		*slash = '\0';
1591da177e4SLinus Torvalds 		if (chdir(from)) {
1601da177e4SLinus Torvalds 			*slash = '/';
1611da177e4SLinus Torvalds 			cow_printf("absolutize : Can't cd to '%s' - "
1621da177e4SLinus Torvalds 				   "errno = %d\n", from, errno);
163cb8fa61cSJeff Dike 			return -1;
1641da177e4SLinus Torvalds 		}
1651da177e4SLinus Torvalds 		*slash = '/';
1661da177e4SLinus Torvalds 		if (getcwd(to, size) == NULL) {
1671da177e4SLinus Torvalds 			cow_printf("absolutize : unable to get cwd of '%s' - "
1681da177e4SLinus Torvalds 			       "errno = %d\n", from, errno);
169cb8fa61cSJeff Dike 			return -1;
1701da177e4SLinus Torvalds 		}
1711da177e4SLinus Torvalds 		remaining = size - strlen(to);
1721da177e4SLinus Torvalds 		if (strlen(slash) + 1 > remaining) {
1731da177e4SLinus Torvalds 			cow_printf("absolutize : unable to fit '%s' into %d "
1741da177e4SLinus Torvalds 			       "chars\n", from, size);
175cb8fa61cSJeff Dike 			return -1;
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 		strcat(to, slash);
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 	else {
1801da177e4SLinus Torvalds 		if (strlen(save_cwd) + 1 + strlen(from) + 1 > size) {
1811da177e4SLinus Torvalds 			cow_printf("absolutize : unable to fit '%s' into %d "
1821da177e4SLinus Torvalds 			       "chars\n", from, size);
183cb8fa61cSJeff Dike 			return -1;
1841da177e4SLinus Torvalds 		}
1851da177e4SLinus Torvalds 		strcpy(to, save_cwd);
1861da177e4SLinus Torvalds 		strcat(to, "/");
1871da177e4SLinus Torvalds 		strcat(to, from);
1881da177e4SLinus Torvalds 	}
1897a99ae7cSVitaliy Ivanov 	if (chdir(save_cwd)) {
1907a99ae7cSVitaliy Ivanov 		cow_printf("absolutize : Can't cd to '%s' - "
1917a99ae7cSVitaliy Ivanov 			   "errno = %d\n", save_cwd, errno);
1927a99ae7cSVitaliy Ivanov 		return -1;
1937a99ae7cSVitaliy Ivanov 	}
194cb8fa61cSJeff Dike 	return 0;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
write_cow_header(char * cow_file,int fd,char * backing_file,int sectorsize,int alignment,unsigned long long * size)1971da177e4SLinus Torvalds int write_cow_header(char *cow_file, int fd, char *backing_file,
1981da177e4SLinus Torvalds 		     int sectorsize, int alignment, unsigned long long *size)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct cow_header_v3 *header;
201*853bc0abSArnd Bergmann 	long long modtime;
2021da177e4SLinus Torvalds 	int err;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	err = cow_seek_file(fd, 0);
2051da177e4SLinus Torvalds 	if (err < 0) {
2061da177e4SLinus Torvalds 		cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
2071da177e4SLinus Torvalds 		goto out;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	err = -ENOMEM;
2111da177e4SLinus Torvalds 	header = cow_malloc(sizeof(*header));
2121da177e4SLinus Torvalds 	if (header == NULL) {
213cb8fa61cSJeff Dike 		cow_printf("write_cow_header - failed to allocate COW V3 "
214cb8fa61cSJeff Dike 			   "header\n");
2151da177e4SLinus Torvalds 		goto out;
2161da177e4SLinus Torvalds 	}
217d824d063SAl Viro 	header->magic = htobe32(COW_MAGIC);
218d824d063SAl Viro 	header->version = htobe32(COW_VERSION);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	err = -EINVAL;
2211da177e4SLinus Torvalds 	if (strlen(backing_file) > sizeof(header->backing_file) - 1) {
2226dad2d3fSPaolo 'Blaisorblade' Giarrusso 		/* Below, %zd is for a size_t value */
2231da177e4SLinus Torvalds 		cow_printf("Backing file name \"%s\" is too long - names are "
2246dad2d3fSPaolo 'Blaisorblade' Giarrusso 			   "limited to %zd characters\n", backing_file,
2251da177e4SLinus Torvalds 			   sizeof(header->backing_file) - 1);
2261da177e4SLinus Torvalds 		goto out_free;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	if (absolutize(header->backing_file, sizeof(header->backing_file),
2301da177e4SLinus Torvalds 		      backing_file))
2311da177e4SLinus Torvalds 		goto out_free;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	err = os_file_modtime(header->backing_file, &modtime);
2341da177e4SLinus Torvalds 	if (err < 0) {
23531bc5a33SPaolo 'Blaisorblade' Giarrusso 		cow_printf("write_cow_header - backing file '%s' mtime "
23631bc5a33SPaolo 'Blaisorblade' Giarrusso 			   "request failed, err = %d\n", header->backing_file,
23731bc5a33SPaolo 'Blaisorblade' Giarrusso 			   -err);
2381da177e4SLinus Torvalds 		goto out_free;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	err = cow_file_size(header->backing_file, size);
2421da177e4SLinus Torvalds 	if (err < 0) {
24331bc5a33SPaolo 'Blaisorblade' Giarrusso 		cow_printf("write_cow_header - couldn't get size of "
24431bc5a33SPaolo 'Blaisorblade' Giarrusso 			   "backing file '%s', err = %d\n",
24531bc5a33SPaolo 'Blaisorblade' Giarrusso 			   header->backing_file, -err);
2461da177e4SLinus Torvalds 		goto out_free;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
249d824d063SAl Viro 	header->mtime = htobe32(modtime);
250d824d063SAl Viro 	header->size = htobe64(*size);
251d824d063SAl Viro 	header->sectorsize = htobe32(sectorsize);
252d824d063SAl Viro 	header->alignment = htobe32(alignment);
2531da177e4SLinus Torvalds 	header->cow_format = COW_BITMAP;
2541da177e4SLinus Torvalds 
25531bc5a33SPaolo 'Blaisorblade' Giarrusso 	err = cow_write_file(fd, header, sizeof(*header));
2561da177e4SLinus Torvalds 	if (err != sizeof(*header)) {
25731bc5a33SPaolo 'Blaisorblade' Giarrusso 		cow_printf("write_cow_header - write of header to "
25831bc5a33SPaolo 'Blaisorblade' Giarrusso 			   "new COW file '%s' failed, err = %d\n", cow_file,
25931bc5a33SPaolo 'Blaisorblade' Giarrusso 			   -err);
2601da177e4SLinus Torvalds 		goto out_free;
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 	err = 0;
2631da177e4SLinus Torvalds  out_free:
2641da177e4SLinus Torvalds 	cow_free(header);
2651da177e4SLinus Torvalds  out:
266cb8fa61cSJeff Dike 	return err;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
file_reader(__u64 offset,char * buf,int len,void * arg)2691da177e4SLinus Torvalds int file_reader(__u64 offset, char *buf, int len, void *arg)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	int fd = *((int *) arg);
2721da177e4SLinus Torvalds 
273cb8fa61cSJeff Dike 	return pread(fd, buf, len, offset);
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /* XXX Need to sanity-check the values read from the header */
2771da177e4SLinus Torvalds 
read_cow_header(int (* reader)(__u64,char *,int,void *),void * arg,__u32 * version_out,char ** backing_file_out,long long * mtime_out,unsigned long long * size_out,int * sectorsize_out,__u32 * align_out,int * bitmap_offset_out)2781da177e4SLinus Torvalds int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
2791da177e4SLinus Torvalds 		    __u32 *version_out, char **backing_file_out,
280*853bc0abSArnd Bergmann 		    long long *mtime_out, unsigned long long *size_out,
2811da177e4SLinus Torvalds 		    int *sectorsize_out, __u32 *align_out,
2821da177e4SLinus Torvalds 		    int *bitmap_offset_out)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	union cow_header *header;
2851da177e4SLinus Torvalds 	char *file;
2861da177e4SLinus Torvalds 	int err, n;
2871da177e4SLinus Torvalds 	unsigned long version, magic;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	header = cow_malloc(sizeof(*header));
2901da177e4SLinus Torvalds 	if (header == NULL) {
2911da177e4SLinus Torvalds 	        cow_printf("read_cow_header - Failed to allocate header\n");
292cb8fa61cSJeff Dike 		return -ENOMEM;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	err = -EINVAL;
2951da177e4SLinus Torvalds 	n = (*reader)(0, (char *) header, sizeof(*header), arg);
2961da177e4SLinus Torvalds 	if (n < offsetof(typeof(header->v1), backing_file)) {
2971da177e4SLinus Torvalds 		cow_printf("read_cow_header - short header\n");
2981da177e4SLinus Torvalds 		goto out;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	magic = header->v1.magic;
302cb8fa61cSJeff Dike 	if (magic == COW_MAGIC)
3031da177e4SLinus Torvalds 		version = header->v1.version;
304d824d063SAl Viro 	else if (magic == be32toh(COW_MAGIC))
305d824d063SAl Viro 		version = be32toh(header->v1.version);
3061da177e4SLinus Torvalds 	/* No error printed because the non-COW case comes through here */
3071da177e4SLinus Torvalds 	else goto out;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	*version_out = version;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (version == 1) {
3121da177e4SLinus Torvalds 		if (n < sizeof(header->v1)) {
3131da177e4SLinus Torvalds 			cow_printf("read_cow_header - failed to read V1 "
3141da177e4SLinus Torvalds 				   "header\n");
3151da177e4SLinus Torvalds 			goto out;
3161da177e4SLinus Torvalds 		}
3171da177e4SLinus Torvalds 		*mtime_out = header->v1.mtime;
3181da177e4SLinus Torvalds 		*size_out = header->v1.size;
3191da177e4SLinus Torvalds 		*sectorsize_out = header->v1.sectorsize;
3201da177e4SLinus Torvalds 		*bitmap_offset_out = sizeof(header->v1);
3211da177e4SLinus Torvalds 		*align_out = *sectorsize_out;
3221da177e4SLinus Torvalds 		file = header->v1.backing_file;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 	else if (version == 2) {
3251da177e4SLinus Torvalds 		if (n < sizeof(header->v2)) {
3261da177e4SLinus Torvalds 			cow_printf("read_cow_header - failed to read V2 "
3271da177e4SLinus Torvalds 				   "header\n");
3281da177e4SLinus Torvalds 			goto out;
3291da177e4SLinus Torvalds 		}
330d824d063SAl Viro 		*mtime_out = be32toh(header->v2.mtime);
331d824d063SAl Viro 		*size_out = be64toh(header->v2.size);
332d824d063SAl Viro 		*sectorsize_out = be32toh(header->v2.sectorsize);
3331da177e4SLinus Torvalds 		*bitmap_offset_out = sizeof(header->v2);
3341da177e4SLinus Torvalds 		*align_out = *sectorsize_out;
3351da177e4SLinus Torvalds 		file = header->v2.backing_file;
3361da177e4SLinus Torvalds 	}
337f2ea3940SPaolo 'Blaisorblade' Giarrusso 	/* This is very subtle - see above at union cow_header definition */
338f2ea3940SPaolo 'Blaisorblade' Giarrusso 	else if (version == 3 && (*((int*)header->v3.backing_file) != 0)) {
3391da177e4SLinus Torvalds 		if (n < sizeof(header->v3)) {
34031bc5a33SPaolo 'Blaisorblade' Giarrusso 			cow_printf("read_cow_header - failed to read V3 "
3411da177e4SLinus Torvalds 				   "header\n");
3421da177e4SLinus Torvalds 			goto out;
3431da177e4SLinus Torvalds 		}
344d824d063SAl Viro 		*mtime_out = be32toh(header->v3.mtime);
345d824d063SAl Viro 		*size_out = be64toh(header->v3.size);
346d824d063SAl Viro 		*sectorsize_out = be32toh(header->v3.sectorsize);
347d824d063SAl Viro 		*align_out = be32toh(header->v3.alignment);
348f2ea3940SPaolo 'Blaisorblade' Giarrusso 		if (*align_out == 0) {
349f2ea3940SPaolo 'Blaisorblade' Giarrusso 			cow_printf("read_cow_header - invalid COW header, "
350f2ea3940SPaolo 'Blaisorblade' Giarrusso 				   "align == 0\n");
351f2ea3940SPaolo 'Blaisorblade' Giarrusso 		}
3521da177e4SLinus Torvalds 		*bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
3531da177e4SLinus Torvalds 		file = header->v3.backing_file;
3541da177e4SLinus Torvalds 	}
355f2ea3940SPaolo 'Blaisorblade' Giarrusso 	else if (version == 3) {
356f2ea3940SPaolo 'Blaisorblade' Giarrusso 		cow_printf("read_cow_header - broken V3 file with"
357f2ea3940SPaolo 'Blaisorblade' Giarrusso 			   " 64-bit layout - recovering content.\n");
358f2ea3940SPaolo 'Blaisorblade' Giarrusso 
359f2ea3940SPaolo 'Blaisorblade' Giarrusso 		if (n < sizeof(header->v3_b)) {
360f2ea3940SPaolo 'Blaisorblade' Giarrusso 			cow_printf("read_cow_header - failed to read V3 "
361f2ea3940SPaolo 'Blaisorblade' Giarrusso 				   "header\n");
362f2ea3940SPaolo 'Blaisorblade' Giarrusso 			goto out;
363f2ea3940SPaolo 'Blaisorblade' Giarrusso 		}
364f2ea3940SPaolo 'Blaisorblade' Giarrusso 
365cb8fa61cSJeff Dike 		/*
366cb8fa61cSJeff Dike 		 * this was used until Dec2005 - 64bits are needed to represent
367*853bc0abSArnd Bergmann 		 * 2106+. I.e. we can safely do this truncating cast.
368f2ea3940SPaolo 'Blaisorblade' Giarrusso 		 *
369d824d063SAl Viro 		 * Additionally, we must use be32toh() instead of be64toh(), since
370f2ea3940SPaolo 'Blaisorblade' Giarrusso 		 * the program used to use the former (tested - I got mtime
371f2ea3940SPaolo 'Blaisorblade' Giarrusso 		 * mismatch "0 vs whatever").
372f2ea3940SPaolo 'Blaisorblade' Giarrusso 		 *
373f2ea3940SPaolo 'Blaisorblade' Giarrusso 		 * Ever heard about bug-to-bug-compatibility ? ;-) */
374d824d063SAl Viro 		*mtime_out = (time32_t) be32toh(header->v3_b.mtime);
375f2ea3940SPaolo 'Blaisorblade' Giarrusso 
376d824d063SAl Viro 		*size_out = be64toh(header->v3_b.size);
377d824d063SAl Viro 		*sectorsize_out = be32toh(header->v3_b.sectorsize);
378d824d063SAl Viro 		*align_out = be32toh(header->v3_b.alignment);
379f2ea3940SPaolo 'Blaisorblade' Giarrusso 		if (*align_out == 0) {
380f2ea3940SPaolo 'Blaisorblade' Giarrusso 			cow_printf("read_cow_header - invalid COW header, "
381f2ea3940SPaolo 'Blaisorblade' Giarrusso 				   "align == 0\n");
382f2ea3940SPaolo 'Blaisorblade' Giarrusso 		}
383f2ea3940SPaolo 'Blaisorblade' Giarrusso 		*bitmap_offset_out = ROUND_UP(sizeof(header->v3_b), *align_out);
384f2ea3940SPaolo 'Blaisorblade' Giarrusso 		file = header->v3_b.backing_file;
385f2ea3940SPaolo 'Blaisorblade' Giarrusso 	}
3861da177e4SLinus Torvalds 	else {
3871da177e4SLinus Torvalds 		cow_printf("read_cow_header - invalid COW version\n");
3881da177e4SLinus Torvalds 		goto out;
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 	err = -ENOMEM;
3911da177e4SLinus Torvalds 	*backing_file_out = cow_strdup(file);
3921da177e4SLinus Torvalds 	if (*backing_file_out == NULL) {
3931da177e4SLinus Torvalds 		cow_printf("read_cow_header - failed to allocate backing "
3941da177e4SLinus Torvalds 			   "file\n");
3951da177e4SLinus Torvalds 		goto out;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 	err = 0;
3981da177e4SLinus Torvalds  out:
3991da177e4SLinus Torvalds 	cow_free(header);
400cb8fa61cSJeff Dike 	return err;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
init_cow_file(int fd,char * cow_file,char * backing_file,int sectorsize,int alignment,int * bitmap_offset_out,unsigned long * bitmap_len_out,int * data_offset_out)4031da177e4SLinus Torvalds int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
4041da177e4SLinus Torvalds 		  int alignment, int *bitmap_offset_out,
4051da177e4SLinus Torvalds 		  unsigned long *bitmap_len_out, int *data_offset_out)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	unsigned long long size, offset;
4081da177e4SLinus Torvalds 	char zero = 0;
4091da177e4SLinus Torvalds 	int err;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	err = write_cow_header(cow_file, fd, backing_file, sectorsize,
4121da177e4SLinus Torvalds 			       alignment, &size);
4131da177e4SLinus Torvalds 	if (err)
4141da177e4SLinus Torvalds 		goto out;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	*bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
4171da177e4SLinus Torvalds 	cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
4181da177e4SLinus Torvalds 		  bitmap_len_out, data_offset_out);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	offset = *data_offset_out + size - sizeof(zero);
4211da177e4SLinus Torvalds 	err = cow_seek_file(fd, offset);
4221da177e4SLinus Torvalds 	if (err < 0) {
4231da177e4SLinus Torvalds 		cow_printf("cow bitmap lseek failed : err = %d\n", -err);
4241da177e4SLinus Torvalds 		goto out;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
427cb8fa61cSJeff Dike 	/*
428cb8fa61cSJeff Dike 	 * does not really matter how much we write it is just to set EOF
4291da177e4SLinus Torvalds 	 * this also sets the entire COW bitmap
4301da177e4SLinus Torvalds 	 * to zero without having to allocate it
4311da177e4SLinus Torvalds 	 */
4321da177e4SLinus Torvalds 	err = cow_write_file(fd, &zero, sizeof(zero));
4331da177e4SLinus Torvalds 	if (err != sizeof(zero)) {
4341da177e4SLinus Torvalds 		cow_printf("Write of bitmap to new COW file '%s' failed, "
4351da177e4SLinus Torvalds 			   "err = %d\n", cow_file, -err);
436fe1db50cSPaolo 'Blaisorblade' Giarrusso 		if (err >= 0)
4371da177e4SLinus Torvalds 			err = -EINVAL;
4381da177e4SLinus Torvalds 		goto out;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
441cb8fa61cSJeff Dike 	return 0;
4421da177e4SLinus Torvalds  out:
443cb8fa61cSJeff Dike 	return err;
4441da177e4SLinus Torvalds }
445