cow_user.c 12 KB

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