Skip to content

Commit 06367df

Browse files
author
Atsushi Abe
authored
Change the sync behavior to fit the LTFS format Spec 2.5 (#461)
1 parent d19dfcf commit 06367df

File tree

9 files changed

+129
-41
lines changed

9 files changed

+129
-41
lines changed

src/iosched/unified.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,7 @@ int _unified_write_index_after_perm(int write_ret, struct unified_data *priv)
22912291
return ret;
22922292
}
22932293

2294+
ltfs_set_commit_message_reason(SYNC_WRITE_PERM, priv->vol);
22942295
ret = ltfs_write_index(ltfs_ip_id(priv->vol), SYNC_WRITE_PERM, priv->vol);
22952296

22962297
return ret;

src/libltfs/ltfs.c

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
17791779
}
17801780
INTERRUPTED_GOTO(ret, out_unlock);
17811781
ltfsmsg(LTFS_INFO, 11022I);
1782+
ltfs_set_commit_message_reason(SYNC_RECOVERY, vol);
17821783
ret = ltfs_write_index(vol->label->partid_ip, SYNC_RECOVERY, vol);
17831784
if (ret < 0)
17841785
goto out_unlock;
@@ -1864,13 +1865,6 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
18641865
if (vol->index->uid_number == 0)
18651866
ltfsmsg(LTFS_WARN, 11307W, vol->label->vol_uuid);
18661867

1867-
/* Clear the commit message so it doesn't carry over from the previous session */
1868-
/* TODO: is this the right place to clear the commit message? */
1869-
if (vol->index->commit_message) {
1870-
free(vol->index->commit_message);
1871-
vol->index->commit_message = NULL;
1872-
}
1873-
18741868
/* If we reach this point, both partitions end in an index file. */
18751869
vol->ip_index_file_end = true;
18761870
vol->dp_index_file_end = true;
@@ -2535,6 +2529,7 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol)
25352529

25362530
write_perm = true;
25372531
reason = SYNC_WRITE_PERM;
2532+
ltfs_set_commit_message_reason(SYNC_WRITE_PERM, vol);
25382533
}
25392534
/* ignore return value: we want to keep trying even if, e.g., the DP fills up */
25402535
}
@@ -3137,6 +3132,7 @@ int ltfs_format_tape(struct ltfs_volume *vol, int density_code, bool destructive
31373132
if (ret < 0)
31383133
return ret;
31393134
ltfsmsg(LTFS_INFO, 11278I, vol->label->partid_dp); /* "Writing Index to ..." */
3135+
ltfs_set_commit_message_reason(SYNC_FORMAT, vol);
31403136
ret = ltfs_write_index(vol->label->partid_dp, SYNC_FORMAT, vol);
31413137
if (ret < 0) {
31423138
ltfsmsg(LTFS_ERR, 11279E, vol->label->partid_dp, ret);
@@ -3633,6 +3629,7 @@ int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol)
36333629
* For now, write the same index on IP and return an error against a sync
36343630
* request.
36353631
*/
3632+
ltfs_set_commit_message_reason(SYNC_WRITE_PERM, vol);
36363633
ret_r = ltfs_write_index(ltfs_ip_id(vol), SYNC_WRITE_PERM, vol);
36373634
if (!ret_r) {
36383635
ltfsmsg(LTFS_INFO, 11344I, bc_print);
@@ -4634,3 +4631,61 @@ int ltfs_build_fullpath(char **dest, struct dentry *d)
46344631

46354632
return ret;
46364633
}
4634+
4635+
/**
4636+
* Update commit message of index to the prefixed message based on sync reason.
4637+
* @param reason sync reason defined in ltfs.h
4638+
* @param vol LTFS colume
4639+
*/
4640+
void ltfs_set_commit_message_reason(char *reason, struct ltfs_volume *vol)
4641+
{
4642+
ltfs_mutex_lock(&vol->index->dirty_lock);
4643+
ltfs_set_commit_message_reason_unlocked(reason, vol);
4644+
ltfs_mutex_unlock(&vol->index->dirty_lock);
4645+
4646+
return;
4647+
}
4648+
4649+
/**
4650+
* Update commit message of index to the prefixed message based on sync reason. Caller need to
4651+
* take index->dirty_lock before calling this function.
4652+
* @param reason sync reason defined in ltfs.h
4653+
* @param vol LTFS volume
4654+
*/
4655+
void ltfs_set_commit_message_reason_unlocked(char *reason, struct ltfs_volume *vol)
4656+
{
4657+
bool dirty = false;
4658+
int ret = 0;
4659+
char *str_now = NULL, *msg = NULL;
4660+
struct ltfs_timespec now;
4661+
4662+
if (!vol->index->dirty) {
4663+
/* Do nothing because no update was made */
4664+
goto out;
4665+
}
4666+
4667+
if (vol->index->commit_message) {
4668+
free(vol->index->commit_message);
4669+
dirty = true;
4670+
}
4671+
4672+
ret = get_current_timespec(&now);
4673+
if (ret)
4674+
goto out;
4675+
4676+
ret = xml_format_time(now, &str_now);
4677+
if (ret)
4678+
goto out;
4679+
4680+
ret = asprintf(&msg, "%s - %s", reason, str_now);
4681+
if (ret) {
4682+
vol->index->commit_message = msg;
4683+
dirty = true;
4684+
}
4685+
4686+
out:
4687+
if (dirty)
4688+
ltfs_set_index_dirty(false, false, vol->index);
4689+
4690+
return;
4691+
}

src/libltfs/ltfs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ int ltfs_profiler_set(uint64_t source, struct ltfs_volume *vol);
730730
int ltfs_get_rao_list(char *path, struct ltfs_volume *vol);
731731
int ltfs_build_fullpath(char **dest, struct dentry *d);
732732

733+
void ltfs_set_commit_message_reason(char *reason, struct ltfs_volume *vol);
734+
void ltfs_set_commit_message_reason_unlocked(char *reason, struct ltfs_volume *vol);
735+
733736
#ifdef __cplusplus
734737
}
735738
#endif

