cow_user.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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("write_cow_header - 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("write_cow_header - backing file '%s' mtime "
  176. "request failed, err = %d\n", header->backing_file,
  177. -err);
  178. goto out_free;
  179. }
  180. err = cow_file_size(header->backing_file, size);
  181. if(err < 0){
  182. cow_printf("write_cow_header - couldn't get size of "
  183. "backing file '%s', err = %d\n",
  184. header->backing_file, -err);
  185. goto out_free;
  186. }
  187. header->mtime = htonl(modtime);
  188. header->size = htonll(*size);
  189. header->sectorsize = htonl(sectorsize);
  190. header->alignment = htonl(alignment);
  191. header->cow_format = COW_BITMAP;
  192. err = cow_write_file(fd, header, sizeof(*header));
  193. if(err != sizeof(*header)){
  194. cow_printf("write_cow_header - write of header to "
  195. "new COW file '%s' failed, err = %d\n", cow_file,
  196. -err);
  197. goto out_free;
  198. }
  199. err = 0;
  200. out_free:
  201. cow_free(header);
  202. out:
  203. return(err);
  204. }
  205. int file_reader(__u64 offset, char *buf, int len, void *arg)
  206. {
  207. int fd = *((int *) arg);
  208. return(pread(fd, buf, len, offset));
  209. }
  210. /* XXX Need to sanity-check the values read from the header */
  211. int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
  212. __u32 *version_out, char **backing_file_out,
  213. time_t *mtime_out, unsigned long long *size_out,
  214. int *sectorsize_out, __u32 *align_out,
  215. int *bitmap_offset_out)
  216. {
  217. union cow_header *header;
  218. char *file;
  219. int err, n;
  220. unsigned long version, magic;
  221. header = cow_malloc(sizeof(*header));
  222. if(header == NULL){
  223. cow_printf("read_cow_header - Failed to allocate header\n");
  224. return(-ENOMEM);
  225. }
  226. err = -EINVAL;
  227. n = (*reader)(0, (char *) header, sizeof(*header), arg);
  228. if(n < offsetof(typeof(header->v1), backing_file)){
  229. cow_printf("read_cow_header - short header\n");
  230. goto out;
  231. }
  232. magic = header->v1.magic;
  233. if(magic == COW_MAGIC) {
  234. version = header->v1.version;
  235. }
  236. else if(magic == ntohl(COW_MAGIC)){
  237. version = ntohl(header->v1.version);
  238. }
  239. /* No error printed because the non-COW case comes through here */
  240. else goto out;
  241. *version_out = version;
  242. if(version == 1){
  243. if(n < sizeof(header->v1)){
  244. cow_printf("read_cow_header - failed to read V1 "
  245. "header\n");
  246. goto out;
  247. }
  248. *mtime_out = header->v1.mtime;
  249. *size_out = header->v1.size;
  250. *sectorsize_out = header->v1.sectorsize;
  251. *bitmap_offset_out = sizeof(header->v1);
  252. *align_out = *sectorsize_out;
  253. file = header->v1.backing_file;
  254. }
  255. else if(version == 2){
  256. if(n < sizeof(header->v2)){
  257. cow_printf("read_cow_header - failed to read V2 "
  258. "header\n");
  259. goto out;
  260. }
  261. *mtime_out = ntohl(header->v2.mtime);
  262. *size_out = ntohll(header->v2.size);
  263. *sectorsize_out = ntohl(header->v2.sectorsize);
  264. *bitmap_offset_out = sizeof(header->v2);
  265. *align_out = *sectorsize_out;
  266. file = header->v2.backing_file;
  267. }
  268. else if(version == 3){
  269. if(n < sizeof(header->v3)){
  270. cow_printf("read_cow_header - failed to read V3 "
  271. "header\n");
  272. goto out;
  273. }
  274. *mtime_out = ntohl(header->v3.mtime);
  275. *size_out = ntohll(header->v3.size);
  276. *sectorsize_out = ntohl(header->v3.sectorsize);
  277. *align_out = ntohl(header->v3.alignment);
  278. *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
  279. file = header->v3.backing_file;
  280. }
  281. else {
  282. cow_printf("read_cow_header - invalid COW version\n");
  283. goto out;
  284. }
  285. err = -ENOMEM;
  286. *backing_file_out = cow_strdup(file);
  287. if(*backing_file_out == NULL){
  288. cow_printf("read_cow_header - failed to allocate backing "
  289. "file\n");
  290. goto out;
  291. }
  292. err = 0;
  293. out:
  294. cow_free(header);
  295. return(err);
  296. }
  297. int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
  298. int alignment, int *bitmap_offset_out,
  299. unsigned long *bitmap_len_out, int *data_offset_out)
  300. {
  301. unsigned long long size, offset;
  302. char zero = 0;
  303. int err;
  304. err = write_cow_header(cow_file, fd, backing_file, sectorsize,
  305. alignment, &size);
  306. if(err)
  307. goto out;
  308. *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
  309. cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
  310. bitmap_len_out, data_offset_out);
  311. offset = *data_offset_out + size - sizeof(zero);
  312. err = cow_seek_file(fd, offset);
  313. if(err < 0){
  314. cow_printf("cow bitmap lseek failed : err = %d\n", -err);
  315. goto out;
  316. }
  317. /* does not really matter how much we write it is just to set EOF
  318. * this also sets the entire COW bitmap
  319. * to zero without having to allocate it
  320. */
  321. err = cow_write_file(fd, &zero, sizeof(zero));
  322. if(err != sizeof(zero)){
  323. cow_printf("Write of bitmap to new COW file '%s' failed, "
  324. "err = %d\n", cow_file, -err);
  325. if (err >= 0)
  326. err = -EINVAL;
  327. goto out;
  328. }
  329. return(0);
  330. out:
  331. return(err);
  332. }
  333. /*
  334. * ---------------------------------------------------------------------------
  335. * Local variables:
  336. * c-file-style: "linux"
  337. * End:
  338. */