Linux内核学习:EXT4 文件系统在 Linux 内核系统中的读写过程【转】

转自:https://blog.csdn.net/qq_32473685/article/details/103494398

目录

1 概述

2 虚拟文件系统 与 Ext4 文件系统

2.1 sys_write( ) 代码跟踪

2.2 sys_write( ) 过程分析

2.3 sys_write( ) 的核心部分 vfs_write( )

2.4 ext4_file_write( )

2.4.1 ext4文件系统的extent

2.4.2 ext4_file_write( ) 

2.5 generic_file_write_iter( )

2.6 __generic_file_write_iter( )

2.7 generic_perform_write( )

2.7.1 ext4文件系统address_space_operations

2.7.2 ext4文件系统delay allocation机制

2.7.3 执行完 generate_write_back( )后


1 概述

用户进程通过系统调用write()往磁盘上写数据,但write()执行结束后,数据是否 立即写到磁盘上?内核读文件数据时,使用到了“提前读”;写数据时,则使用了“延迟写”, 即write()执行结束后,数据并没有立即立即将请求放入块设备驱动请求队列,然后写到 硬盘上。

跟踪的时候通过

dump_stack

重新编译linux内核,跟踪函数执行过程。

2 虚拟文件系统 与 Ext4 文件系统

首先文件系统在内核中的读写过程是在 sys_write( ) 中定义的。

2.1 sys_write( ) 代码跟踪

sys_write( ) 定义在 include/linux/syscalls.h 中:

asmlinkage long sys_write(unsigned int fd, const char __user *buf, 568 size_t count);

sys_write( )的具体实现在 fs/read_write.c 中:

  1.  
    SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  2.  
    size_t, count)
  3.  
    {
  4.  
    struct fd f = fdget_pos(fd);
  5.  
    ssize_t ret = -EBADF;
  6.  
    if (f.file) {
  7.  
    loff_t pos = file_pos_read(f.file);
  8.  
    ret = vfs_write(f.file, buf, count, &pos);
  9.  
    if (ret >= 0)
  10.  
    file_pos_write(f.file, pos);
  11.  
    fdput_pos(f);
  12.  
    }
  13.  
    return ret;
  14.  
    }

2.2 sys_write( ) 过程分析

可以看出在实现 sys_write( ) 的时候,分为如下几步:

1) 根据打开文件号 fd找到该已打开文件file结构:

struct fd f = fdget_pos(fd);

2) 读取当前文件的读写位置:

loff_t pos = file_pos_read(f.file);

3) 写入:

ret = vfs_write(f.file, buf, count, &pos);

4) 根据读文件结果,更新文件读写位置 :

file_pos_write(f.file, pos);

2)和  4)可以作为写入之前和之后的对应操作来看,一个是读取当前文件的位置,一个是根据写文件的结果,更新文件的读写位置,主要代码还是在 fs/read_write.c 中:

  1.  
    static inline loff_t file_pos_read(struct file *file)
  2.  
    {
  3.  
    return file->f_pos;
  4.  
    }
  5.  
     
  6.  
    static inline void file_pos_write(struct file *file, loff_t pos)
  7.  
    {
  8.  
    file->f_pos = pos;
  9.  
    }

3) 是整个 sys_write( ) 中最为重要的一部分,下面我们仔细分析一下这个函数。

2.3 sys_write( ) 的核心部分 vfs_write( )

  1.  
    ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos){
  2.  
    ssize_t ret;
  3.  
     
  4.  
    if (!(file->f_mode & FMODE_WRITE))
  5.  
    return -EBADF;
  6.  
    if (!(file->f_mode & FMODE_CAN_WRITE))
  7.  
    return -EINVAL;
  8.  
    if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  9.  
    return -EFAULT;
  10.  
    ret = rw_verify_area(WRITE, file, pos, count);
  11.  
    if (ret >= 0) {
  12.  
    count = ret;
  13.  
    file_start_write(file);
  14.  
    if (file->f_op->write)
  15.  
    ret = file->f_op->write(file, buf, count, pos);
  16.  
    else if (file->f_op->aio_write)
  17.  
    ret = do_sync_write(file, buf, count, pos);
  18.  
    else
  19.  
    ret = new_sync_write(file, buf, count, pos);
  20.  
    if (ret > 0) {
  21.  
    fsnotify_modify(file);
  22.  
    add_wchar(current, ret);
  23.  
    }
  24.  
    inc_syscw(current);
  25.  
    file_end_write(file);
  26.  
    }
  27.  
     
  28.  
    return ret;
  29.  
    }

