Revision cac8f4a6

b/block/sheepdog.c
27 27
#define SD_OP_CREATE_AND_WRITE_OBJ  0x01
28 28
#define SD_OP_READ_OBJ       0x02
29 29
#define SD_OP_WRITE_OBJ      0x03
30
/* 0x04 is used internally by Sheepdog */
31
#define SD_OP_DISCARD_OBJ    0x05
30 32

  
31 33
#define SD_OP_NEW_VDI        0x11
32 34
#define SD_OP_LOCK_VDI       0x12
......
269 271
    AIOCB_WRITE_UDATA,
270 272
    AIOCB_READ_UDATA,
271 273
    AIOCB_FLUSH_CACHE,
274
    AIOCB_DISCARD_OBJ,
272 275
};
273 276

  
274 277
struct SheepdogAIOCB {
......
298 301
    char name[SD_MAX_VDI_LEN];
299 302
    bool is_snapshot;
300 303
    uint32_t cache_flags;
304
    bool discard_supported;
301 305

  
302 306
    char *host_spec;
303 307
    bool is_unix;
......
656 660
    int ret;
657 661
    AIOReq *aio_req = NULL;
658 662
    SheepdogAIOCB *acb;
659
    unsigned long idx;
663
    uint64_t idx;
660 664

  
661 665
    if (QLIST_EMPTY(&s->inflight_aio_head)) {
662 666
        goto out;
......
727 731
            rsp.result = SD_RES_SUCCESS;
728 732
        }
729 733
        break;
734
    case AIOCB_DISCARD_OBJ:
735
        switch (rsp.result) {
736
        case SD_RES_INVALID_PARMS:
737
            error_report("sheep(%s) doesn't support discard command",
738
                         s->host_spec);
739
            rsp.result = SD_RES_SUCCESS;
740
            s->discard_supported = false;
741
            break;
742
        case SD_RES_SUCCESS:
743
            idx = data_oid_to_idx(aio_req->oid);
744
            s->inode.data_vdi_id[idx] = 0;
745
            break;
746
        default:
747
            break;
748
        }
730 749
    }
731 750

  
732 751
    if (rsp.result != SD_RES_SUCCESS) {
......
1016 1035
        wlen = datalen;
1017 1036
        hdr.flags = SD_FLAG_CMD_WRITE | flags;
1018 1037
        break;
1038
    case AIOCB_DISCARD_OBJ:
1039
        hdr.opcode = SD_OP_DISCARD_OBJ;
1040
        break;
1019 1041
    }
1020 1042

  
1021 1043
    if (s->cache_flags) {
......
1197 1219
    if (flags & BDRV_O_NOCACHE) {
1198 1220
        s->cache_flags = SD_FLAG_CMD_DIRECT;
1199 1221
    }
1222
    s->discard_supported = true;
1200 1223

  
1201 1224
    if (snapid || tag[0] != '\0') {
1202 1225
        dprintf("%" PRIx32 " snapshot inode was open.\n", vid);
......
1662 1685
                flags = SD_FLAG_CMD_COW;
1663 1686
            }
1664 1687
            break;
1688
        case AIOCB_DISCARD_OBJ:
1689
            /*
1690
             * We discard the object only when the whole object is
1691
             * 1) allocated 2) trimmed. Otherwise, simply skip it.
1692
             */
1693
            if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) {
1694
                goto done;
1695
            }
1696
            break;
1665 1697
        default:
1666 1698
            break;
1667 1699
        }
......
2107 2139
}
2108 2140

  
2109 2141

  
2142
static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num,
2143
                                      int nb_sectors)
2144
{
2145
    SheepdogAIOCB *acb;
2146
    QEMUIOVector dummy;
2147
    BDRVSheepdogState *s = bs->opaque;
2148
    int ret;
2149

  
2150
    if (!s->discard_supported) {
2151
            return 0;
2152
    }
2153

  
2154
    acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors);
2155
    acb->aiocb_type = AIOCB_DISCARD_OBJ;
2156
    acb->aio_done_func = sd_finish_aiocb;
2157

  
2158
    ret = sd_co_rw_vector(acb);
2159
    if (ret <= 0) {
2160
        qemu_aio_release(acb);
2161
        return ret;
2162
    }
2163

  
2164
    qemu_coroutine_yield();
2165

  
2166
    return acb->ret;
2167
}
2168

  
2110 2169
static QEMUOptionParameter sd_create_options[] = {
2111 2170
    {
2112 2171
        .name = BLOCK_OPT_SIZE,
......
2139 2198
    .bdrv_co_readv  = sd_co_readv,
2140 2199
    .bdrv_co_writev = sd_co_writev,
2141 2200
    .bdrv_co_flush_to_disk  = sd_co_flush_to_disk,
2201
    .bdrv_co_discard = sd_co_discard,
2142 2202

  
2143 2203
    .bdrv_snapshot_create   = sd_snapshot_create,
2144 2204
    .bdrv_snapshot_goto     = sd_snapshot_goto,
......
2164 2224
    .bdrv_co_readv  = sd_co_readv,
2165 2225
    .bdrv_co_writev = sd_co_writev,
2166 2226
    .bdrv_co_flush_to_disk  = sd_co_flush_to_disk,
2227
    .bdrv_co_discard = sd_co_discard,
2167 2228

  
2168 2229
    .bdrv_snapshot_create   = sd_snapshot_create,
2169 2230
    .bdrv_snapshot_goto     = sd_snapshot_goto,
......
2189 2250
    .bdrv_co_readv  = sd_co_readv,
2190 2251
    .bdrv_co_writev = sd_co_writev,
2191 2252
    .bdrv_co_flush_to_disk  = sd_co_flush_to_disk,
2253
    .bdrv_co_discard = sd_co_discard,
2192 2254

  
2193 2255
    .bdrv_snapshot_create   = sd_snapshot_create,
2194 2256
    .bdrv_snapshot_goto     = sd_snapshot_goto,

Also available in: Unified diff