cow_user.c 9.7 KB

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