From 1b842e3f13b91e166f9d33ad81898d80321d7417 Mon Sep 17 00:00:00 2001 From: Brian Behlendorf Date: Fri, 13 Jan 2017 10:03:44 -0800 Subject: [PATCH] Retire .write/.read file operations The .write/.read file operations callbacks can be retired since support for .read_iter/.write_iter and .aio_read/.aio_write has been added. The vfs_write()/vfs_read() entry functions will select the correct interface for the kernel. This is desirable because all VFS write/read operations now rely on common code. This change also add the generic write checks to make sure that ulimits are enforced correctly on write. A similar check was added to zpl_fallocate() so the truncate case is also handled properly. Signed-off-by: Brian Behlendorf Issue #5587 --- module/zfs/zpl_file.c | 64 +++++++++---------- tests/runfiles/linux.run | 6 +- .../tests/functional/large_files/Makefile.am | 1 + .../large_files/large_files_002_pos.ksh | 55 ++++++++++++++++ 4 files changed, 91 insertions(+), 35 deletions(-) create mode 100755 tests/zfs-tests/tests/functional/large_files/large_files_002_pos.ksh diff --git a/module/zfs/zpl_file.c b/module/zfs/zpl_file.c index 332fb992e7a2..1b59035fad4a 100644 --- a/module/zfs/zpl_file.c +++ b/module/zfs/zpl_file.c @@ -258,21 +258,6 @@ zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos, flags, cr, 0)); } -static ssize_t -zpl_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) -{ - cred_t *cr = CRED(); - ssize_t read; - - crhold(cr); - read = zpl_read_common(filp->f_mapping->host, buf, len, ppos, - UIO_USERSPACE, filp->f_flags, cr); - crfree(cr); - - file_accessed(filp); - return (read); -} - static ssize_t zpl_iter_read_common(struct kiocb *kiocb, const struct iovec *iovp, unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip) @@ -349,6 +334,7 @@ zpl_write_common_iovec(struct inode *ip, const struct iovec *iovp, size_t count, return (wrote); } + inline ssize_t zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos, uio_seg_t segment, int flags, cred_t *cr) @@ -362,20 +348,6 @@ zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos, flags, cr, 0)); } -static ssize_t -zpl_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) -{ - cred_t *cr = CRED(); - ssize_t wrote; - - crhold(cr); - wrote = zpl_write_common(filp->f_mapping->host, buf, len, ppos, - UIO_USERSPACE, filp->f_flags, cr); - crfree(cr); - - return (wrote); -} - static ssize_t zpl_iter_write_common(struct kiocb *kiocb, const struct iovec *iovp, unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip) @@ -398,14 +370,21 @@ zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from) { ssize_t ret; uio_seg_t seg = UIO_USERSPACE; + + ret = generic_write_checks(kiocb, from); + if (ret <= 0) + return (ret); + if (from->type & ITER_KVEC) seg = UIO_SYSSPACE; if (from->type & ITER_BVEC) seg = UIO_BVEC; + ret = zpl_iter_write_common(kiocb, from->iov, from->nr_segs, iov_iter_count(from), seg, from->iov_offset); if (ret > 0) iov_iter_advance(from, ret); + return (ret); } #else @@ -413,6 +392,21 @@ static ssize_t zpl_aio_write(struct kiocb *kiocb, const struct iovec *iovp, unsigned long nr_segs, loff_t pos) { + struct file *file = kiocb->ki_filp; + struct address_space *mapping = file->f_mapping; + struct inode *ip = mapping->host; + int isblk = S_ISBLK(ip->i_mode); + size_t count; + ssize_t ret; + + ret = generic_segment_checks(iovp, &nr_segs, &count, VERIFY_READ); + if (ret) + return (ret); + + ret = generic_write_checks(file, &pos, &count, isblk); + if (ret) + return (ret); + return (zpl_iter_write_common(kiocb, iovp, nr_segs, kiocb->ki_nbytes, UIO_USERSPACE, 0)); } @@ -669,8 +663,16 @@ zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len) spl_inode_unlock(ip); return (0); } - if (offset + len > olen) + if (offset + len > olen) { + error = inode_newsize_ok(ip, offset + len); + if (error) { + spl_inode_unlock(ip); + return (error); + } + len = olen - offset; + } + bf.l_type = F_WRLCK; bf.l_whence = 0; bf.l_start = offset; @@ -835,8 +837,6 @@ const struct file_operations zpl_file_operations = { .open = zpl_open, .release = zpl_release, .llseek = zpl_llseek, - .read = zpl_read, - .write = zpl_write, #ifdef HAVE_VFS_RW_ITERATE .read_iter = zpl_iter_read, .write_iter = zpl_iter_write, diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index 49e6a47d328e..5df5f8461b3d 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -440,9 +440,9 @@ pre = tests = ['inuse_004_pos'] post = -# DISABLED: needs investigation -#[tests/functional/large_files] -#tests = ['large_files_001_pos'] +[tests/functional/large_files] +tests = ['large_files_001_pos'] +tests = ['large_files_002_pos'] # DISABLED: needs investigation #[tests/functional/largest_pool] diff --git a/tests/zfs-tests/tests/functional/large_files/Makefile.am b/tests/zfs-tests/tests/functional/large_files/Makefile.am index d78ea4722887..38f9975af2d7 100644 --- a/tests/zfs-tests/tests/functional/large_files/Makefile.am +++ b/tests/zfs-tests/tests/functional/large_files/Makefile.am @@ -3,3 +3,4 @@ dist_pkgdata_SCRIPTS = \ setup.ksh \ cleanup.ksh \ large_files_001_pos.ksh + large_files_002_pos.ksh diff --git a/tests/zfs-tests/tests/functional/large_files/large_files_002_pos.ksh b/tests/zfs-tests/tests/functional/large_files/large_files_002_pos.ksh new file mode 100755 index 000000000000..9d0e8772f7fb --- /dev/null +++ b/tests/zfs-tests/tests/functional/large_files/large_files_002_pos.ksh @@ -0,0 +1,55 @@ +#!/bin/ksh -p +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or http://www.opensolaris.org/os/licensing. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright (c) 2015 by Lawrence Livermore National Security, LLC. +# All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib + +# +# DESCRIPTION: +# Verify 'ulimit -f' file size restrictions are enforced. +# +# STRATEGY: +# 1. Set ulimit file size to unlimited, verify files can be created. +# 2. Reduce ulimit file size, verify the expected error is returned. +# + +verify_runnable "both" + +log_assert "Verify 'ulimit -f' maximum file size is enforced" + +# Verify 'ulimit -f unlimited' works +log_must ulimit -f unlimited +log_must sh -c 'yes | head -n2097152 >$TESTDIR/ulimit_write_file' +log_must sh -c '$TRUNCATE -s2M $TESTDIR/ulimit_trunc_file' +log_must $RM $TESTDIR/ulimit_write_file $TESTDIR/ulimit_trunc_file + +# Verify 'ulimit -f ' works +log_must ulimit -f 1024 +log_mustnot sh -c 'yes | head -n2097152 >$TESTDIR/ulimit_write_file' +log_mustnot sh -c '$TRUNCATE -s2M $TESTDIR/ulimit_trunc_file' +log_must $RM $TESTDIR/ulimit_write_file $TESTDIR/ulimit_trunc_file + +log_pass "Successfully enforced 'ulimit -f' maximum file size"