5 char *odb_pack_name(struct strbuf
*buf
,
6 const unsigned char *sha1
,
10 strbuf_addf(buf
, "%s/pack/pack-%s.%s", get_object_directory(),
11 sha1_to_hex(sha1
), ext
);
15 char *sha1_pack_name(const unsigned char *sha1
)
17 static struct strbuf buf
= STRBUF_INIT
;
18 return odb_pack_name(&buf
, sha1
, "pack");
21 char *sha1_pack_index_name(const unsigned char *sha1
)
23 static struct strbuf buf
= STRBUF_INIT
;
24 return odb_pack_name(&buf
, sha1
, "idx");
27 static unsigned int pack_used_ctr
;
28 static unsigned int pack_mmap_calls
;
29 static unsigned int peak_pack_open_windows
;
30 static unsigned int pack_open_windows
;
31 unsigned int pack_open_fds
;
32 static unsigned int pack_max_fds
;
33 static size_t peak_pack_mapped
;
34 static size_t pack_mapped
;
35 struct packed_git
*packed_git
;
37 static struct mru packed_git_mru_storage
;
38 struct mru
*packed_git_mru
= &packed_git_mru_storage
;
40 #define SZ_FMT PRIuMAX
41 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
43 void pack_report(void)
46 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
47 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
48 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
49 sz_fmt(getpagesize()),
50 sz_fmt(packed_git_window_size
),
51 sz_fmt(packed_git_limit
));
53 "pack_report: pack_used_ctr = %10u\n"
54 "pack_report: pack_mmap_calls = %10u\n"
55 "pack_report: pack_open_windows = %10u / %10u\n"
56 "pack_report: pack_mapped = "
57 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
60 pack_open_windows
, peak_pack_open_windows
,
61 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
65 * Open and mmap the index file at path, perform a couple of
66 * consistency checks, then record its information to p. Return 0 on
69 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
72 struct pack_idx_header
*hdr
;
74 uint32_t version
, nr
, i
, *index
;
75 int fd
= git_open(path
);
84 idx_size
= xsize_t(st
.st_size
);
85 if (idx_size
< 4 * 256 + 20 + 20) {
87 return error("index file %s is too small", path
);
89 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
93 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
94 version
= ntohl(hdr
->idx_version
);
95 if (version
< 2 || version
> 2) {
96 munmap(idx_map
, idx_size
);
97 return error("index file %s is version %"PRIu32
98 " and is not supported by this binary"
99 " (try upgrading GIT to a newer version)",
108 index
+= 2; /* skip index header */
109 for (i
= 0; i
< 256; i
++) {
110 uint32_t n
= ntohl(index
[i
]);
112 munmap(idx_map
, idx_size
);
113 return error("non-monotonic index %s", path
);
121 * - 256 index entries 4 bytes each
122 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
123 * - 20-byte SHA1 of the packfile
124 * - 20-byte SHA1 file checksum
126 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
127 munmap(idx_map
, idx_size
);
128 return error("wrong index v1 file size in %s", path
);
130 } else if (version
== 2) {
133 * - 8 bytes of header
134 * - 256 index entries 4 bytes each
135 * - 20-byte sha1 entry * nr
136 * - 4-byte crc entry * nr
137 * - 4-byte offset entry * nr
138 * - 20-byte SHA1 of the packfile
139 * - 20-byte SHA1 file checksum
140 * And after the 4-byte offset table might be a
141 * variable sized table containing 8-byte entries
142 * for offsets larger than 2^31.
144 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
145 unsigned long max_size
= min_size
;
147 max_size
+= (nr
- 1)*8;
148 if (idx_size
< min_size
|| idx_size
> max_size
) {
149 munmap(idx_map
, idx_size
);
150 return error("wrong index v2 file size in %s", path
);
152 if (idx_size
!= min_size
&&
154 * make sure we can deal with large pack offsets.
155 * 31-bit signed offset won't be enough, neither
156 * 32-bit unsigned one will be.
158 (sizeof(off_t
) <= 4)) {
159 munmap(idx_map
, idx_size
);
160 return error("pack too large for current definition of off_t in %s", path
);
164 p
->index_version
= version
;
165 p
->index_data
= idx_map
;
166 p
->index_size
= idx_size
;
171 int open_pack_index(struct packed_git
*p
)
180 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
181 die("BUG: pack_name does not end in .pack");
182 idx_name
= xstrfmt("%.*s.idx", (int)len
, p
->pack_name
);
183 ret
= check_packed_git_idx(idx_name
, p
);
188 static struct packed_git
*alloc_packed_git(int extra
)
190 struct packed_git
*p
= xmalloc(st_add(sizeof(*p
), extra
));
191 memset(p
, 0, sizeof(*p
));
196 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
198 const char *path
= sha1_pack_name(sha1
);
199 size_t alloc
= st_add(strlen(path
), 1);
200 struct packed_git
*p
= alloc_packed_git(alloc
);
202 memcpy(p
->pack_name
, path
, alloc
); /* includes NUL */
203 hashcpy(p
->sha1
, sha1
);
204 if (check_packed_git_idx(idx_path
, p
)) {
212 static void scan_windows(struct packed_git
*p
,
213 struct packed_git
**lru_p
,
214 struct pack_window
**lru_w
,
215 struct pack_window
**lru_l
)
217 struct pack_window
*w
, *w_l
;
219 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
221 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
231 static int unuse_one_window(struct packed_git
*current
)
233 struct packed_git
*p
, *lru_p
= NULL
;
234 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
237 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
238 for (p
= packed_git
; p
; p
= p
->next
)
239 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
241 munmap(lru_w
->base
, lru_w
->len
);
242 pack_mapped
-= lru_w
->len
;
244 lru_l
->next
= lru_w
->next
;
246 lru_p
->windows
= lru_w
->next
;
254 void release_pack_memory(size_t need
)
256 size_t cur
= pack_mapped
;
257 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
))
261 void close_pack_windows(struct packed_git
*p
)
264 struct pack_window
*w
= p
->windows
;
267 die("pack '%s' still has open windows to it",
269 munmap(w
->base
, w
->len
);
270 pack_mapped
-= w
->len
;
272 p
->windows
= w
->next
;
277 static int close_pack_fd(struct packed_git
*p
)
289 void close_pack_index(struct packed_git
*p
)
292 munmap((void *)p
->index_data
, p
->index_size
);
293 p
->index_data
= NULL
;
297 static void close_pack(struct packed_git
*p
)
299 close_pack_windows(p
);
304 void close_all_packs(void)
306 struct packed_git
*p
;
308 for (p
= packed_git
; p
; p
= p
->next
)
310 die("BUG: want to close pack marked 'do-not-close'");
316 * The LRU pack is the one with the oldest MRU window, preferring packs
317 * with no used windows, or the oldest mtime if it has no windows allocated.
319 static void find_lru_pack(struct packed_git
*p
, struct packed_git
**lru_p
, struct pack_window
**mru_w
, int *accept_windows_inuse
)
321 struct pack_window
*w
, *this_mru_w
;
322 int has_windows_inuse
= 0;
325 * Reject this pack if it has windows and the previously selected
326 * one does not. If this pack does not have windows, reject
327 * it if the pack file is newer than the previously selected one.
329 if (*lru_p
&& !*mru_w
&& (p
->windows
|| p
->mtime
> (*lru_p
)->mtime
))
332 for (w
= this_mru_w
= p
->windows
; w
; w
= w
->next
) {
334 * Reject this pack if any of its windows are in use,
335 * but the previously selected pack did not have any
336 * inuse windows. Otherwise, record that this pack
337 * has windows in use.
340 if (*accept_windows_inuse
)
341 has_windows_inuse
= 1;
346 if (w
->last_used
> this_mru_w
->last_used
)
350 * Reject this pack if it has windows that have been
351 * used more recently than the previously selected pack.
352 * If the previously selected pack had windows inuse and
353 * we have not encountered a window in this pack that is
354 * inuse, skip this check since we prefer a pack with no
355 * inuse windows to one that has inuse windows.
357 if (*mru_w
&& *accept_windows_inuse
== has_windows_inuse
&&
358 this_mru_w
->last_used
> (*mru_w
)->last_used
)
367 *accept_windows_inuse
= has_windows_inuse
;
370 static int close_one_pack(void)
372 struct packed_git
*p
, *lru_p
= NULL
;
373 struct pack_window
*mru_w
= NULL
;
374 int accept_windows_inuse
= 1;
376 for (p
= packed_git
; p
; p
= p
->next
) {
377 if (p
->pack_fd
== -1)
379 find_lru_pack(p
, &lru_p
, &mru_w
, &accept_windows_inuse
);
383 return close_pack_fd(lru_p
);
388 static unsigned int get_max_fd_limit(void)
394 if (!getrlimit(RLIMIT_NOFILE
, &lim
))
401 long open_max
= sysconf(_SC_OPEN_MAX
);
405 * Otherwise, we got -1 for one of the two
408 * (1) sysconf() did not understand _SC_OPEN_MAX
409 * and signaled an error with -1; or
410 * (2) sysconf() said there is no limit.
412 * We _could_ clear errno before calling sysconf() to
413 * tell these two cases apart and return a huge number
414 * in the latter case to let the caller cap it to a
415 * value that is not so selfish, but letting the
416 * fallback OPEN_MAX codepath take care of these cases
425 return 1; /* see the caller ;-) */
430 * Do not call this directly as this leaks p->pack_fd on error return;
431 * call open_packed_git() instead.
433 static int open_packed_git_1(struct packed_git
*p
)
436 struct pack_header hdr
;
437 unsigned char sha1
[20];
438 unsigned char *idx_sha1
;
441 if (!p
->index_data
&& open_pack_index(p
))
442 return error("packfile %s index unavailable", p
->pack_name
);
445 unsigned int max_fds
= get_max_fd_limit();
447 /* Save 3 for stdin/stdout/stderr, 22 for work */
449 pack_max_fds
= max_fds
- 25;
454 while (pack_max_fds
<= pack_open_fds
&& close_one_pack())
457 p
->pack_fd
= git_open(p
->pack_name
);
458 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
462 /* If we created the struct before we had the pack we lack size. */
464 if (!S_ISREG(st
.st_mode
))
465 return error("packfile %s not a regular file", p
->pack_name
);
466 p
->pack_size
= st
.st_size
;
467 } else if (p
->pack_size
!= st
.st_size
)
468 return error("packfile %s size changed", p
->pack_name
);
470 /* We leave these file descriptors open with sliding mmap;
471 * there is no point keeping them open across exec(), though.
473 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
475 return error("cannot determine file descriptor flags");
476 fd_flag
|= FD_CLOEXEC
;
477 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
478 return error("cannot set FD_CLOEXEC");
480 /* Verify we recognize this pack file format. */
481 if (read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
482 return error("file %s is far too short to be a packfile", p
->pack_name
);
483 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
484 return error("file %s is not a GIT packfile", p
->pack_name
);
485 if (!pack_version_ok(hdr
.hdr_version
))
486 return error("packfile %s is version %"PRIu32
" and not"
487 " supported (try upgrading GIT to a newer version)",
488 p
->pack_name
, ntohl(hdr
.hdr_version
));
490 /* Verify the pack matches its index. */
491 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
492 return error("packfile %s claims to have %"PRIu32
" objects"
493 " while index indicates %"PRIu32
" objects",
494 p
->pack_name
, ntohl(hdr
.hdr_entries
),
496 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
497 return error("end of packfile %s is unavailable", p
->pack_name
);
498 if (read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
)) != sizeof(sha1
))
499 return error("packfile %s signature is unavailable", p
->pack_name
);
500 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
501 if (hashcmp(sha1
, idx_sha1
))
502 return error("packfile %s does not match index", p
->pack_name
);
506 int open_packed_git(struct packed_git
*p
)
508 if (!open_packed_git_1(p
))
514 static int in_window(struct pack_window
*win
, off_t offset
)
516 /* We must promise at least 20 bytes (one hash) after the
517 * offset is available from this window, otherwise the offset
518 * is not actually in this window and a different window (which
519 * has that one hash excess) must be used. This is to support
520 * the object header and delta base parsing routines below.
522 off_t win_off
= win
->offset
;
523 return win_off
<= offset
524 && (offset
+ 20) <= (win_off
+ win
->len
);
527 unsigned char *use_pack(struct packed_git
*p
,
528 struct pack_window
**w_cursor
,
532 struct pack_window
*win
= *w_cursor
;
534 /* Since packfiles end in a hash of their content and it's
535 * pointless to ask for an offset into the middle of that
536 * hash, and the in_window function above wouldn't match
537 * don't allow an offset too close to the end of the file.
539 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
540 die("packfile %s cannot be accessed", p
->pack_name
);
541 if (offset
> (p
->pack_size
- 20))
542 die("offset beyond end of packfile (truncated pack?)");
544 die(_("offset before end of packfile (broken .idx?)"));
546 if (!win
|| !in_window(win
, offset
)) {
549 for (win
= p
->windows
; win
; win
= win
->next
) {
550 if (in_window(win
, offset
))
554 size_t window_align
= packed_git_window_size
/ 2;
557 if (p
->pack_fd
== -1 && open_packed_git(p
))
558 die("packfile %s cannot be accessed", p
->pack_name
);
560 win
= xcalloc(1, sizeof(*win
));
561 win
->offset
= (offset
/ window_align
) * window_align
;
562 len
= p
->pack_size
- win
->offset
;
563 if (len
> packed_git_window_size
)
564 len
= packed_git_window_size
;
565 win
->len
= (size_t)len
;
566 pack_mapped
+= win
->len
;
567 while (packed_git_limit
< pack_mapped
568 && unuse_one_window(p
))
570 win
->base
= xmmap(NULL
, win
->len
,
571 PROT_READ
, MAP_PRIVATE
,
572 p
->pack_fd
, win
->offset
);
573 if (win
->base
== MAP_FAILED
)
574 die_errno("packfile %s cannot be mapped",
576 if (!win
->offset
&& win
->len
== p
->pack_size
581 if (pack_mapped
> peak_pack_mapped
)
582 peak_pack_mapped
= pack_mapped
;
583 if (pack_open_windows
> peak_pack_open_windows
)
584 peak_pack_open_windows
= pack_open_windows
;
585 win
->next
= p
->windows
;
589 if (win
!= *w_cursor
) {
590 win
->last_used
= pack_used_ctr
++;
594 offset
-= win
->offset
;
596 *left
= win
->len
- xsize_t(offset
);
597 return win
->base
+ offset
;
600 void unuse_pack(struct pack_window
**w_cursor
)
602 struct pack_window
*w
= *w_cursor
;