2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/atomic.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
21 #include <linux/genhd.h>
22 #include <linux/mutex.h>
23 #include <linux/sched.h>
24 #include <linux/uio.h>
26 int dax_clear_blocks(struct inode *inode, sector_t block, long size)
28 struct block_device *bdev = inode->i_sb->s_bdev;
29 sector_t sector = block << (inode->i_blkbits - 9);
37 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
42 unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
46 memset(addr, 0, pgsz);
60 EXPORT_SYMBOL_GPL(dax_clear_blocks);
62 static long dax_get_addr(struct buffer_head *bh, void **addr, unsigned blkbits)
65 sector_t sector = bh->b_blocknr << (blkbits - 9);
66 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
69 static void dax_new_buf(void *addr, unsigned size, unsigned first, loff_t pos,
72 loff_t final = end - pos + first; /* The final byte of the buffer */
75 memset(addr, 0, first);
77 memset(addr + final, 0, size - final);
80 static bool buffer_written(struct buffer_head *bh)
82 return buffer_mapped(bh) && !buffer_unwritten(bh);
86 * When ext4 encounters a hole, it returns without modifying the buffer_head
87 * which means that we can't trust b_size. To cope with this, we set b_state
88 * to 0 before calling get_block and, if any bit is set, we know we can trust
89 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
90 * and would save us time calling get_block repeatedly.
92 static bool buffer_size_valid(struct buffer_head *bh)
94 return bh->b_state != 0;
97 static ssize_t dax_io(int rw, struct inode *inode, struct iov_iter *iter,
98 loff_t start, loff_t end, get_block_t get_block,
99 struct buffer_head *bh)
104 loff_t bh_max = start;
109 end = min(end, i_size_read(inode));
114 unsigned blkbits = inode->i_blkbits;
115 sector_t block = pos >> blkbits;
116 unsigned first = pos - (block << blkbits);
120 bh->b_size = PAGE_ALIGN(end - pos);
122 retval = get_block(inode, block, bh,
126 if (!buffer_size_valid(bh))
127 bh->b_size = 1 << blkbits;
128 bh_max = pos - first + bh->b_size;
130 unsigned done = bh->b_size -
131 (bh_max - (pos - first));
132 bh->b_blocknr += done >> blkbits;
136 hole = (rw != WRITE) && !buffer_written(bh);
139 size = bh->b_size - first;
141 retval = dax_get_addr(bh, &addr, blkbits);
144 if (buffer_unwritten(bh) || buffer_new(bh))
145 dax_new_buf(addr, retval, first, pos,
148 size = retval - first;
150 max = min(pos + size, end);
154 len = copy_from_iter(addr, max - pos, iter);
156 len = copy_to_iter(addr, max - pos, iter);
158 len = iov_iter_zero(max - pos, iter);
167 return (pos == start) ? retval : pos - start;
171 * dax_do_io - Perform I/O to a DAX file
172 * @rw: READ to read or WRITE to write
173 * @iocb: The control block for this I/O
174 * @inode: The file which the I/O is directed at
175 * @iter: The addresses to do I/O from or to
176 * @pos: The file offset where the I/O starts
177 * @get_block: The filesystem method used to translate file offsets to blocks
178 * @end_io: A filesystem callback for I/O completion
181 * This function uses the same locking scheme as do_blockdev_direct_IO:
182 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
183 * caller for writes. For reads, we take and release the i_mutex ourselves.
184 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
185 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
188 ssize_t dax_do_io(int rw, struct kiocb *iocb, struct inode *inode,
189 struct iov_iter *iter, loff_t pos,
190 get_block_t get_block, dio_iodone_t end_io, int flags)
192 struct buffer_head bh;
193 ssize_t retval = -EINVAL;
194 loff_t end = pos + iov_iter_count(iter);
196 memset(&bh, 0, sizeof(bh));
198 if ((flags & DIO_LOCKING) && (rw == READ)) {
199 struct address_space *mapping = inode->i_mapping;
200 mutex_lock(&inode->i_mutex);
201 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
203 mutex_unlock(&inode->i_mutex);
208 /* Protects against truncate */
209 atomic_inc(&inode->i_dio_count);
211 retval = dax_io(rw, inode, iter, pos, end, get_block, &bh);
213 if ((flags & DIO_LOCKING) && (rw == READ))
214 mutex_unlock(&inode->i_mutex);
216 if ((retval > 0) && end_io)
217 end_io(iocb, pos, retval, bh.b_private);
219 inode_dio_done(inode);
223 EXPORT_SYMBOL_GPL(dax_do_io);