2 * Copyright (c) 2011, Google Inc.
4 #include "git-compat-util.h"
6 #include "bulk-checkin.h"
7 #include "environment.h"
11 #include "repository.h"
12 #include "csum-file.h"
15 #include "string-list.h"
16 #include "tmp-objdir.h"
18 #include "object-file.h"
19 #include "object-store-ll.h"
22 static int odb_transaction_nesting
;
24 static struct tmp_objdir
*bulk_fsync_objdir
;
26 static struct bulk_checkin_packfile
{
30 struct pack_idx_option pack_idx_opts
;
32 struct pack_idx_entry
**written
;
33 uint32_t alloc_written
;
35 } bulk_checkin_packfile
;
37 static void finish_tmp_packfile(struct strbuf
*basename
,
38 const char *pack_tmp_name
,
39 struct pack_idx_entry
**written_list
,
41 struct pack_idx_option
*pack_idx_opts
,
44 char *idx_tmp_name
= NULL
;
46 stage_tmp_packfiles(basename
, pack_tmp_name
, written_list
, nr_written
,
47 NULL
, pack_idx_opts
, hash
, &idx_tmp_name
);
48 rename_tmp_packfile_idx(basename
, &idx_tmp_name
);
53 static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile
*state
)
55 unsigned char hash
[GIT_MAX_RAWSZ
];
56 struct strbuf packname
= STRBUF_INIT
;
62 if (state
->nr_written
== 0) {
64 unlink(state
->pack_tmp_name
);
66 } else if (state
->nr_written
== 1) {
67 finalize_hashfile(state
->f
, hash
, FSYNC_COMPONENT_PACK
,
68 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
70 int fd
= finalize_hashfile(state
->f
, hash
, FSYNC_COMPONENT_PACK
, 0);
71 fixup_pack_header_footer(fd
, hash
, state
->pack_tmp_name
,
72 state
->nr_written
, hash
,
77 strbuf_addf(&packname
, "%s/pack/pack-%s.", get_object_directory(),
79 finish_tmp_packfile(&packname
, state
->pack_tmp_name
,
80 state
->written
, state
->nr_written
,
81 &state
->pack_idx_opts
, hash
);
82 for (i
= 0; i
< state
->nr_written
; i
++)
83 free(state
->written
[i
]);
87 memset(state
, 0, sizeof(*state
));
89 strbuf_release(&packname
);
90 /* Make objects we just wrote available to ourselves */
91 reprepare_packed_git(the_repository
);
95 * Cleanup after batch-mode fsync_object_files.
97 static void flush_batch_fsync(void)
99 struct strbuf temp_path
= STRBUF_INIT
;
100 struct tempfile
*temp
;
102 if (!bulk_fsync_objdir
)
106 * Issue a full hardware flush against a temporary file to ensure
107 * that all objects are durable before any renames occur. The code in
108 * fsync_loose_object_bulk_checkin has already issued a writeout
109 * request, but it has not flushed any writeback cache in the storage
110 * hardware or any filesystem logs. This fsync call acts as a barrier
111 * to ensure that the data in each new object file is durable before
112 * the final name is visible.
114 strbuf_addf(&temp_path
, "%s/bulk_fsync_XXXXXX", get_object_directory());
115 temp
= xmks_tempfile(temp_path
.buf
);
116 fsync_or_die(get_tempfile_fd(temp
), get_tempfile_path(temp
));
117 delete_tempfile(&temp
);
118 strbuf_release(&temp_path
);
121 * Make the object files visible in the primary ODB after their data is
124 tmp_objdir_migrate(bulk_fsync_objdir
);
125 bulk_fsync_objdir
= NULL
;
128 static int already_written(struct bulk_checkin_packfile
*state
, struct object_id
*oid
)
132 /* The object may already exist in the repository */
133 if (repo_has_object_file(the_repository
, oid
))
136 /* Might want to keep the list sorted */
137 for (i
= 0; i
< state
->nr_written
; i
++)
138 if (oideq(&state
->written
[i
]->oid
, oid
))
141 /* This is a new object we need to keep */
146 * Read the contents from fd for size bytes, streaming it to the
147 * packfile in state while updating the hash in ctx. Signal a failure
148 * by returning a negative value when the resulting pack would exceed
149 * the pack size limit and this is not the first object in the pack,
150 * so that the caller can discard what we wrote from the current pack
151 * by truncating it and opening a new one. The caller will then call
152 * us again after rewinding the input fd.
154 * The already_hashed_to pointer is kept untouched by the caller to
155 * make sure we do not hash the same byte when we are called
156 * again. This way, the caller does not have to checkpoint its hash
157 * status before calling us just in case we ask it to call us again
160 static int stream_to_pack(struct bulk_checkin_packfile
*state
,
161 git_hash_ctx
*ctx
, off_t
*already_hashed_to
,
162 int fd
, size_t size
, enum object_type type
,
163 const char *path
, unsigned flags
)
166 unsigned char ibuf
[16384];
167 unsigned char obuf
[16384];
170 int write_object
= (flags
& HASH_WRITE_OBJECT
);
173 git_deflate_init(&s
, pack_compression_level
);
175 hdrlen
= encode_in_pack_object_header(obuf
, sizeof(obuf
), type
, size
);
176 s
.next_out
= obuf
+ hdrlen
;
177 s
.avail_out
= sizeof(obuf
) - hdrlen
;
179 while (status
!= Z_STREAM_END
) {
180 if (size
&& !s
.avail_in
) {
181 ssize_t rsize
= size
< sizeof(ibuf
) ? size
: sizeof(ibuf
);
182 ssize_t read_result
= read_in_full(fd
, ibuf
, rsize
);
184 die_errno("failed to read from '%s'", path
);
185 if (read_result
!= rsize
)
186 die("failed to read %d bytes from '%s'",
189 if (*already_hashed_to
< offset
) {
190 size_t hsize
= offset
- *already_hashed_to
;
194 the_hash_algo
->update_fn(ctx
, ibuf
, hsize
);
195 *already_hashed_to
= offset
;
202 status
= git_deflate(&s
, size
? 0 : Z_FINISH
);
204 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
206 size_t written
= s
.next_out
- obuf
;
208 /* would we bust the size limit? */
209 if (state
->nr_written
&&
210 pack_size_limit_cfg
&&
211 pack_size_limit_cfg
< state
->offset
+ written
) {
212 git_deflate_abort(&s
);
216 hashwrite(state
->f
, obuf
, written
);
217 state
->offset
+= written
;
220 s
.avail_out
= sizeof(obuf
);
229 die("unexpected deflate failure: %d", status
);
236 /* Lazily create backing packfile for the state */
237 static void prepare_to_stream(struct bulk_checkin_packfile
*state
,
240 if (!(flags
& HASH_WRITE_OBJECT
) || state
->f
)
243 state
->f
= create_tmp_packfile(&state
->pack_tmp_name
);
244 reset_pack_idx_option(&state
->pack_idx_opts
);
246 /* Pretend we are going to write only one object */
247 state
->offset
= write_pack_header(state
->f
, 1);
249 die_errno("unable to write pack header");
252 static int deflate_to_pack(struct bulk_checkin_packfile
*state
,
253 struct object_id
*result_oid
,
255 enum object_type type
, const char *path
,
258 off_t seekback
, already_hashed_to
;
260 unsigned char obuf
[16384];
262 struct hashfile_checkpoint checkpoint
= {0};
263 struct pack_idx_entry
*idx
= NULL
;
265 seekback
= lseek(fd
, 0, SEEK_CUR
);
266 if (seekback
== (off_t
) -1)
267 return error("cannot find the current offset");
269 header_len
= format_object_header((char *)obuf
, sizeof(obuf
),
271 the_hash_algo
->init_fn(&ctx
);
272 the_hash_algo
->update_fn(&ctx
, obuf
, header_len
);
274 /* Note: idx is non-NULL when we are writing */
275 if ((flags
& HASH_WRITE_OBJECT
) != 0)
276 CALLOC_ARRAY(idx
, 1);
278 already_hashed_to
= 0;
281 prepare_to_stream(state
, flags
);
283 hashfile_checkpoint(state
->f
, &checkpoint
);
284 idx
->offset
= state
->offset
;
285 crc32_begin(state
->f
);
287 if (!stream_to_pack(state
, &ctx
, &already_hashed_to
,
288 fd
, size
, type
, path
, flags
))
291 * Writing this object to the current pack will make
292 * it too big; we need to truncate it, start a new
293 * pack, and write into it.
296 BUG("should not happen");
297 hashfile_truncate(state
->f
, &checkpoint
);
298 state
->offset
= checkpoint
.offset
;
299 flush_bulk_checkin_packfile(state
);
300 if (lseek(fd
, seekback
, SEEK_SET
) == (off_t
) -1)
301 return error("cannot seek back");
303 the_hash_algo
->final_oid_fn(result_oid
, &ctx
);
307 idx
->crc32
= crc32_end(state
->f
);
308 if (already_written(state
, result_oid
)) {
309 hashfile_truncate(state
->f
, &checkpoint
);
310 state
->offset
= checkpoint
.offset
;
313 oidcpy(&idx
->oid
, result_oid
);
314 ALLOC_GROW(state
->written
,
315 state
->nr_written
+ 1,
316 state
->alloc_written
);
317 state
->written
[state
->nr_written
++] = idx
;
322 void prepare_loose_object_bulk_checkin(void)
325 * We lazily create the temporary object directory
326 * the first time an object might be added, since
327 * callers may not know whether any objects will be
328 * added at the time they call begin_odb_transaction.
330 if (!odb_transaction_nesting
|| bulk_fsync_objdir
)
333 bulk_fsync_objdir
= tmp_objdir_create("bulk-fsync");
334 if (bulk_fsync_objdir
)
335 tmp_objdir_replace_primary_odb(bulk_fsync_objdir
, 0);
338 void fsync_loose_object_bulk_checkin(int fd
, const char *filename
)
341 * If we have an active ODB transaction, we issue a call that
342 * cleans the filesystem page cache but avoids a hardware flush
343 * command. Later on we will issue a single hardware flush
344 * before renaming the objects to their final names as part of
347 if (!bulk_fsync_objdir
||
348 git_fsync(fd
, FSYNC_WRITEOUT_ONLY
) < 0) {
350 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
351 fsync_or_die(fd
, filename
);
355 int index_bulk_checkin(struct object_id
*oid
,
356 int fd
, size_t size
, enum object_type type
,
357 const char *path
, unsigned flags
)
359 int status
= deflate_to_pack(&bulk_checkin_packfile
, oid
, fd
, size
, type
,
361 if (!odb_transaction_nesting
)
362 flush_bulk_checkin_packfile(&bulk_checkin_packfile
);
366 void begin_odb_transaction(void)
368 odb_transaction_nesting
+= 1;
371 void flush_odb_transaction(void)
374 flush_bulk_checkin_packfile(&bulk_checkin_packfile
);
377 void end_odb_transaction(void)
379 odb_transaction_nesting
-= 1;
380 if (odb_transaction_nesting
< 0)
381 BUG("Unbalanced ODB transaction nesting");
383 if (odb_transaction_nesting
)
386 flush_odb_transaction();