首先函数在 rw_verify_area(WRITE, file, pos, count); 检查文件是否从当前位置 pos 开始的 count 字节是否对写操作加上了 “强制锁”,这是通过调用函数完成的。

通过合法性检查后,就调用具体文件系统 file_operations中 write 的方法。对于ext4文件系统,file_operations方法定义在 fs/ext4/file.c 中。从定义中可知 write 方法实现函数为 do_sync_write( )。 

下面是ext4文件系统操作的数据结构:

  1.  
    const struct file_operations ext4_file_operations = {
  2.  
    .llseek = ext4_llseek,
  3.  
    .read = new_sync_read,
  4.  
    .write = new_sync_write,
  5.  
    .read_iter = generic_file_read_iter,
  6.  
    .write_iter = ext4_file_write_iter,
  7.  
    .unlocked_ioctl = ext4_ioctl,
  8.  
    #ifdef CONFIG_COMPAT
  9.  
    .compat_ioctl = ext4_compat_ioctl,
  10.  
    #endif
  11.  
    .mmap = ext4_file_mmap,
  12.  
    .open = ext4_file_open,
  13.  
    .release = ext4_release_file,
  14.  
    .fsync = ext4_sync_file,
  15.  
    .splice_read = generic_file_splice_read,
  16.  
    .splice_write = iter_file_splice_write,
  17.  
    .fallocate = ext4_fallocate,
  18.  
    };

下面是do_sync_write( )的具体代码,也在fs/read_write.c中:

  1.  
    ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  2.  
    {
  3.  
    struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  4.  
    struct kiocb kiocb;
  5.  
    ssize_t ret;
  6.  
    init_sync_kiocb(&kiocb, filp);
  7.  
    kiocb.ki_pos = *ppos;
  8.  
    kiocb.ki_nbytes = len;
  9.  
    ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  10.  
    if (-EIOCBQUEUED == ret)
  11.  
    ret = wait_on_sync_kiocb(&kiocb);
  12.  
    *ppos = kiocb.ki_pos;
  13.  
    return ret;
  14.  
    }
  15.  
    EXPORT_SYMBOL(do_sync_write);

异步I/O允许用户空间来初始化操作而不必等待它们的完成,因此,一个应用程序可以在他的I/O处理进行中做其他的处理。

块和网络驱动在整个时间是完全异步的,因此只有字符驱动对于明确的异步I/O支持是候选的。实现异步I/O操作的file_operations方法,都使用I/O Control Block,其定义在 include/linux/aio.h中

定义了一个临时变量iov,这个变量记录了用户空间缓冲区地址buf和所要写的字节数len,用户空间的缓冲区地址buf是保存在iov中的。初始化异步I/O数据结构后,就用file_operations 中的aio_write方法。拓展到ext4文件中的时,该方法就是ext4_file_operations结构体中的ext4_file_write( )。

下面就具体到ext4的文件系统,这个函数也是aio_write( ) 的延展。

2.4 ext4_file_write( )

2.4.1 ext4文件系统的extent

Ext2/3等老Linux文件系统使用间接映射模式 (block mapping),  文件的每一个块都要被记录下来,这使得大文件操作(删除)效率低下。Ext4 引入extents这一概念来代替 Ext2/3 使用的传统的块映射方式。ext4中一个extent最大可以映射128MB的连续物理存储空间。

Ext3采用间接块映射,当操作大文件的时候,效率极其低下,比如一个100MB大小的文件,在Ext3中要建立25600个数据块的映射表,每个数据块大小为4KB,而Ext4引入了extents,每个extent为一组连续的数据块,上述文件表示为,该文件数据保存在接下来的25600个数据块中,提高了不少效率。

