719{ 720 zio_t *zio; 721 722 zio = zio_create(pio, spa, txg, bp, data, size, done, private, 723 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 724 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 725 726 return (zio); 727} 728 729void 730zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite) 731{ 732 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 733 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 734 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 735 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa)); 736 737 /* 738 * We must reset the io_prop to match the values that existed 739 * when the bp was first written by dmu_sync() keeping in mind 740 * that nopwrite and dedup are mutually exclusive. 741 */ 742 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup; 743 zio->io_prop.zp_nopwrite = nopwrite; 744 zio->io_prop.zp_copies = copies; 745 zio->io_bp_override = bp; 746} 747 748void 749zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp) 750{ 751 752 /* 753 * The check for EMBEDDED is a performance optimization. We 754 * process the free here (by ignoring it) rather than 755 * putting it on the list and then processing it in zio_free_sync(). 756 */ 757 if (BP_IS_EMBEDDED(bp)) 758 return; 759 metaslab_check_free(spa, bp); 760 761 /* 762 * Frees that are for the currently-syncing txg, are not going to be 763 * deferred, and which will not need to do a read (i.e. not GANG or 764 * DEDUP), can be processed immediately. Otherwise, put them on the 765 * in-memory list for later processing. 766 */ 767 if (zfs_trim_enabled || BP_IS_GANG(bp) || BP_GET_DEDUP(bp) || 768 txg != spa->spa_syncing_txg || 769 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) { 770 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp); 771 } else { 772 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 773 BP_GET_PSIZE(bp), 0))); 774 } 775} 776 777zio_t * 778zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 779 uint64_t size, enum zio_flag flags) 780{ 781 zio_t *zio; 782 enum zio_stage stage = ZIO_FREE_PIPELINE; 783 784 ASSERT(!BP_IS_HOLE(bp)); 785 ASSERT(spa_syncing_txg(spa) == txg); 786 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free); 787 788 if (BP_IS_EMBEDDED(bp)) 789 return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 790 791 metaslab_check_free(spa, bp); 792 arc_freed(spa, bp); 793 794 if (zfs_trim_enabled) 795 stage |= ZIO_STAGE_ISSUE_ASYNC | ZIO_STAGE_VDEV_IO_START | 796 ZIO_STAGE_VDEV_IO_ASSESS; 797 /* 798 * GANG and DEDUP blocks can induce a read (for the gang block header, 799 * or the DDT), so issue them asynchronously so that this thread is 800 * not tied up. 801 */ 802 else if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp)) 803 stage |= ZIO_STAGE_ISSUE_ASYNC; 804 805 flags |= ZIO_FLAG_DONT_QUEUE; 806 807 zio = zio_create(pio, spa, txg, bp, NULL, size, 808 NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags, 809 NULL, 0, NULL, ZIO_STAGE_OPEN, stage); 810 811 return (zio); 812} 813 814zio_t * 815zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 816 zio_done_func_t *done, void *private, enum zio_flag flags) 817{ 818 zio_t *zio; 819 820 dprintf_bp(bp, "claiming in txg %llu", txg); 821 822 if (BP_IS_EMBEDDED(bp)) 823 return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 824 825 /* 826 * A claim is an allocation of a specific block. Claims are needed 827 * to support immediate writes in the intent log. The issue is that 828 * immediate writes contain committed data, but in a txg that was 829 * *not* committed. Upon opening the pool after an unclean shutdown, 830 * the intent log claims all blocks that contain immediate write data 831 * so that the SPA knows they're in use. 832 * 833 * All claims *must* be resolved in the first txg -- before the SPA 834 * starts allocating blocks -- so that nothing is allocated twice. 835 * If txg == 0 we just verify that the block is claimable. 836 */ 837 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 838 ASSERT(txg == spa_first_txg(spa) || txg == 0); 839 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */ 840 841 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 842 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags, 843 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 844 845 return (zio); 846} 847 848zio_t * 849zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, uint64_t offset, 850 uint64_t size, zio_done_func_t *done, void *private, 851 zio_priority_t priority, enum zio_flag flags) 852{ 853 zio_t *zio; 854 int c; 855 856 if (vd->vdev_children == 0) { 857 zio = zio_create(pio, spa, 0, NULL, NULL, size, done, private, 858 ZIO_TYPE_IOCTL, priority, flags, vd, offset, NULL, 859 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 860 861 zio->io_cmd = cmd; 862 } else { 863 zio = zio_null(pio, spa, NULL, NULL, NULL, flags); 864 865 for (c = 0; c < vd->vdev_children; c++) 866 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 867 offset, size, done, private, priority, flags)); 868 } 869 870 return (zio); 871} 872 873zio_t * 874zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 875 void *data, int checksum, zio_done_func_t *done, void *private, 876 zio_priority_t priority, enum zio_flag flags, boolean_t labels) 877{ 878 zio_t *zio; 879 880 ASSERT(vd->vdev_children == 0); 881 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 882 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 883 ASSERT3U(offset + size, <=, vd->vdev_psize); 884 885 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private, 886 ZIO_TYPE_READ, priority, flags, vd, offset, NULL, 887 ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 888 889 zio->io_prop.zp_checksum = checksum; 890 891 return (zio); 892} 893 894zio_t * 895zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 896 void *data, int checksum, zio_done_func_t *done, void *private, 897 zio_priority_t priority, enum zio_flag flags, boolean_t labels) 898{ 899 zio_t *zio; 900 901 ASSERT(vd->vdev_children == 0); 902 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 903 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 904 ASSERT3U(offset + size, <=, vd->vdev_psize); 905 906 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private, 907 ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL, 908 ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 909 910 zio->io_prop.zp_checksum = checksum; 911 912 if (zio_checksum_table[checksum].ci_eck) { 913 /* 914 * zec checksums are necessarily destructive -- they modify 915 * the end of the write buffer to hold the verifier/checksum. 916 * Therefore, we must make a local copy in case the data is 917 * being written to multiple places in parallel. 918 */ 919 void *wbuf = zio_buf_alloc(size); 920 bcopy(data, wbuf, size); 921 zio_push_transform(zio, wbuf, size, size, NULL); 922 } 923 924 return (zio); 925} 926 927/* 928 * Create a child I/O to do some work for us. 929 */ 930zio_t * 931zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 932 void *data, uint64_t size, int type, zio_priority_t priority, 933 enum zio_flag flags, zio_done_func_t *done, void *private) 934{ 935 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE; 936 zio_t *zio; 937 938 ASSERT(vd->vdev_parent == 939 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev)); 940 941 if (type == ZIO_TYPE_READ && bp != NULL) { 942 /* 943 * If we have the bp, then the child should perform the 944 * checksum and the parent need not. This pushes error 945 * detection as close to the leaves as possible and 946 * eliminates redundant checksums in the interior nodes. 947 */ 948 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY; 949 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 950 } 951 952 /* Not all IO types require vdev io done stage e.g. free */ 953 if (!(pio->io_pipeline & ZIO_STAGE_VDEV_IO_DONE)) 954 pipeline &= ~ZIO_STAGE_VDEV_IO_DONE; 955 956 if (vd->vdev_children == 0) 957 offset += VDEV_LABEL_START_SIZE; 958 959 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE; 960 961 /* 962 * If we've decided to do a repair, the write is not speculative -- 963 * even if the original read was. 964 */ 965 if (flags & ZIO_FLAG_IO_REPAIR) 966 flags &= ~ZIO_FLAG_SPECULATIVE; 967 968 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, 969 done, private, type, priority, flags, vd, offset, &pio->io_bookmark, 970 ZIO_STAGE_VDEV_IO_START >> 1, pipeline); 971 972 zio->io_physdone = pio->io_physdone; 973 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL) 974 zio->io_logical->io_phys_children++; 975 976 return (zio); 977} 978 979zio_t * 980zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size, 981 int type, zio_priority_t priority, enum zio_flag flags, 982 zio_done_func_t *done, void *private) 983{ 984 zio_t *zio; 985 986 ASSERT(vd->vdev_ops->vdev_op_leaf); 987 988 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, 989 data, size, done, private, type, priority, 990 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED, 991 vd, offset, NULL, 992 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE); 993 994 return (zio); 995} 996 997void 998zio_flush(zio_t *zio, vdev_t *vd) 999{ 1000 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 0, 0, 1001 NULL, NULL, ZIO_PRIORITY_NOW, 1002 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY)); 1003} 1004 1005zio_t * 1006zio_trim(zio_t *zio, spa_t *spa, vdev_t *vd, uint64_t offset, uint64_t size) 1007{ 1008 1009 ASSERT(vd->vdev_ops->vdev_op_leaf); 1010 1011 return (zio_create(zio, spa, 0, NULL, NULL, size, NULL, NULL, 1012 ZIO_TYPE_FREE, ZIO_PRIORITY_TRIM, ZIO_FLAG_DONT_AGGREGATE | 1013 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, 1014 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PHYS_PIPELINE)); 1015} 1016 1017void 1018zio_shrink(zio_t *zio, uint64_t size) 1019{ 1020 ASSERT(zio->io_executor == NULL); 1021 ASSERT(zio->io_orig_size == zio->io_size); 1022 ASSERT(size <= zio->io_size); 1023 1024 /* 1025 * We don't shrink for raidz because of problems with the 1026 * reconstruction when reading back less than the block size. 1027 * Note, BP_IS_RAIDZ() assumes no compression. 1028 */ 1029 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 1030 if (!BP_IS_RAIDZ(zio->io_bp)) 1031 zio->io_orig_size = zio->io_size = size; 1032} 1033 1034/* 1035 * ========================================================================== 1036 * Prepare to read and write logical blocks 1037 * ========================================================================== 1038 */ 1039 1040static int 1041zio_read_bp_init(zio_t **ziop) 1042{ 1043 zio_t *zio = *ziop; 1044 blkptr_t *bp = zio->io_bp; 1045 1046 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF && 1047 zio->io_child_type == ZIO_CHILD_LOGICAL && 1048 !(zio->io_flags & ZIO_FLAG_RAW)) { 1049 uint64_t psize = 1050 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp); 1051 void *cbuf = zio_buf_alloc(psize); 1052 1053 zio_push_transform(zio, cbuf, psize, psize, zio_decompress); 1054 } 1055 1056 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) { 1057 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1058 decode_embedded_bp_compressed(bp, zio->io_data); 1059 } else { 1060 ASSERT(!BP_IS_EMBEDDED(bp)); 1061 } 1062 1063 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0) 1064 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1065 1066 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP) 1067 zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1068 1069 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL) 1070 zio->io_pipeline = ZIO_DDT_READ_PIPELINE; 1071 1072 return (ZIO_PIPELINE_CONTINUE); 1073} 1074 1075static int 1076zio_write_bp_init(zio_t **ziop) 1077{ 1078 zio_t *zio = *ziop; 1079 spa_t *spa = zio->io_spa; 1080 zio_prop_t *zp = &zio->io_prop; 1081 enum zio_compress compress = zp->zp_compress; 1082 blkptr_t *bp = zio->io_bp; 1083 uint64_t lsize = zio->io_size; 1084 uint64_t psize = lsize; 1085 int pass = 1; 1086 1087 /* 1088 * If our children haven't all reached the ready stage, 1089 * wait for them and then repeat this pipeline stage. 1090 */ 1091 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) || 1092 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY)) 1093 return (ZIO_PIPELINE_STOP); 1094 1095 if (!IO_IS_ALLOCATING(zio)) 1096 return (ZIO_PIPELINE_CONTINUE); 1097 1098 ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 1099 1100 if (zio->io_bp_override) { 1101 ASSERT(bp->blk_birth != zio->io_txg); 1102 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0); 1103 1104 *bp = *zio->io_bp_override; 1105 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1106 1107 if (BP_IS_EMBEDDED(bp)) 1108 return (ZIO_PIPELINE_CONTINUE); 1109 1110 /* 1111 * If we've been overridden and nopwrite is set then 1112 * set the flag accordingly to indicate that a nopwrite 1113 * has already occurred. 1114 */ 1115 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) { 1116 ASSERT(!zp->zp_dedup); 1117 zio->io_flags |= ZIO_FLAG_NOPWRITE; 1118 return (ZIO_PIPELINE_CONTINUE); 1119 } 1120 1121 ASSERT(!zp->zp_nopwrite); 1122 1123 if (BP_IS_HOLE(bp) || !zp->zp_dedup) 1124 return (ZIO_PIPELINE_CONTINUE); 1125 1126 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup || 1127 zp->zp_dedup_verify); 1128 1129 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) { 1130 BP_SET_DEDUP(bp, 1); 1131 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE; 1132 return (ZIO_PIPELINE_CONTINUE); 1133 } 1134 zio->io_bp_override = NULL; 1135 BP_ZERO(bp); 1136 } 1137 1138 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) { 1139 /* 1140 * We're rewriting an existing block, which means we're 1141 * working on behalf of spa_sync(). For spa_sync() to 1142 * converge, it must eventually be the case that we don't 1143 * have to allocate new blocks. But compression changes 1144 * the blocksize, which forces a reallocate, and makes 1145 * convergence take longer. Therefore, after the first 1146 * few passes, stop compressing to ensure convergence. 1147 */ 1148 pass = spa_sync_pass(spa); 1149 1150 ASSERT(zio->io_txg == spa_syncing_txg(spa)); 1151 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1152 ASSERT(!BP_GET_DEDUP(bp)); 1153 1154 if (pass >= zfs_sync_pass_dont_compress) 1155 compress = ZIO_COMPRESS_OFF; 1156 1157 /* Make sure someone doesn't change their mind on overwrites */ 1158 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp), 1159 spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 1160 } 1161 1162 if (compress != ZIO_COMPRESS_OFF) { 1163 metaslab_class_t *mc = spa_normal_class(spa); 1164 void *cbuf = zio_buf_alloc(lsize); 1165 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize, 1166 (size_t)metaslab_class_get_minblocksize(mc)); 1167 if (psize == 0 || psize == lsize) { 1168 compress = ZIO_COMPRESS_OFF; 1169 zio_buf_free(cbuf, lsize); 1170 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE && 1171 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) && 1172 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) { 1173 encode_embedded_bp_compressed(bp, 1174 cbuf, compress, lsize, psize); 1175 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA); 1176 BP_SET_TYPE(bp, zio->io_prop.zp_type); 1177 BP_SET_LEVEL(bp, zio->io_prop.zp_level); 1178 zio_buf_free(cbuf, lsize); 1179 bp->blk_birth = zio->io_txg; 1180 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1181 ASSERT(spa_feature_is_active(spa, 1182 SPA_FEATURE_EMBEDDED_DATA)); 1183 return (ZIO_PIPELINE_CONTINUE); 1184 } else { 1185 /* 1186 * Round up compressed size to MINBLOCKSIZE and 1187 * zero the tail. 1188 */ 1189 size_t rounded = 1190 P2ROUNDUP(psize, (size_t)SPA_MINBLOCKSIZE); 1191 if (rounded > psize) { 1192 bzero((char *)cbuf + psize, rounded - psize); 1193 psize = rounded; 1194 } 1195 if (psize == lsize) { 1196 compress = ZIO_COMPRESS_OFF; 1197 zio_buf_free(cbuf, lsize); 1198 } else { 1199 zio_push_transform(zio, cbuf, 1200 psize, lsize, NULL); 1201 } 1202 } 1203 } 1204 1205 /* 1206 * The final pass of spa_sync() must be all rewrites, but the first 1207 * few passes offer a trade-off: allocating blocks defers convergence, 1208 * but newly allocated blocks are sequential, so they can be written 1209 * to disk faster. Therefore, we allow the first few passes of 1210 * spa_sync() to allocate new blocks, but force rewrites after that. 1211 * There should only be a handful of blocks after pass 1 in any case. 1212 */ 1213 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg && 1214 BP_GET_PSIZE(bp) == psize && 1215 pass >= zfs_sync_pass_rewrite) { 1216 ASSERT(psize != 0); 1217 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 1218 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 1219 zio->io_flags |= ZIO_FLAG_IO_REWRITE; 1220 } else { 1221 BP_ZERO(bp); 1222 zio->io_pipeline = ZIO_WRITE_PIPELINE; 1223 } 1224 1225 if (psize == 0) { 1226 if (zio->io_bp_orig.blk_birth != 0 && 1227 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 1228 BP_SET_LSIZE(bp, lsize); 1229 BP_SET_TYPE(bp, zp->zp_type); 1230 BP_SET_LEVEL(bp, zp->zp_level); 1231 BP_SET_BIRTH(bp, zio->io_txg, 0); 1232 } 1233 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1234 } else { 1235 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 1236 BP_SET_LSIZE(bp, lsize); 1237 BP_SET_TYPE(bp, zp->zp_type); 1238 BP_SET_LEVEL(bp, zp->zp_level); 1239 BP_SET_PSIZE(bp, psize); 1240 BP_SET_COMPRESS(bp, compress); 1241 BP_SET_CHECKSUM(bp, zp->zp_checksum); 1242 BP_SET_DEDUP(bp, zp->zp_dedup); 1243 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1244 if (zp->zp_dedup) { 1245 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1246 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1247 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 1248 } 1249 if (zp->zp_nopwrite) { 1250 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1251 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1252 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE; 1253 } 1254 } 1255 1256 return (ZIO_PIPELINE_CONTINUE); 1257} 1258 1259static int 1260zio_free_bp_init(zio_t **ziop) 1261{ 1262 zio_t *zio = *ziop; 1263 blkptr_t *bp = zio->io_bp; 1264 1265 if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 1266 if (BP_GET_DEDUP(bp)) 1267 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE; 1268 } 1269 1270 return (ZIO_PIPELINE_CONTINUE); 1271} 1272 1273/* 1274 * ========================================================================== 1275 * Execute the I/O pipeline 1276 * ========================================================================== 1277 */ 1278 1279static void 1280zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline) 1281{ 1282 spa_t *spa = zio->io_spa; 1283 zio_type_t t = zio->io_type; 1284 int flags = (cutinline ? TQ_FRONT : 0); 1285 1286 ASSERT(q == ZIO_TASKQ_ISSUE || q == ZIO_TASKQ_INTERRUPT); 1287 1288 /* 1289 * If we're a config writer or a probe, the normal issue and 1290 * interrupt threads may all be blocked waiting for the config lock. 1291 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 1292 */ 1293 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 1294 t = ZIO_TYPE_NULL; 1295 1296 /* 1297 * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 1298 */ 1299 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 1300 t = ZIO_TYPE_NULL; 1301 1302 /* 1303 * If this is a high priority I/O, then use the high priority taskq if 1304 * available. 1305 */ 1306 if (zio->io_priority == ZIO_PRIORITY_NOW && 1307 spa->spa_zio_taskq[t][q + 1].stqs_count != 0) 1308 q++; 1309 1310 ASSERT3U(q, <, ZIO_TASKQ_TYPES); 1311 1312 /* 1313 * NB: We are assuming that the zio can only be dispatched 1314 * to a single taskq at a time. It would be a grievous error 1315 * to dispatch the zio to another taskq at the same time. 1316 */ 1317#if defined(illumos) || !defined(_KERNEL) 1318 ASSERT(zio->io_tqent.tqent_next == NULL); 1319#else 1320 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0); 1321#endif 1322 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio, 1323 flags, &zio->io_tqent); 1324} 1325 1326static boolean_t 1327zio_taskq_member(zio_t *zio, zio_taskq_type_t q) 1328{ 1329 kthread_t *executor = zio->io_executor; 1330 spa_t *spa = zio->io_spa; 1331 1332 for (zio_type_t t = 0; t < ZIO_TYPES; t++) { 1333 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1334 uint_t i; 1335 for (i = 0; i < tqs->stqs_count; i++) { 1336 if (taskq_member(tqs->stqs_taskq[i], executor)) 1337 return (B_TRUE); 1338 } 1339 } 1340 1341 return (B_FALSE); 1342} 1343 1344static int 1345zio_issue_async(zio_t **ziop) 1346{ 1347 zio_t *zio = *ziop; 1348 1349 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1350 1351 return (ZIO_PIPELINE_STOP); 1352} 1353 1354void 1355zio_interrupt(zio_t *zio) 1356{ 1357 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 1358} 1359 1360/* 1361 * Execute the I/O pipeline until one of the following occurs: 1362 * 1363 * (1) the I/O completes 1364 * (2) the pipeline stalls waiting for dependent child I/Os 1365 * (3) the I/O issues, so we're waiting for an I/O completion interrupt 1366 * (4) the I/O is delegated by vdev-level caching or aggregation 1367 * (5) the I/O is deferred due to vdev-level queueing 1368 * (6) the I/O is handed off to another thread. 1369 * 1370 * In all cases, the pipeline stops whenever there's no CPU work; it never 1371 * burns a thread in cv_wait(). 1372 * 1373 * There's no locking on io_stage because there's no legitimate way 1374 * for multiple threads to be attempting to process the same I/O. 1375 */ 1376static zio_pipe_stage_t *zio_pipeline[]; 1377 1378void 1379zio_execute(zio_t *zio) 1380{ 1381 zio->io_executor = curthread; 1382 1383 while (zio->io_stage < ZIO_STAGE_DONE) { 1384 enum zio_stage pipeline = zio->io_pipeline; 1385 enum zio_stage stage = zio->io_stage; 1386 int rv; 1387 1388 ASSERT(!MUTEX_HELD(&zio->io_lock)); 1389 ASSERT(ISP2(stage)); 1390 ASSERT(zio->io_stall == NULL); 1391 1392 do { 1393 stage <<= 1; 1394 } while ((stage & pipeline) == 0); 1395 1396 ASSERT(stage <= ZIO_STAGE_DONE); 1397 1398 /* 1399 * If we are in interrupt context and this pipeline stage 1400 * will grab a config lock that is held across I/O, 1401 * or may wait for an I/O that needs an interrupt thread 1402 * to complete, issue async to avoid deadlock. 1403 * 1404 * For VDEV_IO_START, we cut in line so that the io will 1405 * be sent to disk promptly. 1406 */ 1407 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 1408 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 1409 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 1410 zio_requeue_io_start_cut_in_line : B_FALSE; 1411 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 1412 return; 1413 } 1414 1415 zio->io_stage = stage; 1416 rv = zio_pipeline[highbit64(stage) - 1](&zio); 1417 1418 if (rv == ZIO_PIPELINE_STOP) 1419 return; 1420 1421 ASSERT(rv == ZIO_PIPELINE_CONTINUE); 1422 } 1423} 1424 1425/* 1426 * ========================================================================== 1427 * Initiate I/O, either sync or async 1428 * ========================================================================== 1429 */ 1430int 1431zio_wait(zio_t *zio) 1432{ 1433 int error; 1434 1435 ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 1436 ASSERT(zio->io_executor == NULL); 1437 1438 zio->io_waiter = curthread; 1439 1440 zio_execute(zio); 1441 1442 mutex_enter(&zio->io_lock); 1443 while (zio->io_executor != NULL) 1444 cv_wait(&zio->io_cv, &zio->io_lock); 1445 mutex_exit(&zio->io_lock); 1446 1447 error = zio->io_error; 1448 zio_destroy(zio); 1449 1450 return (error); 1451} 1452 1453void 1454zio_nowait(zio_t *zio) 1455{ 1456 ASSERT(zio->io_executor == NULL); 1457 1458 if (zio->io_child_type == ZIO_CHILD_LOGICAL && 1459 zio_unique_parent(zio) == NULL) { 1460 /* 1461 * This is a logical async I/O with no parent to wait for it. 1462 * We add it to the spa_async_root_zio "Godfather" I/O which 1463 * will ensure they complete prior to unloading the pool. 1464 */ 1465 spa_t *spa = zio->io_spa; 1466 1467 zio_add_child(spa->spa_async_zio_root, zio); 1468 } 1469 1470 zio_execute(zio); 1471} 1472 1473/* 1474 * ========================================================================== 1475 * Reexecute or suspend/resume failed I/O 1476 * ========================================================================== 1477 */ 1478 1479static void 1480zio_reexecute(zio_t *pio) 1481{ 1482 zio_t *cio, *cio_next; 1483 1484 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 1485 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 1486 ASSERT(pio->io_gang_leader == NULL); 1487 ASSERT(pio->io_gang_tree == NULL); 1488 1489 pio->io_flags = pio->io_orig_flags; 1490 pio->io_stage = pio->io_orig_stage; 1491 pio->io_pipeline = pio->io_orig_pipeline; 1492 pio->io_reexecute = 0; 1493 pio->io_flags |= ZIO_FLAG_REEXECUTED; 1494 pio->io_error = 0; 1495 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1496 pio->io_state[w] = 0; 1497 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 1498 pio->io_child_error[c] = 0; 1499 1500 if (IO_IS_ALLOCATING(pio)) 1501 BP_ZERO(pio->io_bp); 1502 1503 /* 1504 * As we reexecute pio's children, new children could be created. 1505 * New children go to the head of pio's io_child_list, however, 1506 * so we will (correctly) not reexecute them. The key is that 1507 * the remainder of pio's io_child_list, from 'cio_next' onward, 1508 * cannot be affected by any side effects of reexecuting 'cio'. 1509 */ 1510 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) { 1511 cio_next = zio_walk_children(pio); 1512 mutex_enter(&pio->io_lock); 1513 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1514 pio->io_children[cio->io_child_type][w]++; 1515 mutex_exit(&pio->io_lock); 1516 zio_reexecute(cio); 1517 } 1518 1519 /* 1520 * Now that all children have been reexecuted, execute the parent. 1521 * We don't reexecute "The Godfather" I/O here as it's the 1522 * responsibility of the caller to wait on him. 1523 */ 1524 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) 1525 zio_execute(pio); 1526} 1527 1528void 1529zio_suspend(spa_t *spa, zio_t *zio) 1530{ 1531 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 1532 fm_panic("Pool '%s' has encountered an uncorrectable I/O " 1533 "failure and the failure mode property for this pool " 1534 "is set to panic.", spa_name(spa)); 1535 1536 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0); 1537 1538 mutex_enter(&spa->spa_suspend_lock); 1539 1540 if (spa->spa_suspend_zio_root == NULL) 1541 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 1542 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 1543 ZIO_FLAG_GODFATHER); 1544 1545 spa->spa_suspended = B_TRUE; 1546 1547 if (zio != NULL) { 1548 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 1549 ASSERT(zio != spa->spa_suspend_zio_root); 1550 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1551 ASSERT(zio_unique_parent(zio) == NULL); 1552 ASSERT(zio->io_stage == ZIO_STAGE_DONE); 1553 zio_add_child(spa->spa_suspend_zio_root, zio); 1554 } 1555 1556 mutex_exit(&spa->spa_suspend_lock); 1557} 1558 1559int 1560zio_resume(spa_t *spa) 1561{ 1562 zio_t *pio; 1563 1564 /* 1565 * Reexecute all previously suspended i/o. 1566 */ 1567 mutex_enter(&spa->spa_suspend_lock); 1568 spa->spa_suspended = B_FALSE; 1569 cv_broadcast(&spa->spa_suspend_cv); 1570 pio = spa->spa_suspend_zio_root; 1571 spa->spa_suspend_zio_root = NULL; 1572 mutex_exit(&spa->spa_suspend_lock); 1573 1574 if (pio == NULL) 1575 return (0); 1576 1577 zio_reexecute(pio); 1578 return (zio_wait(pio)); 1579} 1580 1581void 1582zio_resume_wait(spa_t *spa) 1583{ 1584 mutex_enter(&spa->spa_suspend_lock); 1585 while (spa_suspended(spa)) 1586 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 1587 mutex_exit(&spa->spa_suspend_lock); 1588} 1589 1590/* 1591 * ========================================================================== 1592 * Gang blocks. 1593 * 1594 * A gang block is a collection of small blocks that looks to the DMU 1595 * like one large block. When zio_dva_allocate() cannot find a block 1596 * of the requested size, due to either severe fragmentation or the pool 1597 * being nearly full, it calls zio_write_gang_block() to construct the 1598 * block from smaller fragments. 1599 * 1600 * A gang block consists of a gang header (zio_gbh_phys_t) and up to 1601 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like 1602 * an indirect block: it's an array of block pointers. It consumes 1603 * only one sector and hence is allocatable regardless of fragmentation. 1604 * The gang header's bps point to its gang members, which hold the data. 1605 * 1606 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 1607 * as the verifier to ensure uniqueness of the SHA256 checksum. 1608 * Critically, the gang block bp's blk_cksum is the checksum of the data, 1609 * not the gang header. This ensures that data block signatures (needed for 1610 * deduplication) are independent of how the block is physically stored. 1611 * 1612 * Gang blocks can be nested: a gang member may itself be a gang block. 1613 * Thus every gang block is a tree in which root and all interior nodes are 1614 * gang headers, and the leaves are normal blocks that contain user data. 1615 * The root of the gang tree is called the gang leader. 1616 * 1617 * To perform any operation (read, rewrite, free, claim) on a gang block, 1618 * zio_gang_assemble() first assembles the gang tree (minus data leaves) 1619 * in the io_gang_tree field of the original logical i/o by recursively 1620 * reading the gang leader and all gang headers below it. This yields 1621 * an in-core tree containing the contents of every gang header and the 1622 * bps for every constituent of the gang block. 1623 * 1624 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 1625 * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 1626 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 1627 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 1628 * zio_read_gang() is a wrapper around zio_read() that omits reading gang 1629 * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 1630 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 1631 * of the gang header plus zio_checksum_compute() of the data to update the 1632 * gang header's blk_cksum as described above. 1633 * 1634 * The two-phase assemble/issue model solves the problem of partial failure -- 1635 * what if you'd freed part of a gang block but then couldn't read the 1636 * gang header for another part? Assembling the entire gang tree first 1637 * ensures that all the necessary gang header I/O has succeeded before 1638 * starting the actual work of free, claim, or write. Once the gang tree 1639 * is assembled, free and claim are in-memory operations that cannot fail. 1640 * 1641 * In the event that a gang write fails, zio_dva_unallocate() walks the 1642 * gang tree to immediately free (i.e. insert back into the space map) 1643 * everything we've allocated. This ensures that we don't get ENOSPC 1644 * errors during repeated suspend/resume cycles due to a flaky device. 1645 * 1646 * Gang rewrites only happen during sync-to-convergence. If we can't assemble 1647 * the gang tree, we won't modify the block, so we can safely defer the free 1648 * (knowing that the block is still intact). If we *can* assemble the gang 1649 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 1650 * each constituent bp and we can allocate a new block on the next sync pass. 1651 * 1652 * In all cases, the gang tree allows complete recovery from partial failure. 1653 * ========================================================================== 1654 */ 1655 1656static zio_t * 1657zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1658{ 1659 if (gn != NULL) 1660 return (pio); 1661 1662 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp), 1663 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1664 &pio->io_bookmark)); 1665} 1666 1667zio_t * 1668zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1669{ 1670 zio_t *zio; 1671 1672 if (gn != NULL) { 1673 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1674 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority, 1675 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1676 /* 1677 * As we rewrite each gang header, the pipeline will compute 1678 * a new gang block header checksum for it; but no one will 1679 * compute a new data checksum, so we do that here. The one 1680 * exception is the gang leader: the pipeline already computed 1681 * its data checksum because that stage precedes gang assembly. 1682 * (Presently, nothing actually uses interior data checksums; 1683 * this is just good hygiene.) 1684 */ 1685 if (gn != pio->io_gang_leader->io_gang_tree) { 1686 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 1687 data, BP_GET_PSIZE(bp)); 1688 } 1689 /* 1690 * If we are here to damage data for testing purposes, 1691 * leave the GBH alone so that we can detect the damage. 1692 */ 1693 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 1694 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 1695 } else { 1696 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1697 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority, 1698 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1699 } 1700 1701 return (zio); 1702} 1703 1704/* ARGSUSED */ 1705zio_t * 1706zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1707{ 1708 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 1709 BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp), 1710 ZIO_GANG_CHILD_FLAGS(pio))); 1711} 1712 1713/* ARGSUSED */ 1714zio_t * 1715zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data) 1716{ 1717 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 1718 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 1719} 1720 1721static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 1722 NULL, 1723 zio_read_gang, 1724 zio_rewrite_gang, 1725 zio_free_gang, 1726 zio_claim_gang, 1727 NULL 1728}; 1729 1730static void zio_gang_tree_assemble_done(zio_t *zio); 1731 1732static zio_gang_node_t * 1733zio_gang_node_alloc(zio_gang_node_t **gnpp) 1734{ 1735 zio_gang_node_t *gn; 1736 1737 ASSERT(*gnpp == NULL); 1738 1739 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP); 1740 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE); 1741 *gnpp = gn; 1742 1743 return (gn); 1744} 1745 1746static void 1747zio_gang_node_free(zio_gang_node_t **gnpp) 1748{ 1749 zio_gang_node_t *gn = *gnpp; 1750 1751 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1752 ASSERT(gn->gn_child[g] == NULL); 1753 1754 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE); 1755 kmem_free(gn, sizeof (*gn)); 1756 *gnpp = NULL; 1757} 1758 1759static void 1760zio_gang_tree_free(zio_gang_node_t **gnpp) 1761{ 1762 zio_gang_node_t *gn = *gnpp; 1763 1764 if (gn == NULL) 1765 return; 1766 1767 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 1768 zio_gang_tree_free(&gn->gn_child[g]); 1769 1770 zio_gang_node_free(gnpp); 1771} 1772 1773static void 1774zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 1775{ 1776 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp); 1777 1778 ASSERT(gio->io_gang_leader == gio); 1779 ASSERT(BP_IS_GANG(bp)); 1780 1781 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh, 1782 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn, 1783 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 1784} 1785 1786static void 1787zio_gang_tree_assemble_done(zio_t *zio) 1788{ 1789 zio_t *gio = zio->io_gang_leader; 1790 zio_gang_node_t *gn = zio->io_private; 1791 blkptr_t *bp = zio->io_bp; 1792 1793 ASSERT(gio == zio_unique_parent(zio)); 1794 ASSERT(zio->io_child_count == 0); 1795 1796 if (zio->io_error) 1797 return; 1798 1799 if (BP_SHOULD_BYTESWAP(bp)) 1800 byteswap_uint64_array(zio->io_data, zio->io_size); 1801 1802 ASSERT(zio->io_data == gn->gn_gbh); 1803 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 1804 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1805 1806 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1807 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1808 if (!BP_IS_GANG(gbp)) 1809 continue; 1810 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 1811 } 1812} 1813 1814static void 1815zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data) 1816{ 1817 zio_t *gio = pio->io_gang_leader; 1818 zio_t *zio; 1819 1820 ASSERT(BP_IS_GANG(bp) == !!gn); 1821 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 1822 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 1823 1824 /* 1825 * If you're a gang header, your data is in gn->gn_gbh. 1826 * If you're a gang member, your data is in 'data' and gn == NULL. 1827 */ 1828 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data); 1829 1830 if (gn != NULL) { 1831 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 1832 1833 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1834 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 1835 if (BP_IS_HOLE(gbp)) 1836 continue; 1837 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data); 1838 data = (char *)data + BP_GET_PSIZE(gbp); 1839 } 1840 } 1841 1842 if (gn == gio->io_gang_tree && gio->io_data != NULL) 1843 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data); 1844 1845 if (zio != pio) 1846 zio_nowait(zio); 1847} 1848 1849static int 1850zio_gang_assemble(zio_t **ziop) 1851{ 1852 zio_t *zio = *ziop; 1853 blkptr_t *bp = zio->io_bp; 1854 1855 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 1856 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1857 1858 zio->io_gang_leader = zio; 1859 1860 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 1861 1862 return (ZIO_PIPELINE_CONTINUE); 1863} 1864 1865static int 1866zio_gang_issue(zio_t **ziop) 1867{ 1868 zio_t *zio = *ziop; 1869 blkptr_t *bp = zio->io_bp; 1870 1871 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE)) 1872 return (ZIO_PIPELINE_STOP); 1873 1874 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 1875 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 1876 1877 if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 1878 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data); 1879 else 1880 zio_gang_tree_free(&zio->io_gang_tree); 1881 1882 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1883 1884 return (ZIO_PIPELINE_CONTINUE); 1885} 1886 1887static void 1888zio_write_gang_member_ready(zio_t *zio) 1889{ 1890 zio_t *pio = zio_unique_parent(zio); 1891 zio_t *gio = zio->io_gang_leader; 1892 dva_t *cdva = zio->io_bp->blk_dva; 1893 dva_t *pdva = pio->io_bp->blk_dva; 1894 uint64_t asize; 1895 1896 if (BP_IS_HOLE(zio->io_bp)) 1897 return; 1898 1899 ASSERT(BP_IS_HOLE(&zio->io_bp_orig)); 1900 1901 ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 1902 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 1903 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 1904 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 1905 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 1906 1907 mutex_enter(&pio->io_lock); 1908 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 1909 ASSERT(DVA_GET_GANG(&pdva[d])); 1910 asize = DVA_GET_ASIZE(&pdva[d]); 1911 asize += DVA_GET_ASIZE(&cdva[d]); 1912 DVA_SET_ASIZE(&pdva[d], asize); 1913 } 1914 mutex_exit(&pio->io_lock); 1915} 1916 1917static int 1918zio_write_gang_block(zio_t *pio) 1919{ 1920 spa_t *spa = pio->io_spa; 1921 blkptr_t *bp = pio->io_bp; 1922 zio_t *gio = pio->io_gang_leader; 1923 zio_t *zio; 1924 zio_gang_node_t *gn, **gnpp; 1925 zio_gbh_phys_t *gbh; 1926 uint64_t txg = pio->io_txg; 1927 uint64_t resid = pio->io_size; 1928 uint64_t lsize; 1929 int copies = gio->io_prop.zp_copies; 1930 int gbh_copies = MIN(copies + 1, spa_max_replication(spa)); 1931 zio_prop_t zp; 1932 int error; 1933 1934 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE, 1935 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, 1936 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER); 1937 if (error) { 1938 pio->io_error = error; 1939 return (ZIO_PIPELINE_CONTINUE); 1940 } 1941 1942 if (pio == gio) { 1943 gnpp = &gio->io_gang_tree; 1944 } else { 1945 gnpp = pio->io_private; 1946 ASSERT(pio->io_ready == zio_write_gang_member_ready); 1947 } 1948 1949 gn = zio_gang_node_alloc(gnpp); 1950 gbh = gn->gn_gbh; 1951 bzero(gbh, SPA_GANGBLOCKSIZE); 1952 1953 /* 1954 * Create the gang header. 1955 */ 1956 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL, 1957 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1958 1959 /* 1960 * Create and nowait the gang children. 1961 */ 1962 for (int g = 0; resid != 0; resid -= lsize, g++) { 1963 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g), 1964 SPA_MINBLOCKSIZE); 1965 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid); 1966 1967 zp.zp_checksum = gio->io_prop.zp_checksum; 1968 zp.zp_compress = ZIO_COMPRESS_OFF; 1969 zp.zp_type = DMU_OT_NONE; 1970 zp.zp_level = 0; 1971 zp.zp_copies = gio->io_prop.zp_copies; 1972 zp.zp_dedup = B_FALSE; 1973 zp.zp_dedup_verify = B_FALSE; 1974 zp.zp_nopwrite = B_FALSE; 1975 1976 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g], 1977 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp, 1978 zio_write_gang_member_ready, NULL, NULL, &gn->gn_child[g], 1979 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1980 &pio->io_bookmark)); 1981 } 1982 1983 /* 1984 * Set pio's pipeline to just wait for zio to finish. 1985 */ 1986 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1987 1988 zio_nowait(zio); 1989 1990 return (ZIO_PIPELINE_CONTINUE); 1991} 1992 1993/* 1994 * The zio_nop_write stage in the pipeline determines if allocating 1995 * a new bp is necessary. By leveraging a cryptographically secure checksum, 1996 * such as SHA256, we can compare the checksums of the new data and the old 1997 * to determine if allocating a new block is required. The nopwrite 1998 * feature can handle writes in either syncing or open context (i.e. zil 1999 * writes) and as a result is mutually exclusive with dedup. 2000 */ 2001static int 2002zio_nop_write(zio_t **ziop) 2003{ 2004 zio_t *zio = *ziop; 2005 blkptr_t *bp = zio->io_bp; 2006 blkptr_t *bp_orig = &zio->io_bp_orig; 2007 zio_prop_t *zp = &zio->io_prop; 2008 2009 ASSERT(BP_GET_LEVEL(bp) == 0); 2010 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 2011 ASSERT(zp->zp_nopwrite); 2012 ASSERT(!zp->zp_dedup); 2013 ASSERT(zio->io_bp_override == NULL); 2014 ASSERT(IO_IS_ALLOCATING(zio)); 2015 2016 /* 2017 * Check to see if the original bp and the new bp have matching 2018 * characteristics (i.e. same checksum, compression algorithms, etc). 2019 * If they don't then just continue with the pipeline which will 2020 * allocate a new bp. 2021 */ 2022 if (BP_IS_HOLE(bp_orig) || 2023 !zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_dedup || 2024 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) || 2025 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) || 2026 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) || 2027 zp->zp_copies != BP_GET_NDVAS(bp_orig)) 2028 return (ZIO_PIPELINE_CONTINUE); 2029 2030 /* 2031 * If the checksums match then reset the pipeline so that we 2032 * avoid allocating a new bp and issuing any I/O. 2033 */ 2034 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) { 2035 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup); 2036 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig)); 2037 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig)); 2038 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF); 2039 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop, 2040 sizeof (uint64_t)) == 0); 2041 2042 *bp = *bp_orig; 2043 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2044 zio->io_flags |= ZIO_FLAG_NOPWRITE; 2045 } 2046 2047 return (ZIO_PIPELINE_CONTINUE); 2048} 2049 2050/* 2051 * ========================================================================== 2052 * Dedup 2053 * ========================================================================== 2054 */ 2055static void 2056zio_ddt_child_read_done(zio_t *zio) 2057{ 2058 blkptr_t *bp = zio->io_bp; 2059 ddt_entry_t *dde = zio->io_private; 2060 ddt_phys_t *ddp; 2061 zio_t *pio = zio_unique_parent(zio); 2062 2063 mutex_enter(&pio->io_lock); 2064 ddp = ddt_phys_select(dde, bp); 2065 if (zio->io_error == 0) 2066 ddt_phys_clear(ddp); /* this ddp doesn't need repair */ 2067 if (zio->io_error == 0 && dde->dde_repair_data == NULL) 2068 dde->dde_repair_data = zio->io_data; 2069 else 2070 zio_buf_free(zio->io_data, zio->io_size); 2071 mutex_exit(&pio->io_lock); 2072} 2073 2074static int 2075zio_ddt_read_start(zio_t **ziop) 2076{ 2077 zio_t *zio = *ziop; 2078 blkptr_t *bp = zio->io_bp; 2079 2080 ASSERT(BP_GET_DEDUP(bp)); 2081 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2082 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2083 2084 if (zio->io_child_error[ZIO_CHILD_DDT]) { 2085 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2086 ddt_entry_t *dde = ddt_repair_start(ddt, bp); 2087 ddt_phys_t *ddp = dde->dde_phys; 2088 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp); 2089 blkptr_t blk; 2090 2091 ASSERT(zio->io_vsd == NULL); 2092 zio->io_vsd = dde; 2093 2094 if (ddp_self == NULL) 2095 return (ZIO_PIPELINE_CONTINUE); 2096 2097 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2098 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self) 2099 continue; 2100 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp, 2101 &blk); 2102 zio_nowait(zio_read(zio, zio->io_spa, &blk, 2103 zio_buf_alloc(zio->io_size), zio->io_size, 2104 zio_ddt_child_read_done, dde, zio->io_priority, 2105 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE, 2106 &zio->io_bookmark)); 2107 } 2108 return (ZIO_PIPELINE_CONTINUE); 2109 } 2110 2111 zio_nowait(zio_read(zio, zio->io_spa, bp, 2112 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority, 2113 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 2114 2115 return (ZIO_PIPELINE_CONTINUE); 2116} 2117 2118static int 2119zio_ddt_read_done(zio_t **ziop) 2120{ 2121 zio_t *zio = *ziop; 2122 blkptr_t *bp = zio->io_bp; 2123 2124 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE)) 2125 return (ZIO_PIPELINE_STOP); 2126 2127 ASSERT(BP_GET_DEDUP(bp)); 2128 ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2129 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2130 2131 if (zio->io_child_error[ZIO_CHILD_DDT]) { 2132 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2133 ddt_entry_t *dde = zio->io_vsd; 2134 if (ddt == NULL) { 2135 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 2136 return (ZIO_PIPELINE_CONTINUE); 2137 } 2138 if (dde == NULL) { 2139 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 2140 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 2141 return (ZIO_PIPELINE_STOP); 2142 } 2143 if (dde->dde_repair_data != NULL) { 2144 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size); 2145 zio->io_child_error[ZIO_CHILD_DDT] = 0; 2146 } 2147 ddt_repair_done(ddt, dde); 2148 zio->io_vsd = NULL; 2149 } 2150 2151 ASSERT(zio->io_vsd == NULL); 2152 2153 return (ZIO_PIPELINE_CONTINUE); 2154} 2155 2156static boolean_t 2157zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 2158{ 2159 spa_t *spa = zio->io_spa; 2160 2161 /* 2162 * Note: we compare the original data, not the transformed data, 2163 * because when zio->io_bp is an override bp, we will not have 2164 * pushed the I/O transforms. That's an important optimization 2165 * because otherwise we'd compress/encrypt all dmu_sync() data twice. 2166 */ 2167 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2168 zio_t *lio = dde->dde_lead_zio[p]; 2169 2170 if (lio != NULL) { 2171 return (lio->io_orig_size != zio->io_orig_size || 2172 bcmp(zio->io_orig_data, lio->io_orig_data, 2173 zio->io_orig_size) != 0); 2174 } 2175 } 2176 2177 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2178 ddt_phys_t *ddp = &dde->dde_phys[p]; 2179 2180 if (ddp->ddp_phys_birth != 0) { 2181 arc_buf_t *abuf = NULL; 2182 uint32_t aflags = ARC_WAIT; 2183 blkptr_t blk = *zio->io_bp; 2184 int error; 2185 2186 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 2187 2188 ddt_exit(ddt); 2189 2190 error = arc_read(NULL, spa, &blk, 2191 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 2192 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 2193 &aflags, &zio->io_bookmark); 2194 2195 if (error == 0) { 2196 if (arc_buf_size(abuf) != zio->io_orig_size || 2197 bcmp(abuf->b_data, zio->io_orig_data, 2198 zio->io_orig_size) != 0) 2199 error = SET_ERROR(EEXIST); 2200 VERIFY(arc_buf_remove_ref(abuf, &abuf)); 2201 } 2202 2203 ddt_enter(ddt); 2204 return (error != 0); 2205 } 2206 } 2207 2208 return (B_FALSE); 2209} 2210 2211static void 2212zio_ddt_child_write_ready(zio_t *zio) 2213{ 2214 int p = zio->io_prop.zp_copies; 2215 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2216 ddt_entry_t *dde = zio->io_private; 2217 ddt_phys_t *ddp = &dde->dde_phys[p]; 2218 zio_t *pio; 2219 2220 if (zio->io_error) 2221 return; 2222 2223 ddt_enter(ddt); 2224 2225 ASSERT(dde->dde_lead_zio[p] == zio); 2226 2227 ddt_phys_fill(ddp, zio->io_bp); 2228 2229 while ((pio = zio_walk_parents(zio)) != NULL) 2230 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg); 2231 2232 ddt_exit(ddt); 2233} 2234 2235static void 2236zio_ddt_child_write_done(zio_t *zio) 2237{ 2238 int p = zio->io_prop.zp_copies; 2239 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2240 ddt_entry_t *dde = zio->io_private; 2241 ddt_phys_t *ddp = &dde->dde_phys[p]; 2242 2243 ddt_enter(ddt); 2244 2245 ASSERT(ddp->ddp_refcnt == 0); 2246 ASSERT(dde->dde_lead_zio[p] == zio); 2247 dde->dde_lead_zio[p] = NULL; 2248 2249 if (zio->io_error == 0) { 2250 while (zio_walk_parents(zio) != NULL) 2251 ddt_phys_addref(ddp); 2252 } else { 2253 ddt_phys_clear(ddp); 2254 } 2255 2256 ddt_exit(ddt); 2257} 2258 2259static void 2260zio_ddt_ditto_write_done(zio_t *zio) 2261{ 2262 int p = DDT_PHYS_DITTO; 2263 zio_prop_t *zp = &zio->io_prop; 2264 blkptr_t *bp = zio->io_bp; 2265 ddt_t *ddt = ddt_select(zio->io_spa, bp); 2266 ddt_entry_t *dde = zio->io_private; 2267 ddt_phys_t *ddp = &dde->dde_phys[p]; 2268 ddt_key_t *ddk = &dde->dde_key; 2269 2270 ddt_enter(ddt); 2271 2272 ASSERT(ddp->ddp_refcnt == 0); 2273 ASSERT(dde->dde_lead_zio[p] == zio); 2274 dde->dde_lead_zio[p] = NULL; 2275 2276 if (zio->io_error == 0) { 2277 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum)); 2278 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP); 2279 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp)); 2280 if (ddp->ddp_phys_birth != 0) 2281 ddt_phys_free(ddt, ddk, ddp, zio->io_txg); 2282 ddt_phys_fill(ddp, bp); 2283 } 2284 2285 ddt_exit(ddt); 2286} 2287 2288static int 2289zio_ddt_write(zio_t **ziop) 2290{ 2291 zio_t *zio = *ziop; 2292 spa_t *spa = zio->io_spa; 2293 blkptr_t *bp = zio->io_bp; 2294 uint64_t txg = zio->io_txg; 2295 zio_prop_t *zp = &zio->io_prop; 2296 int p = zp->zp_copies; 2297 int ditto_copies; 2298 zio_t *cio = NULL; 2299 zio_t *dio = NULL; 2300 ddt_t *ddt = ddt_select(spa, bp); 2301 ddt_entry_t *dde; 2302 ddt_phys_t *ddp; 2303 2304 ASSERT(BP_GET_DEDUP(bp)); 2305 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 2306 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 2307 2308 ddt_enter(ddt); 2309 dde = ddt_lookup(ddt, bp, B_TRUE); 2310 ddp = &dde->dde_phys[p]; 2311 2312 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 2313 /* 2314 * If we're using a weak checksum, upgrade to a strong checksum 2315 * and try again. If we're already using a strong checksum, 2316 * we can't resolve it, so just convert to an ordinary write. 2317 * (And automatically e-mail a paper to Nature?) 2318 */ 2319 if (!zio_checksum_table[zp->zp_checksum].ci_dedup) { 2320 zp->zp_checksum = spa_dedup_checksum(spa); 2321 zio_pop_transforms(zio); 2322 zio->io_stage = ZIO_STAGE_OPEN; 2323 BP_ZERO(bp); 2324 } else { 2325 zp->zp_dedup = B_FALSE; 2326 } 2327 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2328 ddt_exit(ddt); 2329 return (ZIO_PIPELINE_CONTINUE); 2330 } 2331 2332 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp); 2333 ASSERT(ditto_copies < SPA_DVAS_PER_BP); 2334 2335 if (ditto_copies > ddt_ditto_copies_present(dde) && 2336 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) { 2337 zio_prop_t czp = *zp; 2338 2339 czp.zp_copies = ditto_copies; 2340 2341 /* 2342 * If we arrived here with an override bp, we won't have run 2343 * the transform stack, so we won't have the data we need to 2344 * generate a child i/o. So, toss the override bp and restart. 2345 * This is safe, because using the override bp is just an 2346 * optimization; and it's rare, so the cost doesn't matter. 2347 */ 2348 if (zio->io_bp_override) { 2349 zio_pop_transforms(zio); 2350 zio->io_stage = ZIO_STAGE_OPEN; 2351 zio->io_pipeline = ZIO_WRITE_PIPELINE; 2352 zio->io_bp_override = NULL; 2353 BP_ZERO(bp); 2354 ddt_exit(ddt); 2355 return (ZIO_PIPELINE_CONTINUE); 2356 } 2357 2358 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2359 zio->io_orig_size, &czp, NULL, NULL, 2360 zio_ddt_ditto_write_done, dde, zio->io_priority, 2361 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2362 2363 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL); 2364 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio; 2365 } 2366 2367 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) { 2368 if (ddp->ddp_phys_birth != 0) 2369 ddt_bp_fill(ddp, bp, txg); 2370 if (dde->dde_lead_zio[p] != NULL) 2371 zio_add_child(zio, dde->dde_lead_zio[p]); 2372 else 2373 ddt_phys_addref(ddp); 2374 } else if (zio->io_bp_override) { 2375 ASSERT(bp->blk_birth == txg); 2376 ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 2377 ddt_phys_fill(ddp, bp); 2378 ddt_phys_addref(ddp); 2379 } else { 2380 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data, 2381 zio->io_orig_size, zp, zio_ddt_child_write_ready, NULL, 2382 zio_ddt_child_write_done, dde, zio->io_priority, 2383 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2384 2385 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL); 2386 dde->dde_lead_zio[p] = cio; 2387 } 2388 2389 ddt_exit(ddt); 2390 2391 if (cio) 2392 zio_nowait(cio); 2393 if (dio) 2394 zio_nowait(dio); 2395 2396 return (ZIO_PIPELINE_CONTINUE); 2397} 2398 2399ddt_entry_t *freedde; /* for debugging */ 2400 2401static int 2402zio_ddt_free(zio_t **ziop) 2403{ 2404 zio_t *zio = *ziop; 2405 spa_t *spa = zio->io_spa; 2406 blkptr_t *bp = zio->io_bp; 2407 ddt_t *ddt = ddt_select(spa, bp); 2408 ddt_entry_t *dde; 2409 ddt_phys_t *ddp; 2410 2411 ASSERT(BP_GET_DEDUP(bp)); 2412 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2413 2414 ddt_enter(ddt); 2415 freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 2416 ddp = ddt_phys_select(dde, bp); 2417 ddt_phys_decref(ddp); 2418 ddt_exit(ddt); 2419 2420 return (ZIO_PIPELINE_CONTINUE); 2421} 2422 2423/* 2424 * ========================================================================== 2425 * Allocate and free blocks 2426 * ========================================================================== 2427 */ 2428static int 2429zio_dva_allocate(zio_t **ziop) 2430{ 2431 zio_t *zio = *ziop; 2432 spa_t *spa = zio->io_spa; 2433 metaslab_class_t *mc = spa_normal_class(spa); 2434 blkptr_t *bp = zio->io_bp; 2435 int error; 2436 int flags = 0; 2437 2438 if (zio->io_gang_leader == NULL) { 2439 ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2440 zio->io_gang_leader = zio; 2441 } 2442 2443 ASSERT(BP_IS_HOLE(bp)); 2444 ASSERT0(BP_GET_NDVAS(bp)); 2445 ASSERT3U(zio->io_prop.zp_copies, >, 0); 2446 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 2447 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 2448 2449 /* 2450 * The dump device does not support gang blocks so allocation on 2451 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid 2452 * the "fast" gang feature. 2453 */ 2454 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0; 2455 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ? 2456 METASLAB_GANG_CHILD : 0; 2457 error = metaslab_alloc(spa, mc, zio->io_size, bp, 2458 zio->io_prop.zp_copies, zio->io_txg, NULL, flags); 2459 2460 if (error) { 2461 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, " 2462 "size %llu, error %d", spa_name(spa), zio, zio->io_size, 2463 error); 2464 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) 2465 return (zio_write_gang_block(zio)); 2466 zio->io_error = error; 2467 } 2468 2469 return (ZIO_PIPELINE_CONTINUE); 2470} 2471 2472static int 2473zio_dva_free(zio_t **ziop) 2474{ 2475 zio_t *zio = *ziop; 2476 2477 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 2478 2479 return (ZIO_PIPELINE_CONTINUE); 2480} 2481 2482static int 2483zio_dva_claim(zio_t **ziop) 2484{ 2485 zio_t *zio = *ziop; 2486 int error; 2487 2488 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 2489 if (error) 2490 zio->io_error = error; 2491 2492 return (ZIO_PIPELINE_CONTINUE); 2493} 2494 2495/* 2496 * Undo an allocation. This is used by zio_done() when an I/O fails 2497 * and we want to give back the block we just allocated. 2498 * This handles both normal blocks and gang blocks. 2499 */ 2500static void 2501zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 2502{ 2503 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 2504 ASSERT(zio->io_bp_override == NULL); 2505 2506 if (!BP_IS_HOLE(bp)) 2507 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE); 2508 2509 if (gn != NULL) { 2510 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2511 zio_dva_unallocate(zio, gn->gn_child[g], 2512 &gn->gn_gbh->zg_blkptr[g]); 2513 } 2514 } 2515} 2516 2517/* 2518 * Try to allocate an intent log block. Return 0 on success, errno on failure. 2519 */ 2520int 2521zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp, 2522 uint64_t size, boolean_t use_slog) 2523{ 2524 int error = 1; 2525 2526 ASSERT(txg > spa_syncing_txg(spa)); 2527 2528 /* 2529 * ZIL blocks are always contiguous (i.e. not gang blocks) so we 2530 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang" 2531 * when allocating them. 2532 */ 2533 if (use_slog) { 2534 error = metaslab_alloc(spa, spa_log_class(spa), size, 2535 new_bp, 1, txg, old_bp, 2536 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID); 2537 } 2538 2539 if (error) { 2540 error = metaslab_alloc(spa, spa_normal_class(spa), size, 2541 new_bp, 1, txg, old_bp, 2542 METASLAB_HINTBP_AVOID); 2543 } 2544 2545 if (error == 0) { 2546 BP_SET_LSIZE(new_bp, size); 2547 BP_SET_PSIZE(new_bp, size); 2548 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 2549 BP_SET_CHECKSUM(new_bp, 2550 spa_version(spa) >= SPA_VERSION_SLIM_ZIL 2551 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 2552 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 2553 BP_SET_LEVEL(new_bp, 0); 2554 BP_SET_DEDUP(new_bp, 0); 2555 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 2556 } 2557 2558 return (error); 2559} 2560 2561/* 2562 * Free an intent log block. 2563 */ 2564void 2565zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp) 2566{ 2567 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG); 2568 ASSERT(!BP_IS_GANG(bp)); 2569 2570 zio_free(spa, txg, bp); 2571} 2572 2573/* 2574 * ========================================================================== 2575 * Read, write and delete to physical devices 2576 * ========================================================================== 2577 */ 2578static int 2579zio_vdev_io_start(zio_t **ziop) 2580{ 2581 zio_t *zio = *ziop; 2582 vdev_t *vd = zio->io_vd; 2583 uint64_t align; 2584 spa_t *spa = zio->io_spa; 2585 2586 ASSERT(zio->io_error == 0); 2587 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0); 2588 2589 if (vd == NULL) { 2590 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2591 spa_config_enter(spa, SCL_ZIO, zio, RW_READER); 2592 2593 /* 2594 * The mirror_ops handle multiple DVAs in a single BP. 2595 */ 2596 return (vdev_mirror_ops.vdev_op_io_start(zio)); 2597 } 2598 2599 if (vd->vdev_ops->vdev_op_leaf && zio->io_type == ZIO_TYPE_FREE && 2600 zio->io_priority == ZIO_PRIORITY_NOW) { 2601 trim_map_free(vd, zio->io_offset, zio->io_size, zio->io_txg); 2602 return (ZIO_PIPELINE_CONTINUE); 2603 } 2604 2605 /* 2606 * We keep track of time-sensitive I/Os so that the scan thread 2607 * can quickly react to certain workloads. In particular, we care 2608 * about non-scrubbing, top-level reads and writes with the following 2609 * characteristics: 2610 * - synchronous writes of user data to non-slog devices 2611 * - any reads of user data 2612 * When these conditions are met, adjust the timestamp of spa_last_io 2613 * which allows the scan thread to adjust its workload accordingly. 2614 */ 2615 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL && 2616 vd == vd->vdev_top && !vd->vdev_islog && 2617 zio->io_bookmark.zb_objset != DMU_META_OBJSET && 2618 zio->io_txg != spa_syncing_txg(spa)) { 2619 uint64_t old = spa->spa_last_io; 2620 uint64_t new = ddi_get_lbolt64(); 2621 if (old != new) 2622 (void) atomic_cas_64(&spa->spa_last_io, old, new); 2623 } 2624 2625 align = 1ULL << vd->vdev_top->vdev_ashift; 2626 2627 if (P2PHASE(zio->io_size, align) != 0) { 2628 uint64_t asize = P2ROUNDUP(zio->io_size, align); 2629 char *abuf = NULL; 2630 if (zio->io_type == ZIO_TYPE_READ || 2631 zio->io_type == ZIO_TYPE_WRITE) 2632 abuf = zio_buf_alloc(asize); 2633 ASSERT(vd == vd->vdev_top); 2634 if (zio->io_type == ZIO_TYPE_WRITE) { 2635 bcopy(zio->io_data, abuf, zio->io_size); 2636 bzero(abuf + zio->io_size, asize - zio->io_size); 2637 } 2638 zio_push_transform(zio, abuf, asize, abuf ? asize : 0, 2639 zio_subblock); 2640 } 2641 2642 ASSERT(P2PHASE(zio->io_offset, align) == 0); 2643 ASSERT(P2PHASE(zio->io_size, align) == 0); 2644 VERIFY(zio->io_type == ZIO_TYPE_READ || spa_writeable(spa)); 2645 2646 /* 2647 * If this is a repair I/O, and there's no self-healing involved -- 2648 * that is, we're just resilvering what we expect to resilver -- 2649 * then don't do the I/O unless zio's txg is actually in vd's DTL. 2650 * This prevents spurious resilvering with nested replication. 2651 * For example, given a mirror of mirrors, (A+B)+(C+D), if only 2652 * A is out of date, we'll read from C+D, then use the data to 2653 * resilver A+B -- but we don't actually want to resilver B, just A. 2654 * The top-level mirror has no way to know this, so instead we just 2655 * discard unnecessary repairs as we work our way down the vdev tree. 2656 * The same logic applies to any form of nested replication: 2657 * ditto + mirror, RAID-Z + replacing, etc. This covers them all. 2658 */ 2659 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 2660 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 2661 zio->io_txg != 0 && /* not a delegated i/o */ 2662 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 2663 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 2664 zio_vdev_io_bypass(zio); 2665 return (ZIO_PIPELINE_CONTINUE); 2666 } 2667 2668 if (vd->vdev_ops->vdev_op_leaf) { 2669 switch (zio->io_type) { 2670 case ZIO_TYPE_READ: 2671 if (vdev_cache_read(zio)) 2672 return (ZIO_PIPELINE_CONTINUE); 2673 /* FALLTHROUGH */ 2674 case ZIO_TYPE_WRITE: 2675 case ZIO_TYPE_FREE: 2676 if ((zio = vdev_queue_io(zio)) == NULL) 2677 return (ZIO_PIPELINE_STOP); 2678 *ziop = zio; 2679 2680 if (!vdev_accessible(vd, zio)) { 2681 zio->io_error = SET_ERROR(ENXIO); 2682 zio_interrupt(zio); 2683 return (ZIO_PIPELINE_STOP); 2684 } 2685 break; 2686 } 2687 /* 2688 * Note that we ignore repair writes for TRIM because they can 2689 * conflict with normal writes. This isn't an issue because, by 2690 * definition, we only repair blocks that aren't freed. 2691 */ 2692 if (zio->io_type == ZIO_TYPE_WRITE && 2693 !(zio->io_flags & ZIO_FLAG_IO_REPAIR) && 2694 !trim_map_write_start(zio)) 2695 return (ZIO_PIPELINE_STOP); 2696 } 2697 2698 return (vd->vdev_ops->vdev_op_io_start(zio)); 2699} 2700 2701static int 2702zio_vdev_io_done(zio_t **ziop) 2703{ 2704 zio_t *zio = *ziop; 2705 vdev_t *vd = zio->io_vd; 2706 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 2707 boolean_t unexpected_error = B_FALSE; 2708 2709 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2710 return (ZIO_PIPELINE_STOP); 2711 2712 ASSERT(zio->io_type == ZIO_TYPE_READ || 2713 zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_FREE); 2714 2715 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 2716 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE || 2717 zio->io_type == ZIO_TYPE_FREE)) { 2718 2719 if (zio->io_type == ZIO_TYPE_WRITE && 2720 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) 2721 trim_map_write_done(zio); 2722 2723 vdev_queue_io_done(zio); 2724 2725 if (zio->io_type == ZIO_TYPE_WRITE) 2726 vdev_cache_write(zio); 2727 2728 if (zio_injection_enabled && zio->io_error == 0) 2729 zio->io_error = zio_handle_device_injection(vd, 2730 zio, EIO); 2731 2732 if (zio_injection_enabled && zio->io_error == 0) 2733 zio->io_error = zio_handle_label_injection(zio, EIO); 2734 2735 if (zio->io_error) { 2736 if (zio->io_error == ENOTSUP && 2737 zio->io_type == ZIO_TYPE_FREE) { 2738 /* Not all devices support TRIM. */ 2739 } else if (!vdev_accessible(vd, zio)) { 2740 zio->io_error = SET_ERROR(ENXIO); 2741 } else { 2742 unexpected_error = B_TRUE; 2743 } 2744 } 2745 } 2746 2747 ops->vdev_op_io_done(zio); 2748 2749 if (unexpected_error) 2750 VERIFY(vdev_probe(vd, zio) == NULL); 2751 2752 return (ZIO_PIPELINE_CONTINUE); 2753} 2754 2755/* 2756 * For non-raidz ZIOs, we can just copy aside the bad data read from the 2757 * disk, and use that to finish the checksum ereport later. 2758 */ 2759static void 2760zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 2761 const void *good_buf) 2762{ 2763 /* no processing needed */ 2764 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 2765} 2766 2767/*ARGSUSED*/ 2768void 2769zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored) 2770{ 2771 void *buf = zio_buf_alloc(zio->io_size); 2772 2773 bcopy(zio->io_data, buf, zio->io_size); 2774 2775 zcr->zcr_cbinfo = zio->io_size; 2776 zcr->zcr_cbdata = buf; 2777 zcr->zcr_finish = zio_vsd_default_cksum_finish; 2778 zcr->zcr_free = zio_buf_free; 2779} 2780 2781static int 2782zio_vdev_io_assess(zio_t **ziop) 2783{ 2784 zio_t *zio = *ziop; 2785 vdev_t *vd = zio->io_vd; 2786 2787 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE)) 2788 return (ZIO_PIPELINE_STOP); 2789 2790 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 2791 spa_config_exit(zio->io_spa, SCL_ZIO, zio); 2792 2793 if (zio->io_vsd != NULL) { 2794 zio->io_vsd_ops->vsd_free(zio); 2795 zio->io_vsd = NULL; 2796 } 2797 2798 if (zio_injection_enabled && zio->io_error == 0) 2799 zio->io_error = zio_handle_fault_injection(zio, EIO); 2800 2801 if (zio->io_type == ZIO_TYPE_FREE && 2802 zio->io_priority != ZIO_PRIORITY_NOW) { 2803 switch (zio->io_error) { 2804 case 0: 2805 ZIO_TRIM_STAT_INCR(bytes, zio->io_size); 2806 ZIO_TRIM_STAT_BUMP(success); 2807 break; 2808 case EOPNOTSUPP: 2809 ZIO_TRIM_STAT_BUMP(unsupported); 2810 break; 2811 default: 2812 ZIO_TRIM_STAT_BUMP(failed); 2813 break; 2814 } 2815 } 2816 2817 /* 2818 * If the I/O failed, determine whether we should attempt to retry it. 2819 * 2820 * On retry, we cut in line in the issue queue, since we don't want 2821 * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 2822 */ 2823 if (zio->io_error && vd == NULL && 2824 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 2825 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 2826 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 2827 zio->io_error = 0; 2828 zio->io_flags |= ZIO_FLAG_IO_RETRY | 2829 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE; 2830 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 2831 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 2832 zio_requeue_io_start_cut_in_line); 2833 return (ZIO_PIPELINE_STOP); 2834 } 2835 2836 /* 2837 * If we got an error on a leaf device, convert it to ENXIO 2838 * if the device is not accessible at all. 2839 */ 2840 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 2841 !vdev_accessible(vd, zio)) 2842 zio->io_error = SET_ERROR(ENXIO); 2843 2844 /* 2845 * If we can't write to an interior vdev (mirror or RAID-Z), 2846 * set vdev_cant_write so that we stop trying to allocate from it. 2847 */ 2848 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 2849 vd != NULL && !vd->vdev_ops->vdev_op_leaf) { 2850 vd->vdev_cant_write = B_TRUE; 2851 } 2852 2853 if (zio->io_error) 2854 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2855 2856 if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 2857 zio->io_physdone != NULL) { 2858 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED)); 2859 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV); 2860 zio->io_physdone(zio->io_logical); 2861 } 2862 2863 return (ZIO_PIPELINE_CONTINUE); 2864} 2865 2866void 2867zio_vdev_io_reissue(zio_t *zio) 2868{ 2869 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2870 ASSERT(zio->io_error == 0); 2871 2872 zio->io_stage >>= 1; 2873} 2874 2875void 2876zio_vdev_io_redone(zio_t *zio) 2877{ 2878 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 2879 2880 zio->io_stage >>= 1; 2881} 2882 2883void 2884zio_vdev_io_bypass(zio_t *zio) 2885{ 2886 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 2887 ASSERT(zio->io_error == 0); 2888 2889 zio->io_flags |= ZIO_FLAG_IO_BYPASS; 2890 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 2891} 2892 2893/* 2894 * ========================================================================== 2895 * Generate and verify checksums 2896 * ========================================================================== 2897 */ 2898static int 2899zio_checksum_generate(zio_t **ziop) 2900{ 2901 zio_t *zio = *ziop; 2902 blkptr_t *bp = zio->io_bp; 2903 enum zio_checksum checksum; 2904 2905 if (bp == NULL) { 2906 /* 2907 * This is zio_write_phys(). 2908 * We're either generating a label checksum, or none at all. 2909 */ 2910 checksum = zio->io_prop.zp_checksum; 2911 2912 if (checksum == ZIO_CHECKSUM_OFF) 2913 return (ZIO_PIPELINE_CONTINUE); 2914 2915 ASSERT(checksum == ZIO_CHECKSUM_LABEL); 2916 } else { 2917 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 2918 ASSERT(!IO_IS_ALLOCATING(zio)); 2919 checksum = ZIO_CHECKSUM_GANG_HEADER; 2920 } else { 2921 checksum = BP_GET_CHECKSUM(bp); 2922 } 2923 } 2924 2925 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size); 2926 2927 return (ZIO_PIPELINE_CONTINUE); 2928} 2929 2930static int 2931zio_checksum_verify(zio_t **ziop) 2932{ 2933 zio_t *zio = *ziop; 2934 zio_bad_cksum_t info; 2935 blkptr_t *bp = zio->io_bp; 2936 int error; 2937 2938 ASSERT(zio->io_vd != NULL); 2939 2940 if (bp == NULL) { 2941 /* 2942 * This is zio_read_phys(). 2943 * We're either verifying a label checksum, or nothing at all. 2944 */ 2945 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 2946 return (ZIO_PIPELINE_CONTINUE); 2947 2948 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL); 2949 } 2950 2951 if ((error = zio_checksum_error(zio, &info)) != 0) { 2952 zio->io_error = error; 2953 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2954 zfs_ereport_start_checksum(zio->io_spa, 2955 zio->io_vd, zio, zio->io_offset, 2956 zio->io_size, NULL, &info); 2957 } 2958 } 2959 2960 return (ZIO_PIPELINE_CONTINUE); 2961} 2962 2963/* 2964 * Called by RAID-Z to ensure we don't compute the checksum twice. 2965 */ 2966void 2967zio_checksum_verified(zio_t *zio) 2968{ 2969 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 2970} 2971 2972/* 2973 * ========================================================================== 2974 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 2975 * An error of 0 indicates success. ENXIO indicates whole-device failure, 2976 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO 2977 * indicate errors that are specific to one I/O, and most likely permanent. 2978 * Any other error is presumed to be worse because we weren't expecting it. 2979 * ========================================================================== 2980 */ 2981int 2982zio_worst_error(int e1, int e2) 2983{ 2984 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 2985 int r1, r2; 2986 2987 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 2988 if (e1 == zio_error_rank[r1]) 2989 break; 2990 2991 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 2992 if (e2 == zio_error_rank[r2]) 2993 break; 2994 2995 return (r1 > r2 ? e1 : e2); 2996} 2997 2998/* 2999 * ========================================================================== 3000 * I/O completion 3001 * ========================================================================== 3002 */ 3003static int 3004zio_ready(zio_t **ziop) 3005{ 3006 zio_t *zio = *ziop; 3007 blkptr_t *bp = zio->io_bp; 3008 zio_t *pio, *pio_next; 3009 3010 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) || 3011 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY)) 3012 return (ZIO_PIPELINE_STOP); 3013 3014 if (zio->io_ready) { 3015 ASSERT(IO_IS_ALLOCATING(zio)); 3016 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) || 3017 (zio->io_flags & ZIO_FLAG_NOPWRITE)); 3018 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0); 3019 3020 zio->io_ready(zio); 3021 } 3022 3023 if (bp != NULL && bp != &zio->io_bp_copy) 3024 zio->io_bp_copy = *bp; 3025 3026 if (zio->io_error) 3027 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3028 3029 mutex_enter(&zio->io_lock); 3030 zio->io_state[ZIO_WAIT_READY] = 1; 3031 pio = zio_walk_parents(zio); 3032 mutex_exit(&zio->io_lock); 3033 3034 /* 3035 * As we notify zio's parents, new parents could be added. 3036 * New parents go to the head of zio's io_parent_list, however, 3037 * so we will (correctly) not notify them. The remainder of zio's 3038 * io_parent_list, from 'pio_next' onward, cannot change because 3039 * all parents must wait for us to be done before they can be done. 3040 */ 3041 for (; pio != NULL; pio = pio_next) { 3042 pio_next = zio_walk_parents(zio); 3043 zio_notify_parent(pio, zio, ZIO_WAIT_READY); 3044 } 3045 3046 if (zio->io_flags & ZIO_FLAG_NODATA) { 3047 if (BP_IS_GANG(bp)) { 3048 zio->io_flags &= ~ZIO_FLAG_NODATA; 3049 } else { 3050 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE); 3051 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 3052 } 3053 } 3054 3055 if (zio_injection_enabled && 3056 zio->io_spa->spa_syncing_txg == zio->io_txg) 3057 zio_handle_ignored_writes(zio); 3058 3059 return (ZIO_PIPELINE_CONTINUE); 3060} 3061 3062static int 3063zio_done(zio_t **ziop) 3064{ 3065 zio_t *zio = *ziop; 3066 spa_t *spa = zio->io_spa; 3067 zio_t *lio = zio->io_logical; 3068 blkptr_t *bp = zio->io_bp; 3069 vdev_t *vd = zio->io_vd; 3070 uint64_t psize = zio->io_size; 3071 zio_t *pio, *pio_next; 3072 3073 /* 3074 * If our children haven't all completed, 3075 * wait for them and then repeat this pipeline stage. 3076 */ 3077 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) || 3078 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) || 3079 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) || 3080 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE)) 3081 return (ZIO_PIPELINE_STOP); 3082 3083 for (int c = 0; c < ZIO_CHILD_TYPES; c++) 3084 for (int w = 0; w < ZIO_WAIT_TYPES; w++) 3085 ASSERT(zio->io_children[c][w] == 0); 3086 3087 if (bp != NULL && !BP_IS_EMBEDDED(bp)) { 3088 ASSERT(bp->blk_pad[0] == 0); 3089 ASSERT(bp->blk_pad[1] == 0); 3090 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 || 3091 (bp == zio_unique_parent(zio)->io_bp)); 3092 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 3093 zio->io_bp_override == NULL && 3094 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 3095 ASSERT(!BP_SHOULD_BYTESWAP(bp)); 3096 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp)); 3097 ASSERT(BP_COUNT_GANG(bp) == 0 || 3098 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 3099 } 3100 if (zio->io_flags & ZIO_FLAG_NOPWRITE) 3101 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig)); 3102 } 3103 3104 /* 3105 * If there were child vdev/gang/ddt errors, they apply to us now. 3106 */ 3107 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 3108 zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 3109 zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 3110 3111 /* 3112 * If the I/O on the transformed data was successful, generate any 3113 * checksum reports now while we still have the transformed data. 3114 */ 3115 if (zio->io_error == 0) { 3116 while (zio->io_cksum_report != NULL) { 3117 zio_cksum_report_t *zcr = zio->io_cksum_report; 3118 uint64_t align = zcr->zcr_align; 3119 uint64_t asize = P2ROUNDUP(psize, align); 3120 char *abuf = zio->io_data; 3121 3122 if (asize != psize) { 3123 abuf = zio_buf_alloc(asize); 3124 bcopy(zio->io_data, abuf, psize); 3125 bzero(abuf + psize, asize - psize); 3126 } 3127 3128 zio->io_cksum_report = zcr->zcr_next; 3129 zcr->zcr_next = NULL; 3130 zcr->zcr_finish(zcr, abuf); 3131 zfs_ereport_free_checksum(zcr); 3132 3133 if (asize != psize) 3134 zio_buf_free(abuf, asize); 3135 } 3136 } 3137 3138 zio_pop_transforms(zio); /* note: may set zio->io_error */ 3139 3140 vdev_stat_update(zio, psize); 3141 3142 if (zio->io_error) { 3143 /* 3144 * If this I/O is attached to a particular vdev, 3145 * generate an error message describing the I/O failure 3146 * at the block level. We ignore these errors if the 3147 * device is currently unavailable. 3148 */ 3149 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 3150 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0); 3151 3152 if ((zio->io_error == EIO || !(zio->io_flags & 3153 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 3154 zio == lio) { 3155 /* 3156 * For logical I/O requests, tell the SPA to log the 3157 * error and generate a logical data ereport. 3158 */ 3159 spa_log_error(spa, zio); 3160 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio, 3161 0, 0); 3162 } 3163 } 3164 3165 if (zio->io_error && zio == lio) { 3166 /* 3167 * Determine whether zio should be reexecuted. This will 3168 * propagate all the way to the root via zio_notify_parent(). 3169 */ 3170 ASSERT(vd == NULL && bp != NULL); 3171 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3172 3173 if (IO_IS_ALLOCATING(zio) && 3174 !(zio->io_flags & ZIO_FLAG_CANFAIL)) { 3175 if (zio->io_error != ENOSPC) 3176 zio->io_reexecute |= ZIO_REEXECUTE_NOW; 3177 else 3178 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3179 } 3180 3181 if ((zio->io_type == ZIO_TYPE_READ || 3182 zio->io_type == ZIO_TYPE_FREE) && 3183 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 3184 zio->io_error == ENXIO && 3185 spa_load_state(spa) == SPA_LOAD_NONE && 3186 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE) 3187 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3188 3189 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute) 3190 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3191 3192 /* 3193 * Here is a possibly good place to attempt to do 3194 * either combinatorial reconstruction or error correction 3195 * based on checksums. It also might be a good place 3196 * to send out preliminary ereports before we suspend 3197 * processing. 3198 */ 3199 } 3200 3201 /* 3202 * If there were logical child errors, they apply to us now. 3203 * We defer this until now to avoid conflating logical child 3204 * errors with errors that happened to the zio itself when 3205 * updating vdev stats and reporting FMA events above. 3206 */ 3207 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 3208 3209 if ((zio->io_error || zio->io_reexecute) && 3210 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 3211 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE))) 3212 zio_dva_unallocate(zio, zio->io_gang_tree, bp); 3213 3214 zio_gang_tree_free(&zio->io_gang_tree); 3215 3216 /* 3217 * Godfather I/Os should never suspend. 3218 */ 3219 if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 3220 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) 3221 zio->io_reexecute = 0; 3222 3223 if (zio->io_reexecute) { 3224 /* 3225 * This is a logical I/O that wants to reexecute. 3226 * 3227 * Reexecute is top-down. When an i/o fails, if it's not 3228 * the root, it simply notifies its parent and sticks around. 3229 * The parent, seeing that it still has children in zio_done(), 3230 * does the same. This percolates all the way up to the root. 3231 * The root i/o will reexecute or suspend the entire tree. 3232 * 3233 * This approach ensures that zio_reexecute() honors 3234 * all the original i/o dependency relationships, e.g. 3235 * parents not executing until children are ready. 3236 */ 3237 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3238 3239 zio->io_gang_leader = NULL; 3240 3241 mutex_enter(&zio->io_lock); 3242 zio->io_state[ZIO_WAIT_DONE] = 1; 3243 mutex_exit(&zio->io_lock); 3244 3245 /* 3246 * "The Godfather" I/O monitors its children but is 3247 * not a true parent to them. It will track them through 3248 * the pipeline but severs its ties whenever they get into 3249 * trouble (e.g. suspended). This allows "The Godfather" 3250 * I/O to return status without blocking. 3251 */ 3252 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 3253 zio_link_t *zl = zio->io_walk_link; 3254 pio_next = zio_walk_parents(zio); 3255 3256 if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 3257 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) { 3258 zio_remove_child(pio, zio, zl); 3259 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3260 } 3261 } 3262 3263 if ((pio = zio_unique_parent(zio)) != NULL) { 3264 /* 3265 * We're not a root i/o, so there's nothing to do 3266 * but notify our parent. Don't propagate errors 3267 * upward since we haven't permanently failed yet. 3268 */ 3269 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 3270 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 3271 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3272 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) { 3273 /* 3274 * We'd fail again if we reexecuted now, so suspend 3275 * until conditions improve (e.g. device comes online). 3276 */ 3277 zio_suspend(spa, zio); 3278 } else { 3279 /* 3280 * Reexecution is potentially a huge amount of work. 3281 * Hand it off to the otherwise-unused claim taskq. 3282 */ 3283#if defined(illumos) || !defined(_KERNEL) 3284 ASSERT(zio->io_tqent.tqent_next == NULL); 3285#else 3286 ASSERT(zio->io_tqent.tqent_task.ta_pending == 0); 3287#endif 3288 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM, 3289 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio, 3290 0, &zio->io_tqent); 3291 } 3292 return (ZIO_PIPELINE_STOP); 3293 } 3294 3295 ASSERT(zio->io_child_count == 0); 3296 ASSERT(zio->io_reexecute == 0); 3297 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 3298 3299 /* 3300 * Report any checksum errors, since the I/O is complete. 3301 */ 3302 while (zio->io_cksum_report != NULL) { 3303 zio_cksum_report_t *zcr = zio->io_cksum_report; 3304 zio->io_cksum_report = zcr->zcr_next; 3305 zcr->zcr_next = NULL; 3306 zcr->zcr_finish(zcr, NULL); 3307 zfs_ereport_free_checksum(zcr); 3308 } 3309 3310 /* 3311 * It is the responsibility of the done callback to ensure that this 3312 * particular zio is no longer discoverable for adoption, and as 3313 * such, cannot acquire any new parents. 3314 */ 3315 if (zio->io_done) 3316 zio->io_done(zio); 3317 3318 mutex_enter(&zio->io_lock); 3319 zio->io_state[ZIO_WAIT_DONE] = 1; 3320 mutex_exit(&zio->io_lock); 3321 3322 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) { 3323 zio_link_t *zl = zio->io_walk_link; 3324 pio_next = zio_walk_parents(zio); 3325 zio_remove_child(pio, zio, zl); 3326 zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3327 } 3328 3329 if (zio->io_waiter != NULL) { 3330 mutex_enter(&zio->io_lock); 3331 zio->io_executor = NULL; 3332 cv_broadcast(&zio->io_cv); 3333 mutex_exit(&zio->io_lock); 3334 } else { 3335 zio_destroy(zio); 3336 } 3337 3338 return (ZIO_PIPELINE_STOP); 3339} 3340 3341/* 3342 * ========================================================================== 3343 * I/O pipeline definition 3344 * ========================================================================== 3345 */ 3346static zio_pipe_stage_t *zio_pipeline[] = { 3347 NULL, 3348 zio_read_bp_init, 3349 zio_free_bp_init, 3350 zio_issue_async, 3351 zio_write_bp_init, 3352 zio_checksum_generate, 3353 zio_nop_write, 3354 zio_ddt_read_start, 3355 zio_ddt_read_done, 3356 zio_ddt_write, 3357 zio_ddt_free, 3358 zio_gang_assemble, 3359 zio_gang_issue, 3360 zio_dva_allocate, 3361 zio_dva_free, 3362 zio_dva_claim, 3363 zio_ready, 3364 zio_vdev_io_start, 3365 zio_vdev_io_done, 3366 zio_vdev_io_assess, 3367 zio_checksum_verify, 3368 zio_done 3369}; 3370 3371/* dnp is the dnode for zb1->zb_object */ 3372boolean_t
|