file-posix.c (1b507e55f8199eaad99744613823f6929e4d57c6) | file-posix.c (72373e40fbc7e4218061a8211384db362d3e7348) |
---|---|
1/* 2 * Block driver for RAW files (posix) 3 * 4 * Copyright (c) 2006 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights --- 161 unchanged lines hidden (view full) --- 170 uint64_t discard_nb_failed; 171 uint64_t discard_bytes_ok; 172 } stats; 173 174 PRManager *pr_mgr; 175} BDRVRawState; 176 177typedef struct BDRVRawReopenState { | 1/* 2 * Block driver for RAW files (posix) 3 * 4 * Copyright (c) 2006 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights --- 161 unchanged lines hidden (view full) --- 170 uint64_t discard_nb_failed; 171 uint64_t discard_bytes_ok; 172 } stats; 173 174 PRManager *pr_mgr; 175} BDRVRawState; 176 177typedef struct BDRVRawReopenState { |
178 int fd; | |
179 int open_flags; 180 bool drop_cache; 181 bool check_cache_dropped; 182} BDRVRawReopenState; 183 184static int fd_open(BlockDriverState *bs); 185static int64_t raw_getlength(BlockDriverState *bs); 186 --- 883 unchanged lines hidden (view full) --- 1070 1071static int raw_reopen_prepare(BDRVReopenState *state, 1072 BlockReopenQueue *queue, Error **errp) 1073{ 1074 BDRVRawState *s; 1075 BDRVRawReopenState *rs; 1076 QemuOpts *opts; 1077 int ret; | 178 int open_flags; 179 bool drop_cache; 180 bool check_cache_dropped; 181} BDRVRawReopenState; 182 183static int fd_open(BlockDriverState *bs); 184static int64_t raw_getlength(BlockDriverState *bs); 185 --- 883 unchanged lines hidden (view full) --- 1069 1070static int raw_reopen_prepare(BDRVReopenState *state, 1071 BlockReopenQueue *queue, Error **errp) 1072{ 1073 BDRVRawState *s; 1074 BDRVRawReopenState *rs; 1075 QemuOpts *opts; 1076 int ret; |
1078 Error *local_err = NULL; | |
1079 1080 assert(state != NULL); 1081 assert(state->bs != NULL); 1082 1083 s = state->bs->opaque; 1084 1085 state->opaque = g_new0(BDRVRawReopenState, 1); 1086 rs = state->opaque; --- 9 unchanged lines hidden (view full) --- 1096 rs->check_cache_dropped = 1097 qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false); 1098 1099 /* This driver's reopen function doesn't currently allow changing 1100 * other options, so let's put them back in the original QDict and 1101 * bdrv_reopen_prepare() will detect changes and complain. */ 1102 qemu_opts_to_qdict(opts, state->options); 1103 | 1077 1078 assert(state != NULL); 1079 assert(state->bs != NULL); 1080 1081 s = state->bs->opaque; 1082 1083 state->opaque = g_new0(BDRVRawReopenState, 1); 1084 rs = state->opaque; --- 9 unchanged lines hidden (view full) --- 1094 rs->check_cache_dropped = 1095 qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false); 1096 1097 /* This driver's reopen function doesn't currently allow changing 1098 * other options, so let's put them back in the original QDict and 1099 * bdrv_reopen_prepare() will detect changes and complain. */ 1100 qemu_opts_to_qdict(opts, state->options); 1101 |
1104 rs->fd = raw_reconfigure_getfd(state->bs, state->flags, &rs->open_flags, 1105 state->perm, true, &local_err); 1106 if (local_err) { 1107 error_propagate(errp, local_err); 1108 ret = -1; 1109 goto out; 1110 } | 1102 /* 1103 * As part of reopen prepare we also want to create new fd by 1104 * raw_reconfigure_getfd(). But it wants updated "perm", when in 1105 * bdrv_reopen_multiple() .bdrv_reopen_prepare() callback called prior to 1106 * permission update. Happily, permission update is always a part (a seprate 1107 * stage) of bdrv_reopen_multiple() so we can rely on this fact and 1108 * reconfigure fd in raw_check_perm(). 1109 */ |
1111 | 1110 |
1112 /* Fail already reopen_prepare() if we can't get a working O_DIRECT 1113 * alignment with the new fd. */ 1114 if (rs->fd != -1) { 1115 raw_probe_alignment(state->bs, rs->fd, &local_err); 1116 if (local_err) { 1117 error_propagate(errp, local_err); 1118 ret = -EINVAL; 1119 goto out_fd; 1120 } 1121 } 1122 | |
1123 s->reopen_state = state; 1124 ret = 0; | 1111 s->reopen_state = state; 1112 ret = 0; |
1125out_fd: 1126 if (ret < 0) { 1127 qemu_close(rs->fd); 1128 rs->fd = -1; 1129 } | 1113 |
1130out: 1131 qemu_opts_del(opts); 1132 return ret; 1133} 1134 1135static void raw_reopen_commit(BDRVReopenState *state) 1136{ 1137 BDRVRawReopenState *rs = state->opaque; 1138 BDRVRawState *s = state->bs->opaque; 1139 1140 s->drop_cache = rs->drop_cache; 1141 s->check_cache_dropped = rs->check_cache_dropped; 1142 s->open_flags = rs->open_flags; | 1114out: 1115 qemu_opts_del(opts); 1116 return ret; 1117} 1118 1119static void raw_reopen_commit(BDRVReopenState *state) 1120{ 1121 BDRVRawReopenState *rs = state->opaque; 1122 BDRVRawState *s = state->bs->opaque; 1123 1124 s->drop_cache = rs->drop_cache; 1125 s->check_cache_dropped = rs->check_cache_dropped; 1126 s->open_flags = rs->open_flags; |
1143 1144 qemu_close(s->fd); 1145 s->fd = rs->fd; 1146 | |
1147 g_free(state->opaque); 1148 state->opaque = NULL; 1149 1150 assert(s->reopen_state == state); 1151 s->reopen_state = NULL; 1152} 1153 1154 1155static void raw_reopen_abort(BDRVReopenState *state) 1156{ 1157 BDRVRawReopenState *rs = state->opaque; 1158 BDRVRawState *s = state->bs->opaque; 1159 1160 /* nothing to do if NULL, we didn't get far enough */ 1161 if (rs == NULL) { 1162 return; 1163 } 1164 | 1127 g_free(state->opaque); 1128 state->opaque = NULL; 1129 1130 assert(s->reopen_state == state); 1131 s->reopen_state = NULL; 1132} 1133 1134 1135static void raw_reopen_abort(BDRVReopenState *state) 1136{ 1137 BDRVRawReopenState *rs = state->opaque; 1138 BDRVRawState *s = state->bs->opaque; 1139 1140 /* nothing to do if NULL, we didn't get far enough */ 1141 if (rs == NULL) { 1142 return; 1143 } 1144 |
1165 if (rs->fd >= 0) { 1166 qemu_close(rs->fd); 1167 rs->fd = -1; 1168 } | |
1169 g_free(state->opaque); 1170 state->opaque = NULL; 1171 1172 assert(s->reopen_state == state); 1173 s->reopen_state = NULL; 1174} 1175 1176static int sg_get_max_transfer_length(int fd) --- 1891 unchanged lines hidden (view full) --- 3068 { /* end of list */ } 3069 } 3070}; 3071 3072static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, 3073 Error **errp) 3074{ 3075 BDRVRawState *s = bs->opaque; | 1145 g_free(state->opaque); 1146 state->opaque = NULL; 1147 1148 assert(s->reopen_state == state); 1149 s->reopen_state = NULL; 1150} 1151 1152static int sg_get_max_transfer_length(int fd) --- 1891 unchanged lines hidden (view full) --- 3044 { /* end of list */ } 3045 } 3046}; 3047 3048static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, 3049 Error **errp) 3050{ 3051 BDRVRawState *s = bs->opaque; |
3076 BDRVRawReopenState *rs = NULL; | 3052 int input_flags = s->reopen_state ? s->reopen_state->flags : bs->open_flags; |
3077 int open_flags; 3078 int ret; 3079 | 3053 int open_flags; 3054 int ret; 3055 |
3080 if (s->perm_change_fd) { | 3056 /* We may need a new fd if auto-read-only switches the mode */ 3057 ret = raw_reconfigure_getfd(bs, input_flags, &open_flags, perm, 3058 false, errp); 3059 if (ret < 0) { 3060 return ret; 3061 } else if (ret != s->fd) { 3062 Error *local_err = NULL; 3063 |
3081 /* | 3064 /* |
3082 * In the context of reopen, this function may be called several times 3083 * (directly and recursively while change permissions of the parent). 3084 * This is even true for children that don't inherit from the original 3085 * reopen node, so s->reopen_state is not set. 3086 * 3087 * Ignore all but the first call. | 3065 * Fail already check_perm() if we can't get a working O_DIRECT 3066 * alignment with the new fd. |
3088 */ | 3067 */ |
3089 return 0; 3090 } 3091 3092 if (s->reopen_state) { 3093 /* We already have a new file descriptor to set permissions for */ 3094 assert(s->reopen_state->perm == perm); 3095 assert(s->reopen_state->shared_perm == shared); 3096 rs = s->reopen_state->opaque; 3097 s->perm_change_fd = rs->fd; 3098 s->perm_change_flags = rs->open_flags; 3099 } else { 3100 /* We may need a new fd if auto-read-only switches the mode */ 3101 ret = raw_reconfigure_getfd(bs, bs->open_flags, &open_flags, perm, 3102 false, errp); 3103 if (ret < 0) { 3104 return ret; 3105 } else if (ret != s->fd) { 3106 s->perm_change_fd = ret; 3107 s->perm_change_flags = open_flags; | 3068 raw_probe_alignment(bs, ret, &local_err); 3069 if (local_err) { 3070 error_propagate(errp, local_err); 3071 return -EINVAL; |
3108 } | 3072 } |
3073 3074 s->perm_change_fd = ret; 3075 s->perm_change_flags = open_flags; |
|
3109 } 3110 3111 /* Prepare permissions on old fd to avoid conflicts between old and new, 3112 * but keep everything locked that new will need. */ 3113 ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); 3114 if (ret < 0) { 3115 goto fail; 3116 } --- 5 unchanged lines hidden (view full) --- 3122 if (ret < 0) { 3123 raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 3124 goto fail; 3125 } 3126 } 3127 return 0; 3128 3129fail: | 3076 } 3077 3078 /* Prepare permissions on old fd to avoid conflicts between old and new, 3079 * but keep everything locked that new will need. */ 3080 ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); 3081 if (ret < 0) { 3082 goto fail; 3083 } --- 5 unchanged lines hidden (view full) --- 3089 if (ret < 0) { 3090 raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 3091 goto fail; 3092 } 3093 } 3094 return 0; 3095 3096fail: |
3130 if (s->perm_change_fd && !s->reopen_state) { | 3097 if (s->perm_change_fd) { |
3131 qemu_close(s->perm_change_fd); 3132 } 3133 s->perm_change_fd = 0; 3134 return ret; 3135} 3136 3137static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) 3138{ --- 14 unchanged lines hidden (view full) --- 3153} 3154 3155static void raw_abort_perm_update(BlockDriverState *bs) 3156{ 3157 BDRVRawState *s = bs->opaque; 3158 3159 /* For reopen, .bdrv_reopen_abort is called afterwards and will close 3160 * the file descriptor. */ | 3098 qemu_close(s->perm_change_fd); 3099 } 3100 s->perm_change_fd = 0; 3101 return ret; 3102} 3103 3104static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) 3105{ --- 14 unchanged lines hidden (view full) --- 3120} 3121 3122static void raw_abort_perm_update(BlockDriverState *bs) 3123{ 3124 BDRVRawState *s = bs->opaque; 3125 3126 /* For reopen, .bdrv_reopen_abort is called afterwards and will close 3127 * the file descriptor. */ |
3161 if (s->perm_change_fd && !s->reopen_state) { | 3128 if (s->perm_change_fd) { |
3162 qemu_close(s->perm_change_fd); 3163 } 3164 s->perm_change_fd = 0; 3165 3166 raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 3167} 3168 3169static int coroutine_fn raw_co_copy_range_from( --- 735 unchanged lines hidden --- | 3129 qemu_close(s->perm_change_fd); 3130 } 3131 s->perm_change_fd = 0; 3132 3133 raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 3134} 3135 3136static int coroutine_fn raw_co_copy_range_from( --- 735 unchanged lines hidden --- |