今天@子团来问ext4对trim支持相关的问题:trim的语义是将设备某一部分的内容全置为0,如果ext4下层是机械硬盘,ext4也会给它发trim,频繁的将硬盘上的数据置0吗?
我翻了一会儿rhel 2.6.32-279 kernel的代码,找到了jbd2完成时调用trim的地方:

static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)       
{                                                                                  
    struct super_block *sb = journal->j_private;                                   
    struct ext4_buddy e4b;                                                         
    struct ext4_group_info *db;                                                    
    int err, count = 0, count2 = 0;                                                
    struct ext4_free_data *entry;                                                  
    struct list_head *l, *ltmp;                                                    
                                                                                   
    list_for_each_safe(l, ltmp, &txn->t_private_list) {                            
        entry = list_entry(l, struct ext4_free_data, list);                        
                                                                                   
        mb_debug(1, "gonna free %u blocks in group %u (0x%p):",                    
             entry->count, entry->group, entry);                                   
                                                                                   
        if (test_opt(sb, DISCARD))                                                 
            ext4_issue_discard(sb, entry->group,                                   
                       entry->start_cluster, entry->count);                        
......

如果mount时设置了DISCARD(mount -t ext4 -o discard),则调用ext4_issue_discard,给设备发一个DISCARD,看来不管什么设备,这个调用是绕不开的,继续看:

ext4_issue_discard
    --> sb_issue_discard
        --> blkdev_issue_discard
int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
        sector_t nr_sects, gfp_t gfp_mask, int flags)
{
    DECLARE_COMPLETION_ONSTACK(wait);
    struct request_queue *q = bdev_get_queue(bdev);
    int type = (1 << BIO_RW) | (1 << BIO_RW_DISCARD);
    unsigned int max_discard_sectors;
    struct bio_batch bb;
    struct bio *bio;
    int ret = 0;

    /*
     * DEPRECATED support for DISCARD_FL_BARRIER which will
     * fail with -EOPNOTSUPP (due to implicit BIO_RW_BARRIER)
     */ 
    if (flags & DISCARD_FL_BARRIER)
        type = DISCARD_BARRIER;

    if (!q)
        return -ENXIO;

    if (!blk_queue_discard(q))
        return -EOPNOTSUPP;
......

看来block层的代码如果发现该设备不支持DISCARD(blk_queue_discard(q)),则直接返回-EOPNOTSUPP,不会做任何其它操作。
所以,不用担心,普通机械硬盘也可以使用discard参数来mount ext4,kernel直接忽略discard请求,不会对硬盘造成额外的物理损伤。