1、android init.rc中可以看到, 系统启动mount /data 目录时,mount命令添加了参数:discard
mount ext4 /emmc@usrdata /data noatime nosuid nodev wait noauto_da_alloc,discard
2、
mount的discard参数传递到ext4_mount函数的flags参数
//kernel/fs/ext4/super.c
static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
const char *dev_name, void *data)
{
return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
}
//kernel/fs/super.c
struct dentry *mount_bdev(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data,
int (*fill_super)(struct super_block *, void *, int))
{
struct block_device *bdev;
struct super_block *s;
fmode_t mode = FMODE_READ | FMODE_EXCL;
int error = 0;
.................
s->s_flags = flags | MS_NOSEC; //将mount flag保存在sb的s_flags成员中
s->s_mode = mode;
strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
sb_set_blocksize(s, block_size(bdev));
error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); //调用ext4的fill super :ext4_fill_super, 通过sb传递mount flag
.........................
return dget(s->s_root);
}
//kernel/fs/ext4/super.c
static int ext4_fill_super(struct super_block *sb, void *data, int silent)
{
...................
if (!parse_options((char *) data, sb, &journal_devnum,
&journal_ioprio, 0))
....................
}
static const struct mount_opts {
int token;
int mount_opt;
int flags;
} ext4_mount_opts[] = {
...........................
{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
.............................................
}
2、/kernel/drivers/mmc/card/block.c
if (req && req->cmd_flags & REQ_DISCARD) {
...
ret = mmc_blk_issue_discard_rq(mq, req);
} else if (req && req->cmd_flags & REQ_FLUSH) {
/* complete ongoing async transfer before issuing flush */
if (card->host->areq)
mmc_blk_issue_rw_rq(mq, NULL);
ret = mmc_blk_issue_flush(mq, req);
} else {
ret = mmc_blk_issue_rw_rq(mq, req);
}