Deleted Added
full compact
revprops.c (302408) revprops.c (362181)
1/* revprops.c --- everything needed to handle revprops in FSX
2 *
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the

--- 12 unchanged lines hidden (view full) ---

21 */
22
23#include <assert.h>
24#include <apr_md5.h>
25
26#include "svn_pools.h"
27#include "svn_hash.h"
28#include "svn_dirent_uri.h"
1/* revprops.c --- everything needed to handle revprops in FSX
2 *
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the

--- 12 unchanged lines hidden (view full) ---

21 */
22
23#include <assert.h>
24#include <apr_md5.h>
25
26#include "svn_pools.h"
27#include "svn_hash.h"
28#include "svn_dirent_uri.h"
29#include "svn_sorts.h"
29
30#include "fs_x.h"
30
31#include "fs_x.h"
32#include "low_level.h"
31#include "revprops.h"
32#include "util.h"
33#include "transaction.h"
34
33#include "revprops.h"
34#include "util.h"
35#include "transaction.h"
36
37#include "private/svn_packed_data.h"
38#include "private/svn_sorts_private.h"
35#include "private/svn_subr_private.h"
36#include "private/svn_string_private.h"
37#include "../libsvn_fs/fs-loader.h"
38
39#include "svn_private_config.h"
40
41/* Give writing processes 10 seconds to replace an existing revprop
42 file with a new one. After that time, we assume that the writing
43 process got aborted and that we have re-read revprops. */
44#define REVPROP_CHANGE_TIMEOUT (10 * 1000000)
45
46/* In case of an inconsistent read, close the generation file, yield,
47 re-open and re-read. This is the number of times we try this before
48 giving up. */
49#define GENERATION_READ_RETRY_COUNT 100
50
39#include "private/svn_subr_private.h"
40#include "private/svn_string_private.h"
41#include "../libsvn_fs/fs-loader.h"
42
43#include "svn_private_config.h"
44
45/* Give writing processes 10 seconds to replace an existing revprop
46 file with a new one. After that time, we assume that the writing
47 process got aborted and that we have re-read revprops. */
48#define REVPROP_CHANGE_TIMEOUT (10 * 1000000)
49
50/* In case of an inconsistent read, close the generation file, yield,
51 re-open and re-read. This is the number of times we try this before
52 giving up. */
53#define GENERATION_READ_RETRY_COUNT 100
54
51/* Maximum size of the generation number file contents (including NUL). */
52#define CHECKSUMMED_NUMBER_BUFFER_LEN \
53 (SVN_INT64_BUFFER_SIZE + 3 + APR_MD5_DIGESTSIZE * 2)
54
55
55
56svn_error_t *
57svn_fs_x__upgrade_pack_revprops(svn_fs_t *fs,
58 svn_fs_upgrade_notify_t notify_func,
59 void *notify_baton,
60 svn_cancel_func_t cancel_func,
61 void *cancel_baton,
62 apr_pool_t *scratch_pool)
63{
64 svn_fs_x__data_t *ffd = fs->fsap_data;
65 const char *revprops_shard_path;
66 const char *revprops_pack_file_dir;
67 apr_int64_t shard;
68 apr_int64_t first_unpacked_shard
69 = ffd->min_unpacked_rev / ffd->max_files_per_dir;
70
71 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
72 const char *revsprops_dir = svn_dirent_join(fs->path, PATH_REVPROPS_DIR,
73 scratch_pool);
74 int compression_level = ffd->compress_packed_revprops
75 ? SVN_DELTA_COMPRESSION_LEVEL_DEFAULT
76 : SVN_DELTA_COMPRESSION_LEVEL_NONE;
77
78 /* first, pack all revprops shards to match the packed revision shards */
79 for (shard = 0; shard < first_unpacked_shard; ++shard)
80 {
81 svn_pool_clear(iterpool);
82
83 revprops_pack_file_dir = svn_dirent_join(revsprops_dir,
84 apr_psprintf(iterpool,
85 "%" APR_INT64_T_FMT PATH_EXT_PACKED_SHARD,
86 shard),
87 iterpool);
88 revprops_shard_path = svn_dirent_join(revsprops_dir,
89 apr_psprintf(iterpool, "%" APR_INT64_T_FMT, shard),
90 iterpool);
91
92 SVN_ERR(svn_fs_x__pack_revprops_shard(revprops_pack_file_dir,
93 revprops_shard_path,
94 shard, ffd->max_files_per_dir,
95 (int)(0.9 * ffd->revprop_pack_size),
96 compression_level,
97 cancel_func, cancel_baton, iterpool));
98 if (notify_func)
99 SVN_ERR(notify_func(notify_baton, shard,
100 svn_fs_upgrade_pack_revprops, iterpool));
101 }
102
103 svn_pool_destroy(iterpool);
104
105 return SVN_NO_ERROR;
106}
107
108svn_error_t *
109svn_fs_x__upgrade_cleanup_pack_revprops(svn_fs_t *fs,
110 svn_fs_upgrade_notify_t notify_func,
111 void *notify_baton,
112 svn_cancel_func_t cancel_func,
113 void *cancel_baton,
114 apr_pool_t *scratch_pool)
115{
116 svn_fs_x__data_t *ffd = fs->fsap_data;
117 const char *revprops_shard_path;
118 apr_int64_t shard;
119 apr_int64_t first_unpacked_shard
120 = ffd->min_unpacked_rev / ffd->max_files_per_dir;
121
122 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
123 const char *revsprops_dir = svn_dirent_join(fs->path, PATH_REVPROPS_DIR,
124 scratch_pool);
125
126 /* delete the non-packed revprops shards afterwards */
127 for (shard = 0; shard < first_unpacked_shard; ++shard)
128 {
129 svn_pool_clear(iterpool);
130
131 revprops_shard_path = svn_dirent_join(revsprops_dir,
132 apr_psprintf(iterpool, "%" APR_INT64_T_FMT, shard),
133 iterpool);
134 SVN_ERR(svn_fs_x__delete_revprops_shard(revprops_shard_path,
135 shard, ffd->max_files_per_dir,
136 cancel_func, cancel_baton,
137 iterpool));
138 if (notify_func)
139 SVN_ERR(notify_func(notify_baton, shard,
140 svn_fs_upgrade_cleanup_revprops, iterpool));
141 }
142
143 svn_pool_destroy(iterpool);
144
145 return SVN_NO_ERROR;
146}
147
148/* Revprop caching management.
149 *
150 * Mechanism:
151 * ----------
152 *
153 * Revprop caching needs to be activated and will be deactivated for the
154 * respective FS instance if the necessary infrastructure could not be
155 * initialized. As long as no revprops are being read or changed, revprop
156 * caching imposes no overhead.
157 *
158 * When activated, we cache revprops using (revision, generation) pairs
159 * as keys with the generation being incremented upon every revprop change.
160 * Since the cache is process-local, the generation needs to be tracked
161 * for at least as long as the process lives but may be reset afterwards.
56/* Revprop caching management.
57 *
58 * Mechanism:
59 * ----------
60 *
61 * Revprop caching needs to be activated and will be deactivated for the
62 * respective FS instance if the necessary infrastructure could not be
63 * initialized. As long as no revprops are being read or changed, revprop
64 * caching imposes no overhead.
65 *
66 * When activated, we cache revprops using (revision, generation) pairs
67 * as keys with the generation being incremented upon every revprop change.
68 * Since the cache is process-local, the generation needs to be tracked
69 * for at least as long as the process lives but may be reset afterwards.
70 * We track the revprop generation in a file that.
162 *
71 *
163 * We track the revprop generation in a persistent, unbuffered file that
164 * we may keep open for the lifetime of the svn_fs_t. It is the OS'
165 * responsibility to provide us with the latest contents upon read. To
166 * detect incomplete updates due to non-atomic reads, we put a MD5 checksum
167 * next to the actual generation number and verify that it matches.
168 *
169 * Since we cannot guarantee that the OS will provide us with up-to-date
170 * data buffers for open files, we re-open and re-read the file before
171 * modifying it. This will prevent lost updates.
172 *
173 * A race condition exists between switching to the modified revprop data
174 * and bumping the generation number. In particular, the process may crash
175 * just after switching to the new revprop data and before bumping the
176 * generation. To be able to detect this scenario, we bump the generation
177 * twice per revprop change: once immediately before (creating an odd number)
178 * and once after the atomic switch (even generation).
179 *
180 * A writer holding the write lock can immediately assume a crashed writer
181 * in case of an odd generation or they would not have been able to acquire
182 * the lock. A reader detecting an odd generation will use that number and
183 * be forced to re-read any revprop data - usually getting the new revprops
184 * already. If the generation file modification timestamp is too old, the
185 * reader will assume a crashed writer, acquire the write lock and bump
186 * the generation if it is still odd. So, for about REVPROP_CHANGE_TIMEOUT
187 * after the crash, reader caches may be stale.
188 */
189
72 * A race condition exists between switching to the modified revprop data
73 * and bumping the generation number. In particular, the process may crash
74 * just after switching to the new revprop data and before bumping the
75 * generation. To be able to detect this scenario, we bump the generation
76 * twice per revprop change: once immediately before (creating an odd number)
77 * and once after the atomic switch (even generation).
78 *
79 * A writer holding the write lock can immediately assume a crashed writer
80 * in case of an odd generation or they would not have been able to acquire
81 * the lock. A reader detecting an odd generation will use that number and
82 * be forced to re-read any revprop data - usually getting the new revprops
83 * already. If the generation file modification timestamp is too old, the
84 * reader will assume a crashed writer, acquire the write lock and bump
85 * the generation if it is still odd. So, for about REVPROP_CHANGE_TIMEOUT
86 * after the crash, reader caches may be stale.
87 */
88
190/* If the revprop generation file in FS is open, close it. This is a no-op
191 * if the file is not open.
192 */
193static svn_error_t *
194close_revprop_generation_file(svn_fs_t *fs,
195 apr_pool_t *scratch_pool)
196{
197 svn_fs_x__data_t *ffd = fs->fsap_data;
198 if (ffd->revprop_generation_file)
199 {
200 SVN_ERR(svn_io_file_close(ffd->revprop_generation_file, scratch_pool));
201 ffd->revprop_generation_file = NULL;
202 }
203
204 return SVN_NO_ERROR;
205}
206
207/* Make sure the revprop_generation member in FS is set. If READ_ONLY is
208 * set, open the file w/o write permission if the file is not open yet.
209 * The file is kept open if it has sufficient rights (or more) but will be
210 * closed and re-opened if it provided insufficient access rights.
211 *
212 * Call only for repos that support revprop caching.
213 */
214static svn_error_t *
215open_revprop_generation_file(svn_fs_t *fs,
216 svn_boolean_t read_only,
217 apr_pool_t *scratch_pool)
218{
219 svn_fs_x__data_t *ffd = fs->fsap_data;
220 apr_int32_t flags = read_only ? APR_READ : (APR_READ | APR_WRITE);
221
222 /* Close the current file handle if it has insufficient rights. */
223 if ( ffd->revprop_generation_file
224 && (apr_file_flags_get(ffd->revprop_generation_file) & flags) != flags)
225 SVN_ERR(close_revprop_generation_file(fs, scratch_pool));
226
227 /* If not open already, open with sufficient rights. */
228 if (ffd->revprop_generation_file == NULL)
229 {
230 const char *path = svn_fs_x__path_revprop_generation(fs, scratch_pool);
231 SVN_ERR(svn_io_file_open(&ffd->revprop_generation_file, path,
232 flags, APR_OS_DEFAULT, fs->pool));
233 }
234
235 return SVN_NO_ERROR;
236}
237
238/* Return the textual representation of NUMBER and its checksum in *BUFFER.
239 */
240static svn_error_t *
241checkedsummed_number(svn_stringbuf_t **buffer,
242 apr_int64_t number,
243 apr_pool_t *result_pool,
244 apr_pool_t *scratch_pool)
245{
246 svn_checksum_t *checksum;
247 const char *digest;
248
249 char str[SVN_INT64_BUFFER_SIZE];
250 apr_size_t len = svn__i64toa(str, number);
251 str[len] = 0;
252
253 SVN_ERR(svn_checksum(&checksum, svn_checksum_md5, str, len, scratch_pool));
254 digest = svn_checksum_to_cstring_display(checksum, scratch_pool);
255
256 *buffer = svn_stringbuf_createf(result_pool, "%s %s\n", digest, str);
257
258 return SVN_NO_ERROR;
259}
260
261/* Extract the generation number from the text BUFFER of LEN bytes and
262 * verify it against the checksum in the same BUFFER. If they match, return
263 * the generation in *NUMBER. Otherwise, return an error.
264 * BUFFER does not need to be NUL-terminated.
265 */
266static svn_error_t *
267verify_extract_number(apr_int64_t *number,
268 const char *buffer,
269 apr_size_t len,
270 apr_pool_t *scratch_pool)
271{
272 const char *digest_end = strchr(buffer, ' ');
273
274 /* Does the buffer even contain checksum _and_ number? */
275 if (digest_end != NULL)
276 {
277 svn_checksum_t *expected;
278 svn_checksum_t *actual;
279
280 SVN_ERR(svn_checksum_parse_hex(&expected, svn_checksum_md5, buffer,
281 scratch_pool));
282 SVN_ERR(svn_checksum(&actual, svn_checksum_md5, digest_end + 1,
283 (buffer + len) - (digest_end + 1), scratch_pool));
284
285 if (svn_checksum_match(expected, actual))
286 return svn_error_trace(svn_cstring_atoi64(number, digest_end + 1));
287 }
288
289 /* Incomplete buffer or not a match. */
290 return svn_error_create(SVN_ERR_FS_INVALID_GENERATION, NULL,
291 _("Invalid generation number data."));
292}
293
294/* Read revprop generation as stored on disk for repository FS. The result is
295 * returned in *CURRENT. Call only for repos that support revprop caching.
296 */
297static svn_error_t *
298read_revprop_generation_file(apr_int64_t *current,
299 svn_fs_t *fs,
300 apr_pool_t *scratch_pool)
301{
89/* Read revprop generation as stored on disk for repository FS. The result is
90 * returned in *CURRENT. Call only for repos that support revprop caching.
91 */
92static svn_error_t *
93read_revprop_generation_file(apr_int64_t *current,
94 svn_fs_t *fs,
95 apr_pool_t *scratch_pool)
96{
302 svn_fs_x__data_t *ffd = fs->fsap_data;
303 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
97 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
304 char buf[CHECKSUMMED_NUMBER_BUFFER_LEN];
305 apr_size_t len;
306 apr_off_t offset = 0;
307 int i;
308 svn_error_t *err = SVN_NO_ERROR;
98 int i;
99 svn_error_t *err = SVN_NO_ERROR;
100 const char *path = svn_fs_x__path_revprop_generation(fs, scratch_pool);
309
310 /* Retry in case of incomplete file buffer updates. */
311 for (i = 0; i < GENERATION_READ_RETRY_COUNT; ++i)
312 {
101
102 /* Retry in case of incomplete file buffer updates. */
103 for (i = 0; i < GENERATION_READ_RETRY_COUNT; ++i)
104 {
105 svn_stringbuf_t *buf;
106
313 svn_error_clear(err);
314 svn_pool_clear(iterpool);
315
107 svn_error_clear(err);
108 svn_pool_clear(iterpool);
109
316 /* If we can't even access the data, things are very wrong.
317 * Don't retry in that case.
318 */
319 SVN_ERR(open_revprop_generation_file(fs, TRUE, iterpool));
320 SVN_ERR(svn_io_file_seek(ffd->revprop_generation_file, APR_SET, &offset,
321 iterpool));
110 /* Read the generation file. */
111 err = svn_stringbuf_from_file2(&buf, path, iterpool);
322
112
323 len = sizeof(buf);
324 SVN_ERR(svn_io_read_length_line(ffd->revprop_generation_file, buf, &len,
325 iterpool));
326
327 /* Some data has been read. It will most likely be complete and
328 * consistent. Extract and verify anyway. */
329 err = verify_extract_number(current, buf, len, iterpool);
113 /* If we could read the file, it should be complete due to our atomic
114 * file replacement scheme. */
330 if (!err)
115 if (!err)
331 break;
116 {
117 svn_stringbuf_strip_whitespace(buf);
118 SVN_ERR(svn_cstring_atoi64(current, buf->data));
119 break;
120 }
332
121
333 /* Got unlucky and data was invalid. Retry. */
334 SVN_ERR(close_revprop_generation_file(fs, iterpool));
335
122 /* Got unlucky the file was not available. Retry. */
336#if APR_HAS_THREADS
337 apr_thread_yield();
338#else
339 apr_sleep(0);
340#endif
341 }
342
343 svn_pool_destroy(iterpool);

--- 7 unchanged lines hidden (view full) ---

351 */
352static svn_error_t *
353write_revprop_generation_file(svn_fs_t *fs,
354 apr_int64_t current,
355 apr_pool_t *scratch_pool)
356{
357 svn_fs_x__data_t *ffd = fs->fsap_data;
358 svn_stringbuf_t *buffer;
123#if APR_HAS_THREADS
124 apr_thread_yield();
125#else
126 apr_sleep(0);
127#endif
128 }
129
130 svn_pool_destroy(iterpool);

--- 7 unchanged lines hidden (view full) ---

138 */
139static svn_error_t *
140write_revprop_generation_file(svn_fs_t *fs,
141 apr_int64_t current,
142 apr_pool_t *scratch_pool)
143{
144 svn_fs_x__data_t *ffd = fs->fsap_data;
145 svn_stringbuf_t *buffer;
359 apr_off_t offset = 0;
146 const char *path = svn_fs_x__path_revprop_generation(fs, scratch_pool);
360
147
361 SVN_ERR(checkedsummed_number(&buffer, current, scratch_pool, scratch_pool));
148 /* Invalidate our cached revprop generation in case the file operations
149 * below fail. */
150 ffd->revprop_generation = -1;
362
151
363 SVN_ERR(open_revprop_generation_file(fs, FALSE, scratch_pool));
364 SVN_ERR(svn_io_file_seek(ffd->revprop_generation_file, APR_SET, &offset,
365 scratch_pool));
366 SVN_ERR(svn_io_file_write_full(ffd->revprop_generation_file, buffer->data,
367 buffer->len, NULL, scratch_pool));
368 SVN_ERR(svn_io_file_flush_to_disk(ffd->revprop_generation_file,
369 scratch_pool));
152 /* Write the new number. */
153 buffer = svn_stringbuf_createf(scratch_pool, "%" APR_INT64_T_FMT "\n",
154 current);
155 SVN_ERR(svn_io_write_atomic2(path, buffer->data, buffer->len,
156 path /* copy_perms */, FALSE,
157 scratch_pool));
370
158
159 /* Remember it to spare us the re-read. */
160 ffd->revprop_generation = current;
161
371 return SVN_NO_ERROR;
372}
373
374svn_error_t *
375svn_fs_x__reset_revprop_generation_file(svn_fs_t *fs,
376 apr_pool_t *scratch_pool)
377{
162 return SVN_NO_ERROR;
163}
164
165svn_error_t *
166svn_fs_x__reset_revprop_generation_file(svn_fs_t *fs,
167 apr_pool_t *scratch_pool)
168{
378 const char *path = svn_fs_x__path_revprop_generation(fs, scratch_pool);
379 svn_stringbuf_t *buffer;
169 /* Write the initial revprop generation file contents. */
170 SVN_ERR(write_revprop_generation_file(fs, 0, scratch_pool));
380
171
381 /* Unconditionally close the revprop generation file.
382 * Don't care about FS formats. This ensures consistent internal state. */
383 SVN_ERR(close_revprop_generation_file(fs, scratch_pool));
384
385 /* Unconditionally remove any old revprop generation file.
386 * Don't care about FS formats. This ensures consistent on-disk state
387 * for old format repositories. */
388 SVN_ERR(svn_io_remove_file2(path, TRUE, scratch_pool));
389
390 /* Write the initial revprop generation file contents, if supported by
391 * the current format. This ensures consistent on-disk state for new
392 * format repositories. */
393 SVN_ERR(checkedsummed_number(&buffer, 0, scratch_pool, scratch_pool));
394 SVN_ERR(svn_io_write_atomic(path, buffer->data, buffer->len, NULL,
395 scratch_pool));
396
397 /* ffd->revprop_generation_file will be re-opened on demand. */
398
399 return SVN_NO_ERROR;
400}
401
172 return SVN_NO_ERROR;
173}
174
402/* Create an error object with the given MESSAGE and pass it to the
403 WARNING member of FS. Clears UNDERLYING_ERR. */
404static void
405log_revprop_cache_init_warning(svn_fs_t *fs,
406 svn_error_t *underlying_err,
407 const char *message,
408 apr_pool_t *scratch_pool)
409{
410 svn_error_t *err = svn_error_createf(
411 SVN_ERR_FS_REVPROP_CACHE_INIT_FAILURE,
412 underlying_err, message,
413 svn_dirent_local_style(fs->path, scratch_pool));
414
415 if (fs->warning)
416 (fs->warning)(fs->warning_baton, err);
417
418 svn_error_clear(err);
419}
420
421/* Test whether revprop cache and necessary infrastructure are
422 available in FS. */
423static svn_boolean_t
424has_revprop_cache(svn_fs_t *fs,
425 apr_pool_t *scratch_pool)
426{
427 svn_fs_x__data_t *ffd = fs->fsap_data;
175/* Test whether revprop cache and necessary infrastructure are
176 available in FS. */
177static svn_boolean_t
178has_revprop_cache(svn_fs_t *fs,
179 apr_pool_t *scratch_pool)
180{
181 svn_fs_x__data_t *ffd = fs->fsap_data;
428 svn_error_t *error;
429
182
430 /* is the cache (still) enabled? */
431 if (ffd->revprop_cache == NULL)
432 return FALSE;
433
434 /* try initialize our file-backed infrastructure */
435 error = open_revprop_generation_file(fs, TRUE, scratch_pool);
436 if (error)
437 {
438 /* failure -> disable revprop cache for good */
439
440 ffd->revprop_cache = NULL;
441 log_revprop_cache_init_warning(fs, error,
442 "Revprop caching for '%s' disabled "
443 "because infrastructure for revprop "
444 "caching failed to initialize.",
445 scratch_pool);
446
447 return FALSE;
448 }
449
450 return TRUE;
183 /* is the cache enabled? */
184 return ffd->revprop_cache != NULL;
451}
452
453/* Baton structure for revprop_generation_fixup. */
454typedef struct revprop_generation_fixup_t
455{
456 /* revprop generation to read */
457 apr_int64_t *generation;
458

--- 11 unchanged lines hidden (view full) ---

470static svn_error_t *
471revprop_generation_fixup(void *void_baton,
472 apr_pool_t *scratch_pool)
473{
474 revprop_generation_upgrade_t *baton = void_baton;
475 svn_fs_x__data_t *ffd = baton->fs->fsap_data;
476 assert(ffd->has_write_lock);
477
185}
186
187/* Baton structure for revprop_generation_fixup. */
188typedef struct revprop_generation_fixup_t
189{
190 /* revprop generation to read */
191 apr_int64_t *generation;
192

--- 11 unchanged lines hidden (view full) ---

204static svn_error_t *
205revprop_generation_fixup(void *void_baton,
206 apr_pool_t *scratch_pool)
207{
208 revprop_generation_upgrade_t *baton = void_baton;
209 svn_fs_x__data_t *ffd = baton->fs->fsap_data;
210 assert(ffd->has_write_lock);
211
478 /* Make sure we don't operate on stale OS buffers. */
479 SVN_ERR(close_revprop_generation_file(baton->fs, scratch_pool));
480
481 /* Maybe, either the original revprop writer or some other reader has
482 already corrected / bumped the revprop generation. Thus, we need
483 to read it again. However, we will now be the only ones changing
484 the file contents due to us holding the write lock. */
485 SVN_ERR(read_revprop_generation_file(baton->generation, baton->fs,
486 scratch_pool));
487
488 /* Cause everyone to re-read revprops upon their next access, if the

--- 4 unchanged lines hidden (view full) ---

493 SVN_ERR(write_revprop_generation_file(baton->fs,
494 *baton->generation,
495 scratch_pool));
496 }
497
498 return SVN_NO_ERROR;
499}
500
212 /* Maybe, either the original revprop writer or some other reader has
213 already corrected / bumped the revprop generation. Thus, we need
214 to read it again. However, we will now be the only ones changing
215 the file contents due to us holding the write lock. */
216 SVN_ERR(read_revprop_generation_file(baton->generation, baton->fs,
217 scratch_pool));
218
219 /* Cause everyone to re-read revprops upon their next access, if the

--- 4 unchanged lines hidden (view full) ---

224 SVN_ERR(write_revprop_generation_file(baton->fs,
225 *baton->generation,
226 scratch_pool));
227 }
228
229 return SVN_NO_ERROR;
230}
231
501/* Read the current revprop generation and return it in *GENERATION.
502 Also, detect aborted / crashed writers and recover from that.
503 Use the access object in FS to set the shared mem values. */
232/* Read the current revprop generation of FS and its value in FS->FSAP_DATA.
233 Also, detect aborted / crashed writers and recover from that. */
504static svn_error_t *
234static svn_error_t *
505read_revprop_generation(apr_int64_t *generation,
506 svn_fs_t *fs,
235read_revprop_generation(svn_fs_t *fs,
507 apr_pool_t *scratch_pool)
508{
509 apr_int64_t current = 0;
510 svn_fs_x__data_t *ffd = fs->fsap_data;
511
512 /* read the current revprop generation number */
513 SVN_ERR(read_revprop_generation_file(&current, fs, scratch_pool));
514

--- 28 unchanged lines hidden (view full) ---

543 SVN_ERR(revprop_generation_fixup(&baton, scratch_pool));
544 else
545 SVN_ERR(svn_fs_x__with_write_lock(fs, revprop_generation_fixup,
546 &baton, scratch_pool));
547 }
548 }
549
550 /* return the value we just got */
236 apr_pool_t *scratch_pool)
237{
238 apr_int64_t current = 0;
239 svn_fs_x__data_t *ffd = fs->fsap_data;
240
241 /* read the current revprop generation number */
242 SVN_ERR(read_revprop_generation_file(&current, fs, scratch_pool));
243

--- 28 unchanged lines hidden (view full) ---

272 SVN_ERR(revprop_generation_fixup(&baton, scratch_pool));
273 else
274 SVN_ERR(svn_fs_x__with_write_lock(fs, revprop_generation_fixup,
275 &baton, scratch_pool));
276 }
277 }
278
279 /* return the value we just got */
551 *generation = current;
280 ffd->revprop_generation = current;
552 return SVN_NO_ERROR;
553}
554
281 return SVN_NO_ERROR;
282}
283
284void
285svn_fs_x__invalidate_revprop_generation(svn_fs_t *fs)
286{
287 svn_fs_x__data_t *ffd = fs->fsap_data;
288 ffd->revprop_generation = -1;
289}
290
291/* Return TRUE if the revprop generation value in FS->FSAP_DATA is valid. */
292static svn_boolean_t
293is_generation_valid(svn_fs_t *fs)
294{
295 svn_fs_x__data_t *ffd = fs->fsap_data;
296 return ffd->revprop_generation >= 0;
297}
298
555/* Set the revprop generation in FS to the next odd number to indicate
299/* Set the revprop generation in FS to the next odd number to indicate
556 that there is a revprop write process under way. Return that value
557 in *GENERATION. If the change times out, readers shall recover from
558 that state & re-read revprops.
300 that there is a revprop write process under way. Update the value
301 in FS->FSAP_DATA accordingly. If the change times out, readers shall
302 recover from that state & re-read revprops.
559 This is a no-op for repo formats that don't support revprop caching. */
560static svn_error_t *
303 This is a no-op for repo formats that don't support revprop caching. */
304static svn_error_t *
561begin_revprop_change(apr_int64_t *generation,
562 svn_fs_t *fs,
305begin_revprop_change(svn_fs_t *fs,
563 apr_pool_t *scratch_pool)
564{
565 svn_fs_x__data_t *ffd = fs->fsap_data;
566 SVN_ERR_ASSERT(ffd->has_write_lock);
567
306 apr_pool_t *scratch_pool)
307{
308 svn_fs_x__data_t *ffd = fs->fsap_data;
309 SVN_ERR_ASSERT(ffd->has_write_lock);
310
568 /* Close and re-open to make sure we read the latest data. */
569 SVN_ERR(close_revprop_generation_file(fs, scratch_pool));
570 SVN_ERR(open_revprop_generation_file(fs, FALSE, scratch_pool));
571
572 /* Set the revprop generation to an odd value to indicate
573 * that a write is in progress.
574 */
311 /* Set the revprop generation to an odd value to indicate
312 * that a write is in progress.
313 */
575 SVN_ERR(read_revprop_generation(generation, fs, scratch_pool));
576 ++*generation;
577 SVN_ERR(write_revprop_generation_file(fs, *generation, scratch_pool));
314 SVN_ERR(read_revprop_generation(fs, scratch_pool));
315 ++ffd->revprop_generation;
316 SVN_ERR_ASSERT(ffd->revprop_generation % 2);
317 SVN_ERR(write_revprop_generation_file(fs, ffd->revprop_generation,
318 scratch_pool));
578
579 return SVN_NO_ERROR;
580}
581
582/* Set the revprop generation in FS to the next even generation after
319
320 return SVN_NO_ERROR;
321}
322
323/* Set the revprop generation in FS to the next even generation after
583 the odd value in GENERATION to indicate that
324 the odd value in FS->FSAP_DATA to indicate that
584 a) readers shall re-read revprops, and
585 b) the write process has been completed (no recovery required).
586 This is a no-op for repo formats that don't support revprop caching. */
587static svn_error_t *
588end_revprop_change(svn_fs_t *fs,
325 a) readers shall re-read revprops, and
326 b) the write process has been completed (no recovery required).
327 This is a no-op for repo formats that don't support revprop caching. */
328static svn_error_t *
329end_revprop_change(svn_fs_t *fs,
589 apr_int64_t generation,
590 apr_pool_t *scratch_pool)
591{
592 svn_fs_x__data_t *ffd = fs->fsap_data;
593 SVN_ERR_ASSERT(ffd->has_write_lock);
330 apr_pool_t *scratch_pool)
331{
332 svn_fs_x__data_t *ffd = fs->fsap_data;
333 SVN_ERR_ASSERT(ffd->has_write_lock);
594 SVN_ERR_ASSERT(generation % 2);
334 SVN_ERR_ASSERT(ffd->revprop_generation % 2);
595
596 /* Set the revprop generation to an even value to indicate
597 * that a write has been completed. Since we held the write
598 * lock, nobody else could have updated the file contents.
599 */
335
336 /* Set the revprop generation to an even value to indicate
337 * that a write has been completed. Since we held the write
338 * lock, nobody else could have updated the file contents.
339 */
600 SVN_ERR(write_revprop_generation_file(fs, generation + 1, scratch_pool));
340 SVN_ERR(write_revprop_generation_file(fs, ffd->revprop_generation + 1,
341 scratch_pool));
601
602 return SVN_NO_ERROR;
603}
604
342
343 return SVN_NO_ERROR;
344}
345
346/* Represents an entry in the packed revprop manifest.
347 * There is one such entry per pack file. */
348typedef struct manifest_entry_t
349{
350 /* First revision in the pack file. */
351 svn_revnum_t start_rev;
352
353 /* Tag (a counter) appended to the file name to distinguish it from
354 outdated ones. */
355 apr_uint64_t tag;
356} manifest_entry_t;
357
605/* Container for all data required to access the packed revprop file
606 * for a given REVISION. This structure will be filled incrementally
607 * by read_pack_revprops() its sub-routines.
608 */
609typedef struct packed_revprops_t
610{
611 /* revision number to read (not necessarily the first in the pack) */
612 svn_revnum_t revision;
613
358/* Container for all data required to access the packed revprop file
359 * for a given REVISION. This structure will be filled incrementally
360 * by read_pack_revprops() its sub-routines.
361 */
362typedef struct packed_revprops_t
363{
364 /* revision number to read (not necessarily the first in the pack) */
365 svn_revnum_t revision;
366
614 /* current revprop generation. Used when populating the revprop cache */
615 apr_int64_t generation;
616
617 /* the actual revision properties */
618 apr_hash_t *properties;
619
620 /* their size when serialized to a single string
621 * (as found in PACKED_REVPROPS) */
622 apr_size_t serialized_size;
623
624
367 /* the actual revision properties */
368 apr_hash_t *properties;
369
370 /* their size when serialized to a single string
371 * (as found in PACKED_REVPROPS) */
372 apr_size_t serialized_size;
373
374
625 /* name of the pack file (without folder path) */
626 const char *filename;
375 /* manifest entry describing the pack file */
376 manifest_entry_t entry;
627
628 /* packed shard folder path */
629 const char *folder;
630
631 /* sum of values in SIZES */
632 apr_size_t total_size;
633
377
378 /* packed shard folder path */
379 const char *folder;
380
381 /* sum of values in SIZES */
382 apr_size_t total_size;
383
634 /* first revision in the pack (>= MANIFEST_START) */
635 svn_revnum_t start_revision;
384 /* Array of svn_string_t, containing the serialized revprops for
385 * REVISION * I. */
386 apr_array_header_t *revprops;
636
387
637 /* size of the revprops in PACKED_REVPROPS */
638 apr_array_header_t *sizes;
639
640 /* offset of the revprops in PACKED_REVPROPS */
641 apr_array_header_t *offsets;
642
643
644 /* concatenation of the serialized representation of all revprops
645 * in the pack, i.e. the pack content without header and compression */
646 svn_stringbuf_t *packed_revprops;
647
648 /* First revision covered by MANIFEST.
649 * Will equal the shard start revision or 1, for the 1st shard. */
650 svn_revnum_t manifest_start;
651
652 /* content of the manifest.
388 /* content of the manifest.
653 * Maps long(rev - MANIFEST_START) to const char* pack file name */
389 * Sorted list of manifest_entry_t. */
654 apr_array_header_t *manifest;
655} packed_revprops_t;
656
657/* Parse the serialized revprops in CONTENT and return them in *PROPERTIES.
658 * Also, put them into the revprop cache, if activated, for future use.
659 * Three more parameters are being used to update the revprop cache: FS is
390 apr_array_header_t *manifest;
391} packed_revprops_t;
392
393/* Parse the serialized revprops in CONTENT and return them in *PROPERTIES.
394 * Also, put them into the revprop cache, if activated, for future use.
395 * Three more parameters are being used to update the revprop cache: FS is
660 * our file system, the revprops belong to REVISION and the global revprop
661 * GENERATION is used as well.
396 * our file system, the revprops belong to REVISION.
662 *
663 * The returned hash will be allocated in RESULT_POOL, SCRATCH_POOL is
664 * being used for temporary allocations.
665 */
666static svn_error_t *
667parse_revprop(apr_hash_t **properties,
668 svn_fs_t *fs,
669 svn_revnum_t revision,
397 *
398 * The returned hash will be allocated in RESULT_POOL, SCRATCH_POOL is
399 * being used for temporary allocations.
400 */
401static svn_error_t *
402parse_revprop(apr_hash_t **properties,
403 svn_fs_t *fs,
404 svn_revnum_t revision,
670 apr_int64_t generation,
671 svn_string_t *content,
405 const svn_string_t *content,
672 apr_pool_t *result_pool,
673 apr_pool_t *scratch_pool)
674{
406 apr_pool_t *result_pool,
407 apr_pool_t *scratch_pool)
408{
675 svn_stream_t *stream = svn_stream_from_string(content, scratch_pool);
676 *properties = apr_hash_make(result_pool);
409 SVN_ERR_W(svn_fs_x__parse_properties(properties, content, result_pool),
410 apr_psprintf(scratch_pool, "Failed to parse revprops for r%ld.",
411 revision));
677
412
678 SVN_ERR(svn_hash_read2(*properties, stream, SVN_HASH_TERMINATOR,
679 result_pool));
680 if (has_revprop_cache(fs, scratch_pool))
681 {
682 svn_fs_x__data_t *ffd = fs->fsap_data;
683 svn_fs_x__pair_cache_key_t key = { 0 };
684
413 if (has_revprop_cache(fs, scratch_pool))
414 {
415 svn_fs_x__data_t *ffd = fs->fsap_data;
416 svn_fs_x__pair_cache_key_t key = { 0 };
417
418 SVN_ERR_ASSERT(is_generation_valid(fs));
419
685 key.revision = revision;
420 key.revision = revision;
686 key.second = generation;
421 key.second = ffd->revprop_generation;
687 SVN_ERR(svn_cache__set(ffd->revprop_cache, &key, *properties,
688 scratch_pool));
689 }
690
691 return SVN_NO_ERROR;
692}
693
422 SVN_ERR(svn_cache__set(ffd->revprop_cache, &key, *properties,
423 scratch_pool));
424 }
425
426 return SVN_NO_ERROR;
427}
428
429/* Verify the checksum attached to CONTENT and remove it.
430 * Use SCRATCH_POOL for temporary allocations.
431 */
432static svn_error_t *
433verify_checksum(svn_stringbuf_t *content,
434 apr_pool_t *scratch_pool)
435{
436 const apr_byte_t *digest;
437 svn_checksum_t *actual, *expected;
438
439 /* Verify the checksum. */
440 if (content->len < sizeof(apr_uint32_t))
441 return svn_error_create(SVN_ERR_CORRUPT_PACKED_DATA, NULL,
442 "File too short");
443
444 content->len -= sizeof(apr_uint32_t);
445 digest = (apr_byte_t *)content->data + content->len;
446
447 expected = svn_checksum__from_digest_fnv1a_32x4(digest, scratch_pool);
448 SVN_ERR(svn_checksum(&actual, svn_checksum_fnv1a_32x4, content->data,
449 content->len, scratch_pool));
450
451 if (!svn_checksum_match(actual, expected))
452 SVN_ERR(svn_checksum_mismatch_err(expected, actual, scratch_pool,
453 "checksum mismatch"));
454
455 return SVN_NO_ERROR;
456}
457
694/* Read the non-packed revprops for revision REV in FS, put them into the
458/* Read the non-packed revprops for revision REV in FS, put them into the
695 * revprop cache if activated and return them in *PROPERTIES. GENERATION
696 * is the current revprop generation.
459 * revprop cache if activated and return them in *PROPERTIES.
697 *
698 * If the data could not be read due to an otherwise recoverable error,
699 * leave *PROPERTIES unchanged. No error will be returned in that case.
700 *
701 * Allocate *PROPERTIES in RESULT_POOL and temporaries in SCRATCH_POOL.
702 */
703static svn_error_t *
704read_non_packed_revprop(apr_hash_t **properties,
705 svn_fs_t *fs,
706 svn_revnum_t rev,
460 *
461 * If the data could not be read due to an otherwise recoverable error,
462 * leave *PROPERTIES unchanged. No error will be returned in that case.
463 *
464 * Allocate *PROPERTIES in RESULT_POOL and temporaries in SCRATCH_POOL.
465 */
466static svn_error_t *
467read_non_packed_revprop(apr_hash_t **properties,
468 svn_fs_t *fs,
469 svn_revnum_t rev,
707 apr_int64_t generation,
708 apr_pool_t *result_pool,
709 apr_pool_t *scratch_pool)
710{
711 svn_stringbuf_t *content = NULL;
712 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
713 svn_boolean_t missing = FALSE;
714 int i;
715

--- 5 unchanged lines hidden (view full) ---

721 SVN_ERR(svn_fs_x__try_stringbuf_from_file(&content,
722 &missing,
723 svn_fs_x__path_revprops(fs, rev, iterpool),
724 i + 1 < SVN_FS_X__RECOVERABLE_RETRY_COUNT,
725 iterpool));
726 }
727
728 if (content)
470 apr_pool_t *result_pool,
471 apr_pool_t *scratch_pool)
472{
473 svn_stringbuf_t *content = NULL;
474 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
475 svn_boolean_t missing = FALSE;
476 int i;
477

--- 5 unchanged lines hidden (view full) ---

483 SVN_ERR(svn_fs_x__try_stringbuf_from_file(&content,
484 &missing,
485 svn_fs_x__path_revprops(fs, rev, iterpool),
486 i + 1 < SVN_FS_X__RECOVERABLE_RETRY_COUNT,
487 iterpool));
488 }
489
490 if (content)
729 SVN_ERR(parse_revprop(properties, fs, rev, generation,
730 svn_stringbuf__morph_into_string(content),
731 result_pool, iterpool));
491 {
492 svn_string_t *as_string;
732
493
494 /* Consistency check. */
495 SVN_ERR_W(verify_checksum(content, scratch_pool),
496 apr_psprintf(scratch_pool,
497 "Revprop file for r%ld is corrupt",
498 rev));
499
500 /* The contents string becomes part of the *PROPERTIES structure, i.e.
501 * we must make sure it lives at least as long as the latter. */
502 as_string = svn_string_create_from_buf(content, result_pool);
503 SVN_ERR(parse_revprop(properties, fs, rev, as_string,
504 result_pool, iterpool));
505 }
506
733 svn_pool_clear(iterpool);
734
735 return SVN_NO_ERROR;
736}
737
507 svn_pool_clear(iterpool);
508
509 return SVN_NO_ERROR;
510}
511
738/* Return the minimum length of any packed revprop file name in REVPROPS. */
739static apr_size_t
740get_min_filename_len(packed_revprops_t *revprops)
512/* Serialize ROOT into FILE and append a checksum to it.
513 * Use SCRATCH_POOL for temporary allocations.
514 */
515static svn_error_t *
516write_packed_data_checksummed(svn_packed__data_root_t *root,
517 apr_file_t *file,
518 apr_pool_t *scratch_pool)
741{
519{
742 char number_buffer[SVN_INT64_BUFFER_SIZE];
520 svn_checksum_t *checksum;
521 svn_stream_t *stream;
743
522
744 /* The revprop filenames have the format <REV>.<COUNT> - with <REV> being
745 * at least the first rev in the shard and <COUNT> having at least one
746 * digit. Thus, the minimum is 2 + #decimal places in the start rev.
747 */
748 return svn__i64toa(number_buffer, revprops->manifest_start) + 2;
523 stream = svn_stream_from_aprfile2(file, TRUE, scratch_pool);
524 stream = svn_checksum__wrap_write_stream(&checksum, stream,
525 svn_checksum_fnv1a_32x4,
526 scratch_pool);
527 SVN_ERR(svn_packed__data_write(stream, root, scratch_pool));
528 SVN_ERR(svn_stream_close(stream));
529
530 /* Append the checksum */
531 SVN_ERR(svn_io_file_write_full(file, checksum->digest,
532 svn_checksum_size(checksum), NULL,
533 scratch_pool));
534
535 return SVN_NO_ERROR;
749}
750
536}
537
538/* Serialize the packed revprops MANIFEST into FILE.
539 * Use SCRATCH_POOL for temporary allocations.
540 */
541static svn_error_t *
542write_manifest(apr_file_t *file,
543 const apr_array_header_t *manifest,
544 apr_pool_t *scratch_pool)
545{
546 int i;
547 svn_packed__data_root_t *root = svn_packed__data_create_root(scratch_pool);
548
549 /* one top-level stream per struct element */
550 svn_packed__int_stream_t *start_rev_stream
551 = svn_packed__create_int_stream(root, TRUE, FALSE);
552 svn_packed__int_stream_t *tag_stream
553 = svn_packed__create_int_stream(root, FALSE, FALSE);
554
555 /* serialize ENTRIES */
556 for (i = 0; i < manifest->nelts; ++i)
557 {
558 manifest_entry_t *entry = &APR_ARRAY_IDX(manifest, i, manifest_entry_t);
559 svn_packed__add_uint(start_rev_stream, entry->start_rev);
560 svn_packed__add_uint(tag_stream, entry->tag);
561 }
562
563 /* Write to file and calculate the checksum. */
564 SVN_ERR(write_packed_data_checksummed(root, file, scratch_pool));
565
566 return SVN_NO_ERROR;
567}
568
569/* Read *ROOT from CONTENT and verify its checksum. Allocate *ROOT in
570 * RESULT_POOL and use SCRATCH_POOL for temporary allocations.
571 */
572static svn_error_t *
573read_packed_data_checksummed(svn_packed__data_root_t **root,
574 svn_stringbuf_t *content,
575 apr_pool_t *result_pool,
576 apr_pool_t *scratch_pool)
577{
578 svn_stream_t *stream;
579
580 SVN_ERR(verify_checksum(content, scratch_pool));
581
582 stream = svn_stream_from_stringbuf(content, scratch_pool);
583 SVN_ERR(svn_packed__data_read(root, stream, result_pool, scratch_pool));
584
585 return SVN_NO_ERROR;
586}
587
588/* Read the packed revprops manifest from the CONTENT buffer and return it
589 * in *MANIFEST, allocated in RESULT_POOL. REVISION is the revision number
590 * to put into error messages. Use SCRATCH_POOL for temporary allocations.
591 */
592static svn_error_t *
593read_manifest(apr_array_header_t **manifest,
594 svn_stringbuf_t *content,
595 svn_revnum_t revision,
596 apr_pool_t *result_pool,
597 apr_pool_t *scratch_pool)
598{
599 apr_size_t i;
600 apr_size_t count;
601
602 svn_packed__data_root_t *root;
603 svn_packed__int_stream_t *start_rev_stream;
604 svn_packed__int_stream_t *tag_stream;
605
606 /* Verify the checksum and decode packed data. */
607 SVN_ERR_W(read_packed_data_checksummed(&root, content, result_pool,
608 scratch_pool),
609 apr_psprintf(scratch_pool,
610 "Revprop manifest file for r%ld is corrupt",
611 revision));
612
613 /* get streams */
614 start_rev_stream = svn_packed__first_int_stream(root);
615 tag_stream = svn_packed__next_int_stream(start_rev_stream);
616
617 /* read ids array */
618 count = svn_packed__int_count(start_rev_stream);
619 *manifest = apr_array_make(result_pool, (int)count,
620 sizeof(manifest_entry_t));
621
622 for (i = 0; i < count; ++i)
623 {
624 manifest_entry_t *entry = apr_array_push(*manifest);
625 entry->start_rev = (svn_revnum_t)svn_packed__get_int(start_rev_stream);
626 entry->tag = svn_packed__get_uint(tag_stream);
627 }
628
629 return SVN_NO_ERROR;
630}
631
632/* Implements the standard comparison function signature comparing the
633 * manifest_entry_t(lhs).start_rev to svn_revnum_t(rhs). */
634static int
635compare_entry_revision(const void *lhs,
636 const void *rhs)
637{
638 const manifest_entry_t *entry = lhs;
639 const svn_revnum_t *revision = rhs;
640
641 if (entry->start_rev < *revision)
642 return -1;
643
644 return entry->start_rev == *revision ? 0 : 1;
645}
646
647/* Return the index in MANIFEST that has the info for the pack file
648 * containing REVISION. */
649static int
650get_entry(apr_array_header_t *manifest,
651 svn_revnum_t revision)
652{
653 manifest_entry_t *entry;
654 int idx = svn_sort__bsearch_lower_bound(manifest, &revision,
655 compare_entry_revision);
656
657 assert(manifest->nelts > 0);
658 if (idx >= manifest->nelts)
659 return idx - 1;
660
661 entry = &APR_ARRAY_IDX(manifest, idx, manifest_entry_t);
662 if (entry->start_rev > revision && idx > 0)
663 return idx - 1;
664
665 return idx;
666}
667
668/* Return the full path of the revprop pack file given by ENTRY within
669 * REVPROPS. Allocate the result in RESULT_POOL. */
670static const char *
671get_revprop_pack_filepath(packed_revprops_t *revprops,
672 manifest_entry_t *entry,
673 apr_pool_t *result_pool)
674{
675 const char *filename = apr_psprintf(result_pool, "%ld.%" APR_UINT64_T_FMT,
676 entry->start_rev, entry->tag);
677 return svn_dirent_join(revprops->folder, filename, result_pool);
678}
679
751/* Given FS and REVPROPS->REVISION, fill the FILENAME, FOLDER and MANIFEST
752 * members. Use RESULT_POOL for allocating results and SCRATCH_POOL for
753 * temporaries.
754 */
755static svn_error_t *
756get_revprop_packname(svn_fs_t *fs,
757 packed_revprops_t *revprops,
758 apr_pool_t *result_pool,
759 apr_pool_t *scratch_pool)
760{
761 svn_fs_x__data_t *ffd = fs->fsap_data;
762 svn_stringbuf_t *content = NULL;
763 const char *manifest_file_path;
680/* Given FS and REVPROPS->REVISION, fill the FILENAME, FOLDER and MANIFEST
681 * members. Use RESULT_POOL for allocating results and SCRATCH_POOL for
682 * temporaries.
683 */
684static svn_error_t *
685get_revprop_packname(svn_fs_t *fs,
686 packed_revprops_t *revprops,
687 apr_pool_t *result_pool,
688 apr_pool_t *scratch_pool)
689{
690 svn_fs_x__data_t *ffd = fs->fsap_data;
691 svn_stringbuf_t *content = NULL;
692 const char *manifest_file_path;
764 int idx, rev_count;
765 char *buffer, *buffer_end;
766 const char **filenames, **filenames_end;
767 apr_size_t min_filename_len;
693 int idx;
694 svn_revnum_t previous_start_rev;
695 int i;
768
769 /* Determine the dimensions. Rev 0 is excluded from the first shard. */
696
697 /* Determine the dimensions. Rev 0 is excluded from the first shard. */
770 rev_count = ffd->max_files_per_dir;
771 revprops->manifest_start
698 int rev_count = ffd->max_files_per_dir;
699 svn_revnum_t manifest_start
772 = revprops->revision - (revprops->revision % rev_count);
700 = revprops->revision - (revprops->revision % rev_count);
773 if (revprops->manifest_start == 0)
701 if (manifest_start == 0)
774 {
702 {
775 ++revprops->manifest_start;
703 ++manifest_start;
776 --rev_count;
777 }
778
704 --rev_count;
705 }
706
779 revprops->manifest = apr_array_make(result_pool, rev_count,
780 sizeof(const char*));
781
782 /* No line in the file can be less than this number of chars long. */
783 min_filename_len = get_min_filename_len(revprops);
784
785 /* Read the content of the manifest file */
707 /* Read the content of the manifest file */
786 revprops->folder
787 = svn_fs_x__path_revprops_pack_shard(fs, revprops->revision, result_pool);
708 revprops->folder = svn_fs_x__path_pack_shard(fs, revprops->revision,
709 result_pool);
788 manifest_file_path = svn_dirent_join(revprops->folder, PATH_MANIFEST,
789 result_pool);
710 manifest_file_path = svn_dirent_join(revprops->folder, PATH_MANIFEST,
711 result_pool);
790
791 SVN_ERR(svn_fs_x__read_content(&content, manifest_file_path, result_pool));
712 SVN_ERR(svn_fs_x__read_content(&content, manifest_file_path, result_pool));
713 SVN_ERR(read_manifest(&revprops->manifest, content, revprops->revision,
714 result_pool, scratch_pool));
792
715
793 /* There CONTENT must have a certain minimal size and there no
794 * unterminated lines at the end of the file. Both guarantees also
795 * simplify the parser loop below.
796 */
797 if ( content->len < rev_count * (min_filename_len + 1)
798 || content->data[content->len - 1] != '\n')
799 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
800 _("Packed revprop manifest for r%ld not "
801 "properly terminated"), revprops->revision);
716 /* Verify the manifest data. */
717 if (revprops->manifest->nelts == 0)
718 return svn_error_createf(SVN_ERR_FS_CORRUPT_REVPROP_MANIFEST, NULL,
719 "Revprop manifest for r%ld is empty",
720 revprops->revision);
802
721
803 /* Chop (parse) the manifest CONTENT into filenames, one per line.
804 * We only have to replace all newlines with NUL and add all line
805 * starts to REVPROPS->MANIFEST.
806 *
807 * There must be exactly REV_COUNT lines and that is the number of
808 * lines we parse from BUFFER to FILENAMES. Set the end pointer for
809 * the source BUFFER such that BUFFER+MIN_FILENAME_LEN is still valid
810 * BUFFER_END is always valid due to CONTENT->LEN > MIN_FILENAME_LEN.
811 *
812 * Please note that this loop is performance critical for e.g. 'svn log'.
813 * It is run 1000x per revprop access, i.e. per revision and about
814 * 50 million times per sec (and CPU core).
815 */
816 for (filenames = (const char **)revprops->manifest->elts,
817 filenames_end = filenames + rev_count,
818 buffer = content->data,
819 buffer_end = buffer + content->len - min_filename_len;
820 (filenames < filenames_end) && (buffer < buffer_end);
821 ++filenames)
722 previous_start_rev = 0;
723 for (i = 0; i < revprops->manifest->nelts; ++i)
822 {
724 {
823 /* BUFFER always points to the start of the next line / filename. */
824 *filenames = buffer;
725 svn_revnum_t start_rev = APR_ARRAY_IDX(revprops->manifest, i,
726 manifest_entry_t).start_rev;
727 if ( start_rev < manifest_start
728 || start_rev >= manifest_start + rev_count)
729 return svn_error_createf(SVN_ERR_FS_CORRUPT_REVPROP_MANIFEST, NULL,
730 "Revprop manifest for r%ld contains "
731 "out-of-range revision r%ld",
732 revprops->revision, start_rev);
825
733
826 /* Find the next EOL. This is guaranteed to stay within the CONTENT
827 * buffer because we left enough room after BUFFER_END and we know
828 * we will always see a newline as the last non-NUL char. */
829 buffer += min_filename_len;
830 while (*buffer != '\n')
831 ++buffer;
734 if (start_rev < previous_start_rev)
735 return svn_error_createf(SVN_ERR_FS_CORRUPT_REVPROP_MANIFEST, NULL,
736 "Entries in revprop manifest for r%ld "
737 "are not ordered", revprops->revision);
832
738
833 /* Found EOL. Turn it into the filename terminator and move BUFFER
834 * to the start of the next line or CONTENT buffer end. */
835 *buffer = '\0';
836 ++buffer;
739 previous_start_rev = start_rev;
837 }
838
740 }
741
839 /* We must have reached the end of both buffers. */
840 if (buffer < content->data + content->len)
841 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
842 _("Packed revprop manifest for r%ld "
843 "has too many entries"), revprops->revision);
742 /* Now get the pack file description */
743 idx = get_entry(revprops->manifest, revprops->revision);
744 revprops->entry = APR_ARRAY_IDX(revprops->manifest, idx,
745 manifest_entry_t);
844
746
845 if (filenames < filenames_end)
846 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
847 _("Packed revprop manifest for r%ld "
848 "has too few entries"), revprops->revision);
849
850 /* The target array has now exactly one entry per revision. */
851 revprops->manifest->nelts = rev_count;
852
853 /* Now get the file name */
854 idx = (int)(revprops->revision - revprops->manifest_start);
855 revprops->filename = APR_ARRAY_IDX(revprops->manifest, idx, const char*);
856
857 return SVN_NO_ERROR;
858}
859
860/* Return TRUE, if revision R1 and R2 refer to the same shard in FS.
861 */
862static svn_boolean_t
863same_shard(svn_fs_t *fs,
864 svn_revnum_t r1,
865 svn_revnum_t r2)
866{
867 svn_fs_x__data_t *ffd = fs->fsap_data;
868 return (r1 / ffd->max_files_per_dir) == (r2 / ffd->max_files_per_dir);
869}
870
747 return SVN_NO_ERROR;
748}
749
750/* Return TRUE, if revision R1 and R2 refer to the same shard in FS.
751 */
752static svn_boolean_t
753same_shard(svn_fs_t *fs,
754 svn_revnum_t r1,
755 svn_revnum_t r2)
756{
757 svn_fs_x__data_t *ffd = fs->fsap_data;
758 return (r1 / ffd->max_files_per_dir) == (r2 / ffd->max_files_per_dir);
759}
760
871/* Given FS and the full packed file content in REVPROPS->PACKED_REVPROPS,
872 * fill the START_REVISION member, and make PACKED_REVPROPS point to the
873 * first serialized revprop. If READ_ALL is set, initialize the SIZES
874 * and OFFSETS members as well.
761/* Given FS and the full packed file content in CONTENT and make
762 * PACKED_REVPROPS point to the first serialized revprop. If READ_ALL
763 * is set, initialize the SIZES and OFFSETS members as well.
875 *
876 * Parse the revprops for REVPROPS->REVISION and set the PROPERTIES as
877 * well as the SERIALIZED_SIZE member. If revprop caching has been
878 * enabled, parse all revprops in the pack and cache them.
879 */
880static svn_error_t *
881parse_packed_revprops(svn_fs_t *fs,
882 packed_revprops_t *revprops,
764 *
765 * Parse the revprops for REVPROPS->REVISION and set the PROPERTIES as
766 * well as the SERIALIZED_SIZE member. If revprop caching has been
767 * enabled, parse all revprops in the pack and cache them.
768 */
769static svn_error_t *
770parse_packed_revprops(svn_fs_t *fs,
771 packed_revprops_t *revprops,
772 svn_stringbuf_t *content,
883 svn_boolean_t read_all,
884 apr_pool_t *result_pool,
885 apr_pool_t *scratch_pool)
886{
773 svn_boolean_t read_all,
774 apr_pool_t *result_pool,
775 apr_pool_t *scratch_pool)
776{
887 svn_stream_t *stream;
888 apr_int64_t first_rev, count, i;
889 apr_off_t offset;
890 const char *header_end;
777 apr_size_t count, i;
891 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
892 svn_boolean_t cache_all = has_revprop_cache(fs, scratch_pool);
778 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
779 svn_boolean_t cache_all = has_revprop_cache(fs, scratch_pool);
780 svn_packed__data_root_t *root;
781 svn_packed__byte_stream_t *revprops_stream;
782 svn_revnum_t first_rev = revprops->entry.start_rev;
893
783
894 /* decompress (even if the data is only "stored", there is still a
895 * length header to remove) */
896 svn_stringbuf_t *compressed = revprops->packed_revprops;
897 svn_stringbuf_t *uncompressed = svn_stringbuf_create_empty(result_pool);
898 SVN_ERR(svn__decompress(compressed, uncompressed, APR_SIZE_MAX));
784 /* Verify the checksum and decode packed data. */
785 SVN_ERR_W(read_packed_data_checksummed(&root, content, result_pool,
786 scratch_pool),
787 apr_psprintf(scratch_pool,
788 "Revprop pack file for r%ld is corrupt",
789 first_rev));
899
790
900 /* read first revision number and number of revisions in the pack */
901 stream = svn_stream_from_stringbuf(uncompressed, scratch_pool);
902 SVN_ERR(svn_fs_x__read_number_from_stream(&first_rev, NULL, stream,
903 iterpool));
904 SVN_ERR(svn_fs_x__read_number_from_stream(&count, NULL, stream, iterpool));
791 /* get streams */
792 revprops_stream = svn_packed__first_byte_stream(root);
793 count = svn_packed__byte_block_count(revprops_stream);
905
906 /* Check revision range for validity. */
794
795 /* Check revision range for validity. */
907 if ( !same_shard(fs, revprops->revision, first_rev)
908 || !same_shard(fs, revprops->revision, first_rev + count - 1)
909 || count < 1)
796 if (!same_shard(fs, first_rev, first_rev + count - 1) || count < 1)
910 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
911 _("Revprop pack for revision r%ld"
912 " contains revprops for r%ld .. r%ld"),
913 revprops->revision,
914 (svn_revnum_t)first_rev,
915 (svn_revnum_t)(first_rev + count -1));
916
917 /* Since start & end are in the same shard, it is enough to just test
918 * the FIRST_REV for being actually packed. That will also cover the
919 * special case of rev 0 never being packed. */
920 if (!svn_fs_x__is_packed_revprop(fs, first_rev))
921 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
922 _("Revprop pack for revision r%ld"
923 " starts at non-packed revisions r%ld"),
924 revprops->revision, (svn_revnum_t)first_rev);
925
797 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
798 _("Revprop pack for revision r%ld"
799 " contains revprops for r%ld .. r%ld"),
800 revprops->revision,
801 (svn_revnum_t)first_rev,
802 (svn_revnum_t)(first_rev + count -1));
803
804 /* Since start & end are in the same shard, it is enough to just test
805 * the FIRST_REV for being actually packed. That will also cover the
806 * special case of rev 0 never being packed. */
807 if (!svn_fs_x__is_packed_revprop(fs, first_rev))
808 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
809 _("Revprop pack for revision r%ld"
810 " starts at non-packed revisions r%ld"),
811 revprops->revision, (svn_revnum_t)first_rev);
812
926 /* make PACKED_REVPROPS point to the first char after the header.
927 * This is where the serialized revprops are. */
928 header_end = strstr(uncompressed->data, "\n\n");
929 if (header_end == NULL)
930 return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
931 _("Header end not found"));
932
933 offset = header_end - uncompressed->data + 2;
934
935 revprops->packed_revprops = svn_stringbuf_create_empty(result_pool);
936 revprops->packed_revprops->data = uncompressed->data + offset;
937 revprops->packed_revprops->len = (apr_size_t)(uncompressed->len - offset);
938 revprops->packed_revprops->blocksize = (apr_size_t)(uncompressed->blocksize - offset);
939
940 /* STREAM still points to the first entry in the sizes list. */
941 revprops->start_revision = (svn_revnum_t)first_rev;
942 if (read_all)
813 /* Request all data (just references to data already expanded in ROOT) */
814 revprops->revprops = apr_array_make(result_pool, (int)count,
815 sizeof(svn_string_t));
816 for (i = 0, revprops->total_size = 0; i < count; ++i)
943 {
817 {
944 /* Init / construct REVPROPS members. */
945 revprops->sizes = apr_array_make(result_pool, (int)count,
946 sizeof(offset));
947 revprops->offsets = apr_array_make(result_pool, (int)count,
948 sizeof(offset));
818 svn_string_t *props = apr_array_push(revprops->revprops);
819 props->data = svn_packed__get_bytes(revprops_stream, &props->len);
820
821 revprops->total_size += props->len;
949 }
950
822 }
823
951 /* Now parse, revision by revision, the size and content of each
952 * revisions' revprops. */
953 for (i = 0, offset = 0, revprops->total_size = 0; i < count; ++i)
824 /* Now parse the serialized revprops. */
825 for (i = 0; i < count; ++i)
954 {
826 {
955 apr_int64_t size;
956 svn_string_t serialized;
957 svn_revnum_t revision = (svn_revnum_t)(first_rev + i);
827 const svn_string_t *serialized;
828 svn_revnum_t revision;
829
958 svn_pool_clear(iterpool);
959
830 svn_pool_clear(iterpool);
831
960 /* read & check the serialized size */
961 SVN_ERR(svn_fs_x__read_number_from_stream(&size, NULL, stream,
962 iterpool));
963 if (size + offset > (apr_int64_t)revprops->packed_revprops->len)
964 return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
965 _("Packed revprop size exceeds pack file size"));
832 serialized = &APR_ARRAY_IDX(revprops->revprops, (int)i, svn_string_t);
833 revision = first_rev + (long)i;
966
967 /* Parse this revprops list, if necessary */
834
835 /* Parse this revprops list, if necessary */
968 serialized.data = revprops->packed_revprops->data + offset;
969 serialized.len = (apr_size_t)size;
970
971 if (revision == revprops->revision)
972 {
973 /* Parse (and possibly cache) the one revprop list we care about. */
974 SVN_ERR(parse_revprop(&revprops->properties, fs, revision,
836 if (revision == revprops->revision)
837 {
838 /* Parse (and possibly cache) the one revprop list we care about. */
839 SVN_ERR(parse_revprop(&revprops->properties, fs, revision,
975 revprops->generation, &serialized,
976 result_pool, iterpool));
977 revprops->serialized_size = serialized.len;
840 serialized, result_pool, iterpool));
841 revprops->serialized_size = serialized->len;
978
979 /* If we only wanted the revprops for REVISION then we are done. */
980 if (!read_all && !cache_all)
981 break;
982 }
983 else if (cache_all)
984 {
985 /* Parse and cache all other revprop lists. */
986 apr_hash_t *properties;
842
843 /* If we only wanted the revprops for REVISION then we are done. */
844 if (!read_all && !cache_all)
845 break;
846 }
847 else if (cache_all)
848 {
849 /* Parse and cache all other revprop lists. */
850 apr_hash_t *properties;
987 SVN_ERR(parse_revprop(&properties, fs, revision,
988 revprops->generation, &serialized,
851 SVN_ERR(parse_revprop(&properties, fs, revision, serialized,
989 iterpool, iterpool));
990 }
852 iterpool, iterpool));
853 }
991
992 if (read_all)
993 {
994 /* fill REVPROPS data structures */
995 APR_ARRAY_PUSH(revprops->sizes, apr_off_t) = serialized.len;
996 APR_ARRAY_PUSH(revprops->offsets, apr_off_t) = offset;
997 }
998 revprops->total_size += serialized.len;
999
1000 offset += serialized.len;
1001 }
1002
854 }
855
856 svn_pool_destroy(iterpool);
857
1003 return SVN_NO_ERROR;
1004}
1005
1006/* In filesystem FS, read the packed revprops for revision REV into
858 return SVN_NO_ERROR;
859}
860
861/* In filesystem FS, read the packed revprops for revision REV into
1007 * *REVPROPS. Use GENERATION to populate the revprop cache, if enabled.
1008 * If you want to modify revprop contents / update REVPROPS, READ_ALL
1009 * must be set. Otherwise, only the properties of REV are being provided.
862 * *REVPROPS. Populate the revprop cache, if enabled. If you want to
863 * modify revprop contents / update REVPROPS, READ_ALL must be set.
864 * Otherwise, only the properties of REV are being provided.
1010 *
1011 * Allocate *PROPERTIES in RESULT_POOL and temporaries in SCRATCH_POOL.
1012 */
1013static svn_error_t *
1014read_pack_revprop(packed_revprops_t **revprops,
1015 svn_fs_t *fs,
1016 svn_revnum_t rev,
865 *
866 * Allocate *PROPERTIES in RESULT_POOL and temporaries in SCRATCH_POOL.
867 */
868static svn_error_t *
869read_pack_revprop(packed_revprops_t **revprops,
870 svn_fs_t *fs,
871 svn_revnum_t rev,
1017 apr_int64_t generation,
1018 svn_boolean_t read_all,
1019 apr_pool_t *result_pool,
1020 apr_pool_t *scratch_pool)
1021{
1022 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1023 svn_boolean_t missing = FALSE;
872 svn_boolean_t read_all,
873 apr_pool_t *result_pool,
874 apr_pool_t *scratch_pool)
875{
876 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
877 svn_boolean_t missing = FALSE;
1024 svn_error_t *err;
1025 packed_revprops_t *result;
1026 int i;
1027
1028 /* someone insisted that REV is packed. Double-check if necessary */
1029 if (!svn_fs_x__is_packed_revprop(fs, rev))
1030 SVN_ERR(svn_fs_x__update_min_unpacked_rev(fs, iterpool));
1031
1032 if (!svn_fs_x__is_packed_revprop(fs, rev))
1033 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
1034 _("No such packed revision %ld"), rev);
1035
1036 /* initialize the result data structure */
1037 result = apr_pcalloc(result_pool, sizeof(*result));
1038 result->revision = rev;
878 packed_revprops_t *result;
879 int i;
880
881 /* someone insisted that REV is packed. Double-check if necessary */
882 if (!svn_fs_x__is_packed_revprop(fs, rev))
883 SVN_ERR(svn_fs_x__update_min_unpacked_rev(fs, iterpool));
884
885 if (!svn_fs_x__is_packed_revprop(fs, rev))
886 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
887 _("No such packed revision %ld"), rev);
888
889 /* initialize the result data structure */
890 result = apr_pcalloc(result_pool, sizeof(*result));
891 result->revision = rev;
1039 result->generation = generation;
1040
1041 /* try to read the packed revprops. This may require retries if we have
1042 * concurrent writers. */
892
893 /* try to read the packed revprops. This may require retries if we have
894 * concurrent writers. */
1043 for (i = 0;
1044 i < SVN_FS_X__RECOVERABLE_RETRY_COUNT && !result->packed_revprops;
1045 ++i)
895 for (i = 0; i < SVN_FS_X__RECOVERABLE_RETRY_COUNT; ++i)
1046 {
1047 const char *file_path;
896 {
897 const char *file_path;
898 svn_stringbuf_t *contents = NULL;
899
1048 svn_pool_clear(iterpool);
1049
1050 /* there might have been concurrent writes.
1051 * Re-read the manifest and the pack file.
1052 */
1053 SVN_ERR(get_revprop_packname(fs, result, result_pool, iterpool));
900 svn_pool_clear(iterpool);
901
902 /* there might have been concurrent writes.
903 * Re-read the manifest and the pack file.
904 */
905 SVN_ERR(get_revprop_packname(fs, result, result_pool, iterpool));
1054 file_path = svn_dirent_join(result->folder,
1055 result->filename,
1056 iterpool);
1057 SVN_ERR(svn_fs_x__try_stringbuf_from_file(&result->packed_revprops,
906 file_path = get_revprop_pack_filepath(result, &result->entry,
907 iterpool);
908 SVN_ERR(svn_fs_x__try_stringbuf_from_file(&contents,
1058 &missing,
1059 file_path,
1060 i + 1 < SVN_FS_X__RECOVERABLE_RETRY_COUNT,
909 &missing,
910 file_path,
911 i + 1 < SVN_FS_X__RECOVERABLE_RETRY_COUNT,
1061 result_pool));
912 iterpool));
1062
913
914 if (contents)
915 {
916 SVN_ERR_W(parse_packed_revprops(fs, result, contents, read_all,
917 result_pool, iterpool),
918 apr_psprintf(iterpool,
919 "Revprop pack file for r%ld is corrupt",
920 rev));
921 break;
922 }
923
1063 /* If we could not find the file, there was a write.
1064 * So, we should refresh our revprop generation info as well such
1065 * that others may find data we will put into the cache. They would
1066 * consider it outdated, otherwise.
1067 */
1068 if (missing && has_revprop_cache(fs, iterpool))
924 /* If we could not find the file, there was a write.
925 * So, we should refresh our revprop generation info as well such
926 * that others may find data we will put into the cache. They would
927 * consider it outdated, otherwise.
928 */
929 if (missing && has_revprop_cache(fs, iterpool))
1069 SVN_ERR(read_revprop_generation(&result->generation, fs, iterpool));
930 SVN_ERR(read_revprop_generation(fs, iterpool));
1070 }
1071
1072 /* the file content should be available now */
931 }
932
933 /* the file content should be available now */
1073 if (!result->packed_revprops)
934 if (!result->revprops)
1074 return svn_error_createf(SVN_ERR_FS_PACKED_REVPROP_READ_FAILURE, NULL,
1075 _("Failed to read revprop pack file for r%ld"), rev);
1076
935 return svn_error_createf(SVN_ERR_FS_PACKED_REVPROP_READ_FAILURE, NULL,
936 _("Failed to read revprop pack file for r%ld"), rev);
937
1077 /* parse it. RESULT will be complete afterwards. */
1078 err = parse_packed_revprops(fs, result, read_all, result_pool, iterpool);
1079 svn_pool_destroy(iterpool);
1080 if (err)
1081 return svn_error_createf(SVN_ERR_FS_CORRUPT, err,
1082 _("Revprop pack file for r%ld is corrupt"), rev);
1083
1084 *revprops = result;
1085
1086 return SVN_NO_ERROR;
1087}
1088
938 *revprops = result;
939
940 return SVN_NO_ERROR;
941}
942
1089/* Read the revprops for revision REV in FS and return them in *PROPERTIES_P.
1090 *
1091 * Allocations will be done in POOL.
1092 */
1093svn_error_t *
1094svn_fs_x__get_revision_proplist(apr_hash_t **proplist_p,
1095 svn_fs_t *fs,
1096 svn_revnum_t rev,
1097 svn_boolean_t bypass_cache,
943svn_error_t *
944svn_fs_x__get_revision_proplist(apr_hash_t **proplist_p,
945 svn_fs_t *fs,
946 svn_revnum_t rev,
947 svn_boolean_t bypass_cache,
948 svn_boolean_t refresh,
1098 apr_pool_t *result_pool,
1099 apr_pool_t *scratch_pool)
1100{
1101 svn_fs_x__data_t *ffd = fs->fsap_data;
949 apr_pool_t *result_pool,
950 apr_pool_t *scratch_pool)
951{
952 svn_fs_x__data_t *ffd = fs->fsap_data;
1102 apr_int64_t generation = 0;
1103
1104 /* not found, yet */
1105 *proplist_p = NULL;
1106
1107 /* should they be available at all? */
1108 SVN_ERR(svn_fs_x__ensure_revision_exists(rev, fs, scratch_pool));
1109
953
954 /* not found, yet */
955 *proplist_p = NULL;
956
957 /* should they be available at all? */
958 SVN_ERR(svn_fs_x__ensure_revision_exists(rev, fs, scratch_pool));
959
960 /* Ensure that the revprop generation info is valid. */
961 if (refresh || !is_generation_valid(fs))
962 SVN_ERR(read_revprop_generation(fs, scratch_pool));
963
1110 /* Try cache lookup first. */
1111 if (!bypass_cache && has_revprop_cache(fs, scratch_pool))
1112 {
1113 svn_boolean_t is_cached;
1114 svn_fs_x__pair_cache_key_t key = { 0 };
1115
964 /* Try cache lookup first. */
965 if (!bypass_cache && has_revprop_cache(fs, scratch_pool))
966 {
967 svn_boolean_t is_cached;
968 svn_fs_x__pair_cache_key_t key = { 0 };
969
1116 SVN_ERR(read_revprop_generation(&generation, fs, scratch_pool));
1117
1118 key.revision = rev;
970 key.revision = rev;
1119 key.second = generation;
971 key.second = ffd->revprop_generation;
1120 SVN_ERR(svn_cache__get((void **) proplist_p, &is_cached,
1121 ffd->revprop_cache, &key, result_pool));
1122 if (is_cached)
1123 return SVN_NO_ERROR;
1124 }
1125
1126 /* if REV had not been packed when we began, try reading it from the
1127 * non-packed shard. If that fails, we will fall through to packed
1128 * shard reads. */
1129 if (!svn_fs_x__is_packed_revprop(fs, rev))
1130 {
1131 svn_error_t *err = read_non_packed_revprop(proplist_p, fs, rev,
972 SVN_ERR(svn_cache__get((void **) proplist_p, &is_cached,
973 ffd->revprop_cache, &key, result_pool));
974 if (is_cached)
975 return SVN_NO_ERROR;
976 }
977
978 /* if REV had not been packed when we began, try reading it from the
979 * non-packed shard. If that fails, we will fall through to packed
980 * shard reads. */
981 if (!svn_fs_x__is_packed_revprop(fs, rev))
982 {
983 svn_error_t *err = read_non_packed_revprop(proplist_p, fs, rev,
1132 generation, result_pool,
1133 scratch_pool);
984 result_pool, scratch_pool);
1134 if (err)
1135 {
1136 if (!APR_STATUS_IS_ENOENT(err->apr_err))
1137 return svn_error_trace(err);
1138
1139 svn_error_clear(err);
1140 *proplist_p = NULL; /* in case read_non_packed_revprop changed it */
1141 }
1142 }
1143
1144 /* if revprop packing is available and we have not read the revprops, yet,
1145 * try reading them from a packed shard. If that fails, REV is most
1146 * likely invalid (or its revprops highly contested). */
1147 if (!*proplist_p)
1148 {
1149 packed_revprops_t *revprops;
985 if (err)
986 {
987 if (!APR_STATUS_IS_ENOENT(err->apr_err))
988 return svn_error_trace(err);
989
990 svn_error_clear(err);
991 *proplist_p = NULL; /* in case read_non_packed_revprop changed it */
992 }
993 }
994
995 /* if revprop packing is available and we have not read the revprops, yet,
996 * try reading them from a packed shard. If that fails, REV is most
997 * likely invalid (or its revprops highly contested). */
998 if (!*proplist_p)
999 {
1000 packed_revprops_t *revprops;
1150 SVN_ERR(read_pack_revprop(&revprops, fs, rev, generation, FALSE,
1001 SVN_ERR(read_pack_revprop(&revprops, fs, rev, FALSE,
1151 result_pool, scratch_pool));
1152 *proplist_p = revprops->properties;
1153 }
1154
1155 /* The revprops should have been there. Did we get them? */
1156 if (!*proplist_p)
1157 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
1158 _("Could not read revprops for revision %ld"),
1159 rev);
1160
1161 return SVN_NO_ERROR;
1162}
1163
1002 result_pool, scratch_pool));
1003 *proplist_p = revprops->properties;
1004 }
1005
1006 /* The revprops should have been there. Did we get them? */
1007 if (!*proplist_p)
1008 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
1009 _("Could not read revprops for revision %ld"),
1010 rev);
1011
1012 return SVN_NO_ERROR;
1013}
1014
1015svn_error_t *
1016svn_fs_x__write_non_packed_revprops(apr_file_t *file,
1017 apr_hash_t *proplist,
1018 apr_pool_t *scratch_pool)
1019{
1020 svn_stream_t *stream;
1021 svn_checksum_t *checksum;
1022
1023 stream = svn_stream_from_aprfile2(file, TRUE, scratch_pool);
1024 stream = svn_checksum__wrap_write_stream(&checksum, stream,
1025 svn_checksum_fnv1a_32x4,
1026 scratch_pool);
1027 SVN_ERR(svn_fs_x__write_properties(stream, proplist, scratch_pool));
1028 SVN_ERR(svn_stream_close(stream));
1029
1030 /* Append the checksum */
1031 SVN_ERR(svn_io_file_write_full(file, checksum->digest,
1032 svn_checksum_size(checksum), NULL,
1033 scratch_pool));
1034
1035 return SVN_NO_ERROR;
1036}
1037
1164/* Serialize the revision property list PROPLIST of revision REV in
1165 * filesystem FS to a non-packed file. Return the name of that temporary
1166 * file in *TMP_PATH and the file path that it must be moved to in
1038/* Serialize the revision property list PROPLIST of revision REV in
1039 * filesystem FS to a non-packed file. Return the name of that temporary
1040 * file in *TMP_PATH and the file path that it must be moved to in
1167 * *FINAL_PATH.
1041 * *FINAL_PATH. Schedule necessary fsync calls in BATCH.
1168 *
1169 * Allocate *FINAL_PATH and *TMP_PATH in RESULT_POOL. Use SCRATCH_POOL
1170 * for temporary allocations.
1171 */
1172static svn_error_t *
1173write_non_packed_revprop(const char **final_path,
1174 const char **tmp_path,
1175 svn_fs_t *fs,
1176 svn_revnum_t rev,
1177 apr_hash_t *proplist,
1042 *
1043 * Allocate *FINAL_PATH and *TMP_PATH in RESULT_POOL. Use SCRATCH_POOL
1044 * for temporary allocations.
1045 */
1046static svn_error_t *
1047write_non_packed_revprop(const char **final_path,
1048 const char **tmp_path,
1049 svn_fs_t *fs,
1050 svn_revnum_t rev,
1051 apr_hash_t *proplist,
1052 svn_fs_x__batch_fsync_t *batch,
1178 apr_pool_t *result_pool,
1179 apr_pool_t *scratch_pool)
1180{
1053 apr_pool_t *result_pool,
1054 apr_pool_t *scratch_pool)
1055{
1181 svn_stream_t *stream;
1056 apr_file_t *file;
1182 *final_path = svn_fs_x__path_revprops(fs, rev, result_pool);
1183
1057 *final_path = svn_fs_x__path_revprops(fs, rev, result_pool);
1058
1184 /* ### do we have a directory sitting around already? we really shouldn't
1185 ### have to get the dirname here. */
1186 SVN_ERR(svn_stream_open_unique(&stream, tmp_path,
1187 svn_dirent_dirname(*final_path,
1188 scratch_pool),
1189 svn_io_file_del_none,
1190 result_pool, scratch_pool));
1191 SVN_ERR(svn_hash_write2(proplist, stream, SVN_HASH_TERMINATOR,
1192 scratch_pool));
1193 SVN_ERR(svn_stream_close(stream));
1059 *tmp_path = apr_pstrcat(result_pool, *final_path, ".tmp", SVN_VA_NULL);
1060 SVN_ERR(svn_fs_x__batch_fsync_open_file(&file, batch, *tmp_path,
1061 scratch_pool));
1194
1062
1063 SVN_ERR(svn_fs_x__write_non_packed_revprops(file, proplist, scratch_pool));
1064
1195 return SVN_NO_ERROR;
1196}
1197
1198/* After writing the new revprop file(s), call this function to move the
1199 * file at TMP_PATH to FINAL_PATH and give it the permissions from
1065 return SVN_NO_ERROR;
1066}
1067
1068/* After writing the new revprop file(s), call this function to move the
1069 * file at TMP_PATH to FINAL_PATH and give it the permissions from
1200 * PERMS_REFERENCE.
1070 * PERMS_REFERENCE. Schedule necessary fsync calls in BATCH.
1201 *
1202 * If indicated in BUMP_GENERATION, increase FS' revprop generation.
1203 * Finally, delete all the temporary files given in FILES_TO_DELETE.
1204 * The latter may be NULL.
1205 *
1206 * Use SCRATCH_POOL for temporary allocations.
1207 */
1208static svn_error_t *
1209switch_to_new_revprop(svn_fs_t *fs,
1210 const char *final_path,
1211 const char *tmp_path,
1212 const char *perms_reference,
1213 apr_array_header_t *files_to_delete,
1214 svn_boolean_t bump_generation,
1071 *
1072 * If indicated in BUMP_GENERATION, increase FS' revprop generation.
1073 * Finally, delete all the temporary files given in FILES_TO_DELETE.
1074 * The latter may be NULL.
1075 *
1076 * Use SCRATCH_POOL for temporary allocations.
1077 */
1078static svn_error_t *
1079switch_to_new_revprop(svn_fs_t *fs,
1080 const char *final_path,
1081 const char *tmp_path,
1082 const char *perms_reference,
1083 apr_array_header_t *files_to_delete,
1084 svn_boolean_t bump_generation,
1085 svn_fs_x__batch_fsync_t *batch,
1215 apr_pool_t *scratch_pool)
1216{
1086 apr_pool_t *scratch_pool)
1087{
1217 apr_int64_t generation;
1218
1219 /* Now, we may actually be replacing revprops. Make sure that all other
1220 threads and processes will know about this. */
1221 if (bump_generation)
1088 /* Now, we may actually be replacing revprops. Make sure that all other
1089 threads and processes will know about this. */
1090 if (bump_generation)
1222 SVN_ERR(begin_revprop_change(&generation, fs, scratch_pool));
1091 SVN_ERR(begin_revprop_change(fs, scratch_pool));
1223
1092
1093 /* Ensure the new file contents makes it to disk before switching over to
1094 * it. */
1095 SVN_ERR(svn_fs_x__batch_fsync_run(batch, scratch_pool));
1096
1097 /* Make the revision visible to all processes and threads. */
1224 SVN_ERR(svn_fs_x__move_into_place(tmp_path, final_path, perms_reference,
1098 SVN_ERR(svn_fs_x__move_into_place(tmp_path, final_path, perms_reference,
1225 scratch_pool));
1099 batch, scratch_pool));
1100 SVN_ERR(svn_fs_x__batch_fsync_run(batch, scratch_pool));
1226
1227 /* Indicate that the update (if relevant) has been completed. */
1228 if (bump_generation)
1101
1102 /* Indicate that the update (if relevant) has been completed. */
1103 if (bump_generation)
1229 SVN_ERR(end_revprop_change(fs, generation, scratch_pool));
1104 SVN_ERR(end_revprop_change(fs, scratch_pool));
1230
1231 /* Clean up temporary files, if necessary. */
1232 if (files_to_delete)
1233 {
1234 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1235 int i;
1236
1237 for (i = 0; i < files_to_delete->nelts; ++i)

--- 4 unchanged lines hidden (view full) ---

1242 SVN_ERR(svn_io_remove_file2(path, TRUE, iterpool));
1243 }
1244
1245 svn_pool_destroy(iterpool);
1246 }
1247 return SVN_NO_ERROR;
1248}
1249
1105
1106 /* Clean up temporary files, if necessary. */
1107 if (files_to_delete)
1108 {
1109 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1110 int i;
1111
1112 for (i = 0; i < files_to_delete->nelts; ++i)

--- 4 unchanged lines hidden (view full) ---

1117 SVN_ERR(svn_io_remove_file2(path, TRUE, iterpool));
1118 }
1119
1120 svn_pool_destroy(iterpool);
1121 }
1122 return SVN_NO_ERROR;
1123}
1124
1250/* Write a pack file header to STREAM that starts at revision START_REVISION
1251 * and contains the indexes [START,END) of SIZES.
1252 */
1253static svn_error_t *
1254serialize_revprops_header(svn_stream_t *stream,
1255 svn_revnum_t start_revision,
1256 apr_array_header_t *sizes,
1257 int start,
1258 int end,
1259 apr_pool_t *scratch_pool)
1260{
1261 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1262 int i;
1263
1264 SVN_ERR_ASSERT(start < end);
1265
1266 /* start revision and entry count */
1267 SVN_ERR(svn_stream_printf(stream, scratch_pool, "%ld\n", start_revision));
1268 SVN_ERR(svn_stream_printf(stream, scratch_pool, "%d\n", end - start));
1269
1270 /* the sizes array */
1271 for (i = start; i < end; ++i)
1272 {
1273 /* Non-standard pool usage.
1274 *
1275 * We only allocate a few bytes each iteration -- even with a
1276 * million iterations we would still be in good shape memory-wise.
1277 */
1278 apr_off_t size = APR_ARRAY_IDX(sizes, i, apr_off_t);
1279 SVN_ERR(svn_stream_printf(stream, iterpool, "%" APR_OFF_T_FMT "\n",
1280 size));
1281 }
1282
1283 /* the double newline char indicates the end of the header */
1284 SVN_ERR(svn_stream_printf(stream, iterpool, "\n"));
1285
1286 svn_pool_destroy(iterpool);
1287 return SVN_NO_ERROR;
1288}
1289
1290/* Writes the a pack file to FILE_STREAM. It copies the serialized data
1291 * from REVPROPS for the indexes [START,END) except for index CHANGED_INDEX.
1125/* Writes the a pack file to FILE. It copies the serialized data
1126 * from REVPROPS for the indexes [START,END).
1292 *
1127 *
1293 * The data for the latter is taken from NEW_SERIALIZED. Note, that
1294 * CHANGED_INDEX may be outside the [START,END) range, i.e. no new data is
1295 * taken in that case but only a subset of the old data will be copied.
1296 *
1297 * NEW_TOTAL_SIZE is a hint for pre-allocating buffers of appropriate size.
1298 * SCRATCH_POOL is used for temporary allocations.
1299 */
1300static svn_error_t *
1301repack_revprops(svn_fs_t *fs,
1302 packed_revprops_t *revprops,
1303 int start,
1304 int end,
1128 * NEW_TOTAL_SIZE is a hint for pre-allocating buffers of appropriate size.
1129 * SCRATCH_POOL is used for temporary allocations.
1130 */
1131static svn_error_t *
1132repack_revprops(svn_fs_t *fs,
1133 packed_revprops_t *revprops,
1134 int start,
1135 int end,
1305 int changed_index,
1306 svn_stringbuf_t *new_serialized,
1307 apr_off_t new_total_size,
1308 svn_stream_t *file_stream,
1136 apr_size_t new_total_size,
1137 apr_file_t *file,
1309 apr_pool_t *scratch_pool)
1310{
1138 apr_pool_t *scratch_pool)
1139{
1311 svn_fs_x__data_t *ffd = fs->fsap_data;
1312 svn_stream_t *stream;
1313 int i;
1314
1140 int i;
1141
1315 /* create data empty buffers and the stream object */
1316 svn_stringbuf_t *uncompressed
1317 = svn_stringbuf_create_ensure((apr_size_t)new_total_size, scratch_pool);
1318 svn_stringbuf_t *compressed
1319 = svn_stringbuf_create_empty(scratch_pool);
1320 stream = svn_stream_from_stringbuf(uncompressed, scratch_pool);
1142 svn_packed__data_root_t *root = svn_packed__data_create_root(scratch_pool);
1143 svn_packed__byte_stream_t *revprops_stream
1144 = svn_packed__create_bytes_stream(root);
1321
1145
1322 /* write the header*/
1323 SVN_ERR(serialize_revprops_header(stream, revprops->start_revision + start,
1324 revprops->sizes, start, end,
1325 scratch_pool));
1326
1327 /* append the serialized revprops */
1328 for (i = start; i < end; ++i)
1146 /* append the serialized revprops */
1147 for (i = start; i < end; ++i)
1329 if (i == changed_index)
1330 {
1331 SVN_ERR(svn_stream_write(stream,
1332 new_serialized->data,
1333 &new_serialized->len));
1334 }
1335 else
1336 {
1337 apr_size_t size
1338 = (apr_size_t)APR_ARRAY_IDX(revprops->sizes, i, apr_off_t);
1339 apr_size_t offset
1340 = (apr_size_t)APR_ARRAY_IDX(revprops->offsets, i, apr_off_t);
1148 {
1149 const svn_string_t *props
1150 = &APR_ARRAY_IDX(revprops->revprops, i, svn_string_t);
1341
1151
1342 SVN_ERR(svn_stream_write(stream,
1343 revprops->packed_revprops->data + offset,
1344 &size));
1345 }
1152 svn_packed__add_bytes(revprops_stream, props->data, props->len);
1153 }
1346
1154
1347 /* flush the stream buffer (if any) to our underlying data buffer */
1348 SVN_ERR(svn_stream_close(stream));
1155 /* Write to file. */
1156 SVN_ERR(write_packed_data_checksummed(root, file, scratch_pool));
1349
1157
1350 /* compress / store the data */
1351 SVN_ERR(svn__compress(uncompressed,
1352 compressed,
1353 ffd->compress_packed_revprops
1354 ? SVN_DELTA_COMPRESSION_LEVEL_DEFAULT
1355 : SVN_DELTA_COMPRESSION_LEVEL_NONE));
1356
1357 /* finally, write the content to the target stream and close it */
1358 SVN_ERR(svn_stream_write(file_stream, compressed->data, &compressed->len));
1359 SVN_ERR(svn_stream_close(file_stream));
1360
1361 return SVN_NO_ERROR;
1362}
1363
1158 return SVN_NO_ERROR;
1159}
1160
1364/* Allocate a new pack file name for revisions
1365 * [REVPROPS->START_REVISION + START, REVPROPS->START_REVISION + END - 1]
1366 * of REVPROPS->MANIFEST. Add the name of old file to FILES_TO_DELETE,
1367 * auto-create that array if necessary. Return an open file stream to
1368 * the new file in *STREAM allocated in RESULT_POOL. Allocate the paths
1369 * in *FILES_TO_DELETE from the same pool that contains the array itself.
1161/* Allocate a new pack file name for revisions starting at START_REV in
1162 * REVPROPS->MANIFEST. Add the name of old file to FILES_TO_DELETE,
1163 * auto-create that array if necessary. Return an open file *FILE that is
1164 * allocated in RESULT_POOL. Allocate the paths in *FILES_TO_DELETE from
1165 * the same pool that contains the array itself. Schedule necessary fsync
1166 * calls in BATCH.
1370 *
1371 * Use SCRATCH_POOL for temporary allocations.
1372 */
1373static svn_error_t *
1167 *
1168 * Use SCRATCH_POOL for temporary allocations.
1169 */
1170static svn_error_t *
1374repack_stream_open(svn_stream_t **stream,
1375 svn_fs_t *fs,
1376 packed_revprops_t *revprops,
1377 int start,
1378 int end,
1379 apr_array_header_t **files_to_delete,
1380 apr_pool_t *result_pool,
1381 apr_pool_t *scratch_pool)
1171repack_file_open(apr_file_t **file,
1172 svn_fs_t *fs,
1173 packed_revprops_t *revprops,
1174 svn_revnum_t start_rev,
1175 apr_array_header_t **files_to_delete,
1176 svn_fs_x__batch_fsync_t *batch,
1177 apr_pool_t *result_pool,
1178 apr_pool_t *scratch_pool)
1382{
1179{
1383 apr_int64_t tag;
1384 const char *tag_string;
1385 svn_string_t *new_filename;
1386 int i;
1387 apr_file_t *file;
1388 int manifest_offset
1389 = (int)(revprops->start_revision - revprops->manifest_start);
1180 manifest_entry_t new_entry;
1181 const char *new_path;
1182 int idx;
1390
1183
1391 /* get the old (= current) file name and enlist it for later deletion */
1392 const char *old_filename = APR_ARRAY_IDX(revprops->manifest,
1393 start + manifest_offset,
1394 const char*);
1184 /* We always replace whole pack files - possibly by more than one new file.
1185 * When we create the file for the first part of the pack, enlist the old
1186 * one for later deletion */
1187 SVN_ERR_ASSERT(start_rev >= revprops->entry.start_rev);
1395
1396 if (*files_to_delete == NULL)
1397 *files_to_delete = apr_array_make(result_pool, 3, sizeof(const char*));
1398
1188
1189 if (*files_to_delete == NULL)
1190 *files_to_delete = apr_array_make(result_pool, 3, sizeof(const char*));
1191
1399 APR_ARRAY_PUSH(*files_to_delete, const char*)
1400 = svn_dirent_join(revprops->folder, old_filename,
1401 (*files_to_delete)->pool);
1192 if (revprops->entry.start_rev == start_rev)
1193 APR_ARRAY_PUSH(*files_to_delete, const char*)
1194 = get_revprop_pack_filepath(revprops, &revprops->entry,
1195 (*files_to_delete)->pool);
1402
1196
1403 /* increase the tag part, i.e. the counter after the dot */
1404 tag_string = strchr(old_filename, '.');
1405 if (tag_string == NULL)
1406 return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
1407 _("Packed file '%s' misses a tag"),
1408 old_filename);
1197 /* Initialize the new manifest entry. Bump the tag part. */
1198 new_entry.start_rev = start_rev;
1199 new_entry.tag = revprops->entry.tag + 1;
1409
1200
1410 SVN_ERR(svn_cstring_atoi64(&tag, tag_string + 1));
1411 new_filename = svn_string_createf((*files_to_delete)->pool,
1412 "%ld.%" APR_INT64_T_FMT,
1413 revprops->start_revision + start,
1414 ++tag);
1415
1416 /* update the manifest to point to the new file */
1201 /* update the manifest to point to the new file */
1417 for (i = start; i < end; ++i)
1418 APR_ARRAY_IDX(revprops->manifest, i + manifest_offset, const char*)
1419 = new_filename->data;
1202 idx = get_entry(revprops->manifest, start_rev);
1203 if (revprops->entry.start_rev == start_rev)
1204 APR_ARRAY_IDX(revprops->manifest, idx, manifest_entry_t) = new_entry;
1205 else
1206 SVN_ERR(svn_sort__array_insert2(revprops->manifest, &new_path, idx + 1));
1420
1207
1421 /* create a file stream for the new file */
1422 SVN_ERR(svn_io_file_open(&file, svn_dirent_join(revprops->folder,
1423 new_filename->data,
1424 scratch_pool),
1425 APR_WRITE | APR_CREATE, APR_OS_DEFAULT,
1426 result_pool));
1427 *stream = svn_stream_from_aprfile2(file, FALSE, result_pool);
1208 /* open the file */
1209 new_path = get_revprop_pack_filepath(revprops, &new_entry, scratch_pool);
1210 SVN_ERR(svn_fs_x__batch_fsync_open_file(file, batch, new_path,
1211 scratch_pool));
1428
1429 return SVN_NO_ERROR;
1430}
1431
1212
1213 return SVN_NO_ERROR;
1214}
1215
1216/* Return the length of the serialized reprop list of index I in REVPROPS. */
1217static apr_size_t
1218props_len(packed_revprops_t *revprops,
1219 int i)
1220{
1221 return APR_ARRAY_IDX(revprops->revprops, i, svn_string_t).len;
1222}
1223
1432/* For revision REV in filesystem FS, set the revision properties to
1433 * PROPLIST. Return a new file in *TMP_PATH that the caller shall move
1434 * to *FINAL_PATH to make the change visible. Files to be deleted will
1435 * be listed in *FILES_TO_DELETE which may remain unchanged / unallocated.
1224/* For revision REV in filesystem FS, set the revision properties to
1225 * PROPLIST. Return a new file in *TMP_PATH that the caller shall move
1226 * to *FINAL_PATH to make the change visible. Files to be deleted will
1227 * be listed in *FILES_TO_DELETE which may remain unchanged / unallocated.
1228 * Schedule necessary fsync calls in BATCH.
1436 *
1437 * Allocate output values in RESULT_POOL and temporaries from SCRATCH_POOL.
1438 */
1439static svn_error_t *
1440write_packed_revprop(const char **final_path,
1441 const char **tmp_path,
1442 apr_array_header_t **files_to_delete,
1443 svn_fs_t *fs,
1444 svn_revnum_t rev,
1445 apr_hash_t *proplist,
1229 *
1230 * Allocate output values in RESULT_POOL and temporaries from SCRATCH_POOL.
1231 */
1232static svn_error_t *
1233write_packed_revprop(const char **final_path,
1234 const char **tmp_path,
1235 apr_array_header_t **files_to_delete,
1236 svn_fs_t *fs,
1237 svn_revnum_t rev,
1238 apr_hash_t *proplist,
1239 svn_fs_x__batch_fsync_t *batch,
1446 apr_pool_t *result_pool,
1447 apr_pool_t *scratch_pool)
1448{
1449 svn_fs_x__data_t *ffd = fs->fsap_data;
1450 packed_revprops_t *revprops;
1240 apr_pool_t *result_pool,
1241 apr_pool_t *scratch_pool)
1242{
1243 svn_fs_x__data_t *ffd = fs->fsap_data;
1244 packed_revprops_t *revprops;
1451 apr_int64_t generation = 0;
1452 svn_stream_t *stream;
1245 svn_stream_t *stream;
1246 apr_file_t *file;
1453 svn_stringbuf_t *serialized;
1247 svn_stringbuf_t *serialized;
1454 apr_off_t new_total_size;
1248 apr_size_t new_total_size;
1455 int changed_index;
1249 int changed_index;
1250 int count;
1456
1457 /* read the current revprop generation. This value will not change
1458 * while we hold the global write lock to this FS. */
1459 if (has_revprop_cache(fs, scratch_pool))
1251
1252 /* read the current revprop generation. This value will not change
1253 * while we hold the global write lock to this FS. */
1254 if (has_revprop_cache(fs, scratch_pool))
1460 SVN_ERR(read_revprop_generation(&generation, fs, scratch_pool));
1255 SVN_ERR(read_revprop_generation(fs, scratch_pool));
1461
1462 /* read contents of the current pack file */
1256
1257 /* read contents of the current pack file */
1463 SVN_ERR(read_pack_revprop(&revprops, fs, rev, generation, TRUE,
1258 SVN_ERR(read_pack_revprop(&revprops, fs, rev, TRUE,
1464 scratch_pool, scratch_pool));
1465
1466 /* serialize the new revprops */
1467 serialized = svn_stringbuf_create_empty(scratch_pool);
1468 stream = svn_stream_from_stringbuf(serialized, scratch_pool);
1259 scratch_pool, scratch_pool));
1260
1261 /* serialize the new revprops */
1262 serialized = svn_stringbuf_create_empty(scratch_pool);
1263 stream = svn_stream_from_stringbuf(serialized, scratch_pool);
1469 SVN_ERR(svn_hash_write2(proplist, stream, SVN_HASH_TERMINATOR,
1470 scratch_pool));
1264 SVN_ERR(svn_fs_x__write_properties(stream, proplist, scratch_pool));
1471 SVN_ERR(svn_stream_close(stream));
1472
1265 SVN_ERR(svn_stream_close(stream));
1266
1473 /* calculate the size of the new data */
1474 changed_index = (int)(rev - revprops->start_revision);
1267 /* estimate the size of the new data */
1268 count = revprops->revprops->nelts;
1269 changed_index = (int)(rev - revprops->entry.start_rev);
1475 new_total_size = revprops->total_size - revprops->serialized_size
1476 + serialized->len
1270 new_total_size = revprops->total_size - revprops->serialized_size
1271 + serialized->len
1477 + (revprops->offsets->nelts + 2) * SVN_INT64_BUFFER_SIZE;
1272 + (count + 2) * SVN_INT64_BUFFER_SIZE;
1478
1273
1479 APR_ARRAY_IDX(revprops->sizes, changed_index, apr_off_t) = serialized->len;
1274 APR_ARRAY_IDX(revprops->revprops, changed_index, svn_string_t)
1275 = *svn_stringbuf__morph_into_string(serialized);
1480
1481 /* can we put the new data into the same pack as the before? */
1276
1277 /* can we put the new data into the same pack as the before? */
1482 if ( new_total_size < ffd->revprop_pack_size
1483 || revprops->sizes->nelts == 1)
1278 if (new_total_size < ffd->revprop_pack_size || count == 1)
1484 {
1485 /* simply replace the old pack file with new content as we do it
1486 * in the non-packed case */
1487
1279 {
1280 /* simply replace the old pack file with new content as we do it
1281 * in the non-packed case */
1282
1488 *final_path = svn_dirent_join(revprops->folder, revprops->filename,
1489 result_pool);
1490 SVN_ERR(svn_stream_open_unique(&stream, tmp_path, revprops->folder,
1491 svn_io_file_del_none, result_pool,
1492 scratch_pool));
1493 SVN_ERR(repack_revprops(fs, revprops, 0, revprops->sizes->nelts,
1494 changed_index, serialized, new_total_size,
1495 stream, scratch_pool));
1283 *final_path = get_revprop_pack_filepath(revprops, &revprops->entry,
1284 result_pool);
1285 *tmp_path = apr_pstrcat(result_pool, *final_path, ".tmp", SVN_VA_NULL);
1286 SVN_ERR(svn_fs_x__batch_fsync_open_file(&file, batch, *tmp_path,
1287 scratch_pool));
1288 SVN_ERR(repack_revprops(fs, revprops, 0, count,
1289 new_total_size, file, scratch_pool));
1496 }
1497 else
1498 {
1499 /* split the pack file into two of roughly equal size */
1290 }
1291 else
1292 {
1293 /* split the pack file into two of roughly equal size */
1500 int right_count, left_count, i;
1294 int right_count, left_count;
1501
1502 int left = 0;
1295
1296 int left = 0;
1503 int right = revprops->sizes->nelts - 1;
1504 apr_off_t left_size = 2 * SVN_INT64_BUFFER_SIZE;
1505 apr_off_t right_size = 2 * SVN_INT64_BUFFER_SIZE;
1297 int right = count - 1;
1298 apr_size_t left_size = 2 * SVN_INT64_BUFFER_SIZE;
1299 apr_size_t right_size = 2 * SVN_INT64_BUFFER_SIZE;
1506
1507 /* let left and right side grow such that their size difference
1508 * is minimal after each step. */
1509 while (left <= right)
1300
1301 /* let left and right side grow such that their size difference
1302 * is minimal after each step. */
1303 while (left <= right)
1510 if ( left_size + APR_ARRAY_IDX(revprops->sizes, left, apr_off_t)
1511 < right_size + APR_ARRAY_IDX(revprops->sizes, right, apr_off_t))
1304 if ( left_size + props_len(revprops, left)
1305 < right_size + props_len(revprops, right))
1512 {
1306 {
1513 left_size += APR_ARRAY_IDX(revprops->sizes, left, apr_off_t)
1514 + SVN_INT64_BUFFER_SIZE;
1307 left_size += props_len(revprops, left) + SVN_INT64_BUFFER_SIZE;
1515 ++left;
1516 }
1517 else
1518 {
1308 ++left;
1309 }
1310 else
1311 {
1519 right_size += APR_ARRAY_IDX(revprops->sizes, right, apr_off_t)
1520 + SVN_INT64_BUFFER_SIZE;
1312 right_size += props_len(revprops, right) + SVN_INT64_BUFFER_SIZE;
1521 --right;
1522 }
1523
1524 /* since the items need much less than SVN_INT64_BUFFER_SIZE
1525 * bytes to represent their length, the split may not be optimal */
1526 left_count = left;
1313 --right;
1314 }
1315
1316 /* since the items need much less than SVN_INT64_BUFFER_SIZE
1317 * bytes to represent their length, the split may not be optimal */
1318 left_count = left;
1527 right_count = revprops->sizes->nelts - left;
1319 right_count = count - left;
1528
1529 /* if new_size is large, one side may exceed the pack size limit.
1530 * In that case, split before and after the modified revprop.*/
1531 if ( left_size > ffd->revprop_pack_size
1532 || right_size > ffd->revprop_pack_size)
1533 {
1534 left_count = changed_index;
1320
1321 /* if new_size is large, one side may exceed the pack size limit.
1322 * In that case, split before and after the modified revprop.*/
1323 if ( left_size > ffd->revprop_pack_size
1324 || right_size > ffd->revprop_pack_size)
1325 {
1326 left_count = changed_index;
1535 right_count = revprops->sizes->nelts - left_count - 1;
1327 right_count = count - left_count - 1;
1536 }
1537
1538 /* Allocate this here such that we can call the repack functions with
1539 * the scratch pool alone. */
1540 if (*files_to_delete == NULL)
1541 *files_to_delete = apr_array_make(result_pool, 3,
1542 sizeof(const char*));
1543
1544 /* write the new, split files */
1545 if (left_count)
1546 {
1328 }
1329
1330 /* Allocate this here such that we can call the repack functions with
1331 * the scratch pool alone. */
1332 if (*files_to_delete == NULL)
1333 *files_to_delete = apr_array_make(result_pool, 3,
1334 sizeof(const char*));
1335
1336 /* write the new, split files */
1337 if (left_count)
1338 {
1547 SVN_ERR(repack_stream_open(&stream, fs, revprops, 0,
1548 left_count, files_to_delete,
1549 scratch_pool, scratch_pool));
1339 SVN_ERR(repack_file_open(&file, fs, revprops,
1340 revprops->entry.start_rev,
1341 files_to_delete, batch,
1342 scratch_pool, scratch_pool));
1550 SVN_ERR(repack_revprops(fs, revprops, 0, left_count,
1343 SVN_ERR(repack_revprops(fs, revprops, 0, left_count,
1551 changed_index, serialized, new_total_size,
1552 stream, scratch_pool));
1344 new_total_size, file, scratch_pool));
1553 }
1554
1345 }
1346
1555 if (left_count + right_count < revprops->sizes->nelts)
1347 if (left_count + right_count < count)
1556 {
1348 {
1557 SVN_ERR(repack_stream_open(&stream, fs, revprops, changed_index,
1558 changed_index + 1, files_to_delete,
1559 scratch_pool, scratch_pool));
1349 SVN_ERR(repack_file_open(&file, fs, revprops, rev,
1350 files_to_delete, batch,
1351 scratch_pool, scratch_pool));
1560 SVN_ERR(repack_revprops(fs, revprops, changed_index,
1561 changed_index + 1,
1352 SVN_ERR(repack_revprops(fs, revprops, changed_index,
1353 changed_index + 1,
1562 changed_index, serialized, new_total_size,
1563 stream, scratch_pool));
1354 new_total_size, file, scratch_pool));
1564 }
1565
1566 if (right_count)
1567 {
1355 }
1356
1357 if (right_count)
1358 {
1568 SVN_ERR(repack_stream_open(&stream, fs, revprops,
1569 revprops->sizes->nelts - right_count,
1570 revprops->sizes->nelts,
1571 files_to_delete, scratch_pool,
1572 scratch_pool));
1573 SVN_ERR(repack_revprops(fs, revprops,
1574 revprops->sizes->nelts - right_count,
1575 revprops->sizes->nelts, changed_index,
1576 serialized, new_total_size, stream,
1577 scratch_pool));
1359 SVN_ERR(repack_file_open(&file, fs, revprops, rev + 1,
1360 files_to_delete, batch,
1361 scratch_pool, scratch_pool));
1362 SVN_ERR(repack_revprops(fs, revprops, count - right_count, count,
1363 new_total_size, file, scratch_pool));
1578 }
1579
1580 /* write the new manifest */
1581 *final_path = svn_dirent_join(revprops->folder, PATH_MANIFEST,
1582 result_pool);
1364 }
1365
1366 /* write the new manifest */
1367 *final_path = svn_dirent_join(revprops->folder, PATH_MANIFEST,
1368 result_pool);
1583 SVN_ERR(svn_stream_open_unique(&stream, tmp_path, revprops->folder,
1584 svn_io_file_del_none, result_pool,
1585 scratch_pool));
1586
1587 for (i = 0; i < revprops->manifest->nelts; ++i)
1588 {
1589 const char *filename = APR_ARRAY_IDX(revprops->manifest, i,
1590 const char*);
1591 SVN_ERR(svn_stream_printf(stream, scratch_pool, "%s\n", filename));
1592 }
1593
1594 SVN_ERR(svn_stream_close(stream));
1369 *tmp_path = apr_pstrcat(result_pool, *final_path, ".tmp", SVN_VA_NULL);
1370 SVN_ERR(svn_fs_x__batch_fsync_open_file(&file, batch, *tmp_path,
1371 scratch_pool));
1372 SVN_ERR(write_manifest(file, revprops->manifest, scratch_pool));
1595 }
1596
1597 return SVN_NO_ERROR;
1598}
1599
1600/* Set the revision property list of revision REV in filesystem FS to
1601 PROPLIST. Use SCRATCH_POOL for temporary allocations. */
1602svn_error_t *
1603svn_fs_x__set_revision_proplist(svn_fs_t *fs,
1604 svn_revnum_t rev,
1605 apr_hash_t *proplist,
1606 apr_pool_t *scratch_pool)
1607{
1608 svn_boolean_t is_packed;
1609 svn_boolean_t bump_generation = FALSE;
1610 const char *final_path;
1611 const char *tmp_path;
1612 const char *perms_reference;
1613 apr_array_header_t *files_to_delete = NULL;
1373 }
1374
1375 return SVN_NO_ERROR;
1376}
1377
1378/* Set the revision property list of revision REV in filesystem FS to
1379 PROPLIST. Use SCRATCH_POOL for temporary allocations. */
1380svn_error_t *
1381svn_fs_x__set_revision_proplist(svn_fs_t *fs,
1382 svn_revnum_t rev,
1383 apr_hash_t *proplist,
1384 apr_pool_t *scratch_pool)
1385{
1386 svn_boolean_t is_packed;
1387 svn_boolean_t bump_generation = FALSE;
1388 const char *final_path;
1389 const char *tmp_path;
1390 const char *perms_reference;
1391 apr_array_header_t *files_to_delete = NULL;
1392 svn_fs_x__batch_fsync_t *batch;
1393 svn_fs_x__data_t *ffd = fs->fsap_data;
1614
1615 SVN_ERR(svn_fs_x__ensure_revision_exists(rev, fs, scratch_pool));
1616
1394
1395 SVN_ERR(svn_fs_x__ensure_revision_exists(rev, fs, scratch_pool));
1396
1397 /* Perform all fsyncs through this instance. */
1398 SVN_ERR(svn_fs_x__batch_fsync_create(&batch, ffd->flush_to_disk,
1399 scratch_pool));
1400
1617 /* this info will not change while we hold the global FS write lock */
1618 is_packed = svn_fs_x__is_packed_revprop(fs, rev);
1619
1620 /* Test whether revprops already exist for this revision.
1621 * Only then will we need to bump the revprop generation.
1622 * The fact that they did not yet exist is never cached. */
1623 if (is_packed)
1624 {

--- 6 unchanged lines hidden (view full) ---

1631 scratch_pool),
1632 &kind, scratch_pool));
1633 bump_generation = kind != svn_node_none;
1634 }
1635
1636 /* Serialize the new revprop data */
1637 if (is_packed)
1638 SVN_ERR(write_packed_revprop(&final_path, &tmp_path, &files_to_delete,
1401 /* this info will not change while we hold the global FS write lock */
1402 is_packed = svn_fs_x__is_packed_revprop(fs, rev);
1403
1404 /* Test whether revprops already exist for this revision.
1405 * Only then will we need to bump the revprop generation.
1406 * The fact that they did not yet exist is never cached. */
1407 if (is_packed)
1408 {

--- 6 unchanged lines hidden (view full) ---

1415 scratch_pool),
1416 &kind, scratch_pool));
1417 bump_generation = kind != svn_node_none;
1418 }
1419
1420 /* Serialize the new revprop data */
1421 if (is_packed)
1422 SVN_ERR(write_packed_revprop(&final_path, &tmp_path, &files_to_delete,
1639 fs, rev, proplist, scratch_pool,
1423 fs, rev, proplist, batch, scratch_pool,
1640 scratch_pool));
1641 else
1642 SVN_ERR(write_non_packed_revprop(&final_path, &tmp_path,
1424 scratch_pool));
1425 else
1426 SVN_ERR(write_non_packed_revprop(&final_path, &tmp_path,
1643 fs, rev, proplist, scratch_pool,
1644 scratch_pool));
1427 fs, rev, proplist, batch,
1428 scratch_pool, scratch_pool));
1645
1646 /* We use the rev file of this revision as the perms reference,
1647 * because when setting revprops for the first time, the revprop
1648 * file won't exist and therefore can't serve as its own reference.
1649 * (Whereas the rev file should already exist at this point.)
1650 */
1651 perms_reference = svn_fs_x__path_rev_absolute(fs, rev, scratch_pool);
1652
1653 /* Now, switch to the new revprop data. */
1654 SVN_ERR(switch_to_new_revprop(fs, final_path, tmp_path, perms_reference,
1429
1430 /* We use the rev file of this revision as the perms reference,
1431 * because when setting revprops for the first time, the revprop
1432 * file won't exist and therefore can't serve as its own reference.
1433 * (Whereas the rev file should already exist at this point.)
1434 */
1435 perms_reference = svn_fs_x__path_rev_absolute(fs, rev, scratch_pool);
1436
1437 /* Now, switch to the new revprop data. */
1438 SVN_ERR(switch_to_new_revprop(fs, final_path, tmp_path, perms_reference,
1655 files_to_delete, bump_generation,
1439 files_to_delete, bump_generation, batch,
1656 scratch_pool));
1657
1658 return SVN_NO_ERROR;
1659}
1660
1661/* Return TRUE, if for REVISION in FS, we can find the revprop pack file.
1662 * Use SCRATCH_POOL for temporary allocations.
1663 * Set *MISSING, if the reason is a missing manifest or pack file.
1664 */
1665svn_boolean_t
1666svn_fs_x__packed_revprop_available(svn_boolean_t *missing,
1667 svn_fs_t *fs,
1668 svn_revnum_t revision,
1669 apr_pool_t *scratch_pool)
1670{
1440 scratch_pool));
1441
1442 return SVN_NO_ERROR;
1443}
1444
1445/* Return TRUE, if for REVISION in FS, we can find the revprop pack file.
1446 * Use SCRATCH_POOL for temporary allocations.
1447 * Set *MISSING, if the reason is a missing manifest or pack file.
1448 */
1449svn_boolean_t
1450svn_fs_x__packed_revprop_available(svn_boolean_t *missing,
1451 svn_fs_t *fs,
1452 svn_revnum_t revision,
1453 apr_pool_t *scratch_pool)
1454{
1671 svn_fs_x__data_t *ffd = fs->fsap_data;
1672 svn_stringbuf_t *content = NULL;
1455 svn_node_kind_t kind;
1456 packed_revprops_t *revprops;
1457 svn_error_t *err;
1673
1674 /* try to read the manifest file */
1458
1459 /* try to read the manifest file */
1675 const char *folder = svn_fs_x__path_revprops_pack_shard(fs, revision,
1676 scratch_pool);
1677 const char *manifest_path = svn_dirent_join(folder, PATH_MANIFEST,
1678 scratch_pool);
1460 revprops = apr_pcalloc(scratch_pool, sizeof(*revprops));
1461 revprops->revision = revision;
1462 err = get_revprop_packname(fs, revprops, scratch_pool, scratch_pool);
1679
1463
1680 svn_error_t *err = svn_fs_x__try_stringbuf_from_file(&content,
1681 missing,
1682 manifest_path,
1683 FALSE,
1684 scratch_pool);
1685
1686 /* if the manifest cannot be read, consider the pack files inaccessible
1687 * even if the file itself exists. */
1688 if (err)
1689 {
1690 svn_error_clear(err);
1691 return FALSE;
1692 }
1693
1464 /* if the manifest cannot be read, consider the pack files inaccessible
1465 * even if the file itself exists. */
1466 if (err)
1467 {
1468 svn_error_clear(err);
1469 return FALSE;
1470 }
1471
1694 if (*missing)
1695 return FALSE;
1696
1697 /* parse manifest content until we find the entry for REVISION.
1698 * Revision 0 is never packed. */
1699 revision = revision < ffd->max_files_per_dir
1700 ? revision - 1
1701 : revision % ffd->max_files_per_dir;
1702 while (content->data)
1472 /* the respective pack file must exist (and be a file) */
1473 err = svn_io_check_path(get_revprop_pack_filepath(revprops,
1474 &revprops->entry,
1475 scratch_pool),
1476 &kind, scratch_pool);
1477 if (err)
1703 {
1478 {
1704 char *next = strchr(content->data, '\n');
1705 if (next)
1706 {
1707 *next = 0;
1708 ++next;
1709 }
1710
1711 if (revision-- == 0)
1712 {
1713 /* the respective pack file must exist (and be a file) */
1714 svn_node_kind_t kind;
1715 err = svn_io_check_path(svn_dirent_join(folder, content->data,
1716 scratch_pool),
1717 &kind, scratch_pool);
1718 if (err)
1719 {
1720 svn_error_clear(err);
1721 return FALSE;
1722 }
1723
1724 *missing = kind == svn_node_none;
1725 return kind == svn_node_file;
1726 }
1727
1728 content->data = next;
1479 svn_error_clear(err);
1480 return FALSE;
1729 }
1730
1481 }
1482
1731 return FALSE;
1483 *missing = kind == svn_node_none;
1484 return kind == svn_node_file;
1732}
1733
1734
1735/****** Packing FSX shards *********/
1736
1485}
1486
1487
1488/****** Packing FSX shards *********/
1489
1737svn_error_t *
1738svn_fs_x__copy_revprops(const char *pack_file_dir,
1739 const char *pack_filename,
1740 const char *shard_path,
1741 svn_revnum_t start_rev,
1742 svn_revnum_t end_rev,
1743 apr_array_header_t *sizes,
1744 apr_size_t total_size,
1745 int compression_level,
1746 svn_cancel_func_t cancel_func,
1747 void *cancel_baton,
1748 apr_pool_t *scratch_pool)
1490/* Copy revprop files for revisions [START_REV, END_REV) from SHARD_PATH
1491 * in filesystem FS to the pack file at PACK_FILE_NAME in PACK_FILE_DIR.
1492 *
1493 * The file sizes have already been determined and written to SIZES.
1494 * Please note that this function will be executed while the filesystem
1495 * has been locked and that revprops files will therefore not be modified
1496 * while the pack is in progress.
1497 *
1498 * COMPRESSION_LEVEL defines how well the resulting pack file shall be
1499 * compressed or whether is shall be compressed at all. TOTAL_SIZE is
1500 * a hint on which initial buffer size we should use to hold the pack file
1501 * content. Schedule necessary fsync calls in BATCH.
1502 *
1503 * CANCEL_FUNC and CANCEL_BATON are used as usual. Temporary allocations
1504 * are done in SCRATCH_POOL.
1505 */
1506static svn_error_t *
1507copy_revprops(svn_fs_t *fs,
1508 const char *pack_file_dir,
1509 const char *pack_filename,
1510 const char *shard_path,
1511 svn_revnum_t start_rev,
1512 svn_revnum_t end_rev,
1513 apr_array_header_t *sizes,
1514 apr_size_t total_size,
1515 int compression_level,
1516 svn_fs_x__batch_fsync_t *batch,
1517 svn_cancel_func_t cancel_func,
1518 void *cancel_baton,
1519 apr_pool_t *scratch_pool)
1749{
1520{
1750 svn_stream_t *pack_stream;
1751 apr_file_t *pack_file;
1752 svn_revnum_t rev;
1753 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1521 apr_file_t *pack_file;
1522 svn_revnum_t rev;
1523 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1754 svn_stream_t *stream;
1755
1524
1756 /* create empty data buffer and a write stream on top of it */
1757 svn_stringbuf_t *uncompressed
1758 = svn_stringbuf_create_ensure(total_size, scratch_pool);
1759 svn_stringbuf_t *compressed
1760 = svn_stringbuf_create_empty(scratch_pool);
1761 pack_stream = svn_stream_from_stringbuf(uncompressed, scratch_pool);
1525 svn_packed__data_root_t *root = svn_packed__data_create_root(scratch_pool);
1526 svn_packed__byte_stream_t *stream
1527 = svn_packed__create_bytes_stream(root);
1762
1528
1763 /* write the pack file header */
1764 SVN_ERR(serialize_revprops_header(pack_stream, start_rev, sizes, 0,
1765 sizes->nelts, iterpool));
1766
1767 /* Some useful paths. */
1768 SVN_ERR(svn_io_file_open(&pack_file, svn_dirent_join(pack_file_dir,
1769 pack_filename,
1770 scratch_pool),
1771 APR_WRITE | APR_CREATE, APR_OS_DEFAULT,
1772 scratch_pool));
1773
1774 /* Iterate over the revisions in this shard, squashing them together. */
1775 for (rev = start_rev; rev <= end_rev; rev++)
1776 {
1777 const char *path;
1529 /* Iterate over the revisions in this shard, squashing them together. */
1530 for (rev = start_rev; rev <= end_rev; rev++)
1531 {
1532 const char *path;
1533 svn_stringbuf_t *props;
1778
1779 svn_pool_clear(iterpool);
1780
1781 /* Construct the file name. */
1534
1535 svn_pool_clear(iterpool);
1536
1537 /* Construct the file name. */
1782 path = svn_dirent_join(shard_path, apr_psprintf(iterpool, "%ld", rev),
1783 iterpool);
1538 path = svn_fs_x__path_revprops(fs, rev, iterpool);
1784
1785 /* Copy all the bits from the non-packed revprop file to the end of
1786 * the pack file. */
1539
1540 /* Copy all the bits from the non-packed revprop file to the end of
1541 * the pack file. */
1787 SVN_ERR(svn_stream_open_readonly(&stream, path, iterpool, iterpool));
1788 SVN_ERR(svn_stream_copy3(stream, pack_stream,
1789 cancel_func, cancel_baton, iterpool));
1542 SVN_ERR(svn_stringbuf_from_file2(&props, path, iterpool));
1543 SVN_ERR_W(verify_checksum(props, iterpool),
1544 apr_psprintf(iterpool, "Failed to read revprops for r%ld.",
1545 rev));
1546
1547 svn_packed__add_bytes(stream, props->data, props->len);
1790 }
1791
1548 }
1549
1792 /* flush stream buffers to content buffer */
1793 SVN_ERR(svn_stream_close(pack_stream));
1550 /* Create the auto-fsync'ing pack file. */
1551 SVN_ERR(svn_fs_x__batch_fsync_open_file(&pack_file, batch,
1552 svn_dirent_join(pack_file_dir,
1553 pack_filename,
1554 scratch_pool),
1555 scratch_pool));
1794
1556
1795 /* compress the content (or just store it for COMPRESSION_LEVEL 0) */
1796 SVN_ERR(svn__compress(uncompressed, compressed, compression_level));
1557 /* write all to disk */
1558 SVN_ERR(write_packed_data_checksummed(root, pack_file, scratch_pool));
1797
1559
1798 /* write the pack file content to disk */
1799 stream = svn_stream_from_aprfile2(pack_file, FALSE, scratch_pool);
1800 SVN_ERR(svn_stream_write(stream, compressed->data, &compressed->len));
1801 SVN_ERR(svn_stream_close(stream));
1802
1803 svn_pool_destroy(iterpool);
1804
1805 return SVN_NO_ERROR;
1806}
1807
1808svn_error_t *
1560 svn_pool_destroy(iterpool);
1561
1562 return SVN_NO_ERROR;
1563}
1564
1565svn_error_t *
1809svn_fs_x__pack_revprops_shard(const char *pack_file_dir,
1566svn_fs_x__pack_revprops_shard(svn_fs_t *fs,
1567 const char *pack_file_dir,
1810 const char *shard_path,
1811 apr_int64_t shard,
1812 int max_files_per_dir,
1568 const char *shard_path,
1569 apr_int64_t shard,
1570 int max_files_per_dir,
1813 apr_off_t max_pack_size,
1571 apr_int64_t max_pack_size,
1814 int compression_level,
1572 int compression_level,
1573 svn_fs_x__batch_fsync_t *batch,
1815 svn_cancel_func_t cancel_func,
1816 void *cancel_baton,
1817 apr_pool_t *scratch_pool)
1818{
1819 const char *manifest_file_path, *pack_filename = NULL;
1574 svn_cancel_func_t cancel_func,
1575 void *cancel_baton,
1576 apr_pool_t *scratch_pool)
1577{
1578 const char *manifest_file_path, *pack_filename = NULL;
1820 svn_stream_t *manifest_stream;
1579 apr_file_t *manifest_file;
1821 svn_revnum_t start_rev, end_rev, rev;
1580 svn_revnum_t start_rev, end_rev, rev;
1822 apr_off_t total_size;
1581 apr_size_t total_size;
1823 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1824 apr_array_header_t *sizes;
1582 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1583 apr_array_header_t *sizes;
1584 apr_array_header_t *manifest;
1825
1585
1586 /* Sanitize config file values. */
1587 apr_size_t max_size = (apr_size_t)MIN(MAX(max_pack_size, 1),
1588 SVN_MAX_OBJECT_SIZE);
1589
1826 /* Some useful paths. */
1827 manifest_file_path = svn_dirent_join(pack_file_dir, PATH_MANIFEST,
1828 scratch_pool);
1829
1590 /* Some useful paths. */
1591 manifest_file_path = svn_dirent_join(pack_file_dir, PATH_MANIFEST,
1592 scratch_pool);
1593
1830 /* Remove any existing pack file for this shard, since it is incomplete. */
1831 SVN_ERR(svn_io_remove_dir2(pack_file_dir, TRUE, cancel_func, cancel_baton,
1832 scratch_pool));
1594 /* Create the manifest file. */
1595 SVN_ERR(svn_fs_x__batch_fsync_open_file(&manifest_file, batch,
1596 manifest_file_path, scratch_pool));
1833
1597
1834 /* Create the new directory and manifest file stream. */
1835 SVN_ERR(svn_io_dir_make(pack_file_dir, APR_OS_DEFAULT, scratch_pool));
1836 SVN_ERR(svn_stream_open_writable(&manifest_stream, manifest_file_path,
1837 scratch_pool, scratch_pool));
1838
1839 /* revisions to handle. Special case: revision 0 */
1840 start_rev = (svn_revnum_t) (shard * max_files_per_dir);
1841 end_rev = (svn_revnum_t) ((shard + 1) * (max_files_per_dir) - 1);
1842 if (start_rev == 0)
1598 /* revisions to handle. Special case: revision 0 */
1599 start_rev = (svn_revnum_t) (shard * max_files_per_dir);
1600 end_rev = (svn_revnum_t) ((shard + 1) * (max_files_per_dir) - 1);
1601 if (start_rev == 0)
1843 ++start_rev;
1844 /* Special special case: if max_files_per_dir is 1, then at this point
1845 start_rev == 1 and end_rev == 0 (!). Fortunately, everything just
1846 works. */
1602 {
1603 /* Never pack revprops for r0, just copy it. */
1604 SVN_ERR(svn_io_copy_file(svn_fs_x__path_revprops(fs, 0, iterpool),
1605 svn_dirent_join(pack_file_dir, "p0",
1606 scratch_pool),
1607 TRUE,
1608 iterpool));
1847
1609
1610 ++start_rev;
1611 /* Special special case: if max_files_per_dir is 1, then at this point
1612 start_rev == 1 and end_rev == 0 (!). Fortunately, everything just
1613 works. */
1614 }
1615
1848 /* initialize the revprop size info */
1616 /* initialize the revprop size info */
1849 sizes = apr_array_make(scratch_pool, max_files_per_dir, sizeof(apr_off_t));
1617 sizes = apr_array_make(scratch_pool, max_files_per_dir, sizeof(apr_size_t));
1850 total_size = 2 * SVN_INT64_BUFFER_SIZE;
1851
1618 total_size = 2 * SVN_INT64_BUFFER_SIZE;
1619
1620 manifest = apr_array_make(scratch_pool, 4, sizeof(manifest_entry_t));
1621
1852 /* Iterate over the revisions in this shard, determine their size and
1853 * squashing them together into pack files. */
1854 for (rev = start_rev; rev <= end_rev; rev++)
1855 {
1856 apr_finfo_t finfo;
1857 const char *path;
1858
1859 svn_pool_clear(iterpool);
1860
1861 /* Get the size of the file. */
1622 /* Iterate over the revisions in this shard, determine their size and
1623 * squashing them together into pack files. */
1624 for (rev = start_rev; rev <= end_rev; rev++)
1625 {
1626 apr_finfo_t finfo;
1627 const char *path;
1628
1629 svn_pool_clear(iterpool);
1630
1631 /* Get the size of the file. */
1862 path = svn_dirent_join(shard_path, apr_psprintf(iterpool, "%ld", rev),
1863 iterpool);
1632 path = svn_fs_x__path_revprops(fs, rev, iterpool);
1864 SVN_ERR(svn_io_stat(&finfo, path, APR_FINFO_SIZE, iterpool));
1865
1633 SVN_ERR(svn_io_stat(&finfo, path, APR_FINFO_SIZE, iterpool));
1634
1866 /* if we already have started a pack file and this revprop cannot be
1867 * appended to it, write the previous pack file. */
1868 if (sizes->nelts != 0 &&
1869 total_size + SVN_INT64_BUFFER_SIZE + finfo.size > max_pack_size)
1635 /* If we already have started a pack file and this revprop cannot be
1636 * appended to it, write the previous pack file. Note this overflow
1637 * check works because we enforced MAX_SIZE <= SVN_MAX_OBJECT_SIZE. */
1638 if (sizes->nelts != 0
1639 && ( finfo.size > max_size
1640 || total_size > max_size
1641 || SVN_INT64_BUFFER_SIZE + finfo.size > max_size - total_size))
1870 {
1642 {
1871 SVN_ERR(svn_fs_x__copy_revprops(pack_file_dir, pack_filename,
1872 shard_path, start_rev, rev-1,
1873 sizes, (apr_size_t)total_size,
1874 compression_level, cancel_func,
1875 cancel_baton, iterpool));
1643 SVN_ERR(copy_revprops(fs, pack_file_dir, pack_filename,
1644 shard_path, start_rev, rev-1,
1645 sizes, (apr_size_t)total_size,
1646 compression_level, batch, cancel_func,
1647 cancel_baton, iterpool));
1876
1877 /* next pack file starts empty again */
1878 apr_array_clear(sizes);
1879 total_size = 2 * SVN_INT64_BUFFER_SIZE;
1880 start_rev = rev;
1881 }
1882
1883 /* Update the manifest. Allocate a file name for the current pack
1884 * file if it is a new one */
1885 if (sizes->nelts == 0)
1648
1649 /* next pack file starts empty again */
1650 apr_array_clear(sizes);
1651 total_size = 2 * SVN_INT64_BUFFER_SIZE;
1652 start_rev = rev;
1653 }
1654
1655 /* Update the manifest. Allocate a file name for the current pack
1656 * file if it is a new one */
1657 if (sizes->nelts == 0)
1886 pack_filename = apr_psprintf(scratch_pool, "%ld.0", rev);
1658 {
1659 manifest_entry_t *entry = apr_array_push(manifest);
1660 entry->start_rev = rev;
1661 entry->tag = 0;
1887
1662
1888 SVN_ERR(svn_stream_printf(manifest_stream, iterpool, "%s\n",
1889 pack_filename));
1663 pack_filename = apr_psprintf(scratch_pool, "%ld.0", rev);
1664 }
1890
1891 /* add to list of files to put into the current pack file */
1665
1666 /* add to list of files to put into the current pack file */
1892 APR_ARRAY_PUSH(sizes, apr_off_t) = finfo.size;
1667 APR_ARRAY_PUSH(sizes, apr_size_t) = finfo.size;
1893 total_size += SVN_INT64_BUFFER_SIZE + finfo.size;
1894 }
1895
1896 /* write the last pack file */
1897 if (sizes->nelts != 0)
1668 total_size += SVN_INT64_BUFFER_SIZE + finfo.size;
1669 }
1670
1671 /* write the last pack file */
1672 if (sizes->nelts != 0)
1898 SVN_ERR(svn_fs_x__copy_revprops(pack_file_dir, pack_filename, shard_path,
1899 start_rev, rev-1, sizes,
1900 (apr_size_t)total_size, compression_level,
1901 cancel_func, cancel_baton, iterpool));
1673 SVN_ERR(copy_revprops(fs, pack_file_dir, pack_filename, shard_path,
1674 start_rev, rev-1, sizes,
1675 (apr_size_t)total_size, compression_level,
1676 batch, cancel_func, cancel_baton, iterpool));
1902
1677
1903 /* flush the manifest file and update permissions */
1904 SVN_ERR(svn_stream_close(manifest_stream));
1905 SVN_ERR(svn_io_copy_perms(shard_path, pack_file_dir, iterpool));
1678 SVN_ERR(write_manifest(manifest_file, manifest, iterpool));
1906
1679
1680 /* flush all data to disk and update permissions */
1681 SVN_ERR(svn_io_copy_perms(shard_path, pack_file_dir, iterpool));
1907 svn_pool_destroy(iterpool);
1908
1909 return SVN_NO_ERROR;
1910}
1682 svn_pool_destroy(iterpool);
1683
1684 return SVN_NO_ERROR;
1685}
1911
1912svn_error_t *
1913svn_fs_x__delete_revprops_shard(const char *shard_path,
1914 apr_int64_t shard,
1915 int max_files_per_dir,
1916 svn_cancel_func_t cancel_func,
1917 void *cancel_baton,
1918 apr_pool_t *scratch_pool)
1919{
1920 if (shard == 0)
1921 {
1922 apr_pool_t *iterpool = svn_pool_create(scratch_pool);
1923 int i;
1924
1925 /* delete all files except the one for revision 0 */
1926 for (i = 1; i < max_files_per_dir; ++i)
1927 {
1928 const char *path;
1929 svn_pool_clear(iterpool);
1930
1931 path = svn_dirent_join(shard_path,
1932 apr_psprintf(iterpool, "%d", i),
1933 iterpool);
1934 if (cancel_func)
1935 SVN_ERR((*cancel_func)(cancel_baton));
1936
1937 SVN_ERR(svn_io_remove_file2(path, TRUE, iterpool));
1938 }
1939
1940 svn_pool_destroy(iterpool);
1941 }
1942 else
1943 SVN_ERR(svn_io_remove_dir2(shard_path, TRUE,
1944 cancel_func, cancel_baton, scratch_pool));
1945
1946 return SVN_NO_ERROR;
1947}
1948