Extent模式主要数据结构包括ext4_extent, ext4_extent_idx, ext4_extent_header,均定义在文件fs/ext4/ext4_extents.h文件中。

  1.  
    /*
  2.  
    * This is the extent on-disk structure.
  3.  
    * It's used at the bottom of the tree.
  4.  
    */
  5.  
    struct ext4_extent {
  6.  
    __le32 ee_block; /* first logical block extent covers */
  7.  
    __le16 ee_len; /* number of blocks covered by extent */
  8.  
    __le16 ee_start_hi; /* high 16 bits of physical block */
  9.  
    __le32 ee_start_lo; /* low 32 bits of physical block */
  10.  
    };
  11.  
     
  12.  
    /*
  13.  
    * This is index on-disk structure.
  14.  
    * It's used at all the levels except the bottom.
  15.  
    */
  16.  
    struct ext4_extent_idx {
  17.  
    __le32 ei_block; /* index covers logical blocks from 'block' */
  18.  
    __le32 ei_leaf_lo; /* pointer to the physical block of the next *
  19.  
    * level. leaf or next index could be there */
  20.  
    __le16 ei_leaf_hi; /* high 16 bits of physical block */
  21.  
    __u16 ei_unused;
  22.  
    };
  23.  
     
  24.  
    /*
  25.  
    * Each block (leaves and indexes), even inode-stored has header.
  26.  
    */
  27.  
    struct ext4_extent_header {
  28.  
    __le16 eh_magic; /* probably will support different formats */
  29.  
    __le16 eh_entries; /* number of valid entries */
  30.  
    __le16 eh_max; /* capacity of store in entries */
  31.  
    __le16 eh_depth; /* has tree real underlying blocks? */
  32.  
    __le32 eh_generation; /* generation of the tree */
  33.  
    };

2.4.2 ext4_file_write( ) 

  1.  
    static ssize_t
  2.  
    ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  3.  
    {
  4.  
    struct file *file = iocb->ki_filp;
  5.  
    struct inode *inode = file_inode(iocb->ki_filp);
  6.  
    struct mutex *aio_mutex = NULL;
  7.  
    struct blk_plug plug;
  8.  
    int o_direct = io_is_direct(file);
  9.  
    int overwrite = 0;
  10.  
    size_t length = iov_iter_count(from);
  11.  
    ssize_t ret;
  12.  
    loff_t pos = iocb->ki_pos;
  13.  
     
  14.  
    /*
  15.  
    * Unaligned direct AIO must be serialized; see comment above
  16.  
    * In the case of O_APPEND, assume that we must always serialize
  17.  
    */
  18.  
    if (o_direct &&
  19.  
    ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  20.  
    !is_sync_kiocb(iocb) &&
  21.  
    (file->f_flags & O_APPEND ||
  22.  
    ext4_unaligned_aio(inode, from, pos))) {
  23.  
    aio_mutex = ext4_aio_mutex(inode);
  24.  
    mutex_lock(aio_mutex);
  25.  
    ext4_unwritten_wait(inode);
  26.  
    }
  27.  
    mutex_lock(&inode->i_mutex);
  28.  
    if (file->f_flags & O_APPEND)
  29.  
    iocb->ki_pos = pos = i_size_read(inode);
  30.  
     
  31.  
    /*
  32.  
    * If we have encountered a bitmap-format file, the size limit
  33.  
    * is smaller than s_maxbytes, which is for extent-mapped files.
  34.  
    */
  35.  
    if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  36.  
    struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  37.  
     
  38.  
    if ((pos > sbi->s_bitmap_maxbytes) ||
  39.  
    (pos == sbi->s_bitmap_maxbytes && length > 0)) {
  40.  
    mutex_unlock(&inode->i_mutex);
  41.  
    ret = -EFBIG;
  42.  
    goto errout;
  43.  
    }
  44.  
     
  45.  
    if (pos + length > sbi->s_bitmap_maxbytes)
  46.  
    iov_iter_truncate(from, sbi->s_bitmap_maxbytes - pos);
  47.  
    }
  48.  
     
  49.  
    iocb->private = &overwrite;
  50.  
    if (o_direct) {
  51.  
    blk_start_plug(&plug);
  52.  
    /* check whether we do a DIO overwrite or not */
  53.  
    if (ext4_should_dioread_nolock(inode) && !aio_mutex &&
  54.  
    !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
  55.  
    struct ext4_map_blocks map;
  56.  
    unsigned int blkbits = inode->i_blkbits;
  57.  
    int err, len;
  58.  
     
  59.  
    map.m_lblk = pos >> blkbits;
  60.  
    map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
  61.  
    - map.m_lblk;
  62.  
    len = map.m_len;
  63.  
     
  64.  
    err = ext4_map_blocks(NULL, inode, &map, 0);
  65.  
    /*
  66.  
    * 'err==len' means that all of blocks has
  67.  
    * been preallocated no matter they are
  68.  
    * initialized or not. For excluding
  69.  
    * unwritten extents, we need to check
  70.  
    * m_flags. There are two conditions that
  71.  
    * indicate for initialized extents. 1) If we
  72.  
    * hit extent cache, EXT4_MAP_MAPPED flag is
  73.  
    * returned; 2) If we do a real lookup,
  74.  
    * non-flags are returned. So we should check
  75.  
    * these two conditions.
  76.  
    */
  77.  
    if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
  78.  
    overwrite = 1;
  79.  
    }
  80.  
    }
  81.  
     
  82.  
    ret = __generic_file_write_iter(iocb, from);
  83.  
    mutex_unlock(&inode->i_mutex);
  84.  
     
  85.  
    if (ret > 0) {
  86.  
    ssize_t err;
  87.  
    err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  88.  
    if (err < 0)
  89.  
    ret = err;
  90.  
    }
  91.  
    if (o_direct)
  92.  
    blk_finish_plug(&plug);
  93.  
     
  94.  
    errout:
  95.  
    if (aio_mutex)
  96.  
    mutex_unlock(aio_mutex);
  97.  
    return ret;
  98.  
    }

