ext4: fix a dio overwrite nolock bug when we are not writing the entire block

When we are not writing the entire block and ext4 has allocated a block
for us, we will hit this bug.  We can use the following code snippet to
reproduce it.

  #define SECTOR_SIZE     512
  
  int write_at(int fd, size_t size, off_t off)
  {
          int ret;
          char *buf, *content;
          size_t len;
  
          len = sizeof(char) * size;
          content = (char *)malloc(len);
          memset(content, '1', len);
          ret = posix_memalign((void **)&buf, SECTOR_SIZE, len);
          memcpy(buf, content, len);
  
          ret = lseek(fd, off, SEEK_SET);
          ret = write(fd, buf, len);
  }
  
  int main(int argc, char *argv[])
  {
          int fd, ret;
          char *filename = argv[1];
  
          fd = open(filename, O_WRONLY|O_CREAT|
                    O_TRUNC|O_LARGEFILE|O_DIRECT, 0644);
  
          write_at(fd, 512, 0);
          write_at(fd, 1024, 512);
          write_at(fd, 512, 4096);
          write_at(fd, 512, 3584);
          write_at(fd, 2048, 1536);
          write_at(fd, 1024, 512);
          write_at(fd, 512, 4608);
  
          close(fd);
  
          return 0;
  }

The root cause is that we forgot to initialize map->m_flags.  Due to we
don't backport the Ted's patch that use ext4_map_blocks to get a block
mapping, so we don't need to set bh->b_state in get_block().  Meanwhile
for avoiding this mistake later, we will initialize map->m_flags in
ext4_get_blocks().

Reference:
Patch-mainline: in house
Signed-off-by: Zheng Liu <wenqing.lz@taobao.com>
ext4: serialize truncate with owerwrite DIO workers

Jan Kara have spotted interesting issue:
There are  potential data corruption issue with  direct IO overwrites
racing with truncate:
 Like:
  dio write                      truncate_task
  ->ext4_ext_direct_IO
   ->overwrite == 1
    ->down_read(&EXT4_I(inode)->i_data_sem);
    ->mutex_unlock(&inode->i_mutex);
                               ->ext4_setattr()
                                ->inode_dio_wait()
                                ->truncate_setsize()
                                ->ext4_truncate()
                                 ->down_write(&EXT4_I(inode)->i_data_sem);
    ->__blockdev_direct_IO
     ->ext4_get_block
     ->submit_io()
    ->up_read(&EXT4_I(inode)->i_data_sem);
                                 # truncate data blocks, allocate them to
                                 # other inode - bad stuff happens because
                                 # dio is still in flight.

In order to serialize with truncate dio worker should grab extra i_dio_count
reference before drop i_mutex.

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>