cow_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  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. /* unsigned time_t works until year 2106 */
  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. if (chdir(save_cwd)) {
  171. cow_printf("absolutize : Can't cd to '%s' - "
  172. "errno = %d\n", save_cwd, errno);
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. int write_cow_header(char *cow_file, int fd, char *backing_file,
  178. int sectorsize, int alignment, unsigned long long *size)
  179. {
  180. struct cow_header_v3 *header;
  181. long long modtime;
  182. int err;
  183. err = cow_seek_file(fd, 0);
  184. if (err < 0) {
  185. cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
  186. goto out;
  187. }
  188. err = -ENOMEM;
  189. header = cow_malloc(sizeof(*header));
  190. if (header == NULL) {
  191. cow_printf("write_cow_header - failed to allocate COW V3 "
  192. "header\n");
  193. goto out;
  194. }
  195. header->magic = htobe32(COW_MAGIC);
  196. header->version = htobe32(COW_VERSION);
  197. err = -EINVAL;
  198. if (strlen(backing_file) > sizeof(header->backing_file) - 1) {
  199. /* Below, %zd is for a size_t value */
  200. cow_printf("Backing file name \"%s\" is too long - names are "
  201. "limited to %zd characters\n", backing_file,
  202. sizeof(header->backing_file) - 1);
  203. goto out_free;
  204. }
  205. if (absolutize(header->backing_file, sizeof(header->backing_file),
  206. backing_file))
  207. goto out_free;
  208. err = os_file_modtime(header->backing_file, &modtime);
  209. if (err < 0) {
  210. cow_printf("write_cow_header - backing file '%s' mtime "
  211. "request failed, err = %d\n", header->backing_file,
  212. -err);
  213. goto out_free;
  214. }
  215. err = cow_file_size(header->backing_file, size);
  216. if (err < 0) {
  217. cow_printf("write_cow_header - couldn't get size of "
  218. "backing file '%s', err = %d\n",
  219. header->backing_file, -err);
  220. goto out_free;
  221. }
  222. header->mtime = htobe32(modtime);
  223. header->size = htobe64(*size);
  224. header->sectorsize = htobe32(sectorsize);
  225. header->alignment = htobe32(alignment);
  226. header->cow_format = COW_BITMAP;
  227. err = cow_write_file(fd, header, sizeof(*header));
  228. if (err != sizeof(*header)) {
  229. cow_printf("write_cow_header - write of header to "
  230. "new COW file '%s' failed, err = %d\n", cow_file,
  231. -err);
  232. goto out_free;
  233. }
  234. err = 0;
  235. out_free:
  236. cow_free(header);
  237. out:
  238. return err;
  239. }
  240. int file_reader(__u64 offset, char *buf, int len, void *arg)
  241. {
  242. int fd = *((int *) arg);
  243. return pread(fd, buf, len, offset);
  244. }
  245. /* XXX Need to sanity-check the values read from the header */
  246. int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
  247. __u32 *version_out, char **backing_file_out,
  248. long long *mtime_out, unsigned long long *size_out,
  249. int *sectorsize_out, __u32 *align_out,
  250. int *bitmap_offset_out)
  251. {
  252. union cow_header *header;
  253. char *file;
  254. int err, n;
  255. unsigned long version, magic;
  256. header = cow_malloc(sizeof(*header));
  257. if (header == NULL) {
  258. cow_printf("read_cow_header - Failed to allocate header\n");
  259. return -ENOMEM;
  260. }
  261. err = -EINVAL;
  262. n = (*reader)(0, (char *) header, sizeof(*header), arg);
  263. if (n < offsetof(typeof(header->v1), backing_file)) {
  264. cow_printf("read_cow_header - short header\n");
  265. goto out;
  266. }
  267. magic = header->v1.magic;
  268. if (magic == COW_MAGIC)
  269. version = header->v1.version;
  270. else if (magic == be32toh(COW_MAGIC))
  271. version = be32toh(header->v1.version);
  272. /* No error printed because the non-COW case comes through here */
  273. else goto out;
  274. *version_out = version;
  275. if (version == 1) {
  276. if (n < sizeof(header->v1)) {
  277. cow_printf("read_cow_header - failed to read V1 "
  278. "header\n");
  279. goto out;
  280. }
  281. *mtime_out = header->v1.mtime;
  282. *size_out = header->v1.size;
  283. *sectorsize_out = header->v1.sectorsize;
  284. *bitmap_offset_out = sizeof(header->v1);
  285. *align_out = *sectorsize_out;
  286. file = header->v1.backing_file;
  287. }
  288. else if (version == 2) {
  289. if (n < sizeof(header->v2)) {
  290. cow_printf("read_cow_header - failed to read V2 "
  291. "header\n");
  292. goto out;
  293. }
  294. *mtime_out = be32toh(header->v2.mtime);
  295. *size_out = be64toh(header->v2.size);
  296. *sectorsize_out = be32toh(header->v2.sectorsize);
  297. *bitmap_offset_out = sizeof(header->v2);
  298. *align_out = *sectorsize_out;
  299. file = header->v2.backing_file;
  300. }
  301. /* This is very subtle - see above at union cow_header definition */
  302. else if (version == 3 && (*((int*)header->v3.backing_file) != 0)) {
  303. if (n < sizeof(header->v3)) {
  304. cow_printf("read_cow_header - failed to read V3 "
  305. "header\n");
  306. goto out;
  307. }
  308. *mtime_out = be32toh(header->v3.mtime);
  309. *size_out = be64toh(header->v3.size);
  310. *sectorsize_out = be32toh(header->v3.sectorsize);
  311. *align_out = be32toh(header->v3.alignment);
  312. if (*align_out == 0) {
  313. cow_printf("read_cow_header - invalid COW header, "
  314. "align == 0\n");
  315. }
  316. *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
  317. file = header->v3.backing_file;
  318. }
  319. else if (version == 3) {
  320. cow_printf("read_cow_header - broken V3 file with"
  321. " 64-bit layout - recovering content.\n");
  322. if (n < sizeof(header->v3_b)) {
  323. cow_printf("read_cow_header - failed to read V3 "
  324. "header\n");
  325. goto out;
  326. }
  327. /*
  328. * this was used until Dec2005 - 64bits are needed to represent
  329. * 2106+. I.e. we can safely do this truncating cast.
  330. *
  331. * Additionally, we must use be32toh() instead of be64toh(), since
  332. * the program used to use the former (tested - I got mtime
  333. * mismatch "0 vs whatever").
  334. *
  335. * Ever heard about bug-to-bug-compatibility ? ;-) */
  336. *mtime_out = (time32_t) be32toh(header->v3_b.mtime);
  337. *size_out = be64toh(header->v3_b.size);
  338. *sectorsize_out = be32toh(header->v3_b.sectorsize);
  339. *align_out = be32toh(header->v3_b.alignment);
  340. if (*align_out == 0) {
  341. cow_printf("read_cow_header - invalid COW header, "
  342. "align == 0\n");
  343. }
  344. *bitmap_offset_out = ROUND_UP(sizeof(header->v3_b), *align_out);
  345. file = header->v3_b.backing_file;
  346. }
  347. else {
  348. cow_printf("read_cow_header - invalid COW version\n");
  349. goto out;
  350. }
  351. err = -ENOMEM;
  352. *backing_file_out = cow_strdup(file);
  353. if (*backing_file_out == NULL) {
  354. cow_printf("read_cow_header - failed to allocate backing "
  355. "file\n");
  356. goto out;
  357. }
  358. err = 0;
  359. out:
  360. cow_free(header);
  361. return err;
  362. }
  363. int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
  364. int alignment, int *bitmap_offset_out,
  365. unsigned long *bitmap_len_out, int *data_offset_out)
  366. {
  367. unsigned long long size, offset;
  368. char zero = 0;
  369. int err;
  370. err = write_cow_header(cow_file, fd, backing_file, sectorsize,
  371. alignment, &size);
  372. if (err)
  373. goto out;
  374. *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
  375. cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
  376. bitmap_len_out, data_offset_out);
  377. offset = *data_offset_out + size - sizeof(zero);
  378. err = cow_seek_file(fd, offset);
  379. if (err < 0) {
  380. cow_printf("cow bitmap lseek failed : err = %d\n", -err);
  381. goto out;
  382. }
  383. /*
  384. * does not really matter how much we write it is just to set EOF
  385. * this also sets the entire COW bitmap
  386. * to zero without having to allocate it
  387. */
  388. err = cow_write_file(fd, &zero, sizeof(zero));
  389. if (err != sizeof(zero)) {
  390. cow_printf("Write of bitmap to new COW file '%s' failed, "
  391. "err = %d\n", cow_file, -err);
  392. if (err >= 0)
  393. err = -EINVAL;
  394. goto out;
  395. }
  396. return 0;
  397. out:
  398. return err;
  399. }