首先检查文件是否为ext4的extent模式,若为传统的块映射方式,先检查文件是否过大。若当前文件位置加上待写的数据长度,大小若超过最大文件限制,则要做相应的调整,最终文件大小不能超过sbi->s_bitmap_maxbytes。

  1.  
    if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  2.  
    struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3.  
     
  4.  
    if ((pos > sbi->s_bitmap_maxbytes) ||
  5.  
    (pos == sbi->s_bitmap_maxbytes && length > 0)) {
  6.  
    mutex_unlock(&inode->i_mutex);
  7.  
    ret = -EFBIG;
  8.  
    goto errout;
  9.  
    }
  10.  
     
  11.  
    if (pos + length > sbi->s_bitmap_maxbytes)
  12.  
    iov_iter_truncate(from, sbi->s_bitmap_maxbytes - pos);
  13.  
    }

generic_file_aio_write( ) 就是ext4_file_write( )的主体执行语句,若I/O不是块对齐,写操作完成后,还要对i_aio_mutex解锁。

  1.  
    ret = __generic_file_write_iter(iocb, from);
  2.  
    mutex_unlock(&inode->i_mutex);
  3.  
     
  4.  
    if (ret > 0) {
  5.  
    ssize_t err;
  6.  
    err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  7.  
    if (err < 0)
  8.  
    ret = err;
  9.  
    }
  10.  
    if (o_direct)
  11.  
    blk_finish_plug(&plug);

2.5 generic_file_write_iter( )

generic_file_aio_write( )源码如下:

  1.  
    /**
  2.  
    * generic_file_write_iter - write data to a file
  3.  
    * @iocb: IO state structure
  4.  
    * @from: iov_iter with data to write
  5.  
    *
  6.  
    * This is a wrapper around __generic_file_write_iter() to be used by most
  7.  
    * filesystems. It takes care of syncing the file in case of O_SYNC file
  8.  
    * and acquires i_mutex as needed.
  9.  
    */
  10.  
    ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  11.  
    {
  12.  
    struct file *file = iocb->ki_filp;
  13.  
    struct inode *inode = file->f_mapping->host;
  14.  
    ssize_t ret;
  15.  
     
  16.  
    mutex_lock(&inode->i_mutex);
  17.  
    ret = __generic_file_write_iter(iocb, from);
  18.  
    mutex_unlock(&inode->i_mutex);
  19.  
    if (ret > 0) {
  20.  
    ssize_t err;
  21.  
     
  22.  
    err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  23.  
    if (err < 0)
  24.  
    ret = err;
  25.  
    }
  26.  
    return ret;
  27.  
    }
  28.  
    EXPORT_SYMBOL(generic_file_write_iter);

在do_sync_write()中已经将当前文件写的起始位置记录在iocb->ki_pos。接下来执行主体函数__generic_file_write_iter( ),执行写操作前要加锁,完成后解锁,若写操作成功,就返回写完成的字节数,返回值大于0;写操作出现错误,就返回相应的错误码。接下来就要调用generic_write_sync()将数据刷新到硬盘上。

