4073 pair = nvlist_next_nvpair(props, NULL);
4074 while (pair != NULL) {
4075 const char *propname = nvpair_name(pair);
4076 nvpair_t *match;
4077
4078 next_pair = nvlist_next_nvpair(props, pair);
4079
4080 if ((nvlist_lookup_nvpair(origprops, propname,
4081 &match) != 0) || !propval_equals(pair, match))
4082 goto next; /* need to set received value */
4083
4084 /* don't clear the existing received value */
4085 (void) nvlist_remove_nvpair(origprops, match);
4086 /* don't bother receiving the property */
4087 (void) nvlist_remove_nvpair(props, pair);
4088 next:
4089 pair = next_pair;
4090 }
4091 }
4092
4093 #ifdef DEBUG
4094 static boolean_t zfs_ioc_recv_inject_err;
4095 #endif
4096
4097 /*
4098 * inputs:
4099 * zc_name name of containing filesystem
4100 * zc_nvlist_src{_size} nvlist of properties to apply
4101 * zc_value name of snapshot to create
4102 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4103 * zc_cookie file descriptor to recv from
4104 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4105 * zc_guid force flag
4106 * zc_cleanup_fd cleanup-on-exit file descriptor
4107 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4108 * zc_resumable if data is incomplete assume sender will resume
4109 *
4110 * outputs:
4111 * zc_cookie number of bytes read
4112 * zc_nvlist_dst{_size} error for each unapplied received property
4113 * zc_obj zprop_errflags_t
4114 * zc_action_handle handle for this guid/ds mapping
4115 */
4116 static int
4117 zfs_ioc_recv(zfs_cmd_t *zc)
4118 {
4119 file_t *fp;
4120 dmu_recv_cookie_t drc;
4121 boolean_t force = (boolean_t)zc->zc_guid;
4122 int fd;
4123 int error = 0;
4124 int props_error = 0;
4125 nvlist_t *errors;
4126 offset_t off;
4127 nvlist_t *props = NULL; /* sent properties */
4128 nvlist_t *origprops = NULL; /* existing properties */
4129 char *origin = NULL;
4130 char *tosnap;
4131 char tofs[ZFS_MAXNAMELEN];
4132 boolean_t first_recvd_props = B_FALSE;
4133
4134 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4135 strchr(zc->zc_value, '@') == NULL ||
4136 strchr(zc->zc_value, '%'))
4137 return (SET_ERROR(EINVAL));
4138
4139 (void) strcpy(tofs, zc->zc_value);
4140 tosnap = strchr(tofs, '@');
4141 *tosnap++ = '\0';
4142
4143 if (zc->zc_nvlist_src != NULL &&
4144 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4145 zc->zc_iflags, &props)) != 0)
4146 return (error);
4147
4148 fd = zc->zc_cookie;
4189 * regardless.
4190 */
4191 if (!first_recvd_props)
4192 props_reduce(props, origprops);
4193 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4194 (void) nvlist_merge(errors, errlist, 0);
4195 nvlist_free(errlist);
4196
4197 if (clear_received_props(tofs, origprops,
4198 first_recvd_props ? NULL : props) != 0)
4199 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4200 } else {
4201 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4202 }
4203 }
4204
4205 if (props != NULL) {
4206 props_error = dsl_prop_set_hasrecvd(tofs);
4207
4208 if (props_error == 0) {
4209 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4210 props, errors);
4211 }
4212 }
4213
4214 if (zc->zc_nvlist_dst_size != 0 &&
4215 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4216 put_nvlist(zc, errors) != 0)) {
4217 /*
4218 * Caller made zc->zc_nvlist_dst less than the minimum expected
4219 * size or supplied an invalid address.
4220 */
4221 props_error = SET_ERROR(EINVAL);
4222 }
4223
4224 off = fp->f_offset;
4225 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4226 &zc->zc_action_handle);
4227
4228 if (error == 0) {
4229 zfsvfs_t *zfsvfs = NULL;
4230
4231 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4232 /* online recv */
4233 int end_err;
4234
4235 error = zfs_suspend_fs(zfsvfs);
4236 /*
4237 * If the suspend fails, then the recv_end will
4238 * likely also fail, and clean up after itself.
4239 */
4240 end_err = dmu_recv_end(&drc, zfsvfs);
4241 if (error == 0)
4242 error = zfs_resume_fs(zfsvfs, tofs);
4243 error = error ? error : end_err;
4244 VFS_RELE(zfsvfs->z_vfs);
4245 } else {
4246 error = dmu_recv_end(&drc, NULL);
4247 }
4248 }
4249
4250 zc->zc_cookie = off - fp->f_offset;
4251 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4252 fp->f_offset = off;
4253
4254 #ifdef DEBUG
4255 if (zfs_ioc_recv_inject_err) {
4256 zfs_ioc_recv_inject_err = B_FALSE;
4257 error = 1;
4258 }
4259 #endif
4260 /*
4261 * On error, restore the original props.
4262 */
4263 if (error != 0 && props != NULL && !drc.drc_newfs) {
4264 if (clear_received_props(tofs, props, NULL) != 0) {
4265 /*
4266 * We failed to clear the received properties.
4267 * Since we may have left a $recvd value on the
4268 * system, we can't clear the $hasrecvd flag.
4269 */
|
4073 pair = nvlist_next_nvpair(props, NULL);
4074 while (pair != NULL) {
4075 const char *propname = nvpair_name(pair);
4076 nvpair_t *match;
4077
4078 next_pair = nvlist_next_nvpair(props, pair);
4079
4080 if ((nvlist_lookup_nvpair(origprops, propname,
4081 &match) != 0) || !propval_equals(pair, match))
4082 goto next; /* need to set received value */
4083
4084 /* don't clear the existing received value */
4085 (void) nvlist_remove_nvpair(origprops, match);
4086 /* don't bother receiving the property */
4087 (void) nvlist_remove_nvpair(props, pair);
4088 next:
4089 pair = next_pair;
4090 }
4091 }
4092
4093 /*
4094 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4095 * For example, refquota cannot be set until after the receipt of a dataset,
4096 * because a prior snapshot may exceed the refquota, and refquotas only apply
4097 * to the current dataset. The caller (libzfs) will manage these properties
4098 * somewhat as well to make sure they only come down with the last dataset in
4099 * a replication stream, but we still need to be safe about it here in
4100 * kernel-land.
4101 */
4102 static nvlist_t *
4103 extract_delay_props(nvlist_t *props)
4104 {
4105 nvlist_t *delayprops;
4106 nvpair_t *nvp, *tmp;
4107 static const zfs_prop_t delayable[] =
4108 { ZFS_PROP_REFQUOTA, ZFS_PROP_REFRESERVATION, 0 };
4109 boolean_t dontbother = B_TRUE;
4110 int i;
4111
4112 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4113
4114 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4115 nvp = nvlist_next_nvpair(props, nvp)) {
4116 /*
4117 * strcmp() is safe because zfs_prop_to_name() always returns
4118 * a bounded string.
4119 */
4120 for (i = 0; delayable[i] != 0; i++) {
4121 if (strcmp(zfs_prop_to_name(delayable[i]),
4122 nvpair_name(nvp)) == 0) {
4123 break;
4124 }
4125 }
4126 if (delayable[i] != 0) {
4127 tmp = nvlist_prev_nvpair(props, nvp);
4128 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4129 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4130 nvp = tmp;
4131 dontbother = B_FALSE; /* Actually, do bother! */
4132 }
4133 }
4134
4135 if (dontbother) {
4136 nvlist_free(delayprops);
4137 delayprops = NULL;
4138 }
4139 return (delayprops);
4140 }
4141
4142 #ifdef DEBUG
4143 static boolean_t zfs_ioc_recv_inject_err;
4144 #endif
4145
4146 /*
4147 * inputs:
4148 * zc_name name of containing filesystem
4149 * zc_nvlist_src{_size} nvlist of properties to apply
4150 * zc_value name of snapshot to create
4151 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4152 * zc_cookie file descriptor to recv from
4153 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4154 * zc_guid force flag
4155 * zc_cleanup_fd cleanup-on-exit file descriptor
4156 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4157 * zc_resumable if data is incomplete assume sender will resume
4158 *
4159 * outputs:
4160 * zc_cookie number of bytes read
4161 * zc_nvlist_dst{_size} error for each unapplied received property
4162 * zc_obj zprop_errflags_t
4163 * zc_action_handle handle for this guid/ds mapping
4164 */
4165 static int
4166 zfs_ioc_recv(zfs_cmd_t *zc)
4167 {
4168 file_t *fp;
4169 dmu_recv_cookie_t drc;
4170 boolean_t force = (boolean_t)zc->zc_guid;
4171 int fd;
4172 int error = 0;
4173 int props_error = 0;
4174 nvlist_t *errors;
4175 offset_t off;
4176 nvlist_t *props = NULL; /* sent properties */
4177 nvlist_t *origprops = NULL; /* existing properties */
4178 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4179 char *origin = NULL;
4180 char *tosnap;
4181 char tofs[ZFS_MAXNAMELEN];
4182 boolean_t first_recvd_props = B_FALSE;
4183
4184 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4185 strchr(zc->zc_value, '@') == NULL ||
4186 strchr(zc->zc_value, '%'))
4187 return (SET_ERROR(EINVAL));
4188
4189 (void) strcpy(tofs, zc->zc_value);
4190 tosnap = strchr(tofs, '@');
4191 *tosnap++ = '\0';
4192
4193 if (zc->zc_nvlist_src != NULL &&
4194 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4195 zc->zc_iflags, &props)) != 0)
4196 return (error);
4197
4198 fd = zc->zc_cookie;
4239 * regardless.
4240 */
4241 if (!first_recvd_props)
4242 props_reduce(props, origprops);
4243 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4244 (void) nvlist_merge(errors, errlist, 0);
4245 nvlist_free(errlist);
4246
4247 if (clear_received_props(tofs, origprops,
4248 first_recvd_props ? NULL : props) != 0)
4249 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4250 } else {
4251 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4252 }
4253 }
4254
4255 if (props != NULL) {
4256 props_error = dsl_prop_set_hasrecvd(tofs);
4257
4258 if (props_error == 0) {
4259 delayprops = extract_delay_props(props);
4260 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4261 props, errors);
4262 }
4263 }
4264
4265 off = fp->f_offset;
4266 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4267 &zc->zc_action_handle);
4268
4269 if (error == 0) {
4270 zfsvfs_t *zfsvfs = NULL;
4271
4272 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4273 /* online recv */
4274 int end_err;
4275
4276 error = zfs_suspend_fs(zfsvfs);
4277 /*
4278 * If the suspend fails, then the recv_end will
4279 * likely also fail, and clean up after itself.
4280 */
4281 end_err = dmu_recv_end(&drc, zfsvfs);
4282 if (error == 0)
4283 error = zfs_resume_fs(zfsvfs, tofs);
4284 error = error ? error : end_err;
4285 VFS_RELE(zfsvfs->z_vfs);
4286 } else {
4287 error = dmu_recv_end(&drc, NULL);
4288 }
4289
4290 /* Set delayed properties now, after we're done receiving. */
4291 if (delayprops != NULL) {
4292 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4293 delayprops, errors);
4294 }
4295 }
4296
4297 /* Merge delayprops back in with regular props, in case of errors. */
4298 if (delayprops != NULL) {
4299 VERIFY(nvlist_merge(props, delayprops, 0) == 0);
4300 nvlist_free(delayprops);
4301 }
4302
4303 /* Put the props error list into zc AFTER the delayprops. */
4304 if (zc->zc_nvlist_dst_size != 0 &&
4305 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4306 put_nvlist(zc, errors) != 0)) {
4307 /*
4308 * Caller made zc->zc_nvlist_dst less than the minimum expected
4309 * size or supplied an invalid address.
4310 */
4311 props_error = SET_ERROR(EINVAL);
4312 }
4313
4314 zc->zc_cookie = off - fp->f_offset;
4315 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4316 fp->f_offset = off;
4317
4318 #ifdef DEBUG
4319 if (zfs_ioc_recv_inject_err) {
4320 zfs_ioc_recv_inject_err = B_FALSE;
4321 error = 1;
4322 }
4323 #endif
4324 /*
4325 * On error, restore the original props.
4326 */
4327 if (error != 0 && props != NULL && !drc.drc_newfs) {
4328 if (clear_received_props(tofs, props, NULL) != 0) {
4329 /*
4330 * We failed to clear the received properties.
4331 * Since we may have left a $recvd value on the
4332 * system, we can't clear the $hasrecvd flag.
4333 */
|