cow_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  3. * Licensed under the GPL
  4. */
  5. /*
  6. * _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
  7. * that.
  8. */
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <arpa/inet.h>
  13. #include <endian.h>
  14. #include "cow.h"
  15. #include "cow_sys.h"
  16. #define PATH_LEN_V1 256
  17. typedef __u32 time32_t;
  18. struct cow_header_v1 {
  19. __s32 magic;
  20. __s32 version;
  21. char backing_file[PATH_LEN_V1];
  22. time32_t mtime;
  23. __u64 size;
  24. __s32 sectorsize;
  25. } __attribute__((packed));
  26. /*
  27. * Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
  28. * case other systems have different values for MAXPATHLEN.
  29. *
  30. * The same must hold for V2 - we want file format compatibility, not anything
  31. * else.
  32. */
  33. #define PATH_LEN_V3 4096
  34. #define PATH_LEN_V2 PATH_LEN_V3
  35. struct cow_header_v2 {
  36. __u32 magic;
  37. __u32 version;
  38. char backing_file[PATH_LEN_V2];
  39. time32_t mtime;
  40. __u64 size;
  41. __s32 sectorsize;
  42. } __attribute__((packed));
  43. /*
  44. * Changes from V2 -
  45. * PATH_LEN_V3 as described above
  46. * Explicitly specify field bit lengths for systems with different
  47. * lengths for the usual C types. Not sure whether char or
  48. * time_t should be changed, this can be changed later without
  49. * breaking compatibility
  50. * Add alignment field so that different alignments can be used for the
  51. * bitmap and data
  52. * Add cow_format field to allow for the possibility of different ways
  53. * of specifying the COW blocks. For now, the only value is 0,
  54. * for the traditional COW bitmap.
  55. * Move the backing_file field to the end of the header. This allows
  56. * for the possibility of expanding it into the padding required
  57. * by the bitmap alignment.
  58. * The bitmap and data portions of the file will be aligned as specified
  59. * by the alignment field. This is to allow COW files to be
  60. * put on devices with restrictions on access alignments, such as
  61. * /dev/raw, with a 512 byte alignment restriction. This also
  62. * allows the data to be more aligned more strictly than on
  63. * sector boundaries. This is needed for ubd-mmap, which needs
  64. * the data to be page aligned.
  65. * Fixed (finally!) the rounding bug
  66. */
  67. /*
  68. * Until Dec2005, __attribute__((packed)) was left out from the below
  69. * definition, leading on 64-bit systems to 4 bytes of padding after mtime, to
  70. * align size to 8-byte alignment. This shifted all fields above (no padding
  71. * was present on 32-bit, no other padding was added).
  72. *
  73. * However, this _can be detected_: it means that cow_format (always 0 until
  74. * now) is shifted onto the first 4 bytes of backing_file, where it is otherwise
  75. * impossible to find 4 zeros. -bb */
  76. struct cow_header_v3 {
  77. __u32 magic;
  78. __u32 version;
  79. __u32 mtime;
  80. __u64 size;
  81. __u32 sectorsize;
  82. __u32 alignment;
  83. __u32 cow_format;
  84. char backing_file[PATH_LEN_V3];
  85. } __attribute__((packed));
  86. /* This is the broken layout used by some 64-bit binaries. */
  87. struct cow_header_v3_broken {
  88. __u32 magic;
  89. __u32 version;
  90. __s64 mtime;
  91. __u64 size;
  92. __u32 sectorsize;
  93. __u32 alignment;
  94. __u32 cow_format;
  95. char backing_file[PATH_LEN_V3];
  96. };
  97. /* COW format definitions - for now, we have only the usual COW bitmap */
  98. #define COW_BITMAP 0
  99. union cow_header {
  100. struct cow_header_v1 v1;
  101. struct cow_header_v2 v2;
  102. struct cow_header_v3 v3;
  103. struct cow_header_v3_broken v3_b;
  104. };
  105. #define COW_MAGIC 0x4f4f4f4d /* MOOO */
  106. #define COW_VERSION 3
  107. #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
  108. #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
  109. void cow_sizes(int version, __u64 size, int sectorsize, int align,
  110. int bitmap_offset, unsigned long *bitmap_len_out,
  111. int *data_offset_out)
  112. {
  113. if (version < 3) {
  114. *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
  115. *data_offset_out = bitmap_offset + *bitmap_len_out;
  116. *data_offset_out = (*data_offset_out + sectorsize - 1) /
  117. sectorsize;
  118. *data_offset_out *= sectorsize;
  119. }
  120. else {
  121. *bitmap_len_out = DIV_ROUND(size, sectorsize);
  122. *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
  123. *data_offset_out = bitmap_offset + *bitmap_len_out;
  124. *data_offset_out = ROUND_UP(*data_offset_out, align);
  125. }
  126. }
  127. static int absolutize(char *to, int size, char *from)
  128. {
  129. char save_cwd[256], *slash;
  130. int remaining;
  131. if (getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
  132. cow_printf("absolutize : unable to get cwd - errno = %d\n",
  133. errno);
  134. return -1;
  135. }
  136. slash = strrchr(from, '/');
  137. if (slash != NULL) {
  138. *slash = '\0';
  139. if (chdir(from)) {
  140. *slash = '/';
  141. cow_printf("absolutize : Can't cd to '%s' - "
  142. "errno = %d\n", from, errno);
  143. return -1;
  144. }
  145. *slash = '/';
  146. if (getcwd(to, size) == NULL) {
  147. cow_printf("absolutize : unable to get cwd of '%s' - "
  148. "errno = %d\n", from, errno);
  149. return -1;
  150. }
  151. remaining = size - strlen(to);
  152. if (strlen(slash) + 1 > remaining) {
  153. cow_printf("absolutize : unable to fit '%s' into %d "
  154. "chars\n", from, size);
  155. return -1;
  156. }
  157. strcat(to, slash);
  158. }
  159. else {
  160. if (strlen(save_cwd) + 1 + strlen(from) + 1 > size) {
  161. cow_printf("absolutize : unable to fit '%s' into %d "
  162. "chars\n", from, size);
  163. return -1;
  164. }
  165. strcpy(to, save_cwd);
  166. strcat(to, "/");
  167. strcat(to, from);
  168. }
  169. if (chdir(save_cwd)) {
  170. cow_printf("absolutize : Can't cd to '%s' - "
  171. "errno = %d\n", save_cwd, errno);
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. int write_cow_header(char *cow_file, int fd, char *backing_file,
  177. int sectorsize, int alignment, unsigned long long *size)
  178. {
  179. struct cow_header_v3 *header;
  180. unsigned long modtime;
  181. int err;
  182. err = cow_seek_file(fd, 0);
  183. if (err < 0) {
  184. cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
  185. goto out;
  186. }
  187. err = -ENOMEM;
  188. header = cow_malloc(sizeof(*header));
  189. if (header == NULL) {
  190. cow_printf("write_cow_header - failed to allocate COW V3 "
  191. "header\n");
  192. goto out;
  193. }
  194. header->magic = htobe32(COW_MAGIC);
  195. header->version = htobe32(COW_VERSION);
  196. err = -EINVAL;
  197. if (strlen(backing_file) > sizeof(header->backing_file) - 1) {
  198. /* Below, %zd is for a size_t value */
  199. cow_printf("Backing file name \"%s\" is too long - names are "
  200. "limited to %zd characters\n", backing_file,
  201. sizeof(header->backing_file) - 1);
  202. goto out_free;
  203. }
  204. if (absolutize(header->backing_file, sizeof(header->backing_file),
  205. backing_file))
  206. goto out_free;
  207. err = os_file_modtime(header->backing_file, &modtime);
  208. if (err < 0) {
  209. cow_printf("write_cow_header - backing file '%s' mtime "
  210. "request failed, err = %d\n", header->backing_file,
  211. -err);
  212. goto out_free;
  213. }
  214. err = cow_file_size(header->backing_file, size);
  215. if (err < 0) {
  216. cow_printf("write_cow_header - couldn't get size of "
  217. "backing file '%s', err = %d\n",
  218. header->backing_file, -err);
  219. goto out_free;
  220. }
  221. header->mtime = htobe32(modtime);
  222. header->size = htobe64(*size);
  223. header->sectorsize = htobe32(sectorsize);
  224. header->alignment = htobe32(alignment);
  225. header->cow_format = COW_BITMAP;
  226. err = cow_write_file(fd, header, sizeof(*header));
  227. if (err != sizeof(*header)) {
  228. cow_printf("write_cow_header - write of header to "
  229. "new COW file '%s' failed, err = %d\n", cow_file,
  230. -err);
  231. goto out_free;
  232. }
  233. err = 0;
  234. out_free:
  235. cow_free(header);
  236. out:
  237. return err;
  238. }
  239. int file_reader(__u64 offset, char *buf, int len, void *arg)
  240. {
  241. int fd = *((int *) arg);
  242. return pread(fd, buf, len, offset);
  243. }
  244. /* XXX Need to sanity-check the values read from the header */
  245. int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
  246. __u32 *version_out, char **backing_file_out,
  247. time_t *mtime_out, unsigned long long *size_out,
  248. int *sectorsize_out, __u32 *align_out,
  249. int *bitmap_offset_out)
  250. {
  251. union cow_header *header;
  252. char *file;
  253. int err, n;
  254. unsigned long version, magic;
  255. header = cow_malloc(sizeof(*header));
  256. if (header == NULL) {
  257. cow_printf("read_cow_header - Failed to allocate header\n");
  258. return -ENOMEM;
  259. }
  260. err = -EINVAL;
  261. n = (*reader)(0, (char *) header, sizeof(*header), arg);
  262. if (n < offsetof(typeof(header->v1), backing_file)) {
  263. cow_printf("read_cow_header - short header\n");
  264. goto out;
  265. }
  266. magic = header->v1.magic;
  267. if (magic == COW_MAGIC)
  268. version = header->v1.version;
  269. else if (magic == be32toh(COW_MAGIC))
  270. version = be32toh(header->v1.version);
  271. /* No error printed because the non-COW case comes through here */
  272. else goto out;
  273. *version_out = version;
  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 = be32toh(header->v2.mtime);
  294. *size_out = be64toh(header->v2.size);
  295. *sectorsize_out = be32toh(header->v2.sectorsize);
  296. *bitmap_offset_out = sizeof(header->v2);
  297. *align_out = *sectorsize_out;
  298. file = header->v2.backing_file;
  299. }
  300. /* This is very subtle - see above at union cow_header definition */
  301. else if (version == 3 && (*((int*)header->v3.backing_file) != 0)) {
  302. if (n < sizeof(header->v3)) {
  303. cow_printf("read_cow_header - failed to read V3 "
  304. "header\n");
  305. goto out;
  306. }
  307. *mtime_out = be32toh(header->v3.mtime);
  308. *size_out = be64toh(header->v3.size);
  309. *sectorsize_out = be32toh(header->v3.sectorsize);
  310. *align_out = be32toh(header->v3.alignment);
  311. if (*align_out == 0) {
  312. cow_printf("read_cow_header - invalid COW header, "
  313. "align == 0\n");
  314. }
  315. *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
  316. file = header->v3.backing_file;
  317. }
  318. else if (version == 3) {
  319. cow_printf("read_cow_header - broken V3 file with"
  320. " 64-bit layout - recovering content.\n");
  321. if (n < sizeof(header->v3_b)) {
  322. cow_printf("read_cow_header - failed to read V3 "
  323. "header\n");
  324. goto out;
  325. }
  326. /*
  327. * this was used until Dec2005 - 64bits are needed to represent
  328. * 2038+. I.e. we can safely do this truncating cast.
  329. *
  330. * Additionally, we must use be32toh() instead of be64toh(), since
  331. * the program used to use the former (tested - I got mtime
  332. * mismatch "0 vs whatever").
  333. *
  334. * Ever heard about bug-to-bug-compatibility ? ;-) */
  335. *mtime_out = (time32_t) be32toh(header->v3_b.mtime);
  336. *size_out = be64toh(header->v3_b.size);
  337. *sectorsize_out = be32toh(header->v3_b.sectorsize);
  338. *align_out = be32toh(header->v3_b.alignment);
  339. if (*align_out == 0) {
  340. cow_printf("read_cow_header - invalid COW header, "
  341. "align == 0\n");
  342. }
  343. *bitmap_offset_out = ROUND_UP(sizeof(header->v3_b), *align_out);
  344. file = header->v3_b.backing_file;
  345. }
  346. else {
  347. cow_printf("read_cow_header - invalid COW version\n");
  348. goto out;
  349. }
  350. err = -ENOMEM;
  351. *backing_file_out = cow_strdup(file);
  352. if (*backing_file_out == NULL) {
  353. cow_printf("read_cow_header - failed to allocate backing "
  354. "file\n");
  355. goto out;
  356. }
  357. err = 0;
  358. out:
  359. cow_free(header);
  360. return err;
  361. }
  362. int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
  363. int alignment, int *bitmap_offset_out,
  364. unsigned long *bitmap_len_out, int *data_offset_out)
  365. {
  366. unsigned long long size, offset;
  367. char zero = 0;
  368. int err;
  369. err = write_cow_header(cow_file, fd, backing_file, sectorsize,
  370. alignment, &size);
  371. if (err)
  372. goto out;
  373. *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
  374. cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
  375. bitmap_len_out, data_offset_out);
  376. offset = *data_offset_out + size - sizeof(zero);
  377. err = cow_seek_file(fd, offset);
  378. if (err < 0) {
  379. cow_printf("cow bitmap lseek failed : err = %d\n", -err);
  380. goto out;
  381. }
  382. /*
  383. * does not really matter how much we write it is just to set EOF
  384. * this also sets the entire COW bitmap
  385. * to zero without having to allocate it
  386. */
  387. err = cow_write_file(fd, &zero, sizeof(zero));
  388. if (err != sizeof(zero)) {
  389. cow_printf("Write of bitmap to new COW file '%s' failed, "
  390. "err = %d\n", cow_file, -err);
  391. if (err >= 0)
  392. err = -EINVAL;
  393. goto out;
  394. }
  395. return 0;
  396. out:
  397. return err;
  398. }