2.6 __generic_file_write_iter( )

  1.  
    /**
  2.  
    * __generic_file_write_iter - write data to a file
  3.  
    * @iocb: IO state structure (file, offset, etc.)
  4.  
    * @from: iov_iter with data to write
  5.  
    *
  6.  
    * This function does all the work needed for actually writing data to a
  7.  
    * file. It does all basic checks, removes SUID from the file, updates
  8.  
    * modification times and calls proper subroutines depending on whether we
  9.  
    * do direct IO or a standard buffered write.
  10.  
    *
  11.  
    * It expects i_mutex to be grabbed unless we work on a block device or similar
  12.  
    * object which does not need locking at all.
  13.  
    *
  14.  
    * This function does *not* take care of syncing data in case of O_SYNC write.
  15.  
    * A caller has to handle it. This is mainly due to the fact that we want to
  16.  
    * avoid syncing under i_mutex.
  17.  
    * 此功能完成了将数据实际写入文件所需的所有工作。它会进行所有基本检查,从文件中删除SUID,更新修改
  18.  
    * 时间并根据我们执行直接I/O还是标准缓冲写入来调用适当的子例程。除非我们在完全不需要锁定的块设备或
  19.  
    * 类似对象上工作,否则它预计将获取i_mutex。如果是O_SYNC写操作,此功能不会负责同步数据。呼叫者必
  20.  
    * 须处理它。这主要是由于我们要避免在i_mutex下进行同步。
  21.  
    */
  22.  
     
  23.  
    ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  24.  
    {
  25.  
    struct file *file = iocb->ki_filp;
  26.  
    struct address_space * mapping = file->f_mapping;
  27.  
    struct inode *inode = mapping->host;
  28.  
    loff_t pos = iocb->ki_pos;
  29.  
    ssize_t written = 0;
  30.  
    ssize_t err;
  31.  
    ssize_t status;
  32.  
    size_t count = iov_iter_count(from);
  33.  
     
  34.  
    /* We can write back this queue in page reclaim */
  35.  
    current->backing_dev_info = inode_to_bdi(inode);
  36.  
    err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  37.  
    if (err)
  38.  
    goto out;
  39.  
     
  40.  
    if (count == 0)
  41.  
    goto out;
  42.  
    iov_iter_truncate(from, count);
  43.  
     
  44.  
    err = file_remove_suid(file);
  45.  
    if (err)
  46.  
    goto out;
  47.  
     
  48.  
    err = file_update_time(file);
  49.  
    if (err)
  50.  
    goto out;
  51.  
     
  52.  
    if (io_is_direct(file)) {
  53.  
    loff_t endbyte;
  54.  
     
  55.  
    written = generic_file_direct_write(iocb, from, pos);
  56.  
    /*
  57.  
    * If the write stopped short of completing, fall back to
  58.  
    * buffered writes. Some filesystems do this for writes to
  59.  
    * holes, for example. For DAX files, a buffered write will
  60.  
    * not succeed (even if it did, DAX does not handle dirty
  61.  
    * page-cache pages correctly).
  62.  
    */
  63.  
    if (written < 0 || written == count || IS_DAX(inode))
  64.  
    goto out;
  65.  
     
  66.  
    pos += written;
  67.  
    count -= written;
  68.  
     
  69.  
    status = generic_perform_write(file, from, pos);
  70.  
    /*
  71.  
    * If generic_perform_write() returned a synchronous error
  72.  
    * then we want to return the number of bytes which were
  73.  
    * direct-written, or the error code if that was zero. Note
  74.  
    * that this differs from normal direct-io semantics, which
  75.  
    * will return -EFOO even if some bytes were written.
  76.  
    */
  77.  
    if (unlikely(status < 0)) {
  78.  
    err = status;
  79.  
    goto out;
  80.  
    }
  81.  
    iocb->ki_pos = pos + status;
  82.  
    /*
  83.  
    * We need to ensure that the page cache pages are written to
  84.  
    * disk and invalidated to preserve the expected O_DIRECT
  85.  
    * semantics.
  86.  
    */
  87.  
    endbyte = pos + status - 1;
  88.  
    err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  89.  
    if (err == 0) {
  90.  
    written += status;
  91.  
    invalidate_mapping_pages(mapping,
  92.  
    pos >> PAGE_CACHE_SHIFT,
  93.  
    endbyte >> PAGE_CACHE_SHIFT);
  94.  
    } else {
  95.  
    /*
  96.  
    * We don't know how much we wrote, so just return
  97.  
    * the number of bytes which were direct-written
  98.  
    */
  99.  
    }
  100.  
    } else {
  101.  
    written = generic_perform_write(file, from, pos);
  102.  
    if (likely(written >= 0))
  103.  
    iocb->ki_pos = pos + written;
  104.  
    }
  105.  
    out:
  106.  
    current->backing_dev_info = NULL;
  107.  
    return written ? written : err;
  108.  
    }
  109.  
    EXPORT_SYMBOL(__generic_file_write_iter);

 更新检查后的实际可写入数据大小(大多数情况下不变,只有待写的数据超出文件大小限制,count值才会变化)。

