Fix O_DIRECT races in 2.4
Subsystems (3)
PATCH 1/1 Fix O_DIRECT races in 2.4
Hi,
I've found a few races in O_DIRECT in 2.4. There are multiple places where races can occur, mostly affecting sparse files or truncate:
O_DIRECT reads against buffered writes:
Read with O_DIRECT to a sparse area, then submit a buffered write to the same area. The file flush that the O_DIRECT read does initially can happen before the write, so you end up with newly written data in the area which has not yet been flushed to disk by the time the direct read is serviced. Stale data from the disk can be returned.
O_DIRECT writes against buffered reads:
Similar to the above, submit an O_DIRECT write into a sparse region of a file then read from that region while the write is still in progress. The write doesn't lock pages in the page cache so there's no synchronisation against the read: stale data can be returned.
O_DIRECT IOs against truncate:
Submit direct IO against a file then truncate it while the IO is in progress. Writes are OK because direct writes currently hold i_sem, but reads don't --- the data blocks can be deallocated, reallocated to somebody else, and we potentially get to read that other data. The i_sem on writes is actually a problem --- it prevents multiple threads from submitting parallel direct IOs at once, as the semaphore effectively serialises these IOs synchronously.
The patch below fixes this by:
* Prevent direct IO into sparse regions of a file.
For reads, zeros are filled in anyway; for writes, fall back to buffered IO followed by fdatasync().
* Lock against truncate.
Add a new, rwsem (i_alloc_sem) to guard against deallocation of data blocks while a direct IO is in progress: held in shared mode for the duration of all direct IOs, taken exclusively for truncate.
* Guard all direct IO getblk()s with i_sem
The direct IO read path takes an extra i_sem, which means we can't look up data blocks which are still in the process of being filled in by a buffered write.
But also
* Drop i_sem for the actual direct IO, once we've done the getblk() lookups.
Allows multiple direct IOs to be in progress against a file at once.
Holding i_sem just for the getblk() ensures that direct writes beyond EOF are still consistent even with O_APPEND, while still allowing parallelism in the IOs once we've mapped the file blocks. The bulk of the race prevention is in preventing IO to sparse regions and dealing with the truncate locking.
--Stephen
fs/buffer.c renamed from fs/buffer.c.=K0000=.orig +41 -18
@@ -436,26 +436,18 @@ out:return ret; } -asmlinkage long sys_fdatasync (unsigned int fd) +int do_fdatasync (struct file *file) { -struct file * file; -struct dentry * dentry; -struct inode * inode;int ret, err; +struct dentry *dentry; +struct inode *inode; - ret = -EBADF ; - file =fget (fd); -if (!file) - goto out; - +if (unlikely (!file->f_op || !file->f_op->fsync)) +return -EINVAL ; + dentry = file->f_dentry; inode = dentry->d_inode; - ret = -EINVAL ; -if (!file->f_op || !file->f_op->fsync) - goto out_putf; - -down (&inode->i_sem); ret =filemap_fdatasync (inode->i_mapping); err = file->f_op->fsync (file, dentry,1 );if (err && !ret)
@@ -463,6 +455,23 @@ asmlinkage long sys_fdatasync(unsigned i err =filemap_fdatawait (inode->i_mapping);if (err && !ret) ret = err; +return ret; +} + +asmlinkage long sys_fdatasync (unsigned int fd) +{ +struct file * file; +struct inode *inode; +int ret; + + ret = -EBADF ; + file =fget (fd); +if (!file) + goto out; + + inode = file->f_dentry->d_inode; +down (&inode->i_sem); + ret =do_fdatasync (file);up (&inode->i_sem); out_putf:
@@ -2106,7 +2115,8 @@ int generic_direct_IO(int rw, struct inoint i, nr_blocks, retval;unsigned long * blocks = iobuf->blocks;int length; - +int beyond_eof =0 ; + length = iobuf->length; nr_blocks = length / blocksize;/* build the blocklist */
@@ -2117,13 +2127,20 @@ int generic_direct_IO(int rw, struct ino bh.b_dev = inode->i_dev; bh.b_size = blocksize; - retval =get_block (inode, blocknr, &bh, rw ==READ ?0 :1 ); +if (((loff_t ) blocknr) * blocksize >= inode->i_size) + beyond_eof =1 ; + +/* Only allow get_block to create new blocks if we are safely +beyond EOF. O_DIRECT is unsafe inside sparse files. */ + retval =get_block (inode, blocknr, &bh, + ((rw !=READ ) && beyond_eof)); +if (retval) {if (!i)/* report error to userspace */ goto out;else -/* do short I/O utill 'i' */ +/* do short I/O until 'i' */ break ; }
@@ -2139,14 +2156,20 @@ int generic_direct_IO(int rw, struct inoif (buffer_new (&bh))unmap_underlying_metadata (&bh);if (!buffer_mapped (&bh)) -BUG (); +/* upper layers need to pass the error on or +* fall back to buffered IO. */ +return -ENOTBLK ; } blocks[i] = bh.b_blocknr; }/* patch length to handle short I/O */ iobuf->length = i * blocksize; +if (!beyond_eof) +up (&inode->i_sem); retval =brw_kiovec (rw,1 , &iobuf, inode->i_dev, iobuf->blocks, blocksize); +if (!beyond_eof) +down (&inode->i_sem);/* restore orig length */ iobuf->length = length; out:
fs/inode.c renamed from fs/inode.c.=K0000=.orig +1 -0
@@ -150,6 +150,7 @@ void inode_init_once(struct inode *inodeINIT_LIST_HEAD (&inode->i_devices);sema_init (&inode->i_sem,1 );sema_init (&inode->i_zombie,1 ); +init_rwsem (&inode->i_alloc_sem);spin_lock_init (&inode->i_data.i_shared_lock); }
fs/open.c renamed from fs/open.c.=K0000=.orig +2 -0
@@ -105,11 +105,13 @@ int do_truncate(struct dentry *dentry, lif (length <0 )return -EINVAL ; +down_write (&inode->i_alloc_sem);down (&inode->i_sem); newattrs.ia_size = length; newattrs.ia_valid =ATTR_SIZE |ATTR_CTIME ; error =notify_change (dentry, &newattrs);up (&inode->i_sem); +up_write (&inode->i_alloc_sem);return error; }
include/linux/fs.h renamed from include/linux/fs.h.=K0000=.orig +2 -0
@@ -456,6 +456,7 @@ struct inode {unsigned long i_blocks;unsigned long i_version;struct semaphore i_sem; +struct rw_semaphore i_alloc_sem;struct semaphore i_zombie;struct inode_operations *i_op;struct file_operations *i_fop;/* former ->i_op->default_file_ops */
@@ -1268,6 +1269,7 @@ static inline int fsync_inode_data_buffereturn fsync_buffers_list (&inode->i_dirty_data_buffers); }extern int inode_has_buffers (struct inode *); +extern int do_fdatasync (struct file *);extern int filemap_fdatasync (struct address_space *);extern int filemap_fdatawait (struct address_space *);extern void sync_supers (kdev_t dev,int wait);
mm/filemap.c renamed from mm/filemap.c.=K0000=.orig +142 -56
306 lines not on this page · raw message