]>
Commit | Line | Data |
---|---|---|
568508e7 JH |
1 | /* |
2 | * Copyright (c) 2011, Google Inc. | |
3 | */ | |
b6fdc44c | 4 | #include "git-compat-util.h" |
568508e7 | 5 | #include "bulk-checkin.h" |
32a8f510 | 6 | #include "environment.h" |
f394e093 | 7 | #include "gettext.h" |
41771fa4 | 8 | #include "hex.h" |
c0f4752e | 9 | #include "lockfile.h" |
a49d2834 | 10 | #include "repository.h" |
568508e7 JH |
11 | #include "csum-file.h" |
12 | #include "pack.h" | |
58892711 | 13 | #include "strbuf.h" |
c0f4752e | 14 | #include "tmp-objdir.h" |
0abe14f6 | 15 | #include "packfile.h" |
87bed179 | 16 | #include "object-file.h" |
a034e910 | 17 | #include "object-store-ll.h" |
568508e7 | 18 | |
2c23d1b4 | 19 | static int odb_transaction_nesting; |
568508e7 | 20 | |
c0f4752e NS |
21 | static struct tmp_objdir *bulk_fsync_objdir; |
22 | ||
897c9e25 | 23 | static struct bulk_checkin_packfile { |
568508e7 | 24 | char *pack_tmp_name; |
98a3beab | 25 | struct hashfile *f; |
568508e7 JH |
26 | off_t offset; |
27 | struct pack_idx_option pack_idx_opts; | |
28 | ||
29 | struct pack_idx_entry **written; | |
30 | uint32_t alloc_written; | |
31 | uint32_t nr_written; | |
897c9e25 | 32 | } bulk_checkin_packfile; |
568508e7 | 33 | |
2ec02dd5 ÆAB |
34 | static void finish_tmp_packfile(struct strbuf *basename, |
35 | const char *pack_tmp_name, | |
36 | struct pack_idx_entry **written_list, | |
37 | uint32_t nr_written, | |
38 | struct pack_idx_option *pack_idx_opts, | |
39 | unsigned char hash[]) | |
40 | { | |
41 | char *idx_tmp_name = NULL; | |
42 | ||
43 | stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written, | |
1c573cdd | 44 | NULL, pack_idx_opts, hash, &idx_tmp_name); |
2ec02dd5 ÆAB |
45 | rename_tmp_packfile_idx(basename, &idx_tmp_name); |
46 | ||
47 | free(idx_tmp_name); | |
48 | } | |
49 | ||
897c9e25 | 50 | static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state) |
568508e7 | 51 | { |
ae44b5a4 | 52 | unsigned char hash[GIT_MAX_RAWSZ]; |
58892711 | 53 | struct strbuf packname = STRBUF_INIT; |
568508e7 JH |
54 | int i; |
55 | ||
56 | if (!state->f) | |
57 | return; | |
58 | ||
59 | if (state->nr_written == 0) { | |
60 | close(state->f->fd); | |
61 | unlink(state->pack_tmp_name); | |
62 | goto clear_exit; | |
63 | } else if (state->nr_written == 1) { | |
020406ea NS |
64 | finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, |
65 | CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); | |
568508e7 | 66 | } else { |
020406ea | 67 | int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0); |
ae44b5a4 TB |
68 | fixup_pack_header_footer(fd, hash, state->pack_tmp_name, |
69 | state->nr_written, hash, | |
568508e7 JH |
70 | state->offset); |
71 | close(fd); | |
72 | } | |
73 | ||
66833f0e ÆAB |
74 | strbuf_addf(&packname, "%s/pack/pack-%s.", get_object_directory(), |
75 | hash_to_hex(hash)); | |
58892711 | 76 | finish_tmp_packfile(&packname, state->pack_tmp_name, |
568508e7 | 77 | state->written, state->nr_written, |
ae44b5a4 | 78 | &state->pack_idx_opts, hash); |
568508e7 JH |
79 | for (i = 0; i < state->nr_written; i++) |
80 | free(state->written[i]); | |
81 | ||
82 | clear_exit: | |
83 | free(state->written); | |
84 | memset(state, 0, sizeof(*state)); | |
85 | ||
58892711 | 86 | strbuf_release(&packname); |
568508e7 | 87 | /* Make objects we just wrote available to ourselves */ |
a49d2834 | 88 | reprepare_packed_git(the_repository); |
568508e7 JH |
89 | } |
90 | ||
c0f4752e NS |
91 | /* |
92 | * Cleanup after batch-mode fsync_object_files. | |
93 | */ | |
94 | static void flush_batch_fsync(void) | |
95 | { | |
96 | struct strbuf temp_path = STRBUF_INIT; | |
97 | struct tempfile *temp; | |
98 | ||
99 | if (!bulk_fsync_objdir) | |
100 | return; | |
101 | ||
102 | /* | |
103 | * Issue a full hardware flush against a temporary file to ensure | |
104 | * that all objects are durable before any renames occur. The code in | |
105 | * fsync_loose_object_bulk_checkin has already issued a writeout | |
106 | * request, but it has not flushed any writeback cache in the storage | |
107 | * hardware or any filesystem logs. This fsync call acts as a barrier | |
108 | * to ensure that the data in each new object file is durable before | |
109 | * the final name is visible. | |
110 | */ | |
111 | strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", get_object_directory()); | |
112 | temp = xmks_tempfile(temp_path.buf); | |
113 | fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp)); | |
114 | delete_tempfile(&temp); | |
115 | strbuf_release(&temp_path); | |
116 | ||
117 | /* | |
118 | * Make the object files visible in the primary ODB after their data is | |
119 | * fully durable. | |
120 | */ | |
121 | tmp_objdir_migrate(bulk_fsync_objdir); | |
122 | bulk_fsync_objdir = NULL; | |
123 | } | |
124 | ||
897c9e25 | 125 | static int already_written(struct bulk_checkin_packfile *state, struct object_id *oid) |
568508e7 JH |
126 | { |
127 | int i; | |
128 | ||
129 | /* The object may already exist in the repository */ | |
bc726bd0 | 130 | if (repo_has_object_file(the_repository, oid)) |
568508e7 JH |
131 | return 1; |
132 | ||
133 | /* Might want to keep the list sorted */ | |
134 | for (i = 0; i < state->nr_written; i++) | |
4a7e27e9 | 135 | if (oideq(&state->written[i]->oid, oid)) |
568508e7 JH |
136 | return 1; |
137 | ||
138 | /* This is a new object we need to keep */ | |
139 | return 0; | |
140 | } | |
141 | ||
142 | /* | |
143 | * Read the contents from fd for size bytes, streaming it to the | |
144 | * packfile in state while updating the hash in ctx. Signal a failure | |
145 | * by returning a negative value when the resulting pack would exceed | |
146 | * the pack size limit and this is not the first object in the pack, | |
147 | * so that the caller can discard what we wrote from the current pack | |
148 | * by truncating it and opening a new one. The caller will then call | |
149 | * us again after rewinding the input fd. | |
150 | * | |
151 | * The already_hashed_to pointer is kept untouched by the caller to | |
152 | * make sure we do not hash the same byte when we are called | |
153 | * again. This way, the caller does not have to checkpoint its hash | |
154 | * status before calling us just in case we ask it to call us again | |
155 | * with a new pack. | |
156 | */ | |
9eb54197 EB |
157 | static int stream_blob_to_pack(struct bulk_checkin_packfile *state, |
158 | git_hash_ctx *ctx, off_t *already_hashed_to, | |
159 | int fd, size_t size, const char *path, | |
160 | unsigned flags) | |
568508e7 JH |
161 | { |
162 | git_zstream s; | |
f96178c5 | 163 | unsigned char ibuf[16384]; |
568508e7 JH |
164 | unsigned char obuf[16384]; |
165 | unsigned hdrlen; | |
166 | int status = Z_OK; | |
167 | int write_object = (flags & HASH_WRITE_OBJECT); | |
168 | off_t offset = 0; | |
169 | ||
568508e7 JH |
170 | git_deflate_init(&s, pack_compression_level); |
171 | ||
9eb54197 | 172 | hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), OBJ_BLOB, size); |
568508e7 JH |
173 | s.next_out = obuf + hdrlen; |
174 | s.avail_out = sizeof(obuf) - hdrlen; | |
175 | ||
176 | while (status != Z_STREAM_END) { | |
568508e7 JH |
177 | if (size && !s.avail_in) { |
178 | ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf); | |
41dcc4dc JK |
179 | ssize_t read_result = read_in_full(fd, ibuf, rsize); |
180 | if (read_result < 0) | |
181 | die_errno("failed to read from '%s'", path); | |
182 | if (read_result != rsize) | |
568508e7 JH |
183 | die("failed to read %d bytes from '%s'", |
184 | (int)rsize, path); | |
185 | offset += rsize; | |
186 | if (*already_hashed_to < offset) { | |
187 | size_t hsize = offset - *already_hashed_to; | |
188 | if (rsize < hsize) | |
189 | hsize = rsize; | |
190 | if (hsize) | |
f87e8137 | 191 | the_hash_algo->update_fn(ctx, ibuf, hsize); |
568508e7 JH |
192 | *already_hashed_to = offset; |
193 | } | |
194 | s.next_in = ibuf; | |
195 | s.avail_in = rsize; | |
196 | size -= rsize; | |
197 | } | |
198 | ||
199 | status = git_deflate(&s, size ? 0 : Z_FINISH); | |
200 | ||
201 | if (!s.avail_out || status == Z_STREAM_END) { | |
202 | if (write_object) { | |
203 | size_t written = s.next_out - obuf; | |
204 | ||
205 | /* would we bust the size limit? */ | |
206 | if (state->nr_written && | |
207 | pack_size_limit_cfg && | |
208 | pack_size_limit_cfg < state->offset + written) { | |
209 | git_deflate_abort(&s); | |
210 | return -1; | |
211 | } | |
212 | ||
98a3beab | 213 | hashwrite(state->f, obuf, written); |
568508e7 JH |
214 | state->offset += written; |
215 | } | |
216 | s.next_out = obuf; | |
217 | s.avail_out = sizeof(obuf); | |
218 | } | |
219 | ||
220 | switch (status) { | |
221 | case Z_OK: | |
222 | case Z_BUF_ERROR: | |
223 | case Z_STREAM_END: | |
224 | continue; | |
225 | default: | |
226 | die("unexpected deflate failure: %d", status); | |
227 | } | |
228 | } | |
229 | git_deflate_end(&s); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | /* Lazily create backing packfile for the state */ | |
897c9e25 | 234 | static void prepare_to_stream(struct bulk_checkin_packfile *state, |
568508e7 JH |
235 | unsigned flags) |
236 | { | |
237 | if (!(flags & HASH_WRITE_OBJECT) || state->f) | |
238 | return; | |
239 | ||
240 | state->f = create_tmp_packfile(&state->pack_tmp_name); | |
241 | reset_pack_idx_option(&state->pack_idx_opts); | |
242 | ||
243 | /* Pretend we are going to write only one object */ | |
244 | state->offset = write_pack_header(state->f, 1); | |
245 | if (!state->offset) | |
246 | die_errno("unable to write pack header"); | |
247 | } | |
248 | ||
9eb54197 EB |
249 | static int deflate_blob_to_pack(struct bulk_checkin_packfile *state, |
250 | struct object_id *result_oid, | |
251 | int fd, size_t size, | |
252 | const char *path, unsigned flags) | |
568508e7 JH |
253 | { |
254 | off_t seekback, already_hashed_to; | |
f87e8137 | 255 | git_hash_ctx ctx; |
568508e7 JH |
256 | unsigned char obuf[16384]; |
257 | unsigned header_len; | |
7140414d | 258 | struct hashfile_checkpoint checkpoint = {0}; |
568508e7 JH |
259 | struct pack_idx_entry *idx = NULL; |
260 | ||
261 | seekback = lseek(fd, 0, SEEK_CUR); | |
262 | if (seekback == (off_t) -1) | |
263 | return error("cannot find the current offset"); | |
264 | ||
b04cdea4 | 265 | header_len = format_object_header((char *)obuf, sizeof(obuf), |
9eb54197 | 266 | OBJ_BLOB, size); |
f87e8137 | 267 | the_hash_algo->init_fn(&ctx); |
268 | the_hash_algo->update_fn(&ctx, obuf, header_len); | |
e0b8c842 | 269 | the_hash_algo->init_fn(&checkpoint.ctx); |
568508e7 JH |
270 | |
271 | /* Note: idx is non-NULL when we are writing */ | |
272 | if ((flags & HASH_WRITE_OBJECT) != 0) | |
ca56dadb | 273 | CALLOC_ARRAY(idx, 1); |
568508e7 JH |
274 | |
275 | already_hashed_to = 0; | |
276 | ||
277 | while (1) { | |
278 | prepare_to_stream(state, flags); | |
279 | if (idx) { | |
98a3beab | 280 | hashfile_checkpoint(state->f, &checkpoint); |
568508e7 JH |
281 | idx->offset = state->offset; |
282 | crc32_begin(state->f); | |
283 | } | |
9eb54197 EB |
284 | if (!stream_blob_to_pack(state, &ctx, &already_hashed_to, |
285 | fd, size, path, flags)) | |
568508e7 JH |
286 | break; |
287 | /* | |
288 | * Writing this object to the current pack will make | |
289 | * it too big; we need to truncate it, start a new | |
290 | * pack, and write into it. | |
291 | */ | |
292 | if (!idx) | |
033abf97 | 293 | BUG("should not happen"); |
98a3beab | 294 | hashfile_truncate(state->f, &checkpoint); |
568508e7 | 295 | state->offset = checkpoint.offset; |
897c9e25 | 296 | flush_bulk_checkin_packfile(state); |
568508e7 JH |
297 | if (lseek(fd, seekback, SEEK_SET) == (off_t) -1) |
298 | return error("cannot seek back"); | |
299 | } | |
5951bf46 | 300 | the_hash_algo->final_oid_fn(result_oid, &ctx); |
568508e7 JH |
301 | if (!idx) |
302 | return 0; | |
303 | ||
304 | idx->crc32 = crc32_end(state->f); | |
68ee6dfc | 305 | if (already_written(state, result_oid)) { |
98a3beab | 306 | hashfile_truncate(state->f, &checkpoint); |
568508e7 JH |
307 | state->offset = checkpoint.offset; |
308 | free(idx); | |
309 | } else { | |
68ee6dfc | 310 | oidcpy(&idx->oid, result_oid); |
568508e7 JH |
311 | ALLOC_GROW(state->written, |
312 | state->nr_written + 1, | |
313 | state->alloc_written); | |
314 | state->written[state->nr_written++] = idx; | |
315 | } | |
316 | return 0; | |
317 | } | |
318 | ||
c0f4752e NS |
319 | void prepare_loose_object_bulk_checkin(void) |
320 | { | |
321 | /* | |
322 | * We lazily create the temporary object directory | |
323 | * the first time an object might be added, since | |
324 | * callers may not know whether any objects will be | |
325 | * added at the time they call begin_odb_transaction. | |
326 | */ | |
327 | if (!odb_transaction_nesting || bulk_fsync_objdir) | |
328 | return; | |
329 | ||
330 | bulk_fsync_objdir = tmp_objdir_create("bulk-fsync"); | |
331 | if (bulk_fsync_objdir) | |
332 | tmp_objdir_replace_primary_odb(bulk_fsync_objdir, 0); | |
333 | } | |
334 | ||
335 | void fsync_loose_object_bulk_checkin(int fd, const char *filename) | |
336 | { | |
337 | /* | |
338 | * If we have an active ODB transaction, we issue a call that | |
339 | * cleans the filesystem page cache but avoids a hardware flush | |
340 | * command. Later on we will issue a single hardware flush | |
341 | * before renaming the objects to their final names as part of | |
342 | * flush_batch_fsync. | |
343 | */ | |
344 | if (!bulk_fsync_objdir || | |
345 | git_fsync(fd, FSYNC_WRITEOUT_ONLY) < 0) { | |
ce50f1f3 JS |
346 | if (errno == ENOSYS) |
347 | warning(_("core.fsyncMethod = batch is unsupported on this platform")); | |
c0f4752e NS |
348 | fsync_or_die(fd, filename); |
349 | } | |
350 | } | |
351 | ||
9eb54197 EB |
352 | int index_blob_bulk_checkin(struct object_id *oid, |
353 | int fd, size_t size, | |
354 | const char *path, unsigned flags) | |
568508e7 | 355 | { |
9eb54197 EB |
356 | int status = deflate_blob_to_pack(&bulk_checkin_packfile, oid, fd, size, |
357 | path, flags); | |
2c23d1b4 | 358 | if (!odb_transaction_nesting) |
897c9e25 | 359 | flush_bulk_checkin_packfile(&bulk_checkin_packfile); |
568508e7 JH |
360 | return status; |
361 | } | |
362 | ||
2c23d1b4 | 363 | void begin_odb_transaction(void) |
568508e7 | 364 | { |
2c23d1b4 | 365 | odb_transaction_nesting += 1; |
568508e7 JH |
366 | } |
367 | ||
2c23d1b4 | 368 | void flush_odb_transaction(void) |
568508e7 | 369 | { |
c0f4752e | 370 | flush_batch_fsync(); |
897c9e25 | 371 | flush_bulk_checkin_packfile(&bulk_checkin_packfile); |
568508e7 | 372 | } |
2c23d1b4 NS |
373 | |
374 | void end_odb_transaction(void) | |
375 | { | |
376 | odb_transaction_nesting -= 1; | |
377 | if (odb_transaction_nesting < 0) | |
378 | BUG("Unbalanced ODB transaction nesting"); | |
379 | ||
380 | if (odb_transaction_nesting) | |
381 | return; | |
382 | ||
383 | flush_odb_transaction(); | |
568508e7 | 384 | } |