generic_write_checks( )来检查对该文件的是否有相应的写权限,这个和系统中是否对文件大小有限制有关,将文件的suid标志清0,而且如果是可执行文件的话,就将sgid标志也清0,既然写文件,那么文件就会被修改(或创建),修改文件的时间是要记录在inode中的,并且将inode标记为脏(回写到磁盘上)。

若写方式为Direct IO,前面的工作都是一些合法性检查、记录文件改变、修改时间。而写文件的主要工作是调用函数 generic_perform_write( ) 来完成。

2.7 generic_perform_write( )

  1.  
    ssize_t generic_perform_write(struct file *file,
  2.  
    struct iov_iter *i, loff_t pos)
  3.  
    {
  4.  
    struct address_space *mapping = file->f_mapping;
  5.  
    const struct address_space_operations *a_ops = mapping->a_ops;
  6.  
    long status = 0;
  7.  
    ssize_t written = 0;
  8.  
    unsigned int flags = 0;
  9.  
     
  10.  
    /*
  11.  
    * Copies from kernel address space cannot fail (NFSD is a big user).
  12.  
    */
  13.  
    if (!iter_is_iovec(i))
  14.  
    flags |= AOP_FLAG_UNINTERRUPTIBLE;
  15.  
     
  16.  
    // 若当前I/O操作是属于在内核中进行,显然是不能被中断的(用户态的I/O操作可以被中断),就要设置AOP_FLAG_UNINTERRUPTIBLE标志
  17.  
    do {
  18.  
    struct page *page;
  19.  
    unsigned long offset; /* Offset into pagecache page */
  20.  
    unsigned long bytes; /* Bytes to write to page */
  21.  
    size_t copied; /* Bytes copied from user */
  22.  
    void *fsdata;
  23.  
     
  24.  
    offset = (pos & (PAGE_CACHE_SIZE - 1));
  25.  
    bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
  26.  
    iov_iter_count(i));
  27.  
    // index:当前pos位置在pagecache的索引(以页面大小为单位)
  28.  
    // offset:为在页面内的偏移
  29.  
    // bytes:要从用户空间拷贝的数据大小
  30.  
    again:
  31.  
    /*
  32.  
    * Bring in the user page that we will copy from _first_.
  33.  
    * Otherwise there's a nasty deadlock on copying from the
  34.  
    * same page as we're writing to, without it being marked
  35.  
    * up-to-date.
  36.  
    *
  37.  
    * Not only is this an optimisation, but it is also required
  38.  
    * to check that the address is actually valid, when atomic
  39.  
    * usercopies are used, below.
  40.  
    */
  41.  
    if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
  42.  
    status = -EFAULT;
  43.  
    break;
  44.  
    }
  45.  
     
  46.  
    // 调用索引节点(file->f_mapping)中address_space对象的write_begin方法,write_begin方法会为该页分配和初始化缓冲区首部,稍后,我们会详细分析ext4文件 系统实现的write_begin方法ext4_da_write_begin()。
  47.  
     
  48.  
    status = a_ops->write_begin(file, mapping, pos, bytes, flags,
  49.  
    &page, &fsdata);
  50.  
    if (unlikely(status < 0))
  51.  
    break;
  52.  
     
  53.  
    if (mapping_writably_mapped(mapping))
  54.  
    flush_dcache_page(page);
  55.  
     
  56.  
    // mapping->i_mmap_writable 记录 VM_SHAREE 共享映射数。若mapping_writably_mapped()不等于0,则说明该页面被多个共享使用,调用flush_dcache_page()。flush_dcache_page()将dcache相应的page里的数据写到memory里去,以保证dcache内的数据与memory内的数据的一致性。但在x86架构中,flush_dcache_page() 的实现为空,不做任何操作。
  57.  
     
  58.  
    copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
  59.  
    flush_dcache_page(page);
  60.  
     
  61.  
    status = a_ops->write_end(file, mapping, pos, bytes, copied,
  62.  
    page, fsdata);
  63.  
    if (unlikely(status < 0))
  64.  
    break;
  65.  
    copied = status;
  66.  
     
  67.  
    cond_resched();
  68.  
     
  69.  
    //将待写的数据拷贝到内核空间后,调用ext4文件系统的address_space_operations的 write_end方法。前面看到ext4文件系统有4种模式:writeback、ordered、journalled和delay allocation。加载ext4分区时,默认方式为delay allocation。对应的write_end方法为 ext4_da_write_end()。
  70.  
    cond_resched()检查当前进程的TIF_NEED_RESCHED标志,若该标志为设置,则 调用schedule函数,调度一个新程序投入运行。
  71.  
     
  72.  
    iov_iter_advance(i, copied);
  73.  
    if (unlikely(copied == 0)) {
  74.  
    /*
  75.  
    * If we were unable to copy any data at all, we must
  76.  
    * fall back to a single segment length write.
  77.  
    *
  78.  
    * If we didn't fallback here, we could livelock
  79.  
    * because not all segments in the iov can be copied at
  80.  
    * once without a pagefault.
  81.  
    */
  82.  
    bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
  83.  
    iov_iter_single_seg_count(i));
  84.  
    goto again;
  85.  
    }
  86.  
    //当a_ops->write_end()执行完成后,写数据操作完成了(注意,此时数据不一定真正写到磁盘上,因为大多数数据写为异步I/O)。接下来就要更新iov_iter结构体里的信息,包括文件的位置、写数据大小、数据所在位置。若copied值为0,说明没能将数据从用户态拷贝到内核态,就要再次尝试写操作。
  87.  
    pos += copied;
  88.  
    written += copied;
  89.  
    //更新文件位置pos和已完成写的数据大小
  90.  
    balance_dirty_pages_ratelimited(mapping);
  91.  
    if (fatal_signal_pending(current)) {
  92.  
    status = -EINTR;
  93.  
    break;
  94.  
    }
  95.  
    } while (iov_iter_count(i));
  96.  
    //调用 balance_dirty_pages_ratelimited() 来检查页面Cache中的脏页比例是否超过一 个阀值(通常为系统中页的40%)。若超过阀值,就调用 writeback_inodes() 来刷新几十页到磁盘上
  97.  
    return written ? written : status;
  98.  
    }
  99.  
    EXPORT_SYMBOL(generic_perform_write);