src/libltfs/ltfs_fsops.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,10 @@ int ltfs_fsops_volume_sync(char *reason, struct ltfs_volume *vol)
21062106
if (ret < 0)
21072107
return ret;
21082108

2109+
ltfs_mutex_lock(&vol->index->dirty_lock);
2110+
ltfs_set_commit_message_reason_unlocked(reason, vol);
2111+
ltfs_mutex_unlock(&vol->index->dirty_lock);
2112+
21092113
ret = ltfs_sync_index(reason, true, vol);
21102114

21112115
return ret;

src/libltfs/ltfs_internal.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ int ltfs_check_medium(bool fix, bool deep, bool recover_extra, bool recover_syml
13231323
}
13241324
}
13251325
/* write to data partition if it doesn't end in an index file */
1326+
ltfs_set_commit_message_reason(SYNC_RECOVERY, vol);
13261327
if (! dp_have_index || dp_blocks_after) {
13271328
ltfsmsg(LTFS_INFO, 17259I, "DP", vol->index->selfptr.partition, (unsigned long long)vol->index->selfptr.block);
13281329
ret = ltfs_write_index(vol->label->partid_dp, SYNC_RECOVERY, vol);
@@ -1407,12 +1408,15 @@ int ltfs_write_index_conditional(char partition, struct ltfs_volume *vol)
14071408

14081409
CHECK_ARG_NULL(vol, -LTFS_NULL_ARG);
14091410

1410-
if (partition == ltfs_ip_id(vol) && ! vol->ip_index_file_end)
1411+
if (partition == ltfs_ip_id(vol) && ! vol->ip_index_file_end) {
1412+
ltfs_set_commit_message_reason(SYNC_CASCHE_PRESSURE, vol);
14111413
ret = ltfs_write_index(partition, SYNC_CASCHE_PRESSURE, vol);
1412-
else if (partition == ltfs_dp_id(vol) &&
1414+
} else if (partition == ltfs_dp_id(vol) &&
14131415
(! vol->dp_index_file_end ||
1414-
(vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol))))
1416+
(vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol)))) {
1417+
ltfs_set_commit_message_reason(SYNC_CASCHE_PRESSURE, vol);
14151418
ret = ltfs_write_index(partition, SYNC_CASCHE_PRESSURE, vol);
1419+
}
14161420

14171421
return ret;
14181422
}

src/libltfs/periodic_sync.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ ltfs_thread_return periodic_sync_thread(void* data)
101101
ltfsmsg(LTFS_WARN, 17063W, __FUNCTION__);
102102
}
103103

