From patchwork Fri Nov 21 10:08:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 5353931 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4E3D9C11AC for ; Fri, 21 Nov 2014 10:10:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 83F5A200E6 for ; Fri, 21 Nov 2014 10:10:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A5EE5200E0 for ; Fri, 21 Nov 2014 10:10:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758273AbaKUKJs (ORCPT ); Fri, 21 Nov 2014 05:09:48 -0500 Received: from mail-pa0-f49.google.com ([209.85.220.49]:55923 "EHLO mail-pa0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758236AbaKUKJN (ORCPT ); Fri, 21 Nov 2014 05:09:13 -0500 Received: by mail-pa0-f49.google.com with SMTP id eu11so4553569pac.36 for ; Fri, 21 Nov 2014 02:09:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=qjcYbRHrZ0iXK8xInfKQlovQxsWgq1Ae+R6aZrQMYlE=; b=P9qUrAWbinDoZlpHPIcBKm1a+OxEiwI1P56QUwjOxZrKOF5lJm6NV3rHuiQlMu642C QI5iRMUCFzrrjcLRsS3ddcAQkwO+FcPJfhBU20beRg0LuG5ypM78zEqiqARWWPaoTkik LJKkZ7paO8IJBZwFJl3+W4FYgAclGLb9JZ0wstzhUEMbrZsb9xPR1L3bt5NjdDcNo68X UpDNrutJY/DZ6UunlY/rKpUpYHyv0VR0K7BW75YydP1910j5nnJHtfQTHBy2zuidz+Zm no1JTRCnOHZMJr2n+/GrIoXQAq1tTBUxOCA978S1pdTGdzbsAubvHo7GjRtWGy+UpPev +C+g== X-Gm-Message-State: ALoCoQm7DxOSlKzDgWYtuBc2W4WPysROCt+ern+IAHN+isSUjFsmFmRBxiOcCy37upaLtv2USq1E X-Received: by 10.66.242.203 with SMTP id ws11mr5333963pac.69.1416564552480; Fri, 21 Nov 2014 02:09:12 -0800 (PST) Received: from mew.localdomain ([24.19.133.29]) by mx.google.com with ESMTPSA id g12sm4391480pdj.27.2014.11.21.02.09.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 21 Nov 2014 02:09:11 -0800 (PST) From: Omar Sandoval To: Alexander Viro , Andrew Morton , Chris Mason , Josef Bacik , linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, Trond Myklebust , Mel Gorman Cc: Omar Sandoval Subject: [PATCH v2 5/5] btrfs: enable swap file support Date: Fri, 21 Nov 2014 02:08:31 -0800 Message-Id: X-Mailer: git-send-email 2.1.3 In-Reply-To: References: In-Reply-To: References: Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Implement the swap file a_ops on btrfs. Activation simply checks for a usable swap file: it must be fully allocated (no holes), support direct I/O (so no compressed or inline extents) and should be nocow (I'm not sure about that last one). Signed-off-by: Omar Sandoval --- fs/btrfs/inode.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index d23362f..b8fd36b 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9442,6 +9442,75 @@ out_inode: } +static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, + sector_t *span) +{ + struct inode *inode = file_inode(file); + struct btrfs_inode *ip = BTRFS_I(inode); + int ret = 0; + u64 isize = inode->i_size; + struct extent_state *cached_state = NULL; + struct extent_map *em; + u64 start, len; + + if (ip->flags & BTRFS_INODE_COMPRESS) { + /* Can't do direct I/O on a compressed file. */ + pr_err("BTRFS: swapfile is compressed"); + return -EINVAL; + } + if (!(ip->flags & BTRFS_INODE_NODATACOW)) { + /* The swap file can't be copy-on-write. */ + pr_err("BTRFS: swapfile is copy-on-write"); + return -EINVAL; + } + + lock_extent_bits(&ip->io_tree, 0, isize - 1, 0, &cached_state); + + /* + * All of the extents must be allocated and support direct I/O. Inline + * extents and compressed extents fall back to buffered I/O, so those + * are no good. + */ + start = 0; + while (start < isize) { + len = isize - start; + em = btrfs_get_extent(inode, NULL, 0, start, len, 0); + if (IS_ERR(em)) { + ret = PTR_ERR(em); + goto out; + } + + if (test_bit(EXTENT_FLAG_VACANCY, &em->flags) || + em->block_start == EXTENT_MAP_HOLE) { + pr_err("BTRFS: swapfile has holes"); + ret = -EINVAL; + goto out; + } + if (em->block_start == EXTENT_MAP_INLINE) { + pr_err("BTRFS: swapfile is inline"); + ret = -EINVAL; + goto out; + } + if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { + pr_err("BTRFS: swapfile is compresed"); + ret = -EINVAL; + goto out; + } + + start = extent_map_end(em); + free_extent_map(em); + } + +out: + unlock_extent_cached(&ip->io_tree, 0, isize - 1, &cached_state, + GFP_NOFS); + return ret; +} + +static void btrfs_swap_deactivate(struct file *file) +{ +} + static const struct inode_operations btrfs_dir_inode_operations = { .getattr = btrfs_getattr, .lookup = btrfs_lookup, @@ -9519,6 +9588,8 @@ static const struct address_space_operations btrfs_aops = { .releasepage = btrfs_releasepage, .set_page_dirty = btrfs_set_page_dirty, .error_remove_page = generic_error_remove_page, + .swap_activate = btrfs_swap_activate, + .swap_deactivate = btrfs_swap_deactivate, }; static const struct address_space_operations btrfs_symlink_aops = {