Print this page
zero-errno cleanup
Cleanup nvlist_merge case.
Matt's code review fixes
Code review fixes
4986 receiving replication stream fails if any snapshot exceeds refquota


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 in replication streams, an older/earlier snapshot may exceed the
4097  * refquota.  We want to receive the older/earlier snapshot, but setting
4098  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4099  * the older/earlier snapshot from being received (with EDQUOT).
4100  *
4101  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4102  *
4103  * libzfs will need to be judicious handling errors encountered by props
4104  * extracted by this function.
4105  */
4106 static nvlist_t *
4107 extract_delay_props(nvlist_t *props)
4108 {
4109         nvlist_t *delayprops;
4110         nvpair_t *nvp, *tmp;
4111         static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4112         int i;
4113 
4114         VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4115 
4116         for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4117             nvp = nvlist_next_nvpair(props, nvp)) {
4118                 /*
4119                  * strcmp() is safe because zfs_prop_to_name() always returns
4120                  * a bounded string.
4121                  */
4122                 for (i = 0; delayable[i] != 0; i++) {
4123                         if (strcmp(zfs_prop_to_name(delayable[i]),
4124                             nvpair_name(nvp)) == 0) {
4125                                 break;
4126                         }
4127                 }
4128                 if (delayable[i] != 0) {
4129                         tmp = nvlist_prev_nvpair(props, nvp);
4130                         VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4131                         VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4132                         nvp = tmp;
4133                 }
4134         }
4135 
4136         if (nvlist_empty(delayprops)) {
4137                 nvlist_free(delayprops);
4138                 delayprops = NULL;
4139         }
4140         return (delayprops);
4141 }
4142 
4143 #ifdef  DEBUG
4144 static boolean_t zfs_ioc_recv_inject_err;
4145 #endif
4146 
4147 /*
4148  * inputs:
4149  * zc_name              name of containing filesystem
4150  * zc_nvlist_src{_size} nvlist of properties to apply
4151  * zc_value             name of snapshot to create
4152  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
4153  * zc_cookie            file descriptor to recv from
4154  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
4155  * zc_guid              force flag
4156  * zc_cleanup_fd        cleanup-on-exit file descriptor
4157  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
4158  * zc_resumable         if data is incomplete assume sender will resume
4159  *
4160  * outputs:
4161  * zc_cookie            number of bytes read
4162  * zc_nvlist_dst{_size} error for each unapplied received property
4163  * zc_obj               zprop_errflags_t
4164  * zc_action_handle     handle for this guid/ds mapping
4165  */
4166 static int
4167 zfs_ioc_recv(zfs_cmd_t *zc)
4168 {
4169         file_t *fp;
4170         dmu_recv_cookie_t drc;
4171         boolean_t force = (boolean_t)zc->zc_guid;
4172         int fd;
4173         int error = 0;
4174         int props_error = 0;
4175         nvlist_t *errors;
4176         offset_t off;
4177         nvlist_t *props = NULL; /* sent properties */
4178         nvlist_t *origprops = NULL; /* existing properties */
4179         nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4180         char *origin = NULL;
4181         char *tosnap;
4182         char tofs[ZFS_MAXNAMELEN];
4183         boolean_t first_recvd_props = B_FALSE;
4184 
4185         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4186             strchr(zc->zc_value, '@') == NULL ||
4187             strchr(zc->zc_value, '%'))
4188                 return (SET_ERROR(EINVAL));
4189 
4190         (void) strcpy(tofs, zc->zc_value);
4191         tosnap = strchr(tofs, '@');
4192         *tosnap++ = '\0';
4193 
4194         if (zc->zc_nvlist_src != NULL &&
4195             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4196             zc->zc_iflags, &props)) != 0)
4197                 return (error);
4198 
4199         fd = zc->zc_cookie;


4240                          * regardless.
4241                          */
4242                         if (!first_recvd_props)
4243                                 props_reduce(props, origprops);
4244                         if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4245                                 (void) nvlist_merge(errors, errlist, 0);
4246                         nvlist_free(errlist);
4247 
4248                         if (clear_received_props(tofs, origprops,
4249                             first_recvd_props ? NULL : props) != 0)
4250                                 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4251                 } else {
4252                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4253                 }
4254         }
4255 
4256         if (props != NULL) {
4257                 props_error = dsl_prop_set_hasrecvd(tofs);
4258 
4259                 if (props_error == 0) {
4260                         delayprops = extract_delay_props(props);
4261                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4262                             props, errors);
4263                 }
4264         }
4265 










4266         off = fp->f_offset;
4267         error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4268             &zc->zc_action_handle);
4269 
4270         if (error == 0) {
4271                 zfsvfs_t *zfsvfs = NULL;
4272 
4273                 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4274                         /* online recv */
4275                         int end_err;
4276 
4277                         error = zfs_suspend_fs(zfsvfs);
4278                         /*
4279                          * If the suspend fails, then the recv_end will
4280                          * likely also fail, and clean up after itself.
4281                          */
4282                         end_err = dmu_recv_end(&drc, zfsvfs);
4283                         if (error == 0)
4284                                 error = zfs_resume_fs(zfsvfs, tofs);
4285                         error = error ? error : end_err;
4286                         VFS_RELE(zfsvfs->z_vfs);
4287                 } else {
4288                         error = dmu_recv_end(&drc, NULL);
4289                 }
4290 
4291                 /* Set delayed properties now, after we're done receiving. */
4292                 if (delayprops != NULL && error == 0) {
4293                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4294                             delayprops, errors);
4295                 }
4296         }
4297 
4298         if (delayprops != NULL) {
4299                 /*
4300                  * Merge delayed props back in with initial props, in case
4301                  * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4302                  * we have to make sure clear_received_props() includes
4303                  * the delayed properties).
4304                  *
4305                  * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4306                  * using ASSERT() will be just like a VERIFY.
4307                  */
4308                 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4309                 nvlist_free(delayprops);
4310         }
4311 
4312         /*
4313          * Now that all props, initial and delayed, are set, report the prop
4314          * errors to the caller.
4315          */
4316         if (zc->zc_nvlist_dst_size != 0 &&
4317             (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4318             put_nvlist(zc, errors) != 0)) {
4319                 /*
4320                  * Caller made zc->zc_nvlist_dst less than the minimum expected
4321                  * size or supplied an invalid address.
4322                  */
4323                 props_error = SET_ERROR(EINVAL);
4324         }
4325 
4326         zc->zc_cookie = off - fp->f_offset;
4327         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4328                 fp->f_offset = off;
4329 
4330 #ifdef  DEBUG
4331         if (zfs_ioc_recv_inject_err) {
4332                 zfs_ioc_recv_inject_err = B_FALSE;
4333                 error = 1;
4334         }
4335 #endif
4336         /*
4337          * On error, restore the original props.
4338          */
4339         if (error != 0 && props != NULL && !drc.drc_newfs) {
4340                 if (clear_received_props(tofs, props, NULL) != 0) {
4341                         /*
4342                          * We failed to clear the received properties.
4343                          * Since we may have left a $recvd value on the
4344                          * system, we can't clear the $hasrecvd flag.
4345                          */