2.7.1 ext4文件系统address_space_operations

2.7.2 ext4文件系统delay allocation机制

延时分配(Delayed allocation)该技术也称为allocate-on-flush,可以提升文件系统的性能。只有buffer I/O中每次写操作都会涉及的磁盘块分配过程推迟到数据回写时再进行,即数据将要被真正写入磁盘时,文件系统才为其分配块,这与其它文件系统在早期就分配好必要的块是不同的。另外,由于ext4的这种做法可以根据真实的文件大小做块分配决策,它还减少了碎片的产生。

通常在进行Buffer Write时,系统的实际操作仅仅是为这些数据在操作系统内分配内存页(page cache)并保存这些数据,等待用户调用fsync等操作强制刷新或者等待系统触发定时回写过程。在数据拷贝到page cache这一过程中,系统会为这些数据在磁盘上分配对应的磁盘块。

而在使用delalloc(delay allocation)后,上面的流程会略有不同,在每次buffer Write时,数据会被保存到page cache中,但是系统并不会为这些数据分配相应的磁盘块,仅仅会查询是否有已经为这些数据分配过磁盘块,以便决定后面是否需要为这些数据分配磁盘 块。在用户调用fsync或者系统触发回写过程时,系统会尝试为标记需要分配磁盘块的这些 数据分配磁盘块。这样文件系统可以为这些属于同一个文件的数据分配尽量连续的磁盘空间,从而优化后续文件的访问性能。

2.7.3 执行完 generate_write_back( )后

在generic_perform_write()函数执行完成后,我们应知道以下两点:
(1) 写数据已从用户空间拷贝到页面Cache中(内核空间);
(2) 数据页面标记为脏;
(3) 数据还未写到磁盘上去,这就是“延迟写”技术。后面我们会分析何时、在哪里、怎样将数据写到磁盘上的

【作者】张昺华
【大饼教你学系列】https://edu.csdn.net/course/detail/10393
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【微信公众号】 张昺华
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/sky-heaven/p/13737382.html