104+
ltfs_set_commit_message_reason(SYNC_PERIODIC, priv->vol);
104105
ret = ltfs_sync_index(SYNC_PERIODIC, true, priv->vol);
105106
if (ret < 0) {
106107
ltfsmsg(LTFS_INFO, 11030I, ret);

src/libltfs/xattr.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ static int _xattr_get_virtual(struct dentry *d, char *buf, size_t buf_size, cons
881881
ret = _xattr_get_vendorunique_xattr(&val, name, vol);
882882
}
883883
} else if (! strcmp(name, "ltfs.sync")) {
884+
ltfs_set_commit_message_reason(SYNC_EA, vol);
884885
ret = ltfs_sync_index(SYNC_EA, false, vol);
885886
}
886887
}
@@ -915,9 +916,8 @@ static int _xattr_set_virtual(struct dentry *d, const char *name, const char *va
915916
{
916917
int ret = 0;
917918

918-
if (! strcmp(name, "ltfs.sync") && d == vol->index->root)
919-
ret = ltfs_sync_index(SYNC_EA, false, vol);
920-
else if (! strcmp(name, "ltfs.commitMessage") && d == vol->index->root) {
919+
if ((! strcmp(name, "ltfs.commitMessage") || ! strcmp(name, "ltfs.sync"))
920+
&& d == vol->index->root) {
921921
char *value_null_terminated, *new_value;
922922

923923
if (size > INDEX_MAX_COMMENT_LEN) {
@@ -926,38 +926,49 @@ static int _xattr_set_virtual(struct dentry *d, const char *name, const char *va
926926
}
927927

928928
ltfs_mutex_lock(&vol->index->dirty_lock);
929-
if (! value || ! size) {
930-
/* Clear the current comment field */
931-
if (vol->index->commit_message) {
932-
free(vol->index->commit_message);
933-
vol->index->commit_message = NULL;
934-
}
929+
if (! vol->index->dirty) {
930+
/* Do nothing because index is clean */
931+
ret = 0;
935932
} else {
936-
value_null_terminated = malloc(size + 1);
937-
if (! value_null_terminated) {
938-
ltfsmsg(LTFS_ERR, 10001E, "_xattr_set_virtual: commit_message");
939-
ltfs_mutex_unlock(&vol->index->dirty_lock);
940-
return -LTFS_NO_MEMORY;
941-
}
942-
memcpy(value_null_terminated, value, size);
943-
value_null_terminated[size] = '\0';
933+
if (! value || ! size) {
934+
/* Clear the current comment field */
935+
if (vol->index->commit_message) {
936+
free(vol->index->commit_message);
937+
vol->index->commit_message = NULL;
938+
}
939+
} else {
940+
value_null_terminated = malloc(size + 1);
941+
if (! value_null_terminated) {
942+
ltfsmsg(LTFS_ERR, 10001E, "_xattr_set_virtual: commit_message");
943+
ltfs_mutex_unlock(&vol->index->dirty_lock);
944+
return -LTFS_NO_MEMORY;
945+
}
946+
memcpy(value_null_terminated, value, size);
947+
value_null_terminated[size] = '\0';
944948

945-
ret = pathname_format(value_null_terminated, &new_value, false, true);
946-
free(value_null_terminated);
947-
if (ret < 0) {
948-
ltfs_mutex_unlock(&vol->index->dirty_lock);
949-
return ret;
949+
ret = pathname_format(value_null_terminated, &new_value, false, true);
950+
free(value_null_terminated);
951+
if (ret < 0) {
952+
/* Try to sync index even if the value is not valid */
953+
ltfs_set_commit_message_reason_unlocked(SYNC_EA, vol);
954+
ltfs_mutex_unlock(&vol->index->dirty_lock);
955+
956+
ret = ltfs_sync_index(SYNC_EA, false, vol);
957+
return ret;
958+
}
959+
ret = 0;
960+
961+
/* Update THE commit message in the index */
962+
if (vol->index->commit_message)
963+
free(vol->index->commit_message);
964+
vol->index->commit_message = new_value;
950965
}
951-
ret = 0;
952966

953-
/* Update the commit message in the index */
954-
if (vol->index->commit_message)
955-
free(vol->index->commit_message);
956-
vol->index->commit_message = new_value;
967+
ltfs_set_index_dirty(false, false, vol->index);
957968
}
958969

959-
ltfs_set_index_dirty(false, false, vol->index);
960970
ltfs_mutex_unlock(&vol->index->dirty_lock);
971+
ret = ltfs_sync_index(SYNC_EA, false, vol);
961972

962973
} else if (! strcmp(name, "ltfs.volumeName") && d == vol->index->root) {
963974
char *value_null_terminated, *new_value;
@@ -1228,6 +1239,8 @@ static int _xattr_set_virtual(struct dentry *d, const char *name, const char *va
12281239
vol->lock_status = new;
12291240

12301241
ltfs_set_index_dirty(false, false, vol->index);
1242+
ltfs_set_commit_message_reason_unlocked(SYNC_ADV_LOCK, vol);
1243+
12311244
ret = ltfs_sync_index(SYNC_ADV_LOCK, false, vol);
12321245
ret = tape_device_lock(vol->device);
12331246
if (ret < 0) {
@@ -1486,9 +1499,10 @@ int xattr_set(struct dentry *d, const char *name, const char *value, size_t size
14861499

14871500
releasewrite_mrsw(&d->meta_lock);
14881501

1489-
if (write_idx)
1502+
if (write_idx) {
1503+
ltfs_set_commit_message_reason(SYNC_EA, vol);
14901504
ret = ltfs_sync_index(SYNC_EA, false, vol);
1491-
else
1505+
} else
14921506
ret = 0;
14931507

14941508
out_unlock:

src/ltfs_fuse.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,10 @@ int ltfs_fuse_release(const char *path, struct fuse_file_info *fi)
450450

451451
open_write = (((fi->flags & O_WRONLY) == O_WRONLY) || ((fi->flags & O_RDWR) == O_RDWR));
452452
ret = ltfs_fsops_close(file->file_info->dentry_handle, dirty, open_write, true, priv->data);
453-
if (write_index)
453+
if (write_index) {
454+
ltfs_set_commit_message_reason(SYNC_CLOSE, priv->data);
454455
ltfs_sync_index(SYNC_CLOSE, true, priv->data);
456+
}
455457

456458
_file_close(file->file_info, priv);
457459
_free_ltfs_file_handle(file);
@@ -1167,6 +1169,7 @@ void ltfs_fuse_umount(void *userdata)
11671169
if (kmi_initialized(priv->data))
11681170
kmi_destroy(priv->data);
11691171

1172+
ltfs_set_commit_message_reason(SYNC_UNMOUNT, priv->data);
11701173
ltfs_unmount(SYNC_UNMOUNT, priv->data);
11711174

11721175
ltfs_request_trace(FUSE_REQ_EXIT(REQ_UNMOUNT), 0, 0);

src/utils/ltfsck.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ int check_ltfs_volume(struct ltfs_volume *vol, struct other_check_opts *opt)
711711
return LTFSCK_UNCORRECTED;
712712
} else {
713713
print_criteria_info(vol);
714+
ltfs_set_commit_message_reason(SYNC_CHECK, vol);
714715
ltfs_unmount(SYNC_CHECK, vol);
715716
ltfsmsg(LTFS_INFO, 16022I);
716717
return LTFSCK_CORRECTED;
@@ -1174,6 +1175,7 @@ int _rollback_dp(struct ltfs_volume *vol, struct other_check_opts *opt, struct t
11741175
if (ret != LTFSCK_NO_ERRORS)
11751176
ltfsmsg(LTFS_ERR, 16055E, ret);
11761177
} else {
1178+
ltfs_set_commit_message_reason(SYNC_ROLLBACK, vol);
11771179
ret = ltfs_write_index(ltfs_dp_id(vol), SYNC_ROLLBACK, vol);
11781180
if (ret < 0) {
11791181
ltfsmsg(LTFS_ERR, 16056E, ret);
@@ -1288,6 +1290,7 @@ int rollback(struct ltfs_volume *vol, struct other_check_opts *opt)
12881290
r.current_pos = ltfs_get_index_selfpointer(vol);
12891291
ltfsmsg(LTFS_DEBUG, 16081D, ltfs_get_index_generation(vol),
12901292
(int)r.current_pos.partition, (unsigned long long)r.current_pos.block);
1293+
ltfs_set_commit_message_reason(SYNC_ROLLBACK, vol);
12911294
ltfs_unmount(SYNC_ROLLBACK, vol);
12921295
vol->index = NULL;
12931296
}

0 commit comments

Comments
 (0)