Performing direct I/O on sector-aligned requests
Subsystems (3)
RFC PATCH 1/1 Performing direct I/O on sector-aligned requests
NFS daemons always perform buffered IO on files. As a result, write requests that are not aligned on a file system block boundary take about 15 times more time to complete compared to the same writes that are file system block aligned. This patch fixes the problem by analyzing alignment of the IO request that comes to NFS daemon and using Direct I/O mechanism when all of the following are true:
1. Request is not aligned on a file system block boundary
2. Request is aligned on an underlying block device’s sector boundary.
3. Request size is a multiple of the sector size.
In all other cases, buffered IO is performed as has been done before.
After applying a patch, the resulting performance of all types of requests, except unaligned writes remains the same, while performance of unaligned writes improves 15 times.
A new flag is exposed to users through /proc/fs/nfsd/direct_io node. The default value of 1 results in the above behavior. Writing 0 to the node turns off the optimization, and forces NFS daemon to always use buffered IO (as it has done before). Writing 2 to the node tells NFS daemon to use direct I/O even if request is file system block aligned.
I have tested this patch by running concurrent NFS writes to an exported file system and verifying locally that writes reached the disk.
fs/direct-io.c 2012-03-28 15:41:12.000000000 -0400 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -152,11 +152,27 @@
int nr_pages;
nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
- ret = get_user_pages_fast(
-
dio->curr_user_address, /* Where from? */ -
nr_pages, /* How many pages? */ - dio->rw ==
READ, /* Write to memory? */
-
&dio->pages[0]); /* Put results here */ +
+ if (current->mm) {
+ ret = get_user_pages_fast(
+
dio->curr_user_address, /* Where from? */ +
nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */
+
&dio->pages[0]); /* Put results here */ + } else {
+ // For kernel threads mm is NULL, so all we need is to increment
+ // page's reference count and add page to dio->pages array
+ int i;
+ struct page* page;
+ unsigned long start_pfn = __pa(dio->curr_user_address) >> PAGE_SHIFT;
+ for (i = 0; i < nr_pages; i++) { + page = pfn_to_page(start_pfn + i);
+ page_cache_get(page); + dio->pages[i] = page; + }
+
+ ret = nr_pages; // No need to lock pages:
this is kernel thread and the pages are in kernel as well
+ }
if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) {
struct page *page = ZERO_PAGE(0);
fs/nfsd/lockd.c 2012-03-28 15:40:29.000000000 -0400 renamed from fs/nfsd/lockd.c 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -36,7 +36,7 @@
fh.fh_export = NULL;
exp_readlock();
- nfserr = nfsd_open(rqstp, &fh, S_IFREG, NFSD_MAY_LOCK, filp);
+ nfserr = nfsd_open(rqstp, &fh, S_IFREG, NFSD_MAY_LOCK, filp, 0, 0);
fh_put(&fh);
exp_readunlock();
/* We return nlm error codes as nlm doesn't know diff -uNr a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
--- a/fs/nfsd/nfs4state.c 2011-10-24 14:06:32.000000000 -0400
+++ b/fs/nfsd/nfs4state.c 2012-03-28 15:40:29.000000000 -0400 @@ -2557,7 +2557,7 @@
if (!fp->fi_fds[oflag]) {
status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
- &fp->fi_fds[oflag]); + &fp->fi_fds[oflag], 0, 0); if (status)
return status; }
@@ -3951,7 +3951,7 @@
struct file *file;
int err;
- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); + err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file, 0, 0);
if (err)
return err;
err = vfs_test_lock(file, lock);
fs/nfsd/nfsctl.c 2012-03-28 15:40:29.000000000 -0400 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -46,6 +46,7 @@
NFSD_TempPorts,
NFSD_MaxBlkSize,
NFSD_SupportedEnctypes,
+ NFSD_DirectIO,
/*
* The below MUST come last. Otherwise we leave a hole in nfsd_files[]
* with !CONFIG_NFSD_V4 and simple_fill_super() goes oops @@ -78,6 +79,7 @@
static ssize_t write_ports(struct file *file, char *buf, size_t size); static ssize_t write_temp_ports(struct file *file, char *buf, size_t size);
static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
+static ssize_t write_directio(struct file *file, char *buf, size_t size); #ifdef CONFIG_NFSD_V4
static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
@@ -103,6 +105,7 @@
[NFSD_Ports] = write_ports,
[NFSD_TempPorts] = write_temp_ports, [NFSD_MaxBlkSize] = write_maxblksize,
+ [NFSD_DirectIO] = write_directio,
#ifdef CONFIG_NFSD_V4
[NFSD_Leasetime] = write_leasetime, [NFSD_Gracetime] = write_gracetime,
@@ -1348,6 +1351,58 @@
nfsd_max_blksize);
}
+int nfsd_directio_mode = DIO_FS_UNALIGNED;
+
+/**
+ * nfsd_directio_mode - sets conditions when direct IO is activated
+ *
+ * Input:
+ * buf:
ignored
+ * size:
zero
+ *
+ * OR
+ *
+ * Input:
+ * buf: C string containing an unsigned
+ *
integer value representing the new
+ *
NFS direct IO mode
+ * size:
non-zero length of C string in @buf
+ * Output:
+ * On success: passed-in buffer filled with '\n'-terminated C string
+ * containing numeric value of the current direct IO mode
+ * return code is the size in bytes of the string
+ *
+ * Possible modes are:
+ * DIO_NEVER (0) - never use direct I/O
+ * DIO_FS_UNALIGNED (1) - use direct I/O only for requests that FS unaligned
+ * and block device aligned
+ * DIO_SECTOR_ALIGNED (2) - use direct I/O for all block device aligned IO
+ * On error: return code is zero or a negative errno value
+ */
+static ssize_t write_directio(struct file *file, char *buf, size_t size) +{
+ char *mesg = buf;
+ if (size > 0) {
+ int mode;
+ int rv = get_int(&mesg, &mode); + if (rv)
+ return rv;
+ if (mode < DIO_NEVER || mode > DIO_BDEV_ALIGNED)
+ return -EINVAL; + /*
+ There is no need for synchronization here. No harm will happen
+ even if mode was changed between opening a file and choosing whether
+ to choose direct or buffered path. When we choosing a path we make sure
+ that the file has been opened in the compatible mode
+ */
+ nfsd_directio_mode = mode;
+ printk(KERN_WARNING"NFSD direct I/O mode changed to %d.",
+ nfsd_directio_mode); + }
+
+ return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", nfsd_directio_mode);
+}
+
#ifdef CONFIG_NFSD_V4
static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
{
@@ -1525,6 +1580,7 @@
[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
[NFSD_TempPorts] = {"tempportlist", &transaction_ops, S_IWUSR|S_IRUGO},
[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
+ [NFSD_DirectIO] = {"direct_io", &transaction_ops, S_IWUSR|S_IRUGO},
#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE) [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO}, #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
fs/nfsd/nfsd.h 2012-04-17 11:45:55.000000000 -0400 renamed from fs/nfsd/nfsd.h 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -68,6 +68,14 @@
extern int nfsd_max_blksize;
+enum {
+ DIO_NEVER = 0,// Never use Direct I/O. The first value + DIO_FS_UNALIGNED = 1, // Use Direct I/O when request is FS unaligned
+ DIO_BDEV_ALIGNED =2, // Always use Direct I/O when possible. The last value
+};
+
+extern int nfsd_directio_mode;
+
static inline int nfsd_v4client(struct svc_rqst *rq)
{
return rq->rq_prog == NFS_PROGRAM && rq->rq_vers == 4;
fs/nfsd/vfs.c 2012-04-17 11:48:29.000000000 -0400 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -28,6 +28,7 @@
#include <asm/uaccess.h>
#include <linux/exportfs.h>
#include <linux/writeback.h>
+#include <linux/blkdev.h>
#ifdef CONFIG_NFSD_V3
#include "xdr3.h"
@@ -718,6 +719,255 @@
return break_lease(inode, mode | O_NONBLOCK); }
+/*
+ Copies data between two iovec arrays. Individual array elements might have
+ different sizes, but total size of data described by the two arrays must
+ be the same
+*/
+static int nfsd_copy_iovec(const struct iovec* svec, const unsigned int scount,
+ struct iovec* dvec, const unsigned int dcount, size_t size) {
+ size_t cur_size, soff, doff, tocopy, srem , drem ; + unsigned int di, si;
+
+ cur_size = iov_length(svec, scount);
+ if (cur_size != iov_length(dvec, dcount))
+ return -EINVAL;
+
+ srem = drem = 0;
+ di = si = 0;
+ soff = doff = 0;
+ while (cur_size > 0) {
+ if (si >= scount || di >= dcount) + return -EFAULT; +
+ srem = svec[si].iov_len - soff; + drem = dvec[di].iov_len - doff; + tocopy = (srem > drem) ? drem : srem; + memcpy((char*)(dvec[di].iov_base) + doff, (char*)(svec[si].iov_base) + soff, tocopy);
+ cur_size -= tocopy;
+ srem -= tocopy;
+ drem -= tocopy;
+ doff += tocopy;
+ soff += tocopy;
+ if (srem == 0) {
+ si++;
+ soff = 0;
+ }
+ if (drem == 0) {
+ di++;
+ doff = 0;
+ }
+ }
+ if (si != scount || di != dcount || srem !=0 || drem != 0) + {
+ printk(KERN_WARNING"In copy_iovec: si=%lu, scount=%lu, di=%lu, dcount=%lu, srem=%lu, drem=%lu",
+ (unsigned long)si, (unsigned long)scount, (unsigned long)di,
+ (unsigned long)dcount, (unsigned long)srem, (unsigned long)drem);
+ return -EFAULT;
+ }
+
+ return 0;
+}
+
+/*
+ Allocates iovec array where each element has page alligned base address and
+ size of a page. Needed for DIRECT I/O to be possbile from this array
+ */
+static int nfsd_allocate_paged_iovec(size_t size, unsigned int* pcount, + struct iovec** pvec) {
+ unsigned int i;
+ unsigned int page_num = size / PAGE_SIZE;
+ struct iovec * vec = NULL;
+
+ *pvec = NULL;
+ *pcount = 0;
+ if (page_num * PAGE_SIZE != size)
+ page_num++;
+
+ vec = kmalloc(sizeof(struct iovec) * page_num, GFP_KERNEL); + if (!vec)
+ return -ENOMEM;
+ memset(vec, 0, sizeof(struct iovec) * page_num); + *pvec = vec;
+ *pcount = page_num;
+
+ for (i = 0; i < page_num; i++) {
+ vec[i].iov_base = (void*)__get_free_page(GFP_KERNEL);
+ if (!vec[i].iov_base)
+ return -ENOMEM; + vec[i].iov_len = PAGE_SIZE;
+ }
+
+ if (size % PAGE_SIZE)
+ vec[page_num - 1].iov_len = size % PAGE_SIZE;
+
+ return 0;
+}
+
+/*
+ Deallocates iovec array, allocated by nfsd_allocate_paged_iovec +*/
+static void nfsd_free_paged_iovec(unsigned int count, struct iovec* vec) {
+ unsigned int i;
+ if (vec) {
+ for (i = 0; i < count; i++)
+ if (vec[i].iov_base) +free_page((unsigned long)(vec[i].iov_base));
+ kfree(vec);
+ }
+}
+
+/*
+ Performs direct I/O for a given NFS write request
+*/
+static ssize_t nfsd_vfs_write_direct(struct file *file, const struct iovec *vec,
+ unsigned long vlen, loff_t *pos) { + ssize_t result = -EINVAL;
+ unsigned int page_num;
+ struct iovec *aligned_vec = NULL;
+
+ // Check size to be multiple of sectors
+ size_t size = iov_length(vec, vlen);
+
+ if (size == 0)
+ return vfs_writev(file, (struct iovec __user *)vec, vlen, pos);
+
+ // Allocate necesary number of pages
+ result = nfsd_allocate_paged_iovec(size, &page_num, &aligned_vec);
+ if (result) {
+ printk(KERN_WARNING"Cannot allocate aligned_vec.");
+ goto out;
+ }
+
+ // Copy data
+ result = nfsd_copy_iovec(vec, vlen, aligned_vec, page_num, size);
+ if(result) {
+ printk(KERN_WARNING"Wrong amount of data copied to aligned buffer.");
+ goto out;
+ }
+
+ // Call further
+ result = vfs_writev(file, (struct iovec __user *)aligned_vec, page_num, pos);
+
+out:
+ nfsd_free_paged_iovec(page_num, aligned_vec);
+ return result;
+}
+
+
+/*
+ Performs direct I/O for a given NFS read request
+*/
+static ssize_t nfsd_vfs_read_direct(struct file *file, struct iovec *vec, + unsigned long vlen, loff_t *pos) { + unsigned int page_num;
+ struct iovec *aligned_vec = NULL;
+ ssize_t result = -EINVAL;
+ size_t size;
+
+ // Check size to be multiple of sectors
+ size = iov_length(vec, vlen);
+
+ if (size == 0)
+ return vfs_readv(file, (struct iovec __user *)vec, vlen, pos);
+
+ // Allocate necesary number of pages
+ result = nfsd_allocate_paged_iovec(size, &page_num, &aligned_vec);
+ if (result) {
+ printk(KERN_WARNING"Cannot allocate aligned_vec.");
+ goto out;
+ }
+
+ // Call further
+ result = vfs_readv(file, (struct iovec __user *)aligned_vec, page_num, pos);
+ if (result < 0) {
+ printk(KERN_WARNING"Error during read operation.");
+ goto out;
+ }
+
+ // Copy data
+ if(nfsd_copy_iovec(aligned_vec, page_num, vec, vlen, size)) {
+ printk(KERN_WARNING"Wrong amount of data copied from aligned buffer.");
+ goto out;
+ }
+
+out:
+ nfsd_free_paged_iovec(page_num, aligned_vec);
+
+ return result;
+}
+
+// Returns number of terminal zero bits for a given number (number alignment)
+static unsigned int get_alignment(loff_t n) {
+ unsigned int i=0;
+ if (n == 0)
+ return (unsigned int)-1; // 0 is alligned to any number
+ while ((n & 1) == 0 && n > 0) {
+ n = n >> 1;
+ i++;
+ }
+ return i;
+}
+
+// Returns the alignment of I/O request
+static unsigned int io_alignment(const loff_t offset,
+ const unsigned long size) {
+ unsigned int i1, i2;
+
+ i1 = get_alignment(offset);
+ i2 = get_alignment(size);
+
+ return i1 > i2 ? i2 : i1;
+}
+
+
+/*
+ Based on the I/O request and file system parameters determines if
+ direct I/O can be used to perform the given request
+ Either file or sb are needed to retrieve file system and device
+ paramters
+*/
+static int can_use_direct_io(const struct file *file,
+ const struct super_block* sb,
+ const loff_t offset, const unsigned long size) {
+ unsigned int blkbits = 0;
+ struct inode *inode;
+ unsigned int fsblkbits = 0;
+ unsigned int alignment = io_alignment(offset, size); +
+ if (alignment == 0)
+ return 0;
+
+ if (file == NULL && sb == NULL)
+ return 0;
+
+ if (nfsd_directio_mode == DIO_NEVER)
+ return 0;
+
+ if (file != NULL && sb == NULL) {
+ inode = file->f_path.dentry->d_inode; + sb = inode->i_sb;
+ fsblkbits = inode->i_blkbits;
+ }
+
+ if (sb !=NULL) {
+ blkbits = sb->s_blocksize_bits; + fsblkbits = sb->s_blocksize_bits; + if (sb->s_bdev)
+ blkbits = blksize_bits(bdev_logical_block_size(sb->s_bdev));
+ } else
+ blkbits = fsblkbits;
+
+ if (alignment >= fsblkbits && fsblkbits > 0 && nfsd_directio_mode != DIO_BDEV_ALIGNED)
+ return 0;
+
+ if (alignment < blkbits)
+ return 0;
+
+ return 1;
+}
+
+
/*
* Open an existing file or directory.
* The access argument indicates the type of open (read/write/lock) @@ -725,13 +975,15 @@
*/
__be32
nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, - int access, struct file **filp)
+ int access, struct file **filp,
+ const loff_t offset, const unsigned long size)
{
struct dentry *dentry;
struct inode *inode;
int flags = O_RDONLY|O_LARGEFILE; __be32 err;
int host_err = 0;
+ struct super_block* sb;
validate_process_creds();
@@ -774,6 +1026,11 @@
else
flags = O_WRONLY|O_LARGEFILE;
}
+
+ sb = fhp->fh_export->ex_path.mnt->mnt_sb;
+ if (size && can_use_direct_io(NULL, sb, offset, size)) + flags |= O_DIRECT;
+
*filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
flags, current_cred());
if (IS_ERR(*filp))
@@ -885,8 +1142,10 @@
return __splice_from_pipe(pipe, sd, nfsd_splice_actor); }
+
+
static __be32
-nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
+ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
{
mm_segment_t oldfs;
@@ -899,21 +1158,29 @@
if (rqstp->rq_vers >= 3)
file->f_flags |= O_NONBLOCK;
- if (file->f_op->splice_read && rqstp->rq_splice_ok) { - struct splice_desc sd = {
- .len
= 0,
- .total_len = *count,
- .pos
= offset,
- .u.data = rqstp,
- };
-
- rqstp->rq_resused = 1;
- host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
- } else {
+ if (file->f_flags & O_DIRECT) {
+ // So far we do not support splice IO, so always do regular
oldfs = get_fs(); set_fs(KERNEL_DS);
- host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
+ host_err = nfsd_vfs_read_direct(file, (struct iovec*)vec, vlen, &offset);
set_fs(oldfs);
+ } else {
+ if (file->f_op->splice_read && rqstp->rq_splice_ok) {
+ struct splice_desc sd = { +
.len = 0,
+
.total_len = *count,
+
.pos = offset,
+
.u.data = rqstp,
+ };
+
+ rqstp->rq_resused = 1; + host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
+ } else {
+ oldfs = get_fs(); + set_fs(KERNEL_DS); + host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
+ set_fs(oldfs); + }
}
if (host_err >= 0) {
@@ -1024,7 +1291,11 @@
/* Write the data. */
oldfs = get_fs(); set_fs(KERNEL_DS);
- host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
+ if (file->f_flags & O_DIRECT)
+ host_err = nfsd_vfs_write_direct(file, (struct iovec*)vec, vlen, &offset);
+ else
+ host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
+
set_fs(oldfs);
if (host_err < 0)
goto out_nfserr;
@@ -1064,8 +1335,9 @@
struct inode *inode;
struct raparms *ra;
__be32 err;
+ unsigned long size = iov_length((struct iovec*)vec, vlen);
- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); + err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file, offset, size);
if (err)
return err;
@@ -1133,7 +1405,8 @@
err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
stablep); } else {
- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
+ unsigned long size = iov_length((struct iovec*)vec, vlen);
+ err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file, offset, size);
if (err)
goto out;
@@ -1173,7 +1446,7 @@
}
err = nfsd_open(rqstp, fhp, S_IFREG,
-
NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
+
NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file, 0, 0);
if (err)
goto out;
if (EX_ISSYNC(fhp->fh_export)) {
@@ -2018,7 +2291,7 @@
struct file *file;
loff_t offset = *offsetp;
- err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file); + err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file, 0, 0);
if (err)
goto out;
fs/nfsd/vfs.h 2012-03-28 15:40:29.000000000 -0400 renamed from fs/nfsd/vfs.h 2011-10-24 14:06:32.000000000 -0400 +0 -0
@@ -66,7 +66,7 @@
loff_t, unsigned long);
#endif /* CONFIG_NFSD_V3 */
__be32 nfsd_open(struct svc_rqst *, struct svc_fh *, int,
- int, struct file **);
+ int, struct file **, const loff_t, const unsigned long);
void nfsd_close(struct file *);
__be32 nfsd_read(struct svc_rqst *, struct svc_fh *,
loff_t, struct kvec *, int, unsigned long *);
T24gV2VkLCAyMDEyLTA0LTE4IGF0IDA3OjE4IC0wNzAwLCBBbGV4YW5kcmUgRGVwb3V0b3ZpdGNo IHdyb3RlOg0KPiBORlMgZGFlbW9ucyBhbHdheXMgcGVyZm9ybSBidWZmZXJlZCBJTyBvbiBmaWxl cy4gQXMgYSByZXN1bHQsIHdyaXRlDQo+IHJlcXVlc3RzIHRoYXQgYXJlIG5vdCBhbGlnbmVkIG9u IGEgZmlsZSBzeXN0ZW0gYmxvY2sgYm91bmRhcnkgdGFrZSBhYm91dA0KPiAxNSB0aW1lcyBtb3Jl IHRpbWUgdG8gY29tcGxldGUgY29tcGFyZWQgdG8gdGhlIHNhbWUgd3JpdGVzIHRoYXQgYXJlIGZp bGUNCj4gc3lzdGVtIGJsb2NrIGFsaWduZWQuIFRoaXMgcGF0Y2ggZml4ZXMgdGhlIHByb2JsZW0g YnkgYW5hbHl6aW5nIGFsaWdubWVudA0KPiBvZiB0aGUgSU8gcmVxdWVzdCB0aGF0IGNvbWVzIHRv IE5GUyBkYWVtb24gYW5kIHVzaW5nIERpcmVjdCBJL08gbWVjaGFuaXNtDQo+IHdoZW4gYWxsIG9m IHRoZSBmb2xsb3dpbmcgYXJlIHRydWU6DQo+IDEuIFJlcXVlc3QgaXMgbm90IGFsaWduZWQgb24g YSBmaWxlIHN5c3RlbSBibG9jayBib3VuZGFyeQ0KPiAyLiBSZXF1ZXN0IGlzIGFsaWduZWQgb24g YW4gdW5kZXJseWluZyBibG9jayBkZXZpY2XigJlzIHNlY3RvciBib3VuZGFyeS4NCj4gMy4gUmVx dWVzdCBzaXplIGlzIGEgbXVsdGlwbGUgb2YgdGhlIHNlY3RvciBzaXplLg0KPiBJbiBhbGwgb3Ro ZXIgY2FzZXMsIGJ1ZmZlcmVkIElPIGlzIHBlcmZvcm1lZCBhcyBoYXMgYmVlbiBkb25lIGJlZm9y ZS4NCj4gDQo+IEFmdGVyIGFwcGx5aW5nIGEgcGF0Y2gsIHRoZSByZXN1bHRpbmcgcGVyZm9ybWFu Y2Ugb2YgYWxsIHR5cGVzIG9mDQo+IHJlcXVlc3RzLCBleGNlcHQgdW5hbGlnbmVkIHdyaXRlcyBy ZW1haW5zIHRoZSBzYW1lLCB3aGlsZSBwZXJmb3JtYW5jZSBvZg0KPiB1bmFsaWduZWQgd3JpdGVz IGltcHJvdmVzIDE1IHRpbWVzLg0KPiBBIG5ldyBmbGFnIGlzIGV4cG9zZWQgdG8gdXNlcnMgdGhy b3VnaCAvcHJvYy9mcy9uZnNkL2RpcmVjdF9pbyBub2RlLiBUaGUNCj4gZGVmYXVsdCB2YWx1ZSBv ZiAxIHJlc3VsdHMgaW4gdGhlIGFib3ZlIGJlaGF2aW9yLiBXcml0aW5nIDAgdG8gdGhlIG5vZGUN Cj4gdHVybnMgb2ZmIHRoZSBvcHRpbWl6YXRpb24sIGFuZCBmb3JjZXMgTkZTIGRhZW1vbiB0byBh bHdheXMgdXNlIGJ1ZmZlcmVkDQo+IElPIChhcyBpdCBoYXMgZG9uZSBiZWZvcmUpLiBXcml0aW5n IDIgdG8gdGhlIG5vZGUgdGVsbHMgTkZTIGRhZW1vbiB0byB1c2UNCj4gZGlyZWN0IEkvTyBldmVu IGlmIHJlcXVlc3QgaXMgZmlsZSBzeXN0ZW0gYmxvY2sgYWxpZ25lZC4NCj4gDQo+IEkgaGF2ZSB0 ZXN0ZWQgdGhpcyBwYXRjaCBieSBydW5uaW5nIGNvbmN1cnJlbnQgTkZTIHdyaXRlcyB0byBhbiBl eHBvcnRlZA0KPiAgZmlsZSBzeXN0ZW0gYW5kIHZlcmlmeWluZyBsb2NhbGx5IHRoYXQgd3JpdGVz IHJlYWNoZWQgdGhlIGRpc2suDQo+IA0KPHNuaXA+DQo+ICsvKg0KPiArIFBlcmZvcm1zIGRpcmVj dCBJL08gZm9yIGEgZ2l2ZW4gTkZTIHdyaXRlIHJlcXVlc3QNCj4gKyovDQo+ICtzdGF0aWMgc3Np emVfdCBuZnNkX3Zmc193cml0ZV9kaXJlY3Qoc3RydWN0IGZpbGUgKmZpbGUsIGNvbnN0IHN0cnVj dA0KPiBpb3ZlYyAqdmVjLA0KPiArICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdW5z aWduZWQgbG9uZyB2bGVuLCBsb2ZmX3QgKnBvcykgew0KPiArICAgICAgICAgICAgICBzc2l6ZV90 IHJlc3VsdCA9IC1FSU5WQUw7DQo+ICsgICAgICAgICAgICAgIHVuc2lnbmVkIGludCBwYWdlX251 bTsNCj4gKyAgICAgICAgICAgICAgc3RydWN0IGlvdmVjICphbGlnbmVkX3ZlYyA9IE5VTEw7DQo+ ICsgICAgICAgICAgICAgDQo+ICsgICAgICAgICAgICAgIC8vIENoZWNrIHNpemUgdG8gYmUgbXVs dGlwbGUgb2Ygc2VjdG9ycw0KPiArICAgICAgICAgICAgICBzaXplX3Qgc2l6ZSA9IGlvdl9sZW5n dGgodmVjLCB2bGVuKTsNCj4gKw0KPiArICAgICAgICAgICAgICBpZiAoc2l6ZSA9PSAwKQ0KPiAr ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHZmc193cml0ZXYoZmlsZSwgKHN0 cnVjdCBpb3ZlYw0KPiBfX3VzZXIgKil2ZWMsIHZsZW4sIHBvcyk7DQo+ICsNCj4gKyAgICAgICAg ICAgICAgLy8gQWxsb2NhdGUgbmVjZXNhcnkgbnVtYmVyIG9mIHBhZ2VzDQo+ICsgICAgICAgICAg ICAgIHJlc3VsdCA9IG5mc2RfYWxsb2NhdGVfcGFnZWRfaW92ZWMoc2l6ZSwgJnBhZ2VfbnVtLA0K PiAmYWxpZ25lZF92ZWMpOw0KPiArICAgICAgICAgICAgICBpZiAocmVzdWx0KSB7DQo+ICsgICAg ICAgICAgICAgICAgICAgICAgICAgICAgICBwcmludGsoS0VSTl9XQVJOSU5HIkNhbm5vdCBhbGxv Y2F0ZQ0KPiBhbGlnbmVkX3ZlYy4iKTsNCj4gKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAg IGdvdG8gb3V0Ow0KPiArICAgICAgICAgICAgICB9DQo+ICsNCj4gKyAgICAgICAgICAgICAgLy8g Q29weSBkYXRhDQo+ICsgICAgICAgICAgICAgIHJlc3VsdCA9IG5mc2RfY29weV9pb3ZlYyh2ZWMs IHZsZW4sIGFsaWduZWRfdmVjLCBwYWdlX251bSwNCj4gc2l6ZSk7DQo+ICsgICAgICAgICAgICAg IGlmKHJlc3VsdCkgew0KPiArICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJpbnRrKEtF Uk5fV0FSTklORyJXcm9uZyBhbW91bnQgb2YgZGF0YQ0KPiBjb3BpZWQgdG8gYWxpZ25lZCBidWZm ZXIuIik7DQo+ICsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBnb3RvIG91dDsNCj4gKyAg ICAgICAgICAgICAgfQ0KPiArDQo+ICsgICAgICAgICAgICAgIC8vIENhbGwgZnVydGhlcg0KPiAr ICAgICAgICAgICAgICByZXN1bHQgPSB2ZnNfd3JpdGV2KGZpbGUsIChzdHJ1Y3QgaW92ZWMgX191 c2VyDQo+ICopYWxpZ25lZF92ZWMsIHBhZ2VfbnVtLCBwb3MpOw0KPiArDQo+ICtvdXQ6DQo+ICsg ICAgICAgICAgICAgIG5mc2RfZnJlZV9wYWdlZF9pb3ZlYyhwYWdlX251bSwgYWxpZ25lZF92ZWMp Ow0KPiArICAgICAgICAgICAgICByZXR1cm4gcmVzdWx0Ow0KPiArfQ0KPiArDQo+ICsNCg0KQ2Fu IHRoaXMgYmUgcmV3cml0dGVuIHRvIHVzZSBEYXZlIEtsZWlrYW1wJ3MgaW92X2l0ZXIgaW50ZXJm YWNlIHdpdGgNCmFzeW5jaHJvbm91cyByZWFkcyBhbmQgd3JpdGVzPyBPdGhlcndpc2UgSSBjYW4n dCBzZWUgaG93IGl0IGlzIGdvaW5nIHRvDQphdm9pZCBiZWluZyBtaW5kbnVtYmluZ2x5IHNsb3cu DQoNCllvdSBjYW4gc2VlIHRoZSBMV04gZGVzY3JpcHRpb24gYW5kIGxpbmsgdG8gaGlzIHBhdGNo ZXMgaW4NCmh0dHA6Ly9sd24ubmV0L0FydGljbGVzLzQ5MDExNC8NCg0KLS0gDQpUcm9uZCBNeWts ZWJ1c3QNCkxpbnV4IE5GUyBjbGllbnQgbWFpbnRhaW5lcg0KDQpOZXRBcHANClRyb25kLk15a2xl YnVzdEBuZXRhcHAuY29tDQp3d3cubmV0YXBwLmNvbQ0KDQo=
I did not notice any slowdown when doing unaligned or aligned IO, when NFS was mounted in synchronous mode.
I can see possible performance issues with writes when NFS is mounted in asynchronous mode, or do you foresee any other problematic situations? Thank you,
Alex
-----Original Message-----
From: Myklebust, Trond [mailto:[email protected]]
Sent: Wednesday, April 18, 2012 12:20 PM
To: Alexandre Depoutovitch
Cc: [email protected]
Subject: Re: [PATCH RFC] Performing direct I/O on sector-aligned requests
Quoted 23 lines · Alexandre Depoutovitch
NFS daemons always perform buffered IO on files. As a result, write requests that are not aligned on a file system block boundary take about 15 times more time to complete compared to the same writes that are file system block aligned. This patch fixes the problem by analyzing alignment of the IO request that comes to NFS daemon and using Direct I/O mechanism when all of the following are true:
1. Request is not aligned on a file system block boundary
2. Request is aligned on an underlying block device’s sector boundary.
3. Request size is a multiple of the sector size.
In all other cases, buffered IO is performed as has been done before.After applying a patch, the resulting performance of all types of requests, except unaligned writes remains the same, while performance of unaligned writes improves 15 times.
A new flag is exposed to users through/proc/fs/nfsd/direct_ionode. The default value of 1 results in the above behavior. Writing 0 to the node turns off the optimization, and forces NFS daemon to always use buffered IO (as it has done before). Writing 2 to the node tells NFS daemon to use direct I/O even if request is file system block aligned.I have tested this patch by running concurrent NFS writes to an exported
file system and verifying locally that writes reached the disk.
<snip>
Quoted 38 lines
(quoted diff) +4
-0+/* + Performs direct I/O for a given NFS write request +*/ +static ssize_t nfsd_vfs_write_direct(struct file *file, const structiovec *vec,
(quoted diff) +10
-0+ unsigned long vlen, loff_t *pos) { + ssize_t result = -EINVAL; + unsigned int page_num; + struct iovec *aligned_vec = NULL; + + // Check size to be multiple of sectors + size_t size = iov_length(vec, vlen); + + if (size == 0) + return vfs_writev(file, (struct iovec
__user*)vec, vlen, pos);(quoted diff) +3
-0+ + // Allocate necesary number of pages + result = nfsd_allocate_paged_iovec(size, &page_num,
&aligned_vec);(quoted diff) +2
-0+ if (result) { + printk(KERN_WARNING"Cannot allocate
aligned_vec.");(quoted diff) +5
-0+ goto out; + } + + // Copy data + result = nfsd_copy_iovec(vec, vlen, aligned_vec, page_num,size);
(quoted diff) +2
-0+ if(result) { + printk(KERN_WARNING"Wrong amount of datacopied to aligned buffer.");
(quoted diff) +5
-0+ goto out; + } + + // Call further + result = vfs_writev(file, (struct iovec __user*)aligned_vec,
page_num, pos);
Review
Quoted 7 lines
(quoted diff) +7
-0+ +out: + nfsd_free_paged_iovec(page_num, aligned_vec); + return result; +} + +
Can this be rewritten to use Dave Kleikamp's iov_iter interface with asynchronous reads and writes? Otherwise I can't see how it is going to avoid being mindnumbingly slow.
You can see the LWN description and link to his patches in
http://lwn.net/Articles/490114/
Signature · 6 lines
Trond Myklebust Linux NFS client maintainer NetApp [email protected] www.netapp.com
RFC PATCH v2 1/1 Performing direct I/O on sector-aligned requests
In reply to Alexandre DepoutovitchNFS daemons always perform buffered IO on files. As a result, write requests that are not aligned on a file system block boundary take about 15 times more time to complete compared to the same writes that are file system block aligned. This patch fixes the problem by analyzing alignment of the IO request that comes to NFS daemon and using Direct I/O mechanism when all of the following are true:
1. Request is not aligned on a file system block boundary
2. Request is aligned on an underlying block device's sector boundary.
3. Request size is a multiple of the sector size.
In all other cases, buffered IO is performed as has been done before.
A new flag is exposed to users through /proc/fs/nfsd/direct_io node. The default value of 1 results in the above behavior. Writing 0 to the node turns off the direct I/O completely, and forces NFS daemon to always use buffered IO (as it has done before). Writing 2 to the node tells NFS daemon to use direct I/O whenever possible, even if requests are aligned at file system block boundary.
In order to test the patch, the following have been done: I've deployed 2 Linux machines with 3.0 kernel and my modifications. One acted as an NFS server, the other acted as an NFS client. NFS volume was mounted in sync mode.
Number of NFS daemons was increased to 64 in order to have higher chances of catching concurrency issues. Volume was formatted using ext4 file system. Volume was located on a hardware RAID10 array with 8 10K 450GB SAS drives. Raid adapter was HP P410i.
1. During first set of experiments, the client machine created a 200 GB file by writing to it. Then it performed the following access patterns:
Read, random, (4K)
Write, random, (4K)
Read, sequential (4K)
Write, sequential (4K)
Read, sequential (4K, first access at 512 offset)
Write, sequential (4K, first access at 512 offset)
Read, sequential (32K)
Write, sequential (32K)
Read, sequential (32K, first access at 512 offset)
Write, sequential (32K, first access at 512 offset)
Read, sequential (256K)
Write, sequential (256K)
All accesses where done with keeping 64 outstanding IO requests on a client. I compared performance of the above patterns on vanilla Linux and Linux with my changes. All numbers (IOPS, latency) where the same for all cases except for random writes, where IOPS increase was 14 times.
In addition, I have done several correctness tests.
2. Allocated three 200GB files using (a) explicit writes to a file, (b) fallocate() system call, (c) seeking to the end of the file and writing one sector there.
Then, did random and sequential writes to files. After that, I verified that files were indeed modified and contained the latest data. Test for each file ran for 2 hours.
3. Allocated 200GB file and started sequential reads to trigger read-ahead mechanism. Every 100 read operations, one file system unaligned write immediately after the current read position was requested in order to trigger a direct write. After that, read continued. All writes contained a predefined value, so that read can check for it. I have done this, in order to be sure that direct write correctly invalidates already in-memory cache.
Current implementation performs synchronous direct I/O and may trigger higher latencies when NFS volume is mounted in asynchronous mode. In order to avoid it, as per Trond Myklebust's suggestion, iov_iter interface with asynchronous reads and writes can be used. This is why currently, Direct I/O can
be enabled or disabled at boot or run-time without NFS server restart through the /proc/fs/nfsd/direct_io node.
--------------------------------------------------------------------------
diff -uNr linux-orig/fs/direct-io.clinux-3.0.7-0.7.2.8796.vmw/fs/direct-io.c
--- linux-orig/fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400
+++ linux-3.0.7-0.7.2.8796.vmw/fs/direct-io.c 2012-04-25
16:34:30.000000000 -0400
@@ -152,11 +152,30 @@
int nr_pages;
nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES);
- ret = get_user_pages_fast(
- dio->curr_user_address, /* Where from? */
- nr_pages, /* How many pages? */
- dio->rw == READ, /* Write to memory? */
- &dio->pages[0]); /* Put results here */
+
+ if (current->mm) {
+ ret = get_user_pages_fast(
+ dio->curr_user_address, /* Where from? */
+ nr_pages, /* How many pages? */
+ dio->rw == READ, /* Write to memory? */
+ &dio->pages[0]); /* Put results here */
+ } else {
+ /* For kernel threads mm is NULL, so all we need is to increment
+ page's reference count and add page to dio->pages array */
+ int i;
+ struct page* page;
+ unsigned long start_pfn = virt_to_phys((void *)dio->curr_user_address)
+ >> PAGE_SHIFT;
+ /* For kernel threads buffer must be in kernel memory */
+ BUG_ON(dio->curr_user_address < TASK_SIZE_MAX);
+
+ for (i = 0; i < nr_pages; i++) {
+ page = pfn_to_page(start_pfn + i); + page_cache_get(page); + dio->pages[i] = page;
+ }
+ /* No need to lock pages: this is kernel thread and the pages are in
+ kernel as well */
+ ret = nr_pages;
+ }
if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) {
struct page *page = ZERO_PAGE(0);
@@ -972,7 +991,11 @@
break;
}- /* Drop the ref which was taken in get_user_pages() */
+ /*
+ * Drop the ref which was taken in dio_refill_pages
+ * directly (for direct I/O) or by calling get_user_pages
+ * (for buffered IO)
+ */
page_cache_release(page);
block_in_page = 0;
}diff -uNr linux-orig/fs/nfsd/lockd.c linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/lockd.c
--- linux-orig/fs/nfsd/lockd.c 2011-10-24 14:06:32.000000000 -0400 +++ linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/lockd.c 2012-03-28 15:40:29.000000000 -0400
@@ -36,7 +36,7 @@
fh.fh_export = NULL;
exp_readlock();
- nfserr = nfsd_open(rqstp, &fh, S_IFREG, NFSD_MAY_LOCK, filp);
+ nfserr = nfsd_open(rqstp, &fh, S_IFREG, NFSD_MAY_LOCK, filp, 0, 0);
fh_put(&fh);
exp_readunlock();
/* We return nlm error codes as nlm doesn't knowdiff -uNr linux-orig/fs/nfsd/nfs4state.c linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfs4state.c
--- linux-orig/fs/nfsd/nfs4state.c 2011-10-24 14:06:32.000000000 -0400 +++ linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfs4state.c 2012-03-28 15:40:29.000000000 -0400
@@ -2557,7 +2557,7 @@
if (!fp->fi_fds[oflag]) {
status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
- &fp->fi_fds[oflag]);
+ &fp->fi_fds[oflag], 0, 0);
if (status)
return status;
}
@@ -3951,7 +3951,7 @@
struct file *file;
int err;- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
+ err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file, 0, 0);
if (err)
return err;
err = vfs_test_lock(file, lock);diff -uNr linux-orig/fs/nfsd/nfsctl.c linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsctl.c
--- linux-orig/fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +++ linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsctl.c 2012-03-28 15:40:29.000000000 -0400
@@ -46,6 +46,7 @@
NFSD_TempPorts,
NFSD_MaxBlkSize,
NFSD_SupportedEnctypes,
+ NFSD_DirectIO,
/*
* The below MUST come last. Otherwise we leave a hole in nfsd_files[]
* with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
@@ -78,6 +79,7 @@
static ssize_t write_ports(struct file *file, char *buf, size_t size); static ssize_t write_temp_ports(struct file *file, char *buf, size_t size); static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
+static ssize_t write_directio(struct file *file, char *buf, size_t size);
#ifdef CONFIG_NFSD_V4
static ssize_t write_leasetime(struct file *file, char *buf, size_t size); static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
@@ -103,6 +105,7 @@
[NFSD_Ports] = write_ports,
[NFSD_TempPorts] = write_temp_ports,
[NFSD_MaxBlkSize] = write_maxblksize,
+ [NFSD_DirectIO] = write_directio,
#ifdef CONFIG_NFSD_V4
[NFSD_Leasetime] = write_leasetime,
[NFSD_Gracetime] = write_gracetime,
@@ -1348,6 +1351,58 @@
nfsd_max_blksize);
}
+int nfsd_directio_mode = DIO_NEVER;
+
+/**
+ * nfsd_directio_mode - sets conditions when direct IO is activated
+ *
+ * Input:
+ * buf: ignored + * size: zero
+ *
+ * OR
+ *
+ * Input:
+ * buf: C string containing an unsigned
+ * integer value representing the new + * NFS direct IO mode + * size: non-zero length of C string in @buf
+ * Output:
+ * On success: passed-in buffer filled with '\n'-terminated C string
+ * containing numeric value of the current direct IO mode
+ * return code is the size in bytes of the string
+ *
+ * Possible modes are:
+ * DIO_NEVER (0) - never use direct I/O
+ * DIO_FS_UNALIGNED (1) - use direct I/O only for requests that FS
unaligned
+ * and block device aligned
+ * DIO_SECTOR_ALIGNED (3) - use direct I/O for all block device aligned
IO
+ * On error: return code is zero or a negative errno value
+ */
+static ssize_t write_directio(struct file *file, char *buf, size_t size) +{
+ char *mesg = buf;
+ if (size > 0) {
+ int mode; + int rv = get_int(&mesg, &mode); + if (rv) + return rv; + if (mode < DIO_NEVER || mode > DIO_BDEV_ALIGNED) + return -EINVAL; + /* + There is no need for synchronization here. No harm will happen + even if mode was changed between opening a file and choosing whether + to choose direct or buffered path. When we choosing a path we make sure + that the file has been opened in the compatible mode + */ + nfsd_directio_mode = mode; + printk(KERN_WARNING"NFSD direct I/O mode changed to %d.",
+ nfsd_directio_mode);
+ }
+
+ return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",nfsd_directio_mode);
+}
+
#ifdef CONFIG_NFSD_V4
static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t
size, time_t *time)
{
@@ -1525,6 +1580,7 @@
[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, [NFSD_TempPorts] = {"tempportlist", &transaction_ops,
S_IWUSR|S_IRUGO},
[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops,
S_IWUSR|S_IRUGO},
+ [NFSD_DirectIO] = {"direct_io", &transaction_ops, S_IWUSR|S_IRUGO},
#if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
[NFSD_SupportedEnctypes] = {"supported_krb5_enctypes",&supported_enctypes_ops, S_IRUGO},
#endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
diff -uNr linux-orig/fs/nfsd/nfsd.h linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsd.h
--- linux-orig/fs/nfsd/nfsd.h 2011-10-24 14:06:32.000000000 -0400
+++ linux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsd.h 2012-04-17
11:45:55.000000000 -0400
@@ -68,6 +68,14 @@
extern int nfsd_max_blksize;
+enum {
+ DIO_NEVER = 0,// Never use Direct I/O. The first value + DIO_FS_UNALIGNED = 1, // Use Direct I/O when request is FS unaligned + DIO_BDEV_ALIGNED =2, // Always use Direct I/O when possible. The last
value
+};
+
+extern int nfsd_directio_mode;
+
static inline int nfsd_v4client(struct svc_rqst *rq)
{
return rq->rq_prog == NFS_PROGRAM && rq->rq_vers == 4;fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +0 -0
14:21:38.000000000 -0400
@@ -28,6 +28,7 @@
#include <asm/uaccess.h>
#include <linux/exportfs.h>
#include <linux/writeback.h>
+#include <linux/blkdev.h>
#ifdef CONFIG_NFSD_V3
#include "xdr3.h"
@@ -718,6 +719,255 @@
return break_lease(inode, mode | O_NONBLOCK);
}
+/*
+ Copies data between two iovec arrays. Individual array elements might have
+ different sizes, but total size of data described by the two arrays must
+ be the same
+*/
+static int nfsd_copy_iovec(const struct iovec* svec, const unsigned int scount,
+ struct iovec* dvec, const unsigned int dcount, size_t size)
{
+ size_t cur_size, soff, doff, tocopy, srem , drem ;
+ unsigned int di, si;
+
+ cur_size = iov_length(svec, scount);
+ if (cur_size != iov_length(dvec, dcount))
+ return -EINVAL;
+
+ srem = drem = 0;
+ di = si = 0;
+ soff = doff = 0;
+ while (cur_size > 0) {
+ if (si >= scount || di >= dcount)
+ return -EFAULT;
+
+ srem = svec[si].iov_len - soff;
+ drem = dvec[di].iov_len - doff;
+ tocopy = (srem > drem) ? drem : srem;
+ memcpy((char*)(dvec[di].iov_base) + doff,
(char*)(svec[si].iov_base) + soff, tocopy);
+ cur_size -= tocopy;
+ srem -= tocopy;
+ drem -= tocopy;
+ doff += tocopy;
+ soff += tocopy;
+ if (srem == 0) {+ si++;
+ soff = 0;
+ }
+ if (drem == 0) {
+ di++;
+ doff = 0;
+ }
+ }
+ if (si != scount || di != dcount || srem !=0 || drem != 0)
+ {+ printk(KERN_WARNING"In copy_iovec: si=%lu, scount=%lu, di=%lu,
dcount=%lu, srem=%lu, drem=%lu",
+ (unsigned long)si, (unsigned long)scount, (unsigned long)di,
+ (unsigned long)dcount, (unsigned long)srem, (unsigned
long)drem);
+ return -EFAULT;
+ }
+
+ return 0;
+}
+
+/*
+ Allocates iovec array where each element has memory page-aligned base address
+ and size of a page. Needed for DIRECT I/O to be possbile from this array
+ */
+static int nfsd_allocate_paged_iovec(size_t size, unsigned int* pcount,
+ struct iovec** pvec) {
+ unsigned int i; + unsigned int page_num = size / PAGE_SIZE; + struct iovec * vec = NULL; + + *pvec = NULL; + *pcount = 0; + if (page_num * PAGE_SIZE != size)
+ page_num++;
+
+ vec = kmalloc(sizeof(struct iovec) * page_num, GFP_KERNEL);
+ if (!vec)
+ return -ENOMEM;
+ memset(vec, 0, sizeof(struct iovec) * page_num); + *pvec = vec; + *pcount = page_num;
+
+ for (i = 0; i < page_num; i++) {
+ vec[i].iov_base = (void*)__get_free_page(GFP_KERNEL);
+ if (!vec[i].iov_base)
+ return -ENOMEM;
+ vec[i].iov_len = PAGE_SIZE;
+ }
+
+ if (size % PAGE_SIZE)
+ vec[page_num - 1].iov_len = size % PAGE_SIZE;
+
+ return 0;
+}
+
+/*
+ Deallocates iovec array, allocated by nfsd_allocate_paged_iovec +*/
+static void nfsd_free_paged_iovec(unsigned int count, struct iovec* vec) {
+ unsigned int i;
+ if (vec) {
+ for (i = 0; i < count; i++)
+ if (vec[i].iov_base)
+ free_page((unsigned long)(vec[i].iov_base));
+ kfree(vec);
+ }
+}
+
+/*
+ Performs direct I/O for a given NFS write request
+*/
+static ssize_t nfsd_vfs_write_direct(struct file *file, const struct iovec *vec,
+ unsigned long vlen, loff_t *pos) {
+ ssize_t result = -EINVAL; + unsigned int page_num; + struct iovec *aligned_vec = NULL;
+
+ // Check size to be multiple of sectors
+ size_t size = iov_length(vec, vlen);
+
+ if (size == 0)
+ return vfs_writev(file, (struct iovec __user *)vec, vlen, pos);
+
+ // Allocate necesary number of pages
+ result = nfsd_allocate_paged_iovec(size, &page_num, &aligned_vec);
+ if (result) {+ printk(KERN_WARNING"Cannot allocate aligned_vec.");
+ goto out;
+ }
+
+ // Copy data
+ result = nfsd_copy_iovec(vec, vlen, aligned_vec, page_num, size);
+ if(result) {+ printk(KERN_WARNING"Wrong amount of data copied to aligned
buffer.");
+ goto out;
+ }
+
+ // Call further
+ result = vfs_writev(file, (struct iovec __user *)aligned_vec, page_num,
pos);
+
+out:
+ nfsd_free_paged_iovec(page_num, aligned_vec);
+ return result;
+}
+
+
+/*
+ Performs direct I/O for a given NFS read request
+*/
+static ssize_t nfsd_vfs_read_direct(struct file *file, struct iovec *vec,
+ unsigned long vlen, loff_t *pos) {
+ unsigned int page_num; + struct iovec *aligned_vec = NULL; + ssize_t result = -EINVAL; + size_t size;
+
+ // Check size to be multiple of sectors
+ size = iov_length(vec, vlen);
+
+ if (size == 0)
+ return vfs_readv(file, (struct iovec __user *)vec, vlen, pos);
+
+ // Allocate necesary number of pages
+ result = nfsd_allocate_paged_iovec(size, &page_num, &aligned_vec);
+ if (result) {+ printk(KERN_WARNING"Cannot allocate aligned_vec.");
+ goto out;
+ }
+
+ // Call further
+ result = vfs_readv(file, (struct iovec __user *)aligned_vec, page_num,
pos);
+ if (result < 0) {
+ printk(KERN_WARNING"Error during read operation.");
+ goto out;
+ }
+
+ // Copy data
+ if(nfsd_copy_iovec(aligned_vec, page_num, vec, vlen, size)) {
+ printk(KERN_WARNING"Wrong amount of data copied from aligned
buffer.");
+ goto out;
+ }
+
+out:
+ nfsd_free_paged_iovec(page_num, aligned_vec);
+
+ return result;
+}
+
+// Returns number of terminal zero bits for a given number (number alignment)
+static unsigned int get_alignment(loff_t n) {
+ unsigned int i=0;
+ if (n == 0)
+ return (unsigned int)-1; // 0 is alligned to any number
+ while ((n & 1) == 0 && n > 0) {
+ n = n >> 1;
+ i++;
+ }
+ return i;
+}
+
+// Returns the alignment of I/O request
+static unsigned int io_alignment(const loff_t offset,
+ const unsigned long size) {
+ unsigned int i1, i2;
+
+ i1 = get_alignment(offset);
+ i2 = get_alignment(size);
+
+ return i1 > i2 ? i2 : i1;
+}
+
+
+/*
+ Based on the I/O request and file system parameters determines if
+ direct I/O can be used to perform the given request
+ Either file or sb are needed to retrieve file system and device
+ paramters
+*/
+static int can_use_direct_io(const struct file *file,
+ const struct super_block* sb,
+ const loff_t offset, const unsigned long size) {
+ unsigned int blkbits = 0; + struct inode *inode; + unsigned int fsblkbits = 0; + unsigned int alignment = io_alignment(offset, size);
+
+ if (alignment == 0)
+ return 0;
+
+ if (file == NULL && sb == NULL)
+ return 0;
+
+ if (nfsd_directio_mode == DIO_NEVER)
+ return 0;
+
+ if (file != NULL && sb == NULL) {
+ inode = file->f_path.dentry->d_inode;
+ sb = inode->i_sb;
+ fsblkbits = inode->i_blkbits;
+ }
+
+ if (sb !=NULL) {
+ blkbits = sb->s_blocksize_bits;
+ fsblkbits = sb->s_blocksize_bits;
+ if (sb->s_bdev)+ blkbits = blksize_bits(bdev_logical_block_size(sb->s_bdev));
+ } else
+ blkbits = fsblkbits;
+
+ if (alignment >= fsblkbits && fsblkbits > 0 && nfsd_directio_mode !=DIO_BDEV_ALIGNED)
+ return 0;
+
+ if (alignment < blkbits)
+ return 0;
+
+ return 1;
+}
+
+
/*
* Open an existing file or directory.
* The access argument indicates the type of open (read/write/lock)
@@ -725,13 +975,15 @@
*/
__be32
nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
- int access, struct file **filp)
+ int access, struct file **filp,
+ const loff_t offset, const unsigned long size)
{
struct dentry *dentry;
struct inode *inode;
int flags = O_RDONLY|O_LARGEFILE;
__be32 err;
int host_err = 0;+ struct super_block* sb;
validate_process_creds();
@@ -774,6 +1026,11 @@
else
flags = O_WRONLY|O_LARGEFILE;
}+
+ sb = fhp->fh_export->ex_path.mnt->mnt_sb;
+ if (size && can_use_direct_io(NULL, sb, offset, size))
+ flags |= O_DIRECT;
+
*filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
flags, current_cred());
if (IS_ERR(*filp))
@@ -885,8 +1142,10 @@
return __splice_from_pipe(pipe, sd, nfsd_splice_actor);}
+
+
static __be32
-nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
+ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
loff_t offset, struct kvec *vec, int vlen, unsigned long
*count)
{
mm_segment_t oldfs;
@@ -899,21 +1158,29 @@
if (rqstp->rq_vers >= 3)
file->f_flags |= O_NONBLOCK;- if (file->f_op->splice_read && rqstp->rq_splice_ok) {
- struct splice_desc sd = {
- .len = 0, - .total_len = *count, - .pos = offset, - .u.data = rqstp,
- };
-
- rqstp->rq_resused = 1;
- host_err = splice_direct_to_actor(file, &sd,nfsd_direct_splice_actor);
- } else {
+ if (file->f_flags & O_DIRECT) {
+ // So far we do not support splice IO, so always do regular
oldfs = get_fs();
set_fs(KERNEL_DS);
- host_err = vfs_readv(file, (struct iovec __user *)vec, vlen,
&offset);
+ host_err = nfsd_vfs_read_direct(file, (struct iovec*)vec, vlen,
&offset);
set_fs(oldfs);
+ } else {
+ if (file->f_op->splice_read && rqstp->rq_splice_ok) {
+ struct splice_desc sd = {
+ .len = 0,
+ .total_len = *count,
+ .pos = offset,
+ .u.data = rqstp,+ };
+
+ rqstp->rq_resused = 1;
+ host_err = splice_direct_to_actor(file, &sd,nfsd_direct_splice_actor);
+ } else {
+ oldfs = get_fs();
+ set_fs(KERNEL_DS);
+ host_err = vfs_readv(file, (struct iovec __user *)vec, vlen,
&offset);
+ set_fs(oldfs);
+ }
}
if (host_err >= 0) {@@ -1024,7 +1291,11 @@
/* Write the data. */
oldfs = get_fs(); set_fs(KERNEL_DS);- host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
+ if (file->f_flags & O_DIRECT)
+ host_err = nfsd_vfs_write_direct(file, (struct iovec*)vec, vlen,
&offset);
+ else
+ host_err = vfs_writev(file, (struct iovec __user *)vec, vlen,
&offset);
+
set_fs(oldfs);
if (host_err < 0)
goto out_nfserr;
@@ -1064,8 +1335,9 @@
struct inode *inode;
struct raparms *ra;
__be32 err;+ unsigned long size = iov_length((struct iovec*)vec, vlen);
- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
+ err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file, offset,
size);
if (err)
return err;@@ -1133,7 +1405,8 @@
err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
stablep);
} else {
- err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
+ unsigned long size = iov_length((struct iovec*)vec, vlen);
+ err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file, offset,
size);
if (err)
goto out;@@ -1173,7 +1446,7 @@
}
err = nfsd_open(rqstp, fhp, S_IFREG,
- NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
+ NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file, 0, 0);
if (err)
goto out;
if (EX_ISSYNC(fhp->fh_export)) {
@@ -2018,7 +2291,7 @@
struct file *file;
loff_t offset = *offsetp;- err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
+ err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file, 0, 0);
if (err)
goto out;fs/nfsd/vfs.h 2012-03-28 renamed from fs/nfsd/vfs.h 2011-10-24 14:06:32.000000000 -0400 +0 -0
15:40:29.000000000 -0400
@@ -66,7 +66,7 @@
loff_t, unsigned long);
#endif /* CONFIG_NFSD_V3 */
__be32 nfsd_open(struct svc_rqst *, struct svc_fh *, int,
- int, struct file **);
+ int, struct file **, const loff_t, const unsigned long);
void nfsd_close(struct file *);
__be32 nfsd_read(struct svc_rqst *, struct svc_fh *,
loff_t, struct kvec *, int, unsigned long *);
A message that is not in the archive (sent off-list or not yet received): 1508773761.4854678.1335731939770.JavaMail.root-uUpdlAIx0AHkdGAVcyJ/[email protected]
Quoted 9 lines · Alexandre Depoutovitch
NFS daemons always perform buffered IO on files. As a result, write requests that are not aligned on a file system block boundary take about 15 times more time to complete compared to the same writes that are file system block aligned. This patch fixes the problem by analyzing alignment of the IO request that comes to NFS daemon and using Direct I/O mechanism when all of the following are true:
1. Request is not aligned on a file system block boundary
2. Request is aligned on an underlying block device's sector boundary.
3. Request size is a multiple of the sector size.
Why not add:
4. Request is a write
? There is no read-modify-write cycle for a read.
Quoted 28 lines
In all other cases, buffered IO is performed as has been done before.
A new flag is exposed to users through
/proc/fs/nfsd/direct_ionode. The default value of 1 results in the above behavior. Writing 0 to the node turns off the direct I/O completely, and forces NFS daemon to always use buffered IO (as it has done before). Writing 2 to the node tells NFS daemon to use direct I/O whenever possible, even if requests are aligned at file system block boundary.In order to test the patch, the following have been done: I've deployed 2 Linux machines with 3.0 kernel and my modifications. One acted as an NFS server, the other acted as an NFS client. NFS volume was mounted in sync mode.
Number of NFS daemons was increased to 64 in order to have higher chances of catching concurrency issues. Volume was formatted using ext4 file system. Volume was located on a hardware RAID10 array with 8 10K 450GB SAS drives. Raid adapter was HP P410i.1. During first set of experiments, the client machine created a 200 GB file by writing to it. Then it performed the following access patterns:
Read, random, (4K)
Write, random, (4K)
Read, sequential (4K)
Write, sequential (4K)
Read, sequential (4K, first access at 512 offset)
Write, sequential (4K, first access at 512 offset)
Read, sequential (32K)
Write, sequential (32K)
Quoted 8 lines
Read, sequential (32K, first access at 512 offset)
Write, sequential (32K, first access at 512 offset)
Read, sequential (256K)
Write, sequential (256K)
All accesses where done with keeping 64 outstanding IO requests on a client. I compared performance of the above patterns on vanilla Linux and Linux with my changes. All numbers (IOPS, latency) where the same for all cases except for random writes, where IOPS increase was 14 times.
So you only tested the case where the file already exists on the file system, is that right? It would be a good idea to also test workloads that create files. In much the same vein, it would be a good idea to run specsfs or other industry standard benchmark.
Quoted 18 lines
In addition, I have done several correctness tests.
2. Allocated three 200GB files using (a) explicit writes to a file, (b)
fallocate()system call, (c) seeking to the end of the file and writing one sector there.
Then, did random and sequential writes to files. After that, I verified that files were indeed modified and contained the latest data. Test for each file ran for 2 hours.3. Allocated 200GB file and started sequential reads to trigger read-ahead mechanism. Every 100 read operations, one file system unaligned write immediately after the current read position was requested in order to trigger a direct write. After that, read continued. All writes contained a predefined value, so that read can check for it. I have done this, in order to be sure that direct write correctly invalidates already in-memory cache.
Quoted 2 lines
Current implementation performs synchronous direct I/O and may trigger higher latencies when NFS volume is mounted in asynchronous mode. In
It may also cause higher latencies for synchronously mounted file systems. It's never really a good idea to mix buffered and direct I/O. In addition to exposing odd race conditions (which we think don't exist anymore), each time you decide to perform direct I/O, you're going to invalidate the page cache for the range of the file under I/O.
Quoted 4 lines
order to avoid it, as per Trond Myklebust's suggestion,
iov_iterinterface with asynchronous reads and writes can be used. This is why currently, Direct I/O can be enabled or disabled at boot or run-time without NFS server restart through the/proc/fs/nfsd/direct_ionode.
Not sure I understand that last part, but I really think you want to layer this work on top of Dave Kleikamp's patch set:
Subject: loop: Issue O_DIRECT aio using bio_vec
Also, instead of just telling us this is better, it would be good to provide the benchmarks you ran and the raw results.
I've commented on random sections of the code below (not at all a full review, just stuff that jumped out at me).
fs/direct-io.c 2012-04-25
Quoted 6 lines
diff -uNr
linux-orig/fs/direct-io.clinux-3.0.7-0.7.2.8796.vmw/fs/direct-io.cfs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0
-016:34:30.000000000 -0400
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0
-0@@ -152,11 +152,30 @@
Please use the '-p' switch to diff.
Quoted 12 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +4
-5int nr_pages; nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES); - ret = get_user_pages_fast( - dio->curr_user_address, /* Where from? */ - nr_pages, /* How many pages? */ - dio->rw == READ, /* Write to memory? */ - &dio->pages[0]); /* Put results here */ + + if (current->mm) { + ret = get_user_pages_fast( + dio->curr_user_address, /* Where from? */
fs/direct-io.c 2012-04-25 line 168
Quoted 10 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +9
-0+ nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */ + &dio->pages[0]); /* Put results here */ + } else { + /* For kernel threads mm is NULL, so all we need is to increment + page's reference count and add page to dio->pages array */ + int i; + struct page* page; + unsigned long start_pfn = virt_to_phys((void *)dio->curr_user_address)
Your mailer is line-wrapping (and maybe munging white space in other ways). Also, is this a patch against 3.0? Please update your sources next time.
fs/direct-io.c 2012-04-25 line 177
Quoted 9 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +7
-0+/* + Copies data between two iovec arrays. Individual array elements might have + different sizes, but total size of data described by the two arrays must + be the same +*/ +static int nfsd_copy_iovec(const struct iovec* svec, const unsigned int scount, + struct iovec* dvec, const unsigned int dcount, size_t size) {
Another data copy? Ouch.
Quoted 6 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +6
-0+static int can_use_direct_io(const struct file *file, + const struct super_block* sb, + const loff_t offset, const unsigned long size) { + unsigned int blkbits = 0; + struct inode *inode; + unsigned int fsblkbits = 0;
fs/direct-io.c 2012-04-25
Quoted 10 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +10
-0+ unsigned int alignment = io_alignment(offset, size); + + if (alignment == 0) + return 0; + + if (file == NULL && sb == NULL) + return 0; + + if (nfsd_directio_mode == DIO_NEVER) + return 0;
This check should be first, so we don't have to do alignment checks when this is disabled.
Cheers,
Jeff
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
----- Original Message -----
Quoted 23 lines
From: "Jeff Moyer" <[email protected]>
To: "Alexandre Depoutovitch" <[email protected]>
Cc: [email protected], [email protected]
Sent: Monday, April 30, 2012 2:22:32 PM
Subject: Re: [PATCH RFC v2] Performing direct I/O on sector-aligned requestsQuoted 6 lines · Alexandre Depoutovitch
NFS daemons always perform buffered IO on files. As a result, write requests that are not aligned on a file system block boundary take about
15 times more time to complete compared to the same writes that are file
system block aligned. This patch fixes the problem by analyzingQuoted 8 lines
alignment
of the IO request that comes to NFS daemon and using Direct I/O mechanism
when all of the following are true:
1. Request is not aligned on a file system block boundary
2. Request is aligned on an underlying block device's sector boundary.
3. Request size is a multiple of the sector size.
Quoted 5 lines
Why not add:
4. Request is a write
? There is no read-modify-write cycle for a read.
Yes, this makes sense. Thanks.
Quoted 39 lines
Quoted 30 lines
In order to test the patch, the following have been done: I've deployed 2
Linux machines with 3.0 kernel and my modifications. One acted as an NFS
server, the other acted as an NFS client. NFS volume was mounted in sync
mode.
Number of NFS daemons was increased to 64 in order to have higher chances
of catching concurrency issues. Volume was formatted using ext4 file
system. Volume was located on a hardware RAID10 array with 8 10K 450GB SAS
drives. Raid adapter was HP P410i.1. During first set of experiments, the client machine created a 200 GB
file by writing to it. Then it performed the following access patterns:
Read, random, (4K)
Write, random, (4K)
Read, sequential (4K)
Write, sequential (4K)
Read, sequential (4K, first access at 512 offset) Write, sequential (4K, first access at 512 offset) Read, sequential (32K)
Write, sequential (32K)
Read, sequential (32K, first access at 512 offset) Write, sequential (32K, first access at 512 offset) Read, sequential (256K)Quoted 8 lines
Write, sequential (256K)
All accesses where done with keeping 64 outstanding IO requests on a
client. I compared performance of the above patterns on vanilla Linux and
Linux with my changes. All numbers (IOPS, latency) where the same for all
cases except for random writes, where IOPS increase was 14 times.
Quoted 5 lines
So you only tested the case where the file already exists on the file system, is that right? It would be a good idea to also test workloads
that create files. In much the same vein, it would be a good idea to run specsfs or other industry standard benchmark.
I will do this.
Quoted 33 lines
Quoted 28 lines
In addition, I have done several correctness tests.
2. Allocated three 200GB files using (a) explicit writes to a file,
(b)fallocate()system call, (c) seeking to the end of the file and writing
one sector there.
Then, did random and sequential writes to files. After that, I verified
that files were indeed modified and contained the latest data. Test for
each file ran for 2 hours.3. Allocated 200GB file and started sequential reads to trigger read-ahead
mechanism. Every 100 read operations, one file system unaligned write
immediately after the current read position was requested in order to
trigger a direct write. After that, read continued. All writes contained a
predefined value, so that read can check for it. I have done this, in
order to be sure that direct write correctly invalidates already in-memory
cache.Quoted 4 lines
Current implementation performs synchronous direct I/O and may trigger
higher latencies when NFS volume is mounted in asynchronous mode. In
Quoted 7 lines
It may also cause higher latencies for synchronously mounted file systems. It's never really a good idea to mix buffered and direct I/O.
In addition to exposing odd race conditions (which we think don't exist
anymore), each time you decide to perform direct I/O, you're going to invalidate the page cache for the range of the file under I/O.
If there is an unknown bug in Direct I/O code, sure there will be a problem. But I specifically tested for many hours trying to trigger unknown race conditions (Test 2 and 3). As for cache invalidation, this may cause some performance degradation only when workload writes and reads to data cached in memory which has a low possibility for large randomly accessed data sets. As for small data sets, I suggest the direct I/O only as an optimization mode that must be explicitly turned on by the user
Quoted 11 lines
Quoted 7 lines
order to avoid it, as per Trond Myklebust's suggestion,
iov_iterinterface with asynchronous reads and writes can be used. This is why
currently, Direct I/O can be enabled or disabled at boot or run-time
without NFS server restart through the/proc/fs/nfsd/direct_ionode.Not sure I understand that last part, but I really think you want to layer this work on top of Dave Kleikamp's patch set:
Subject: loop: IssueO_DIRECTaio usingbio_vec
Unless I got it wrong, Dave Kleikamp's work allows asynchronous I/O from the kernel. In the proposed NFS direct I/O patch all I/O is done synchronously (as it is done now). What benefit will we get from Dave's patch?
Quoted 3 lines
Also, instead of just telling us this is better, it would be good to provide the benchmarks you ran and the raw results.
Buffered NFS Direct I/O for unaligned Difference
IOPS MB/s Latency(ms) IOPS MB/s Latency (ms)
Read, random (4K OIO=64) 1670 6.5 38 1700 6.8 37 4.62% Write, random (4K OIO=64) 150 0.6 500 2200 8.5 29 1300.00% Read, sequential (4K OIO=64) 22,000 87 2.8 22000 86 2.9 -1.15% Write, sequential (4K OIO=64) 10,000 40 6 11000 42 6 5.00% Read, sequential (32K OIO=1) 1900 59 0.5 2000 62 0.5 5.08% Write, sequential (32K OIO=1) 1100 35 0.9 1100 35 0.9 0.00% Read, sequential (32K OIO=64) 5000 156 13 5100 160 12 2.56% Write, sequential (32K OIO=64) 5700 180 11 5600 175 11 -2.78% Read, sequential (256K OIO=64) 560 140 110 550 140 120 0.00% Write, sequential (256K OIO=64)580 150 110 600 150 100 0.00%
Quoted 3 lines
I've commented on random sections of the code below (not at all a full
review, just stuff that jumped out at me).
Thanks!
Quoted 9 lines
fs/direct-io.c 2012-04-25
Quoted 7 lines
diff -uNr
linux-orig/fs/direct-io.clinux-3.0.7-0.7.2.8796.vmw/fs/direct-io.cfs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0
-016:34:30.000000000 -0400
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0
-0@@ -152,11 +152,30 @@Please use the '-p' switch to diff.
Will do.
Quoted 24 lines
Quoted 22 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +9
-5int nr_pages; nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES); - ret = get_user_pages_fast( - dio->curr_user_address, /* Where from? */ - nr_pages, /* How many pages? */ - dio->rw == READ, /* Write to memory? */ - &dio->pages[0]); /* Put results here */ + + if (current->mm) { + ret = get_user_pages_fast( + dio->curr_user_address, /* Where from? */ + nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */ + &dio->pages[0]); /* Put results here */ + } else { + /* For kernel threads mm is NULL, so all we need is toincrement
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +4
-0+ page's reference count and add page to dio->pages array */ + int i; + struct page* page; + unsigned long start_pfn = virt_to_phys((voidQuoted 1 line
*)dio->curr_user_address)
Quoted 4 lines
Your mailer is line-wrapping (and maybe munging white space in other ways). Also, is this a patch against 3.0? Please update your sources
next time.
I will.
Quoted 7 lines
Quoted 7 lines
+/*
+ Copies data between two iovec arrays. Individual array elements might have
+ different sizes, but total size of data described by the two arrays must
+ be the same
+*/
Quoted 8 lines
Quoted 6 lines
+static int nfsd_copy_iovec(const struct iovec* svec, const unsigned int
scount,
+ struct iovec* dvec, const unsigned int dcount,size_tsize)
{Another data copy? Ouch.
I could not find any easy way to avoid it. The copy is done only in the case when direct I/O is involved.
Quoted 13 lines
Quoted 13 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +12
-0+static int can_use_direct_io(const struct file *file, + const struct super_block* sb, + const loff_t offset, const unsigned long size) { + unsigned int blkbits = 0; + struct inode *inode; + unsigned int fsblkbits = 0; + unsigned int alignment = io_alignment(offset, size); + + if (alignment == 0) + return 0; + + if (file == NULL && sb == NULL)
Quoted 9 lines
fs/direct-io.c 2012-04-25
Quoted 4 lines
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +4
-0+ return 0; + + if (nfsd_directio_mode == DIO_NEVER) + return 0;This check should be first, so we don't have to do alignment checks when
this is disabled.
I will change this.
----- Original Message -----
Quoted 9 lines
From: "J. Bruce Fields" <[email protected]>
To: "Alexandre Depoutovitch" <[email protected]>
Sent: Friday, April 27, 2012 4:51:20 PM
Subject: Re: About Direct I/OQuoted 1 line · Alexandre Depoutovitch
Quoted 6 lines
Quoted 2 lines
The tests have been done on a hardware RAID10 array with 8 10K 450GB SAS drives. Raid adapter was HP P410i.
It might be worth also testing with a single drive if you want to see the worst case for synchronous writes. (That adapater may have a battery-backed cache that lets it respond to writes immediately?)
Yes, the adapter has battery backed cache (1GB), and you are right, it is the main reason for significant improvement when doing direct I/O. Sync random writes happen order of magnitude faster than reads. I also tested Direct I/O on a cheap Western Digital 7.2K SATA drive (WD10EALX) on an Intel 82801 SATA controller. There was no performance gain with direct I/O because write speed was in fact 1.5 slower than read speed. However, there was no performance degradation either, whether direct of buffered I/O was used (in sync mode).
So looks like that Direct I/O for NFS is beneficial for random, f/s unaligned, synchronous writes on adapters with NVRAM. In other cases it can be turned on/off either automatically, based on alignment and O_SYNC flag, or manually, based on hardware characteristics.
Alex
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 6 lines · Alexandre Depoutovitch
A new flag is exposed to users through
/proc/fs/nfsd/direct_ionode. The default value of 1 results in the above behavior. Writing 0 to the node turns off the direct I/O completely, and forces NFS daemon to always use buffered IO (as it has done before). Writing 2 to the node tells NFS daemon to use direct I/O whenever possible, even if requests are aligned at file system block boundary.
Not sure if this was previously discussed, but I assume the default would remain the same (value 0)?
Dean
Quoted 720 lines
In order to test the patch, the following have been done: I've deployed 2 Linux machines with 3.0 kernel and my modifications. One acted as an NFS server, the other acted as an NFS client. NFS volume was mounted in sync mode.
Number of NFS daemons was increased to 64 in order to have higher chances of catching concurrency issues. Volume was formatted using ext4 file system. Volume was located on a hardware RAID10 array with 8 10K 450GB SAS drives. Raid adapter was HP P410i.1. During first set of experiments, the client machine created a 200 GB file by writing to it. Then it performed the following access patterns:
Read, random, (4K)
Write, random, (4K)
Read, sequential (4K)
Write, sequential (4K)
Read, sequential (4K, first access at 512 offset)
Write, sequential (4K, first access at 512 offset)
Read, sequential (32K)
Write, sequential (32K)
Read, sequential (32K, first access at 512 offset)
Write, sequential (32K, first access at 512 offset)
Read, sequential (256K)
Write, sequential (256K)
All accesses where done with keeping 64 outstanding IO requests on a client. I compared performance of the above patterns on vanilla Linux and Linux with my changes. All numbers (IOPS, latency) where the same for all cases except for random writes, where IOPS increase was 14 times.In addition, I have done several correctness tests.
2. Allocated three 200GB files using (a) explicit writes to a file, (b)
fallocate()system call, (c) seeking to the end of the file and writing one sector there.
Then, did random and sequential writes to files. After that, I verified that files were indeed modified and contained the latest data. Test for each file ran for 2 hours.3. Allocated 200GB file and started sequential reads to trigger read-ahead mechanism. Every 100 read operations, one file system unaligned write immediately after the current read position was requested in order to trigger a direct write. After that, read continued. All writes contained a predefined value, so that read can check for it. I have done this, in order to be sure that direct write correctly invalidates already in-memory cache.
Current implementation performs synchronous direct I/O and may trigger higher latencies when NFS volume is mounted in asynchronous mode. In order to avoid it, as per Trond Myklebust's suggestion,
iov_iterinterface with asynchronous reads and writes can be used. This is why currently, Direct I/O can
be enabled or disabled at boot or run-time without NFS server restart through the/proc/fs/nfsd/direct_ionode.--------------------------------------------------------------------------
diff -uNr
linux-orig/fs/direct-io.clinux-3.0.7-0.7.2.8796.vmw/fs/direct-io.cfs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +0
-016:34:30.000000000 -0400
fs/direct-io.c 2012-04-25 renamed from fs/direct-io.c 2011-10-24 14:06:32.000000000 -0400 +31
-6@@ -152,11 +152,30 @@ int nr_pages; nr_pages = min(dio->total_pages - dio->curr_page, DIO_PAGES); - ret = get_user_pages_fast( - dio->curr_user_address, /* Where from? */ - nr_pages, /* How many pages? */ - dio->rw == READ, /* Write to memory? */ -&dio->pages[0]); /* Put results here */ + + if (current->mm) { + ret = get_user_pages_fast( + dio->curr_user_address, /* Where from? */ + nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */ +&dio->pages[0]); /* Put results here */ + } else { + /* For kernel threads mm is NULL, so all we need is to increment + page's reference count and add page to dio->pages array */ + int i; + struct page* page; + unsigned long start_pfn = virt_to_phys((void *)dio->curr_user_address) +>> PAGE_SHIFT; + /* For kernel threads buffer must be in kernel memory */ + BUG_ON(dio->curr_user_address< TASK_SIZE_MAX); + + for (i = 0; i< nr_pages; i++) { + page = pfn_to_page(start_pfn + i); + page_cache_get(page); + dio->pages[i] = page; + } + /* No need to lock pages: this is kernel thread and the pages are in + kernel as well */ + ret = nr_pages; + } if (ret< 0&& dio->blocks_available&& (dio->rw& WRITE)) { struct page *page = ZERO_PAGE(0);@@ -972,7 +991,11 @@ break; } - /* Drop the ref which was taken in get_user_pages() */ + /* + * Drop the ref which was taken in dio_refill_pages + * directly (for direct I/O) or by calling get_user_pages + * (for buffered IO) + */ page_cache_release(page); block_in_page = 0; }diff -uNr
linux-orig/fs/nfsd/lockd.clinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/lockd.cfs/nfsd/lockd.c 2012-03-28 renamed from fs/nfsd/lockd.c 2011-10-24 14:06:32.000000000 -0400 +0
-015:40:29.000000000 -0400
fs/nfsd/lockd.c 2012-03-28 renamed from fs/nfsd/lockd.c 2011-10-24 14:06:32.000000000 -0400 +1
-1@@ -36,7 +36,7 @@ fh.fh_export = NULL; exp_readlock(); - nfserr = nfsd_open(rqstp,&fh, S_IFREG, NFSD_MAY_LOCK, filp); + nfserr = nfsd_open(rqstp,&fh, S_IFREG, NFSD_MAY_LOCK, filp, 0, 0); fh_put(&fh); exp_readunlock(); /* We return nlm error codes as nlm doesn't knowdiff -uNr
linux-orig/fs/nfsd/nfs4state.clinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfs4state.cfs/nfsd/nfs4state.c 2012-03-28 renamed from fs/nfsd/nfs4state.c 2011-10-24 14:06:32.000000000 -0400 +0
-015:40:29.000000000 -0400
fs/nfsd/nfs4state.c 2012-03-28 renamed from fs/nfsd/nfs4state.c 2011-10-24 14:06:32.000000000 -0400 +2
-2@@ -2557,7 +2557,7 @@ if (!fp->fi_fds[oflag]) { status = nfsd_open(rqstp, cur_fh, S_IFREG, access, -&fp->fi_fds[oflag]); +&fp->fi_fds[oflag], 0, 0); if (status) return status; }@@ -3951,7 +3951,7 @@ struct file *file; int err; - err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ,&file); + err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ,&file, 0, 0); if (err) return err; err = vfs_test_lock(file, lock);diff -uNr
linux-orig/fs/nfsd/nfsctl.clinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsctl.cfs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +0
-015:40:29.000000000 -0400
fs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +27
-0@@ -46,6 +46,7 @@ NFSD_TempPorts, NFSD_MaxBlkSize, NFSD_SupportedEnctypes, + NFSD_DirectIO, /* * The below MUST come last. Otherwise we leave a hole in nfsd_files[] * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops@@ -78,6 +79,7 @@ static ssize_t write_ports(struct file *file, char *buf, size_t size); static ssize_t write_temp_ports(struct file *file, char *buf, size_t size); static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); +static ssize_t write_directio(struct file *file, char *buf, size_t size); #ifdef CONFIG_NFSD_V4 static ssize_t write_leasetime(struct file *file, char *buf, size_t size); static ssize_t write_gracetime(struct file *file, char *buf, size_t size);@@ -103,6 +105,7 @@ [NFSD_Ports] = write_ports, [NFSD_TempPorts] = write_temp_ports, [NFSD_MaxBlkSize] = write_maxblksize, + [NFSD_DirectIO] = write_directio, #ifdef CONFIG_NFSD_V4 [NFSD_Leasetime] = write_leasetime, [NFSD_Gracetime] = write_gracetime,@@ -1348,6 +1351,58 @@ nfsd_max_blksize); } +int nfsd_directio_mode = DIO_NEVER; + +/** + * nfsd_directio_mode - sets conditions when direct IO is activated + * + * Input: + * buf: ignored + * size: zero + * + * OR + * + * Input: + * buf: C string containing an unsigned + * integer value representing the new + * NFS direct IO mode + * size: non-zero length of C string in @buf + * Output: + * On success: passed-in buffer filled with '\n'-terminated C string + * containing numeric value of the current direct IO mode + * return code is the size in bytes of the string + * + * Possible modes are: + * DIO_NEVER (0) - never use direct I/O + * DIO_FS_UNALIGNED (1) - use direct I/O only for requests that FSunaligned
+ * and block device aligned
+ *DIO_SECTOR_ALIGNED(3) - use direct I/O for all block device aligned
IO
+ * On error: return code is zero or a negative errno value
+ */fs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +14
-0+static ssize_t write_directio(struct file *file, char *buf, size_t size) +{ + char *mesg = buf; + if (size> 0) { + int mode; + int rv = get_int(&mesg,&mode); + if (rv) + return rv; + if (mode< DIO_NEVER || mode> DIO_BDEV_ALIGNED) + return -EINVAL; + /* + There is no need for synchronization here. No harm will happen + even if mode was changed between opening a file and choosing whether + to choose direct or buffered path. When we choosing a path we makesure
fs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +8
-0+ that the file has been opened in the compatible mode + */ + nfsd_directio_mode = mode; + printk(KERN_WARNING"NFSD direct I/O mode changed to %d.", + nfsd_directio_mode); + } + + return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
nfsd_directio_mode);fs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +2
-0+} + #ifdef CONFIG_NFSD_V4 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_tsize,
time_t*time)
{fs/nfsd/nfsctl.c 2012-03-28 renamed from fs/nfsd/nfsctl.c 2011-10-24 14:06:32.000000000 -0400 +1
-0@@ -1525,6 +1580,7 @@ [NFSD_Ports] = {"portlist",&transaction_ops, S_IWUSR|S_IRUGO}, [NFSD_TempPorts] = {"tempportlist",&transaction_ops, S_IWUSR|S_IRUGO}, [NFSD_MaxBlkSize] = {"max_block_size",&transaction_ops, S_IWUSR|S_IRUGO}, + [NFSD_DirectIO] = {"direct_io",&transaction_ops, S_IWUSR|S_IRUGO}, #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)[
NFSD_SupportedEnctypes] = {"supported_krb5_enctypes",&supported_enctypes_ops,S_IRUGO},
#endif /*CONFIG_SUNRPC_GSSorCONFIG_SUNRPC_GSS_MODULE*/
diff -uNrlinux-orig/fs/nfsd/nfsd.hlinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/nfsd.hfs/nfsd/nfsd.h 2012-04-17 renamed from fs/nfsd/nfsd.h 2011-10-24 14:06:32.000000000 -0400 +0
-011:45:55.000000000 -0400
fs/nfsd/nfsd.h 2012-04-17 renamed from fs/nfsd/nfsd.h 2011-10-24 14:06:32.000000000 -0400 +4
-0@@ -68,6 +68,14 @@ extern int nfsd_max_blksize; +enum { + DIO_NEVER = 0,// Never use Direct I/O. The first value + DIO_FS_UNALIGNED = 1, // Use Direct I/O when request is FS unaligned + DIO_BDEV_ALIGNED =2, // Always use Direct I/O when possible. The lastvalue
fs/nfsd/nfsd.h 2012-04-17 renamed from fs/nfsd/nfsd.h 2011-10-24 14:06:32.000000000 -0400 +4
-0+}; + +extern int nfsd_directio_mode; + static inline int nfsd_v4client(struct svc_rqst *rq) { return rq->rq_prog == NFS_PROGRAM&& rq->rq_vers == 4;diff -uNr
linux-orig/fs/nfsd/vfs.clinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/vfs.cfs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +0
-014:21:38.000000000 -0400
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +43
-0@@ -28,6 +28,7 @@ #include<asm/uaccess.h> #include<linux/exportfs.h> #include<linux/writeback.h> +#include<linux/blkdev.h> #ifdef CONFIG_NFSD_V3 #include "xdr3.h"@@ -718,6 +719,255 @@ return break_lease(inode, mode | O_NONBLOCK); } +/* + Copies data between two iovec arrays. Individual array elements might have + different sizes, but total size of data described by the two arrays must + be the same +*/ +static int nfsd_copy_iovec(const struct iovec* svec, const unsigned int scount, + struct iovec* dvec, const unsigned int dcount, size_t size) { + size_t cur_size, soff, doff, tocopy, srem , drem ; + unsigned int di, si; + + cur_size = iov_length(svec, scount); + if (cur_size != iov_length(dvec, dcount)) + return -EINVAL; + + srem = drem = 0; + di = si = 0; + soff = doff = 0; + while (cur_size> 0) { + if (si>= scount || di>= dcount) + return -EFAULT; + + srem = svec[si].iov_len - soff; + drem = dvec[di].iov_len - doff; + tocopy = (srem> drem) ? drem : srem; + memcpy((char*)(dvec[di].iov_base) + doff, (char*)(svec[si].iov_base) + soff, tocopy); + cur_size -= tocopy; + srem -= tocopy; + drem -= tocopy; + doff += tocopy; + soff += tocopy; + if (srem == 0) { + si++; + soff = 0; + } + if (drem == 0) { + di++; + doff = 0; + } + } + if (si != scount || di != dcount || srem !=0 || drem != 0) + { + printk(KERN_WARNING"In copy_iovec: si=%lu, scount=%lu, di=%lu,dcount=%lu, srem=%lu, drem=%lu",
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +2
-0+ (unsigned long)si, (unsigned long)scount, (unsigned long)di, + (unsigned long)dcount, (unsigned long)srem, (unsignedlong)drem);
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +8
-0+ return -EFAULT; + } + + return 0; +} + +/* + Allocates iovec array where each element has memory page-aligned baseaddress
+ and size of a page. Needed for DIRECT I/O to be possbile from this array
+ */fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +48
-0+static int nfsd_allocate_paged_iovec(size_t size, unsigned int* pcount, + struct iovec** pvec) { + unsigned int i; + unsigned int page_num = size / PAGE_SIZE; + struct iovec * vec = NULL; + + *pvec = NULL; + *pcount = 0; + if (page_num * PAGE_SIZE != size) + page_num++; + + vec = kmalloc(sizeof(struct iovec) * page_num, GFP_KERNEL); + if (!vec) + return -ENOMEM; + memset(vec, 0, sizeof(struct iovec) * page_num); + *pvec = vec; + *pcount = page_num; + + for (i = 0; i< page_num; i++) { + vec[i].iov_base = (void*)__get_free_page(GFP_KERNEL); + if (!vec[i].iov_base) + return -ENOMEM; + vec[i].iov_len = PAGE_SIZE; + } + + if (size % PAGE_SIZE) + vec[page_num - 1].iov_len = size % PAGE_SIZE; + + return 0; +} + +/* + Deallocates iovec array, allocated by nfsd_allocate_paged_iovec +*/ +static void nfsd_free_paged_iovec(unsigned int count, struct iovec* vec) { + unsigned int i; + if (vec) { + for (i = 0; i< count; i++) + if (vec[i].iov_base) + free_page((unsigned long)(vec[i].iov_base)); + kfree(vec); + } +} + +/* + Performs direct I/O for a given NFS write request +*/ +static ssize_t nfsd_vfs_write_direct(struct file *file, const struct iovec*vec,
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +22
-0+ unsigned long vlen, loff_t *pos) { + ssize_t result = -EINVAL; + unsigned int page_num; + struct iovec *aligned_vec = NULL; + + // Check size to be multiple of sectors + size_t size = iov_length(vec, vlen); + + if (size == 0) + return vfs_writev(file, (struct iovec __user *)vec, vlen, pos); + + // Allocate necesary number of pages + result = nfsd_allocate_paged_iovec(size,&page_num,&aligned_vec); + if (result) { + printk(KERN_WARNING"Cannot allocate aligned_vec."); + goto out; + } + + // Copy data + result = nfsd_copy_iovec(vec, vlen, aligned_vec, page_num, size); + if(result) { + printk(KERN_WARNING"Wrong amount of data copied to alignedbuffer.");
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +5
-0+ goto out; + } + + // Call further + result = vfs_writev(file, (struct iovec __user *)aligned_vec, page_num,pos);
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +32
-0+ +out: + nfsd_free_paged_iovec(page_num, aligned_vec); + return result; +} + + +/* + Performs direct I/O for a given NFS read request +*/ +static ssize_t nfsd_vfs_read_direct(struct file *file, struct iovec *vec, + unsigned long vlen, loff_t *pos) { + unsigned int page_num; + struct iovec *aligned_vec = NULL; + ssize_t result = -EINVAL; + size_t size; + + // Check size to be multiple of sectors + size = iov_length(vec, vlen); + + if (size == 0) + return vfs_readv(file, (struct iovec __user *)vec, vlen, pos); + + // Allocate necesary number of pages + result = nfsd_allocate_paged_iovec(size,&page_num,&aligned_vec); + if (result) { + printk(KERN_WARNING"Cannot allocate aligned_vec."); + goto out; + } + + // Call further + result = vfs_readv(file, (struct iovec __user *)aligned_vec, page_num,pos);
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +8
-0+ if (result< 0) { + printk(KERN_WARNING"Error during read operation."); + goto out; + } + + // Copy data + if(nfsd_copy_iovec(aligned_vec, page_num, vec, vlen, size)) { + printk(KERN_WARNING"Wrong amount of data copied from alignedbuffer.");
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +10
-0+ goto out; + } + +out: + nfsd_free_paged_iovec(page_num, aligned_vec); + + return result; +} + +// Returns number of terminal zero bits for a given number (numberalignment)
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +61
-0+static unsigned int get_alignment(loff_t n) { + unsigned int i=0; + if (n == 0) + return (unsigned int)-1; // 0 is alligned to any number + while ((n& 1) == 0&& n> 0) { + n = n>> 1; + i++; + } + return i; +} + +// Returns the alignment of I/O request +static unsigned int io_alignment(const loff_t offset, + const unsigned long size) { + unsigned int i1, i2; + + i1 = get_alignment(offset); + i2 = get_alignment(size); + + return i1> i2 ? i2 : i1; +} + + +/* + Based on the I/O request and file system parameters determines if + direct I/O can be used to perform the given request + Either file or sb are needed to retrieve file system and device + paramters +*/ +static int can_use_direct_io(const struct file *file, + const struct super_block* sb, + const loff_t offset, const unsigned long size) { + unsigned int blkbits = 0; + struct inode *inode; + unsigned int fsblkbits = 0; + unsigned int alignment = io_alignment(offset, size); + + if (alignment == 0) + return 0; + + if (file == NULL&& sb == NULL) + return 0; + + if (nfsd_directio_mode == DIO_NEVER) + return 0; + + if (file != NULL&& sb == NULL) { + inode = file->f_path.dentry->d_inode; + sb = inode->i_sb; + fsblkbits = inode->i_blkbits; + } + + if (sb !=NULL) { + blkbits = sb->s_blocksize_bits; + fsblkbits = sb->s_blocksize_bits; + if (sb->s_bdev) + blkbits = blksize_bits(bdev_logical_block_size(sb->s_bdev)); + } else + blkbits = fsblkbits; + + if (alignment>= fsblkbits&& fsblkbits> 0&& nfsd_directio_mode !=
DIO_BDEV_ALIGNED)fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +20
-2+ return 0; + + if (alignment< blkbits) + return 0; + + return 1; +} + + /* * Open an existing file or directory. * The access argument indicates the type of open (read/write/lock)@@ -725,13 +975,15 @@ */ __be32 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, - int access, struct file **filp) + int access, struct file **filp, + const loff_t offset, const unsigned long size) { struct dentry *dentry; struct inode *inode; int flags = O_RDONLY|O_LARGEFILE; __be32 err; int host_err = 0; + struct super_block* sb; validate_process_creds();@@ -774,6 +1026,11 @@ else flags = O_WRONLY|O_LARGEFILE; } + + sb = fhp->fh_export->ex_path.mnt->mnt_sb; + if (size&& can_use_direct_io(NULL, sb, offset, size)) + flags |= O_DIRECT; + *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt), flags, current_cred()); if (IS_ERR(*filp))@@ -885,8 +1142,10 @@ return __splice_from_pipe(pipe, sd, nfsd_splice_actor); } + + static __be32 -nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, + nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, loff_t offset, struct kvec *vec, int vlen, unsigned long*count)
{ mm_segment_t oldfs;fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +14
-12@@ -899,21 +1158,29 @@ if (rqstp->rq_vers>= 3) file->f_flags |= O_NONBLOCK; - if (file->f_op->splice_read&& rqstp->rq_splice_ok) { - struct splice_desc sd = { - .len = 0, - .total_len = *count, - .pos = offset, - .u.data = rqstp, - }; - - rqstp->rq_resused = 1; - host_err = splice_direct_to_actor(file,&sd, nfsd_direct_splice_actor); - } else { + if (file->f_flags& O_DIRECT) { + // So far we do not support splice IO, so always do regular oldfs = get_fs(); set_fs(KERNEL_DS); - host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset); + host_err = nfsd_vfs_read_direct(file, (struct iovec*)vec, vlen, &offset); set_fs(oldfs); + } else { + if (file->f_op->splice_read&& rqstp->rq_splice_ok) { + struct splice_desc sd = { + .len = 0, + .total_len = *count, + .pos = offset, + .u.data = rqstp, + }; + + rqstp->rq_resused = 1; + host_err = splice_direct_to_actor(file,&sd,
nfsd_direct_splice_actor);fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +4
-0+ } else { + oldfs = get_fs(); + set_fs(KERNEL_DS); + host_err = vfs_readv(file, (struct iovec __user *)vec, vlen,&offset);
fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +7
-1+ set_fs(oldfs); + } } if (host_err>= 0) {@@ -1024,7 +1291,11 @@ /* Write the data. */ oldfs = get_fs(); set_fs(KERNEL_DS); - host_err = vfs_writev(file, (struct iovec __user *)vec, vlen,&offset); + if (file->f_flags& O_DIRECT) + host_err = nfsd_vfs_write_direct(file, (struct iovec*)vec, vlen, &offset); + else + host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset); + set_fs(oldfs);if (host_err< 0) goto out_nfserr;fs/nfsd/vfs.c 2012-04-25 renamed from fs/nfsd/vfs.c 2011-10-24 14:06:32.000000000 -0400 +6
-4@@ -1064,8 +1335,9 @@ struct inode *inode; struct raparms *ra; __be32 err; + unsigned long size = iov_length((struct iovec*)vec, vlen); - err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ,&file); + err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ,&file, offset, size); if (err) return err;@@ -1133,7 +1405,8 @@ err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, stablep); } else { - err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE,&file); + unsigned long size = iov_length((struct iovec*)vec, vlen); + err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE,&file, offset, size); if (err) goto out;@@ -1173,7 +1446,7 @@ } err = nfsd_open(rqstp, fhp, S_IFREG, - NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE,&file); + NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE,&file, 0, 0); if (err) goto out; if (EX_ISSYNC(fhp->fh_export)) {@@ -2018,7 +2291,7 @@ struct file *file; loff_t offset = *offsetp; - err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ,&file); + err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ,&file, 0, 0); if (err) goto out;diff -uNr
linux-orig/fs/nfsd/vfs.hlinux-3.0.7-0.7.2.8796.vmw/fs/nfsd/vfs.hfs/nfsd/vfs.h 2012-03-28 renamed from fs/nfsd/vfs.h 2011-10-24 14:06:32.000000000 -0400 +0
-015:40:29.000000000 -0400
fs/nfsd/vfs.h 2012-03-28 renamed from fs/nfsd/vfs.h 2011-10-24 14:06:32.000000000 -0400 +1
-2@@ -66,7 +66,7 @@ loff_t, unsigned long); #endif /* CONFIG_NFSD_V3 */ __be32 nfsd_open(struct svc_rqst *, struct svc_fh *, int, - int, struct file **); + int, struct file **, const loff_t, const unsigned long); void nfsd_close(struct file *); __be32 nfsd_read(struct svc_rqst *, struct svc_fh *, loff_t, struct kvec *, int, unsigned long *); --
Quoted 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): [email protected]
----- Original Message -----
Quoted 21 lines
From: "Dean" <[email protected]>
To: "Alexandre Depoutovitch" <[email protected]>
Cc: [email protected], [email protected]
Sent: Friday, May 11, 2012 2:36:52 PM
Subject: Re: [PATCH RFC v2] Performing direct I/O on sector-aligned requestsQuoted 11 lines · Alexandre Depoutovitch
A new flag is exposed to users through
/proc/fs/nfsd/direct_ionode. The
default value of 1 results in the above behavior. Writing 0 to the node
turns off the direct I/O completely, and forces NFS daemon to always use
buffered IO (as it has done before). Writing 2 to the node tells NFS
daemon to use direct I/O whenever possible, even if requests are aligned
at file system block boundary.
Quoted 6 lines
Not sure if this was previously discussed, but I assume the default would remain the same (value 0)?
Dean
Yes. The default should be 0 (the behavior will not change, unless the user explicitly enables it). --
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 13 lines · Alexandre Depoutovitch
(quoted diff) +12
-0+ if (current->mm) { + ret = get_user_pages_fast( + dio->curr_user_address, /* Where from? */ + nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */ + &dio->pages[0]); /* Put results here */ + } else { + /* For kernel threads mm is NULL, so all we need is to increment + page's reference count and add page to dio->pages array */ + int i; + struct page* page; + unsigned long start_pfn = virt_to_phys((void*)dio->curr_user_address)
Review
Quoted 3 lines
(quoted diff) +3
-0+ >> PAGE_SHIFT; + /* For kernel threads buffer must be in kernel memory */ + BUG_ON(dio->curr_user_address < TASK_SIZE_MAX);
This is an assumption that isn't true for all architectures. Better just delete this line.
Review
Quoted 2 lines
(quoted diff) +2
-0+ for (i = 0; i < nr_pages; i++) { + page = pfn_to_page(start_pfn + i);
Why are you messing about with pfns? Why not just stay with virtual addresses and call virt_to_page() in this loop? That would ensure that this works to vmapped pages as well as physically contiguous pages.
Quoted 11 lines
(quoted diff) +7
-0+ page_cache_get(page); + dio->pages[i] = page; + } + /* No need to lock pages: this is kernel thread and the pages are in + kernel as well */ + ret = nr_pages; + } if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) { struct page *page = ZERO_PAGE(0);
Review line 998
Quoted 9 lines
(quoted diff) +5
-1@@ -972,7 +991,11 @@ break; } - /* Drop the ref which was taken in get_user_pages() */ + /* + * Drop the ref which was taken in dio_refill_pages + * directly (for direct I/O) or by calling get_user_pages + * (for buffered IO) + */
I think your change to this comment actually makes it more confusing.
Quoted 28 lines
(quoted diff) +24
-0@@ -1348,6 +1351,58 @@ nfsd_max_blksize); } +int nfsd_directio_mode = DIO_NEVER; + +/** + * nfsd_directio_mode - sets conditions when direct IO is activated + * + * Input: + * buf: ignored + * size: zero + * + * OR + * + * Input: + * buf: C string containing an unsigned + * integer value representing the new + * NFS direct IO mode + * size: non-zero length of C string in @buf + * Output: + * On success: passed-in buffer filled with '\n'-terminated C string + * containing numeric value of the current direct IO mode + * return code is the size in bytes of the string + * + * Possible modes are: + * DIO_NEVER (0) - never use direct I/O + * DIO_FS_UNALIGNED (1) - use direct I/O only for requests that FS
Quoted 6 lines
unaligned
+ * and block device aligned
+ *DIO_SECTOR_ALIGNED(3) - use direct I/O for all block device aligned
IO
+ * On error: return code is zero or a negative errno value
+ */
This is not correct kerneldoc formatting.
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 26 lines
From: "Matthew Wilcox" <[email protected]>
To: "Alexandre Depoutovitch" <[email protected]>
Cc: [email protected], [email protected]
Sent: Monday, April 30, 2012 3:56:46 PM
Subject: Re: [PATCH RFC v2] Performing direct I/O on sector-aligned requestsQuoted 14 lines · Alexandre Depoutovitch
(quoted diff) +8
-0+ if (current->mm) { + ret = get_user_pages_fast( + dio->curr_user_address, /* Where from? */ + nr_pages, /* How many pages? */ + dio->rw == READ, /* Write to memory? */ + &dio->pages[0]); /* Put results here */ + } else { + /* For kernel threads mm is NULL, so all we need is toincrement
(quoted diff) +4
-0+ page's reference count and add page to dio->pages array */ + int i; + struct page* page; + unsigned long start_pfn = virt_to_phys((void*)dio->curr_user_address)
Review
Quoted 3 lines
(quoted diff) +3
-0+ >> PAGE_SHIFT; + /* For kernel threads buffer must be in kernel memory */ + BUG_ON(dio->curr_user_address < TASK_SIZE_MAX);
Quoted 3 lines
This is an assumption that isn't true for all architectures. Better just
delete this line.
Thank you, I will do this.
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ for (i = 0; i < nr_pages; i++) { + page = pfn_to_page(start_pfn + i);Why are you messing about with pfns? Why not just stay with virtual addresses and call
virt_to_page()in this loop? That would ensure that
this works to vmapped pages as well as physically contiguous pages.
I have already changed this. Thanks.
Quoted 15 lines
Quoted 15 lines
(quoted diff) +4
-0+ page_cache_get(page); + dio->pages[i] = page; + } + /* No need to lock pages: this is kernel thread and thepages are in
(quoted diff) +3
-0+ kernel as well */ + ret = nr_pages; + } if (ret < 0 && dio->blocks_available && (dio->rw & WRITE)) { struct page *page = ZERO_PAGE(0);@@ -972,7 +991,11 @@ break; }
Quoted 8 lines
Review line 998
Quoted 6 lines
(quoted diff) +5
-1- /* Drop the ref which was taken in get_user_pages() */ + /* + * Drop the ref which was taken in dio_refill_pages + * directly (for direct I/O) or by calling get_user_pages + * (for buffered IO) + */I think your change to this comment actually makes it more confusing.
I will remove it if is confusing.
Quoted 32 lines
Quoted 32 lines
(quoted diff) +4
-0@@ -1348,6 +1351,58 @@ nfsd_max_blksize); } +int nfsd_directio_mode = DIO_NEVER; + +/** + * nfsd_directio_mode - sets conditions when direct IO isactivated
+ *
+ * Input:+ * buf: ignored + * size: zero+ *
+ * OR
+ *
+ * Input:
+ * buf: C string containing an unsigned+ * integer value representing the new + * NFS direct IO mode + * size: non-zero length of C string in @buf+ * Output:
+ * On success: passed-in buffer filled with '\n'-terminated C string
+ * containing numeric value of the current direct IO mode
+ * return code is the size in bytes of the string
+ *
+ * Possible modes are:
+ *DIO_NEVER(0) - never use direct I/O
+ *DIO_FS_UNALIGNED(1) - use direct I/O only for requests
that FS
unaligned
Quoted 8 lines
Quoted 6 lines
+ * and block device aligned
+ *DIO_SECTOR_ALIGNED(3) - use direct I/O for all block
device aligned
IO
+ * On error: return code is zero or a negative errno value
+ */This is not correct kerneldoc formatting.
I copy/pasted it from the previous entry in the same file :(
Quoted 7 lines
--
Matthew Wilcox Intel Open Source Technology Centre
"Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such
a retrograde step."