cow_user.c 9.6 KB

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