From patchwork Tue Sep 11 22:34:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596425 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EDAF0112B for ; Tue, 11 Sep 2018 22:35:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC7CE29C45 for ; Tue, 11 Sep 2018 22:35:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEB8C29DEA; Tue, 11 Sep 2018 22:35:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 09A4329DDD for ; Tue, 11 Sep 2018 22:35:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CEAB38E0013; Tue, 11 Sep 2018 18:35:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B58F28E0001; Tue, 11 Sep 2018 18:35:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F8578E0013; Tue, 11 Sep 2018 18:35:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by kanga.kvack.org (Postfix) with ESMTP id 5846E8E0001 for ; Tue, 11 Sep 2018 18:35:15 -0400 (EDT) Received: by mail-pl1-f198.google.com with SMTP id c5-v6so12129014plo.2 for ; Tue, 11 Sep 2018 15:35:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=T4zypnAVQJfVK6JjN6ITEGJDUN7vtLnMURc1S60reio=; b=RqkraAE2vu2XNBuuYGlxNhriOW5ougA/h77sQ+Y5H0Uwgyf26Srv+B6i9Phg1MnAO1 PBqUT5prq8fmpE3gRe2rTmNWKyZvYg/KhvkjW/P2F37/0kZdYwCLHpDEKceLsfY7yvit zyejd+HQaWRVyj9BmJO5tkFknWXWcAMlnpAOoYdr6AXnZ5Ez3ac4Nnh0OaedsaPJSTGw 02KGgA+mE9ulLMKhjaEhPTyZThFHp/hpUwwE7GvwvB4TgHOnS1EQ0tc7LDDii33KBsdZ XaP+GE09gFeWKKoxroK4aCU9b4+qiyNHTtfqlgMkt6GNyt3210fjM+PmZ6qDV4hWXZyg iy4w== X-Gm-Message-State: APzg51BoIL3jIfrM9jy5SblUY/M2TAvJRxEB4HFW1eqdQ4/HvVW3ZwZA Fo+QfkE+FapLiGC+C2T/f+K6ETJdC2w2ZS9VxyaTUEMRqVXTCorT8JBImwxYndyd6+kiQMqPocQ L7g4l1uwroTa+QMZMwJbb4/LP4gKfKcc0BZpfC2oEJhH4VYlpKxDrQmbMGhV2Xbx7s/rb0V9VFi zpUTtDbjmLxyWCXhqMGDlVQS+UM1Ogo6nag57vInzGl3zAEWWEvkWGABq43n3oEc/7KRiK5Rfli 5DSS5W0Gd/feSdPV5vrf/r5WOl6i0sXVXnQtSCBUGPbK594DE3sp771sJUTjf5csWhDXn49vOJk GHz548njfD5TU0v6qQCUK1rZLgGUkR836rgxZ5DNtJsND9u7BQtyktAOZ6mlinqYYr3tkRDvEWj b X-Received: by 2002:a17:902:704c:: with SMTP id h12-v6mr29223169plt.237.1536705314909; Tue, 11 Sep 2018 15:35:14 -0700 (PDT) X-Received: by 2002:a17:902:704c:: with SMTP id h12-v6mr29223126plt.237.1536705314015; Tue, 11 Sep 2018 15:35:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705313; cv=none; d=google.com; s=arc-20160816; b=KKqQ5bD04aRk7xnV4NhVctqzptSiJotb2jnNOuLzG5T7Z3FmI8ZSGL/C62jixur+mP oaM7+keaQSdyTUq5nau+fdzJBig/P658kDougKUSR8ZaNbWePEosr1mGeBXK2ZlqKfqv WMm2tcqpFkH5K6xjFD6b77ybAfvignfwHQwrtITJOde3hChld0dOzGgLg1cYXRD1OVQH dLu5T+P/COJ54JpxJM4fMjQvvTh9P9WfUhUvJYpaJEg4odq1KWly3QKTXCIG3Pn7xH4k FdCOeBvt5yC4ADsfBB7wUnDBguCoOztvGjkIG/a840AiiGW6Vz/j1I4b55rFVKU6IwFo 4JiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=T4zypnAVQJfVK6JjN6ITEGJDUN7vtLnMURc1S60reio=; b=eY8whRJQrx++eRhrST6XC7EUkAtbHFvanFU7VbaUm9D4CffpcbfXz6hj5S+aqH0iag o6/b4RLSgD6FXszf1mvAA0816qA7yj0qQie2itUOede+lYM8khjdDBdDNvm6t3lbys7u e6JCWjsEpCCQv0FLG9EJNeKitguONAOMRkBCrz7HduV5//U7LYRpM1OFPBIAONcLLGMO CfxMegqj8rzWKbLgJ+hYSIKVQMDW+o+I58m2IUtB/J8Icd1xNFoKsyMTM+Yhcbcfp0mP NIBfR5XggpPxUVROmuLRDpNQi9UG8iT4KC45/MfAyqpsG1qWTVHvGS7wmbgjRA+pOXR1 DA3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=rGJqKX4G; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id 9-v6sor3041870pgp.177.2018.09.11.15.35.13 for (Google Transport Security); Tue, 11 Sep 2018 15:35:13 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=rGJqKX4G; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=T4zypnAVQJfVK6JjN6ITEGJDUN7vtLnMURc1S60reio=; b=rGJqKX4GqOEBzI51/hVK9a/V+lrJQvJwpsuMJsxJj4VrZzqJq6FsqW3SVnr0u7ss2w 2kUdQ4qiMib5tUyKrfqE6zUAtmLN9ls1ghLQ5VpV040OoH25kokfUHpa9CbqmH5cKCrh Pf1pNiVouOyhZ1EvQ0eT/Duxc+ctu4eAMYzYhMUtJrVpKN4o/EOyzyppyy+eZZ+ICmK9 vVhvsHBSTdF9YM1in9ZXnF1LRkVUUS/HCOWnuKQYaICbc/siXVczgAbe6OVS+8JAN7Yt XU/ZvwM6aLeg9+pxhTiv2qDbn4o/hncWNeJ2ahjuFI5Gx/dSW08Uf2qa+ngHEjcZUv26 vfMg== X-Google-Smtp-Source: ANB0VdZAj+z283HOInNN1MV+JK6u0utwXRYvvGx8Aw8Q2aEzyrsjhmI3Tba1dIU8N5FBRzFOGzVoOA== X-Received: by 2002:a65:4043:: with SMTP id h3-v6mr30193789pgp.207.1536705313427; Tue, 11 Sep 2018 15:35:13 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:12 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 1/6] mm: split SWP_FILE into SWP_ACTIVATED and SWP_FS Date: Tue, 11 Sep 2018 15:34:44 -0700 Message-Id: <6d63d8668c4287a4f6d203d65696e96f80abdfc7.1536704650.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval The SWP_FILE flag serves two purposes: to make swap_{read,write}page() go through the filesystem, and to make swapoff() call ->swap_deactivate(). For Btrfs, we want the latter but not the former, so split this flag into two. This makes us always call ->swap_deactivate() if ->swap_activate() succeeded, not just if it didn't add any swap extents itself. This also resolves the issue of the very misleading name of SWP_FILE, which is only used for swap files over NFS. Reviewed-by: Nikolay Borisov Signed-off-by: Omar Sandoval Acked-by: Andrew Morton --- include/linux/swap.h | 13 +++++++------ mm/page_io.c | 6 +++--- mm/swapfile.c | 13 ++++++++----- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 8e2c11e692ba..0fda0aa743f0 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -167,13 +167,14 @@ enum { SWP_SOLIDSTATE = (1 << 4), /* blkdev seeks are cheap */ SWP_CONTINUED = (1 << 5), /* swap_map has count continuation */ SWP_BLKDEV = (1 << 6), /* its a block device */ - SWP_FILE = (1 << 7), /* set after swap_activate success */ - SWP_AREA_DISCARD = (1 << 8), /* single-time swap area discards */ - SWP_PAGE_DISCARD = (1 << 9), /* freed swap page-cluster discards */ - SWP_STABLE_WRITES = (1 << 10), /* no overwrite PG_writeback pages */ - SWP_SYNCHRONOUS_IO = (1 << 11), /* synchronous IO is efficient */ + SWP_ACTIVATED = (1 << 7), /* set after swap_activate success */ + SWP_FS = (1 << 8), /* swap file goes through fs */ + SWP_AREA_DISCARD = (1 << 9), /* single-time swap area discards */ + SWP_PAGE_DISCARD = (1 << 10), /* freed swap page-cluster discards */ + SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */ + SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */ /* add others here before... */ - SWP_SCANNING = (1 << 12), /* refcount in scan_swap_map */ + SWP_SCANNING = (1 << 13), /* refcount in scan_swap_map */ }; #define SWAP_CLUSTER_MAX 32UL diff --git a/mm/page_io.c b/mm/page_io.c index aafd19ec1db4..e8653c368069 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -283,7 +283,7 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc, struct swap_info_struct *sis = page_swap_info(page); VM_BUG_ON_PAGE(!PageSwapCache(page), page); - if (sis->flags & SWP_FILE) { + if (sis->flags & SWP_FS) { struct kiocb kiocb; struct file *swap_file = sis->swap_file; struct address_space *mapping = swap_file->f_mapping; @@ -365,7 +365,7 @@ int swap_readpage(struct page *page, bool synchronous) goto out; } - if (sis->flags & SWP_FILE) { + if (sis->flags & SWP_FS) { struct file *swap_file = sis->swap_file; struct address_space *mapping = swap_file->f_mapping; @@ -423,7 +423,7 @@ int swap_set_page_dirty(struct page *page) { struct swap_info_struct *sis = page_swap_info(page); - if (sis->flags & SWP_FILE) { + if (sis->flags & SWP_FS) { struct address_space *mapping = sis->swap_file->f_mapping; VM_BUG_ON_PAGE(!PageSwapCache(page), page); diff --git a/mm/swapfile.c b/mm/swapfile.c index d954b71c4f9c..d3f95833d12e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -989,7 +989,7 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) goto nextsi; } if (size == SWAPFILE_CLUSTER) { - if (!(si->flags & SWP_FILE)) + if (!(si->flags & SWP_FS)) n_ret = swap_alloc_cluster(si, swp_entries); } else n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, @@ -2310,12 +2310,13 @@ static void destroy_swap_extents(struct swap_info_struct *sis) kfree(se); } - if (sis->flags & SWP_FILE) { + if (sis->flags & SWP_ACTIVATED) { struct file *swap_file = sis->swap_file; struct address_space *mapping = swap_file->f_mapping; - sis->flags &= ~SWP_FILE; - mapping->a_ops->swap_deactivate(swap_file); + sis->flags &= ~SWP_ACTIVATED; + if (mapping->a_ops->swap_deactivate) + mapping->a_ops->swap_deactivate(swap_file); } } @@ -2411,8 +2412,10 @@ static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) if (mapping->a_ops->swap_activate) { ret = mapping->a_ops->swap_activate(sis, swap_file, span); + if (ret >= 0) + sis->flags |= SWP_ACTIVATED; if (!ret) { - sis->flags |= SWP_FILE; + sis->flags |= SWP_FS; ret = add_swap_extent(sis, 0, sis->max, 0); *span = sis->pages; } From patchwork Tue Sep 11 22:34:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596427 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 07CE0112B for ; Tue, 11 Sep 2018 22:35:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EBA3129C45 for ; Tue, 11 Sep 2018 22:35:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DFF6829DEA; Tue, 11 Sep 2018 22:35:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8B2F729C45 for ; Tue, 11 Sep 2018 22:35:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84BFF8E0014; Tue, 11 Sep 2018 18:35:16 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7D69B8E0001; Tue, 11 Sep 2018 18:35:16 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DB9E8E0014; Tue, 11 Sep 2018 18:35:16 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id 1C0548E0001 for ; Tue, 11 Sep 2018 18:35:16 -0400 (EDT) Received: by mail-pl1-f197.google.com with SMTP id bg5-v6so12109799plb.20 for ; Tue, 11 Sep 2018 15:35:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=ozha6QqmfT5JrU9j0wVw3+1nSMpz8bliiwskTMqxkvk=; b=ZJX7H1ex+4E36Aq6zVKTW8JK5w1ncwY9tek9kuawo6JhNQAzCir0OIj6x+h50moOhV hfz1PwIOzoAOfMzHMNPWBU340XFWmX+hFUsBoxME/+7d0P9WDlXwqzqN4XYsiOyqXy24 HZSVSm3j1fyhd92hDez42oIsRBz9SrmIllBmZg1jJ32bEocrdSSiu4rbJYiWfl/dZjeG 5FXSAspDfChch0JHfOg62cWQPX56CI5GXyzW8+QBv8JLMY6uRtxz/tUcitd20dl0Ivq0 2Tj8YPprdH4DEHFiQhJC4MQxDq8Gz8IBWGUEPZ+/08/GHzEQfe8cY3rXO1URJfZfcsDV dYHw== X-Gm-Message-State: APzg51BK/k6JBGQHiQSPXabNf3VWvWh/4EBrHWkJAbM2yHI+S6zJ7SRY LVS1rPs34RI3xCemogzxgs/SKqmGB6W0K9Trjr4H7CPTSPEmRmq82PYmE3h4qPbOMMfAaIabOti bOvsVbilDmyn3hZkAe8b8LyzydxWKDgXE9UjLdi1hU6rVYvWobJvn1Ry/h453Xo/Y669bFvMHwO N1E1q2VvBNdQHQ4pGFJ/AzNSorTh9w1XLIs/w2JF1vEGLGK1JVoGjGoklQPqB/Gj40Sx+Wg5TH2 RHlRUCRmYazgdoIVD/bqFODGimrYYArVaNd4z/k4MoL7xGb2ke6LRrRl+nx0gWfz3lNZAs3mTbP fUsdMFHT76LIzwaSgcjiICUqRta74siVaPVPYjcjH9scWfbk8zidgWcsdRHYOPGDF08okYSx7Nh 8 X-Received: by 2002:a17:902:b491:: with SMTP id y17-v6mr29517136plr.160.1536705315814; Tue, 11 Sep 2018 15:35:15 -0700 (PDT) X-Received: by 2002:a17:902:b491:: with SMTP id y17-v6mr29517096plr.160.1536705315106; Tue, 11 Sep 2018 15:35:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705315; cv=none; d=google.com; s=arc-20160816; b=Rjc8pUYagIGvESa0WNK2k3WJQOvYMnTFRvLvGeqd5XcRJgjfI2e7OSORyUfggqMQqa 6P3s96zzyKFEONLsFkrHVHYGOpKLyy6kIf6QQH5RPXhtm+cu/G9IA8W5v9bHBJmUxghJ sIJX7LVfa5jh+/MS1x+gF26FQeSWmFzjh6KRBg6YwfBdq9w1nH2H2E1+hM2VYxGaqu2W uqOjK4we98p991U09sDT2OufkWyBJIBjgMe9dkZtxd+zUIsRietyNDrSHZ9bNTMWmIkd u+/lw/v6D6RKRk4PXr43vIDTMZPD5EwzP1bXyT9VSraL9d1+eDdYslQsTRn+DNggE0RB QPMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ozha6QqmfT5JrU9j0wVw3+1nSMpz8bliiwskTMqxkvk=; b=JvdyEQMxqlPrwghDlHxvAtInvD8hz18aY7aaUukIIPDCpIfXCFfph9AwYK0+zxU4FU 2OGcIJauZVhUVNMEqyte7RKzIQuJU01EiXFOAr0k2evxh2Wgu+Hj/4GnMC0j+hcXku3d 0J3YakwwH+W7Cb7hxMUJHwdpJXzrS3+4up66dBllGMzsx59jWbZspp/U0LxXy5OdTA6X boQF6GFCbACeYxHx2BxlpIsg2EkoHSgmOVefZojyeMeWaKrS11/T5DJGTEmedaDQbx3q 5xPSJkr0YzfMpxlDOdpnAEg17c98/xDPMfua+Y+rMupy+25c+ittg8vm2V9IrkiHAAbp 7YdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=gkSIn6ty; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id n1-v6sor3594499plk.27.2018.09.11.15.35.14 for (Google Transport Security); Tue, 11 Sep 2018 15:35:15 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=gkSIn6ty; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ozha6QqmfT5JrU9j0wVw3+1nSMpz8bliiwskTMqxkvk=; b=gkSIn6tyrTkosQgqndwLyMXZCgfmjd567GHivwkLnJV+l1gXSEhUK7IHcrX7cenp23 /lfS1nLuG9+RTfcgg4xM4vF49wJPoYkYuLXHo6wi+VsKvaZ6HvQqDlrL2IMwSfhvSp1r pFrf1WCqk/5sSrnLXYFBwc/TgqUstAZqiHQFv0UVTIoJnl71KG4x15DArl/6gYcBX/fh A69pV4vzqQG8GWFSyNGgWJfahodAEnwrgT+fUESWFZg5I8HBcg9xLjO4OWL3qjl0BEBy 8JMmdpWKUWdfOJbkEz2guo5i7T9eS3VFOPqQJffMnineocyHLlYTH7fTSjJzirdfzeVC 3c4w== X-Google-Smtp-Source: ANB0Vdb1RPPJfTzj+TMrju0ZtbGQZ2pVTTrX/4rmVIJaBE85C6AlztKCIwP3GHrvMrOQLHw0dxT/dw== X-Received: by 2002:a17:902:d213:: with SMTP id t19-v6mr29560095ply.63.1536705314519; Tue, 11 Sep 2018 15:35:14 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:13 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 2/6] mm: export add_swap_extent() Date: Tue, 11 Sep 2018 15:34:45 -0700 Message-Id: X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.001714, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval Btrfs will need this for swap file support. Signed-off-by: Omar Sandoval Acked-by: Andrew Morton --- mm/swapfile.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index d3f95833d12e..51cb30de17bc 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2365,6 +2365,7 @@ add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, list_add_tail(&new_se->list, &sis->first_swap_extent.list); return 1; } +EXPORT_SYMBOL_GPL(add_swap_extent); /* * A `swap extent' is a simple thing which maps a contiguous range of pages From patchwork Tue Sep 11 22:34:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596429 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5663614E0 for ; Tue, 11 Sep 2018 22:35:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46CD229C45 for ; Tue, 11 Sep 2018 22:35:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B32429DEA; Tue, 11 Sep 2018 22:35:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A69F329C45 for ; Tue, 11 Sep 2018 22:35:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 517DC8E0015; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4A3338E0001; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F42C8E0015; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id D9AA78E0001 for ; Tue, 11 Sep 2018 18:35:17 -0400 (EDT) Received: by mail-pg1-f198.google.com with SMTP id r20-v6so12999008pgv.20 for ; Tue, 11 Sep 2018 15:35:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=XfhD1s+oXIFOuIlVNDkhy1zo7dmofYMPh5tHBE+eOHA=; b=hrBytV4HYpbilIpWLVP4GkesOqezdaCMBY1HX+2NIvQDlLAkaxojtU3LiNH311qSaB 7U5zca0LimxWi2t81ynv2PDesKj58UWk41/1Lvu2DXKl/EZcip+J2cBX99dh+SIZNl9T AJ1MEpngdbWL7qf7wf6MJlyMQ6ceS4e09OG94f7/KvghfBwXJduofWZGum47oX/2H3+b XXqtwmVwRvRX3bOvKBzObpE6TL7Xb9lQmODA45C6URh8RFsZvY0+8h6c6bDSWXvl0MQO g9nuhq21mVDZYN+MH0cTJH5ia5xBdlo21gHeJXn4gbwz9yROV8Z92rXAWCsyCis0Bji6 WyVw== X-Gm-Message-State: APzg51BZBXUUyS0oC/NqxerEGcuKeABSsPHEgpMe9CRTqu8dJC1ty01z kuVGmUw2vZZmsl8LwdUjfiqMLAhShzlsQc4SRN6HplJ8txXTbyZgnFMVSbOidRFJWZRHDXniIX2 Ia9TA/eh0k3DhiVMgISLu5U56v7TwWiQm7VQ4PDTjbzQHv2+HYj1gB3+vHv1+4pr4d2hHWiQpwG vfMGRR3QG1VhEAlDsDnZWKzJStlBPWk9r+AksvPJDNIqIMpY6GItiEgSZ4bmfZX7T5EgtWF81SW ilPB43q9vAt6n2CiNS+SBjbraIOPQgD2sXeAZ82e/5UrCA0dTxIdkwb3gyk2BThhCxfBahAm+mP t+ZkRRrxo7Cq6+M9ZLVACA4bX8Kd1ch0fAzj9fBSHfZIiqBKnC7HqFpp+k4teO5iA92+77mrJyx F X-Received: by 2002:a17:902:b58a:: with SMTP id a10-v6mr29955782pls.306.1536705317560; Tue, 11 Sep 2018 15:35:17 -0700 (PDT) X-Received: by 2002:a17:902:b58a:: with SMTP id a10-v6mr29955705pls.306.1536705316207; Tue, 11 Sep 2018 15:35:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705316; cv=none; d=google.com; s=arc-20160816; b=WACvl0cxtzkeJcgwdLhYhVW/FxOXkeCOv84J0zy1JC7rzTKrTj94TfcSg7pPlZOGU1 xwvxRG5FTCqOZwwUaj5RqrLasrDIEGP6K3iRjTTdF4SHCW+fM29nGXnvKb9reRYhlLEX lsRQGc2zk5p7PccftsaQXmmCFQgzBWTHmn7kcS3M96yXsEINs5tV/a9G9Z1NhAqUS+DU olHYE5E7jOAYCV63E2QBdU5tj4AvVKhV08WWZ7DyWwZxzqF6/qtUVlJVPfZbvt55oaZe Bqj02FvKY2oGvw4tOP0MmeBgZci4ht4H8Hauquk532PQf9gQ4zr5dSZLkRveOpWAqBpE p8NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XfhD1s+oXIFOuIlVNDkhy1zo7dmofYMPh5tHBE+eOHA=; b=dDsBlyOeiXDLyyL466jV6/+AN3pt2CGXPvT4kHJ18EizarC5bSNo8jTr3amBwCeOQ9 lH9zQqP3BhdyrtBPYHnvweiCoAjX0wRQ+jz/TTlRC1mosBAA/tp0OyMv0hVf6YIGVj2c IspTL82E3B/jzAy8PD+44HQorGUEy2mjto1h3eMwsxfAWvOhADZeDr5w8M3KsVfml1cf 9qqJv4ajGgbSOEUYoKpEUz4Zk6av1Dt5DHFLKvBttmKH8Re2VREuJ5eSgTFapwowxaz9 5eLtWiHMlQ+MwazDkR9mkT+7u5niEoCI6BnoH/7jf+DwOC4fJHX0ZhpBrM13+IOxEN9t 1Ovw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=nVkLNSX9; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u14-v6sor3797123pfa.18.2018.09.11.15.35.16 for (Google Transport Security); Tue, 11 Sep 2018 15:35:16 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=nVkLNSX9; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XfhD1s+oXIFOuIlVNDkhy1zo7dmofYMPh5tHBE+eOHA=; b=nVkLNSX9fRLZ1hzsn8SLpu3zAva7jVS84VE+Xq03vzGZJq+c4Y0ic9FobvTbOiV/e4 Pxa3nv5CkePSM2AbZllqQzDNtu9tYlY+h7Ztvoqpk8QclgjWIZTKU6hbgqSL25IivY3l uJNMvpd+gg7Ihja1NERjPErMoADVgMMO4/QHyeQ84V22HPXF3R+6VDF7Pa6QtT4XbShF JIivYZpM/HqX3XKY4tVx/bslIjMv5lrsvp7SWuqq+1RgeGN7567iq0bFQai+pZpktuUz r7dyOGMnRiYCNaHEH1VWT6rFLHR50UntCnhy3B0WcBw0M7zMb+iTeTc/oX2jREJJsCNb 18dA== X-Google-Smtp-Source: ANB0VdZaPliu05bWLgw/zF77UuAbpoGICYqiLQFjGGnZY02Tf3QOvFVXd9u1vMeHQhaFVsQ8Z+JWXw== X-Received: by 2002:a63:706:: with SMTP id 6-v6mr29990412pgh.137.1536705315598; Tue, 11 Sep 2018 15:35:15 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:14 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 3/6] vfs: update swap_{,de}activate documentation Date: Tue, 11 Sep 2018 15:34:46 -0700 Message-Id: X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval The documentation for these functions is wrong in several ways: - swap_activate() is called with the inode locked - swap_activate() takes a swap_info_struct * and a sector_t * - swap_activate() can also return a positive number of extents it added itself - swap_deactivate() does not return anything Reviewed-by: Nikolay Borisov Signed-off-by: Omar Sandoval --- Documentation/filesystems/Locking | 17 +++++++---------- Documentation/filesystems/vfs.txt | 12 ++++++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Documentation/filesystems/Locking b/Documentation/filesystems/Locking index efea228ccd8a..b970c8c2ee22 100644 --- a/Documentation/filesystems/Locking +++ b/Documentation/filesystems/Locking @@ -210,8 +210,9 @@ prototypes: int (*launder_page)(struct page *); int (*is_partially_uptodate)(struct page *, unsigned long, unsigned long); int (*error_remove_page)(struct address_space *, struct page *); - int (*swap_activate)(struct file *); - int (*swap_deactivate)(struct file *); + int (*swap_activate)(struct swap_info_struct *, struct file *, + sector_t *); + void (*swap_deactivate)(struct file *); locking rules: All except set_page_dirty and freepage may block @@ -235,8 +236,8 @@ putback_page: yes launder_page: yes is_partially_uptodate: yes error_remove_page: yes -swap_activate: no -swap_deactivate: no +swap_activate: yes +swap_deactivate: no ->write_begin(), ->write_end() and ->readpage() may be called from the request handler (/dev/loop). @@ -333,14 +334,10 @@ cleaned, or an error value if not. Note that in order to prevent the page getting mapped back in and redirtied, it needs to be kept locked across the entire operation. - ->swap_activate will be called with a non-zero argument on -files backing (non block device backed) swapfiles. A return value -of zero indicates success, in which case this file can be used for -backing swapspace. The swapspace operations will be proxied to the -address space operations. + ->swap_activate is called from sys_swapon() with the inode locked. ->swap_deactivate() will be called in the sys_swapoff() -path after ->swap_activate() returned success. +path after ->swap_activate() returned success. The inode is not locked. ----------------------- file_lock_operations ------------------------------ prototypes: diff --git a/Documentation/filesystems/vfs.txt b/Documentation/filesystems/vfs.txt index 4b2084d0f1fb..40d6d6d4b76b 100644 --- a/Documentation/filesystems/vfs.txt +++ b/Documentation/filesystems/vfs.txt @@ -652,8 +652,9 @@ struct address_space_operations { unsigned long); void (*is_dirty_writeback) (struct page *, bool *, bool *); int (*error_remove_page) (struct mapping *mapping, struct page *page); - int (*swap_activate)(struct file *); - int (*swap_deactivate)(struct file *); + int (*swap_activate)(struct swap_info_struct *, struct file *, + sector_t *); + void (*swap_deactivate)(struct file *); }; writepage: called by the VM to write a dirty page to backing store. @@ -830,8 +831,11 @@ struct address_space_operations { swap_activate: Called when swapon is used on a file to allocate space if necessary and pin the block lookup information in - memory. A return value of zero indicates success, - in which case this file can be used to back swapspace. + memory. If this returns zero, the swap system will call the address + space operations ->readpage() and ->direct_IO(). Alternatively, this + may call add_swap_extent() and return the number of extents added, in + which case the swap system will use the provided blocks directly + instead of going through the filesystem. swap_deactivate: Called during swapoff on files where swap_activate was successful. From patchwork Tue Sep 11 22:34:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596431 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E5F014E0 for ; Tue, 11 Sep 2018 22:35:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2CED629C45 for ; Tue, 11 Sep 2018 22:35:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 211D929DEA; Tue, 11 Sep 2018 22:35:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3BB1929C45 for ; Tue, 11 Sep 2018 22:35:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D1788E0016; Tue, 11 Sep 2018 18:35:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0AD988E0017; Tue, 11 Sep 2018 18:35:19 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E17E98E0016; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id 973FC8E0001 for ; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) Received: by mail-pl1-f197.google.com with SMTP id d10-v6so12022876pll.22 for ; Tue, 11 Sep 2018 15:35:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=wiD4LyeLsLNo8k6Tqf4iklvtWcnoAUiPaJ9XhETsB/Y=; b=lpBSRii3eG04imCd+UA2xuWF0n4oh9lydR9p3RCPK6UyN6DArS5jzFvHTIrIOUocay 7OfMOBIryS0ZcTZCYPFGcGCOmj4kuu5h+thr63C7ighEPinVCdd0Lfb1G5+Ka8aplHFB /70h6YhjwN6Nc8ttl9nyTBZP9HqXyECXzEmZ0I4E7RGvVG/mXZkOpWkIN7tM5tshbkkX DNNRFP5m7q9oBISGTSRWhwWSt4KAddiFh9242KqRCb4X7KFGJ3VURzguHypZG3wfxxQp x+nJnxLvxPFiuAp8FDBXnTXjJWTECBE40L+mZFlubR2Wbi/L91Uj7erGRc0ADInvaTEp 2f3Q== X-Gm-Message-State: APzg51DeEbjJIChKhMlu315mtLZ2Hvy/3q0wt6YriQb9Ikco0wEzSeiC NQrvadc7lcZ1wATjld4SR+OtAU/Dl+k2F8Ib6o7JkPaWWMRtNEUJdIgZJPYpjYYVPSrRFbg/oQj NGtCYpsvI623iDWOdFQBwexHMyTqvf8PlX6dPwhD2tS6/9e4wNQ7n7NucXAfyMyjx2MTPQmyNod VyFhvvT0AyS2ogKdaKCoDuIipDg7QzXeJVs6SLbU8XE6iN77okgqmZRFheu6QVTd0Ta8tANkqHw aRJDe6ik5hRFtjRq366zhgz6hGrbDFjBLZRJoCyb92epXe42osyFpgrIC/KiMscDR7B7j2KPpXQ K8g4PwRXPoPUOA9KYDK9gm18KejgGesJDGp0Up5sKUw1XgYm8MSltNd7ujPpknBfNIosq/qBYle q X-Received: by 2002:a17:902:d688:: with SMTP id v8-v6mr29374657ply.278.1536705318236; Tue, 11 Sep 2018 15:35:18 -0700 (PDT) X-Received: by 2002:a17:902:d688:: with SMTP id v8-v6mr29374595ply.278.1536705317021; Tue, 11 Sep 2018 15:35:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705316; cv=none; d=google.com; s=arc-20160816; b=NkAF/LLcEJ+u329/o8LTbd8mO5JVH0lFkRMLinpgpMXWDAcZXYBZb5tw4xwMGf0eJq +Q6KTDuCZPsqBnBJie+DDPyH6rVDR3ar1d2jx0/obVjHncEc8oQsjJzpHc2gtpMh1fzx 0Yd+bX/MiTm6zviE++VSjPIvY3QR438heO9KPuGhyrY1wza8S+zvo6VYlYv594weLOWL yHOreNsd+BVSU/areVXwWbPbeRefjN0x3d/VEsd4K7tswggnQhU9w423skxeSlT8Cpf1 GdN/ko4XKouyhQeEIvgHe5fGTTHK2lU27okvVlT0yJrC5zjUWuHVFWCjfCsAMvKUj82b XR9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wiD4LyeLsLNo8k6Tqf4iklvtWcnoAUiPaJ9XhETsB/Y=; b=LWzYaX4K4tG25S5tBhbCb+LPYySWK7kgGgWa4cVRpMYwWasv4kwYJD8IkYjVTak6pB 9HyEUS19ogbbipNIsjvW/bDrX91Q/2tJ/lxUhgG4sI3sBxMmtnNvJ1so37RD+mRkPSL5 fbqr9fxRZB08/mXo45MGQjnqUz0Bxl2jSCNlYq6KMvrELATjVA9zJ63kjrbxnmXj4tCT wGLPagmAVVIFKnU5OuKXZPqacenjveEVNq1SVAJ9uMzigHAZ9aTCDYXjjS/QXUFrkJFK XyWuQDdJck8suVRaKIfSmcrVEUGmdwtKL6tdBzPjuXd7Dg1PMmkC56nNnAp5Co+AF4wh 441w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=GnzChckN; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id c21-v6sor2823905pgi.314.2018.09.11.15.35.16 for (Google Transport Security); Tue, 11 Sep 2018 15:35:16 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=GnzChckN; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wiD4LyeLsLNo8k6Tqf4iklvtWcnoAUiPaJ9XhETsB/Y=; b=GnzChckNCVYbw0uA+YPnaxXD4ImsgoMIDYTJCYY2tt8MEx/NT34aPksvbPVlNcsBZN F/35f6IQI182JvOzKa/2PUdIMqkpjO+JMsid3uKZTsJq6xGEYEkJvf850VzeCCBhOuFc ITH12rKzCNOOdp8liCOIJZDRvUcS3l+KEJh7vCALXGRPLFSB0jbsHnuBoMXTCTTCFGhB WbeDj6IvrhJ1mb3kUDtBnkwdRujojVYVfv99l0W7twKBpEdh1AvjgPlHCQU1Qu5SDIfG GG6HeSz5jYuFYut1kwWkrGclWsvZuD9exTh146LDdJqEY7OeAR6Z4z17jnKjgNS/g8ZN Fqrw== X-Google-Smtp-Source: ANB0Vda692OVmR0m/3RmPfWKtzWWJBQW4U9U3DMgBGwVQV+KFgAEHbOnCSt0scpoOasX/mtfwydv5Q== X-Received: by 2002:a63:555c:: with SMTP id f28-v6mr30984046pgm.37.1536705316417; Tue, 11 Sep 2018 15:35:16 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:15 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 4/6] Btrfs: prevent ioctls from interfering with a swap file Date: Tue, 11 Sep 2018 15:34:47 -0700 Message-Id: <5e9eff44861cdb5c27d3f2880a57b812c9797d66.1536704650.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval When a swap file is active, we must make sure that the extents of the file are not moved and that they don't become shared. That means that the following are not safe: - chattr +c (enable compression) - reflink - dedupe - snapshot - defrag Don't allow those to happen on an active swap file. Additionally, balance, resize, device remove, and device replace are also unsafe if they affect an active swapfile. Add a red-black tree of block groups and devices which contain an active swapfile. Relocation checks each block group against this tree and skips it or errors out for balance or resize, respectively. Device remove and device replace check the tree for the device they will operate on. Note that we don't have to worry about chattr -C (disable nocow), which we ignore for non-empty files, because an active swapfile must be non-empty and can't be truncated. We also don't have to worry about autodefrag because it's only done on COW files. Truncate and fallocate are already taken care of by the generic code. Device add doesn't do relocation so it's not an issue, either. Signed-off-by: Omar Sandoval --- fs/btrfs/ctree.h | 29 +++++++++++++++++++++++ fs/btrfs/dev-replace.c | 8 +++++++ fs/btrfs/disk-io.c | 4 ++++ fs/btrfs/ioctl.c | 31 +++++++++++++++++++++--- fs/btrfs/relocation.c | 18 ++++++++++---- fs/btrfs/volumes.c | 53 ++++++++++++++++++++++++++++++++++++++---- 6 files changed, 131 insertions(+), 12 deletions(-) diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 2cddfe7806a4..08df61b8fc87 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -716,6 +716,28 @@ struct btrfs_fs_devices; struct btrfs_balance_control; struct btrfs_delayed_root; +/* + * Block group or device which contains an active swapfile. Used for preventing + * unsafe operations while a swapfile is active. + * + * These are sorted on (ptr, inode) (note that a block group or device can + * contain more than one swapfile). We compare the pointer values because we + * don't actually care what the object is, we just need a quick check whether + * the object exists in the rbtree. + */ +struct btrfs_swapfile_pin { + struct rb_node node; + void *ptr; + struct inode *inode; + /* + * If true, ptr points to a struct btrfs_block_group_cache. Otherwise, + * ptr points to a struct btrfs_device. + */ + bool is_block_group; +}; + +bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr); + #define BTRFS_FS_BARRIER 1 #define BTRFS_FS_CLOSING_START 2 #define BTRFS_FS_CLOSING_DONE 3 @@ -1121,6 +1143,10 @@ struct btrfs_fs_info { u32 sectorsize; u32 stripesize; + /* Block groups and devices containing active swapfiles. */ + spinlock_t swapfile_pins_lock; + struct rb_root swapfile_pins; + #ifdef CONFIG_BTRFS_FS_REF_VERIFY spinlock_t ref_verify_lock; struct rb_root block_tree; @@ -1286,6 +1312,9 @@ struct btrfs_root { spinlock_t qgroup_meta_rsv_lock; u64 qgroup_meta_rsv_pertrans; u64 qgroup_meta_rsv_prealloc; + + /* Number of active swapfiles */ + atomic_t nr_swapfiles; }; struct btrfs_file_private { diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c index dec01970d8c5..09d2cee2635b 100644 --- a/fs/btrfs/dev-replace.c +++ b/fs/btrfs/dev-replace.c @@ -414,6 +414,14 @@ int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, if (ret) return ret; + if (btrfs_pinned_by_swapfile(fs_info, src_device)) { + btrfs_info_in_rcu(fs_info, + "cannot replace device %s (devid %llu) due to active swapfile", + btrfs_dev_name(src_device), + src_device->devid); + return -ETXTBSY; + } + ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, src_device, &tgt_device); if (ret) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 05dc3c17cb62..2428a73067d2 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1188,6 +1188,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, refcount_set(&root->refs, 1); atomic_set(&root->will_be_snapshotted, 0); atomic_set(&root->snapshot_force_cow, 0); + atomic_set(&root->nr_swapfiles, 0); root->log_transid = 0; root->log_transid_committed = -1; root->last_log_commit = 0; @@ -2782,6 +2783,9 @@ int open_ctree(struct super_block *sb, fs_info->sectorsize = 4096; fs_info->stripesize = 4096; + spin_lock_init(&fs_info->swapfile_pins_lock); + fs_info->swapfile_pins = RB_ROOT; + ret = btrfs_alloc_stripe_hash_table(fs_info); if (ret) { err = ret; diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index d60b6caf09e8..dc2bbaee212d 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -290,6 +290,11 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) } else if (fsflags & FS_COMPR_FL) { const char *comp; + if (IS_SWAPFILE(inode)) { + ret = -ETXTBSY; + goto out_unlock; + } + binode->flags |= BTRFS_INODE_COMPRESS; binode->flags &= ~BTRFS_INODE_NOCOMPRESS; @@ -752,6 +757,12 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir, if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) return -EINVAL; + if (atomic_read(&root->nr_swapfiles)) { + btrfs_info(fs_info, + "cannot snapshot subvolume with active swapfile"); + return -ETXTBSY; + } + pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); if (!pending_snapshot) return -ENOMEM; @@ -1503,9 +1514,13 @@ int btrfs_defrag_file(struct inode *inode, struct file *file, } inode_lock(inode); - if (do_compress) - BTRFS_I(inode)->defrag_compress = compress_type; - ret = cluster_pages_for_defrag(inode, pages, i, cluster); + if (IS_SWAPFILE(inode)) { + ret = -ETXTBSY; + } else { + if (do_compress) + BTRFS_I(inode)->defrag_compress = compress_type; + ret = cluster_pages_for_defrag(inode, pages, i, cluster); + } if (ret < 0) { inode_unlock(inode); goto out_ra; @@ -3573,6 +3588,11 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, goto out_unlock; } + if (IS_SWAPFILE(src) || IS_SWAPFILE(dst)) { + ret = -ETXTBSY; + goto out_unlock; + } + tail_len = olen % BTRFS_MAX_DEDUPE_LEN; chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); if (chunk_count == 0) @@ -4269,6 +4289,11 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src, goto out_unlock; } + if (IS_SWAPFILE(src) || IS_SWAPFILE(inode)) { + ret = -ETXTBSY; + goto out_unlock; + } + /* determine range to clone */ ret = -EINVAL; if (off + len > src->i_size || off + len < off) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 8783a1776540..7468a0f55cd2 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4226,6 +4226,7 @@ static void describe_relocation(struct btrfs_fs_info *fs_info, */ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) { + struct btrfs_block_group_cache *bg; struct btrfs_root *extent_root = fs_info->extent_root; struct reloc_control *rc; struct inode *inode; @@ -4234,14 +4235,23 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) int rw = 0; int err = 0; + bg = btrfs_lookup_block_group(fs_info, group_start); + if (!bg) + return -ENOENT; + + if (btrfs_pinned_by_swapfile(fs_info, bg)) { + btrfs_put_block_group(bg); + return -ETXTBSY; + } + rc = alloc_reloc_control(); - if (!rc) + if (!rc) { + btrfs_put_block_group(bg); return -ENOMEM; + } rc->extent_root = extent_root; - - rc->block_group = btrfs_lookup_block_group(fs_info, group_start); - BUG_ON(!rc->block_group); + rc->block_group = bg; ret = btrfs_inc_block_group_ro(rc->block_group); if (ret) { diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index f4405e430da6..a2761395ed22 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1882,6 +1882,14 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, if (ret) goto out; + if (btrfs_pinned_by_swapfile(fs_info, device)) { + btrfs_info_in_rcu(fs_info, + "cannot remove device %s (devid %llu) due to active swapfile", + rcu_str_deref(device->name), device->devid); + ret = -ETXTBSY; + goto out; + } + if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { ret = BTRFS_ERROR_DEV_TGT_REPLACE; goto out; @@ -3626,10 +3634,15 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info) ret = btrfs_relocate_chunk(fs_info, found_key.offset); mutex_unlock(&fs_info->delete_unused_bgs_mutex); - if (ret && ret != -ENOSPC) - goto error; if (ret == -ENOSPC) { enospc_errors++; + } else if (ret == -ETXTBSY) { + btrfs_info(fs_info, + "skipping relocation of block group %llu due to active swapfile", + found_key.offset); + ret = 0; + } else if (ret) { + goto error; } else { spin_lock(&fs_info->balance_lock); bctl->stat.completed++; @@ -4426,10 +4439,16 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) ret = btrfs_relocate_chunk(fs_info, chunk_offset); mutex_unlock(&fs_info->delete_unused_bgs_mutex); - if (ret && ret != -ENOSPC) - goto done; - if (ret == -ENOSPC) + if (ret == -ENOSPC) { failed++; + } else if (ret) { + if (ret == -ETXTBSY) { + btrfs_info(fs_info, + "could not shrink block group %llu due to active swapfile", + chunk_offset); + } + goto done; + } } while (key.offset-- > 0); if (failed && !retried) { @@ -7530,3 +7549,27 @@ int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) btrfs_free_path(path); return ret; } + +/* + * Check whether the given block group or device is pinned by any inode being + * used as a swapfile. + */ +bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) +{ + struct btrfs_swapfile_pin *sp; + struct rb_node *node; + + spin_lock(&fs_info->swapfile_pins_lock); + node = fs_info->swapfile_pins.rb_node; + while (node) { + sp = rb_entry(node, struct btrfs_swapfile_pin, node); + if (ptr < sp->ptr) + node = node->rb_left; + else if (ptr > sp->ptr) + node = node->rb_right; + else + break; + } + spin_unlock(&fs_info->swapfile_pins_lock); + return node != NULL; +} From patchwork Tue Sep 11 22:34:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EB689112B for ; Tue, 11 Sep 2018 22:35:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAC6229C45 for ; Tue, 11 Sep 2018 22:35:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF31129DEA; Tue, 11 Sep 2018 22:35:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 389CC29C45 for ; Tue, 11 Sep 2018 22:35:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 602FB8E0017; Tue, 11 Sep 2018 18:35:19 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5D9C28E0001; Tue, 11 Sep 2018 18:35:19 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42EAA8E0017; Tue, 11 Sep 2018 18:35:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id 0163D8E0001 for ; Tue, 11 Sep 2018 18:35:18 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id j15-v6so13409295pff.12 for ; Tue, 11 Sep 2018 15:35:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=eji+HpPcmaKQpBHrFlLfdnHI7xruaaDZ0YEYbzrJ3TE=; b=NL0a02MYgsgi5m2F71L+ZzOIaX1H++5lM5q/hYwJijhDjE1ITMwQ/HADcW4sbQrtco Y3OL7h3tNaSPNvIkkrKuqvkyOtpTrnQ06QmOMmO/6PlbckG3o30aFbqj8Zgrcx5HoaZW F3y9K32lOHw/s36ndo86hS37mpYRtp/OZpEB2mJTWPNqcsgkzEk2XoIjbzqczz9RUPoZ WIfnePwwgmpvLWJIdXpdRt8Bj1DNw1G2Wg9RnxMFVkNP4F5SupQ3x2PX2nHkXFmbiDwX J47U+q8sC9xvjEDwGmSBjL6+2w9BTIhikbKtonLjWZubmKysj3MGBTwY2Usayy0Z7GBI kdJw== X-Gm-Message-State: APzg51BhRQcV7Whkgu4WvuM7O9+sCDaMTwZJDceHGIrcaRTtDq3hwICX u2SWcYWnZw8piz8XIwI55+uzKh2tAoEr3ZrIsLHvY1XQDRaSwuE7scEUg3DvvPsCA2RAgN/wJmy QB8y0xQ6iCqo1NgXQKf5DSYHLWtQz5WVxv9J0Hv8AG/5ARx/iW0qoOTUaq2H73A5I7sR04g5hLM 2ol7Q7xnkn8uh4nN+Je75750a3yc7fS9LYSHvv7dciNoEyfTuXPyvdDTwTdwmJs/UoezdhyEEnc G1TUSdyFQR1JMqtCPKkXFri+VqpOlzqUFhYyFGdqb0tF+pF0J0x4Y0zHk8IfL2xWkx6gLfM4AVN WoeQE169thMpwjJqgdOGK2fA9IVRD1Dr0vlEb8UahJfMBPp2boFZHS3An66fOvBxKqudnV3NhJ3 T X-Received: by 2002:a62:4255:: with SMTP id p82-v6mr31975582pfa.238.1536705318648; Tue, 11 Sep 2018 15:35:18 -0700 (PDT) X-Received: by 2002:a62:4255:: with SMTP id p82-v6mr31975533pfa.238.1536705317751; Tue, 11 Sep 2018 15:35:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705317; cv=none; d=google.com; s=arc-20160816; b=XDtkkJUVUzKY0Dolrk7e89xO3/SZB0Q2vhE9M3AvEh+iJBpJ7c9EHBb93F4sJ4ZYMh P/nPNkhzRejs4SY0qGcL2gZuR4lDMIXWx60LLN5pANiHRsvRJLQQXhvj+Mzd2aIdcgOp aH4trsWLpeX1SzDa2YxPlM79ba6YP4igjx/2aooLpHRMee06ihhu1kze9WG6Pt7pffXY Bj2xD6dUtcy8w2uH1MLFqHlDxD75vBx7NKjt10k+qhahoJqlvEnqkJXvyijZmgz96gsI T5l0G/HW7cDQ9enhGqHJtPY6yNRX4GghmUZiyQ/wh3gyRwDoqsUpmR4DscLDEJT96ETl xeaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eji+HpPcmaKQpBHrFlLfdnHI7xruaaDZ0YEYbzrJ3TE=; b=i6bytbWkJaGIvpm30HBjvsBRsCK9ltxTFgX4l4HaoAMRlxbmsA3GiD47nqqcVkcWdd B24gKReudAgcTbrIpMZCjQu3pncIm00pADFR6tUyuWFXOzbhycqod0uKOSkLR3aTJtsp /2K9YxSkS0rFhzmkmPnsHh/8KrlAG+6pFy/fdC14aQbVpvLXSY3g/auqNVXFHRGy91sz JxNCTcogb5fsdo4eHCWjClH7uVAPylMpIjS56RH/OnMoh2vk/2tCHKOoRVyNb1AG+P4w f7HirC5I6+rpOZF7ZFj20wMB/u0plvZZ1hysR8t7Cb1j0mxt2sFF9YjBJiLQicUPnz5G /Ptg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=V6JaV0kM; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id y7-v6sor3209868pgc.197.2018.09.11.15.35.17 for (Google Transport Security); Tue, 11 Sep 2018 15:35:17 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=V6JaV0kM; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eji+HpPcmaKQpBHrFlLfdnHI7xruaaDZ0YEYbzrJ3TE=; b=V6JaV0kMK2tUvcr/p+bxP1cESa3Nt9KfHZhwqy5KkjzNq+F58q7pT2Q342T0Z+ao4M TiAlT4ETJ4boBoLytop3fcL1qCFW5NIPsNdkzzzjXOTwYrEZCnMyfQXPzUz7n9RjiYet rg6/bcxHr4HiYoKST4ByRBzCziFOQigPWn3mBrG1cN7M5ldgph4r0ilQEbOsnvYXWQEt D5pP7KTj7H9Tf6ypifSokPSEPSM5RcSvYzmudE3hFWZpt5DZR/zQpAJknpkinkLlZyVc 1AhNajcoc4Ly/9WLDLEuYO7dUBZ6z9YY1us+CytjFxYY30xC3/KUD/vwp6L2a5SwraZB Wcvg== X-Google-Smtp-Source: ANB0VdbwL2JgvT1bTyhH/OUKtdNZ9mhxUqBIFjv/LfssMXIQieTiQV6WdOoGP9fq3XXMBky0Ff59yg== X-Received: by 2002:a63:b54:: with SMTP id a20-v6mr30884091pgl.280.1536705317160; Tue, 11 Sep 2018 15:35:17 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:16 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 5/6] Btrfs: rename get_chunk_map() and make it non-static Date: Tue, 11 Sep 2018 15:34:48 -0700 Message-Id: <898dc638c0323a2da9401f03f37db89a4df446a2.1536704650.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval The Btrfs swap code is going to need it, so give it a btrfs_ prefix and make it non-static. Reviewed-by: Nikolay Borisov Signed-off-by: Omar Sandoval --- fs/btrfs/volumes.c | 29 ++++++++++++++++++----------- fs/btrfs/volumes.h | 2 ++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index a2761395ed22..fe66b635c023 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2714,8 +2714,15 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) return ret; } -static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info, - u64 logical, u64 length) +/** + * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. + * @logical: Logical block offset in bytes. + * @length: Length of extent in bytes. + * + * Return: Chunk mapping or ERR_PTR. + */ +struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, + u64 logical, u64 length) { struct extent_map_tree *em_tree; struct extent_map *em; @@ -2752,7 +2759,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) int i, ret = 0; struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; - em = get_chunk_map(fs_info, chunk_offset, 1); + em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); if (IS_ERR(em)) { /* * This is a logic error, but we don't want to just rely on the @@ -4902,7 +4909,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, int i = 0; int ret = 0; - em = get_chunk_map(fs_info, chunk_offset, chunk_size); + em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); if (IS_ERR(em)) return PTR_ERR(em); @@ -5044,7 +5051,7 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) int miss_ndevs = 0; int i; - em = get_chunk_map(fs_info, chunk_offset, 1); + em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); if (IS_ERR(em)) return 1; @@ -5104,7 +5111,7 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) struct map_lookup *map; int ret; - em = get_chunk_map(fs_info, logical, len); + em = btrfs_get_chunk_map(fs_info, logical, len); if (IS_ERR(em)) /* * We could return errors for these cases, but that could get @@ -5150,7 +5157,7 @@ unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, struct map_lookup *map; unsigned long len = fs_info->sectorsize; - em = get_chunk_map(fs_info, logical, len); + em = btrfs_get_chunk_map(fs_info, logical, len); if (!WARN_ON(IS_ERR(em))) { map = em->map_lookup; @@ -5167,7 +5174,7 @@ int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) struct map_lookup *map; int ret = 0; - em = get_chunk_map(fs_info, logical, len); + em = btrfs_get_chunk_map(fs_info, logical, len); if(!WARN_ON(IS_ERR(em))) { map = em->map_lookup; @@ -5326,7 +5333,7 @@ static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, /* discard always return a bbio */ ASSERT(bbio_ret); - em = get_chunk_map(fs_info, logical, length); + em = btrfs_get_chunk_map(fs_info, logical, length); if (IS_ERR(em)) return PTR_ERR(em); @@ -5652,7 +5659,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, return __btrfs_map_block_for_discard(fs_info, logical, *length, bbio_ret); - em = get_chunk_map(fs_info, logical, *length); + em = btrfs_get_chunk_map(fs_info, logical, *length); if (IS_ERR(em)) return PTR_ERR(em); @@ -5951,7 +5958,7 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, u64 rmap_len; int i, j, nr = 0; - em = get_chunk_map(fs_info, chunk_start, 1); + em = btrfs_get_chunk_map(fs_info, chunk_start, 1); if (IS_ERR(em)) return -EIO; diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 23e9285d88de..f4c190c2ab84 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -465,6 +465,8 @@ unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, u64 chunk_offset, u64 chunk_size); int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset); +struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, + u64 logical, u64 length); static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) From patchwork Tue Sep 11 22:34:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10596435 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7222914E0 for ; Tue, 11 Sep 2018 22:35:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61AE429C45 for ; Tue, 11 Sep 2018 22:35:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5645329DEA; Tue, 11 Sep 2018 22:35:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7148F29C45 for ; Tue, 11 Sep 2018 22:35:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 258018E0018; Tue, 11 Sep 2018 18:35:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1E22C8E0001; Tue, 11 Sep 2018 18:35:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AD958E0018; Tue, 11 Sep 2018 18:35:20 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by kanga.kvack.org (Postfix) with ESMTP id A759A8E0001 for ; Tue, 11 Sep 2018 18:35:20 -0400 (EDT) Received: by mail-pg1-f197.google.com with SMTP id s11-v6so13010291pgv.9 for ; Tue, 11 Sep 2018 15:35:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=A6phYVM31YfWMc0OSMa9wU6yrWTSurXiNO96yCnyeWY=; b=PLyurGQiDpY8T6eEMRhAvLSldmxfxcqLu54vXGhb5ag3POyHSppd0nME5b3mnowIGL /DeNYmdmWHg8lA0uV2fONlUqfRgDw3pXTzqq2Zkiq+vW+HWZjng8s1DyehXTRTkGz5/j V+8gj1J5clNgeMQTjKt21QRkwJqaJbjxHqDuVRVERHu1vqb/6STYb4Xv76Hrcre6d8s2 g8r7jWyiSXinyW0iu+fjA/XGXKbMxfU37PjBUzz90TmJklNu1At5RChjMNq9HN9tX97Q o5knzdu3U2qFLY+hBwWbGC2UffsmRIc9ceyKsmnS26KU7B1B1+nqH40nO2dquYyr0rUk KBUw== X-Gm-Message-State: APzg51CCPK9hcMp61JkSNFjdP8ceAZDFteOVWIKRL9vcEcFVj1mON3YF RdkgzNDH8a9GjOi7rczOSBN/1E2Se20qy6+JorM+Vg86UWw+zgiA0xqIp5FfvsLjOtIXJqrD141 61LIFiGCGEtHMevE3SJF1qmaIFVS2+uASE1dY+MtjlEqwV0oloaXUfQp6aZiUoNvhH/vX34JsPF JCXdlAxTz17RIx0h9ZvF01bZe1AOAjXgI4aMAXvNKJUXdez31hgdKGqMT2GKkXYIkSWXmOpCZMW z03xX1bmT1biLZy5iAKTyF3Ubu1MbiETtz9PofIoFMYpNCpJGfeYBb9NwxQkSh+5RdpFfG3T89d Y1IOsO1X2Y2vnNA9EV0eeS59EkRLyJLEqTwdiPuWCAcLQ142Wh42xRTOs4/SGH3hSNakEcHDfhV p X-Received: by 2002:a63:f54c:: with SMTP id e12-v6mr30791585pgk.286.1536705320324; Tue, 11 Sep 2018 15:35:20 -0700 (PDT) X-Received: by 2002:a63:f54c:: with SMTP id e12-v6mr30791514pgk.286.1536705319050; Tue, 11 Sep 2018 15:35:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536705319; cv=none; d=google.com; s=arc-20160816; b=RK6sz/HdVIaBPu7v7kxyny2xbgeGgGH9jDzBOqHKJ3jDwpzqVlQPlzdyH8z+zmL2pl mEa9nOUKgvehgGgVwc+8ht8ouRxNUtYt/fSf0GcrjVIwlozQoEgpyMaUw3FswNUxhtmb DadmOA1k1D0LhMpfsJ1aKha9INH7Q40/oT969Doz9RT2luDYu4Ti8bQlchowXv0nioOP vSm1p4L3wMejbFPr9GW9A1Q1a5lVWpSJLCskEwxmw1QBYfbexFEjsHLZFkGzsYNaUx26 O62RTXJtbtQ3udrdDUaUnWvks3qFZvZIv8QcLMOW9dYXzsJxMlbt4pnfhcaSPtMUzdN1 xKpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A6phYVM31YfWMc0OSMa9wU6yrWTSurXiNO96yCnyeWY=; b=x5QYPxfgCZTixV9OUlS7P2Nz+XrMM4UZ6DcJgBoMX7kwvEEOO2Wrqo/b/OOh+k3u6e 5hadsmpQdOFFnrsWdG4YjizGac68Zlm6Lm6skpaCvykzCIDK+I6mBxjMX7UzG9KIpTLF A31R0MS9QHZ9Dl+OzjL8+Da+cSW2qR8ALZl5du70nb8LJrRJv2W8uJUURf6n85gEknjJ FapKMxkwRP7Y6RKKwIjYq64+FW8AIgPtEDBgumhLI3zV37QY1Ni48TR8AeT9y02AJpef 5UDz4fx4TshlmMAC+nSLQZc38VupLWJMkIz2BQPK9Vn4mExD1GjrzsJUmjXtO5qwv/Op 9N1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=fcQ7TxyB; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id m10-v6sor3575620pll.123.2018.09.11.15.35.18 for (Google Transport Security); Tue, 11 Sep 2018 15:35:19 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=fcQ7TxyB; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of osandov@osandov.com) smtp.mailfrom=osandov@osandov.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=A6phYVM31YfWMc0OSMa9wU6yrWTSurXiNO96yCnyeWY=; b=fcQ7TxyB54P6/rWFuBCAYvllKhbo+w6uzW88ruQuHqRePWPZ6ecaEGMYqvISqiOvuP TbFRHjCT9tAyNKyarAElRfZw4ggqlwto5GJ9XJEWrePgOJ7VjhN+U2C28qIzSWSSJNtB 5gRAa09wf2bfDCS6+f+P24ujcOoP9S/8d8uTDOYXUWJCOEmNytBqq2eM6EWK53awyR33 wcKURCWfXM/RtB8BbPFOO/deV/EDXjUGsKnGzHJOAzgFGW7ch7pNg2ISy4bXy5iaM3LB P42wJS8wCEdB3/zJuPivZyHPYGstPamVrRNhi2Sogj6OCJaTo/UcbG4F4T/JbxOuTUg+ /pkw== X-Google-Smtp-Source: ANB0Vdaqtlr2BggzCN1XPoJ3rzjz9Q3l/kqF0RJwmQrpzRBAFnCaNjH9im14YCdm0cr4OMsEmHD9eA== X-Received: by 2002:a17:902:b7c5:: with SMTP id v5-v6mr29983155plz.49.1536705318373; Tue, 11 Sep 2018 15:35:18 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::7:13b4]) by smtp.gmail.com with ESMTPSA id 186-v6sm18710176pgg.56.2018.09.11.15.35.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 15:35:17 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v7 6/6] Btrfs: support swap files Date: Tue, 11 Sep 2018 15:34:49 -0700 Message-Id: <61def3687f0309c9b846677c8d112afc4d6d90f1.1536704650.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval Implement the swap file a_ops on Btrfs. Activation needs to make sure that the file can be used as a swap file, which currently means it must be fully allocated as nocow with no compression on one device. It must also do the proper tracking so that ioctls will not interfere with the swap file. Deactivation clears this tracking. Signed-off-by: Omar Sandoval --- fs/btrfs/inode.c | 317 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 317 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 3ea5339603cf..0586285b1d9f 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "ctree.h" #include "disk-io.h" @@ -10488,6 +10489,320 @@ void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) } } +/* + * Add an entry indicating a block group or device which is pinned by a + * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a + * negative errno on failure. + */ +static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr, + bool is_block_group) +{ + struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; + struct btrfs_swapfile_pin *sp, *entry; + struct rb_node **p; + struct rb_node *parent = NULL; + + sp = kmalloc(sizeof(*sp), GFP_NOFS); + if (!sp) + return -ENOMEM; + sp->ptr = ptr; + sp->inode = inode; + sp->is_block_group = is_block_group; + + spin_lock(&fs_info->swapfile_pins_lock); + p = &fs_info->swapfile_pins.rb_node; + while (*p) { + parent = *p; + entry = rb_entry(parent, struct btrfs_swapfile_pin, node); + if (sp->ptr < entry->ptr || + (sp->ptr == entry->ptr && sp->inode < entry->inode)) { + p = &(*p)->rb_left; + } else if (sp->ptr > entry->ptr || + (sp->ptr == entry->ptr && sp->inode > entry->inode)) { + p = &(*p)->rb_right; + } else { + spin_unlock(&fs_info->swapfile_pins_lock); + kfree(sp); + return 1; + } + } + rb_link_node(&sp->node, parent, p); + rb_insert_color(&sp->node, &fs_info->swapfile_pins); + spin_unlock(&fs_info->swapfile_pins_lock); + return 0; +} + +/* Free all of the entries pinned by this swapfile. */ +static void btrfs_free_swapfile_pins(struct inode *inode) +{ + struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; + struct btrfs_swapfile_pin *sp; + struct rb_node *node, *next; + + spin_lock(&fs_info->swapfile_pins_lock); + node = rb_first(&fs_info->swapfile_pins); + while (node) { + next = rb_next(node); + sp = rb_entry(node, struct btrfs_swapfile_pin, node); + if (sp->inode == inode) { + rb_erase(&sp->node, &fs_info->swapfile_pins); + if (sp->is_block_group) + btrfs_put_block_group(sp->ptr); + kfree(sp); + } + node = next; + } + spin_unlock(&fs_info->swapfile_pins_lock); +} + +struct btrfs_swap_info { + u64 start; + u64 block_start; + u64 block_len; + u64 lowest_ppage; + u64 highest_ppage; + unsigned long nr_pages; + int nr_extents; +}; + +static int btrfs_add_swap_extent(struct swap_info_struct *sis, + struct btrfs_swap_info *bsi) +{ + unsigned long nr_pages; + u64 first_ppage, first_ppage_reported, next_ppage; + int ret; + + first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; + next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, + PAGE_SIZE) >> PAGE_SHIFT; + + if (first_ppage >= next_ppage) + return 0; + nr_pages = next_ppage - first_ppage; + + first_ppage_reported = first_ppage; + if (bsi->start == 0) + first_ppage_reported++; + if (bsi->lowest_ppage > first_ppage_reported) + bsi->lowest_ppage = first_ppage_reported; + if (bsi->highest_ppage < (next_ppage - 1)) + bsi->highest_ppage = next_ppage - 1; + + ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); + if (ret < 0) + return ret; + bsi->nr_extents += ret; + bsi->nr_pages += nr_pages; + return 0; +} + +static void btrfs_swap_deactivate(struct file *file) +{ + struct inode *inode = file_inode(file); + + btrfs_free_swapfile_pins(inode); + atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); +} + +static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, + sector_t *span) +{ + struct inode *inode = file_inode(file); + struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; + struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; + struct extent_state *cached_state = NULL; + struct extent_map *em = NULL; + struct btrfs_device *device = NULL; + struct btrfs_swap_info bsi = { + .lowest_ppage = (sector_t)-1ULL, + }; + int ret = 0; + u64 isize = inode->i_size; + u64 start; + + /* + * If the swap file was just created, make sure delalloc is done. If the + * file changes again after this, the user is doing something stupid and + * we don't really care. + */ + ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); + if (ret) + return ret; + + /* + * The inode is locked, so these flags won't change after we check them. + */ + if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { + btrfs_info(fs_info, "swapfile must not be compressed"); + return -EINVAL; + } + if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { + btrfs_info(fs_info, "swapfile must not be copy-on-write"); + return -EINVAL; + } + + /* + * Balance or device remove/replace/resize can move stuff around from + * under us. The EXCL_OP flag makes sure they aren't running/won't run + * concurrently while we are mapping the swap extents, and + * fs_info->swapfile_pins prevents them from running while the swap file + * is active and moving the extents. Note that this also prevents a + * concurrent device add which isn't actually necessary, but it's not + * really worth the trouble to allow it. + */ + if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) + return -EBUSY; + /* + * Snapshots can create extents which require COW even if NODATACOW is + * set. We use this counter to prevent snapshots. We must increment it + * before walking the extents because we don't want a concurrent + * snapshot to run after we've already checked the extents. + */ + atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles); + + lock_extent_bits(io_tree, 0, isize - 1, &cached_state); + start = 0; + while (start < isize) { + u64 end, logical_block_start, physical_block_start; + struct btrfs_block_group_cache *bg; + u64 len = isize - start; + + em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); + if (IS_ERR(em)) { + ret = PTR_ERR(em); + goto out; + } + end = extent_map_end(em); + + if (em->block_start == EXTENT_MAP_HOLE) { + btrfs_info(fs_info, "swapfile must not have holes"); + ret = -EINVAL; + goto out; + } + if (em->block_start == EXTENT_MAP_INLINE) { + /* + * It's unlikely we'll ever actually find ourselves + * here, as a file small enough to fit inline won't be + * big enough to store more than the swap header, but in + * case something changes in the future, let's catch it + * here rather than later. + */ + btrfs_info(fs_info, "swapfile must not be inline"); + ret = -EINVAL; + goto out; + } + if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { + btrfs_info(fs_info, "swapfile must not be compressed"); + ret = -EINVAL; + goto out; + } + + logical_block_start = em->block_start + (start - em->start); + len = min(len, em->len - (start - em->start)); + free_extent_map(em); + em = NULL; + + ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL); + if (ret < 0) { + goto out; + } else if (ret) { + ret = 0; + } else { + btrfs_info(fs_info, "swapfile must not be copy-on-write"); + ret = -EINVAL; + goto out; + } + + em = btrfs_get_chunk_map(fs_info, logical_block_start, len); + if (IS_ERR(em)) { + ret = PTR_ERR(em); + goto out; + } + + if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { + btrfs_info(fs_info, "swapfile must have single data profile"); + ret = -EINVAL; + goto out; + } + + if (device == NULL) { + device = em->map_lookup->stripes[0].dev; + ret = btrfs_add_swapfile_pin(inode, device, false); + if (ret == 1) + ret = 0; + else if (ret) + goto out; + } else if (device != em->map_lookup->stripes[0].dev) { + btrfs_info(fs_info, "swapfile must be on one device"); + ret = -EINVAL; + goto out; + } + + physical_block_start = (em->map_lookup->stripes[0].physical + + (logical_block_start - em->start)); + len = min(len, em->len - (logical_block_start - em->start)); + free_extent_map(em); + em = NULL; + + bg = btrfs_lookup_block_group(fs_info, logical_block_start); + if (!bg) { + btrfs_info(fs_info, "could not find block group containing swapfile"); + ret = -EINVAL; + goto out; + } + + ret = btrfs_add_swapfile_pin(inode, bg, true); + if (ret) { + btrfs_put_block_group(bg); + if (ret == 1) + ret = 0; + else + goto out; + } + + if (bsi.block_len && + bsi.block_start + bsi.block_len == physical_block_start) { + bsi.block_len += len; + } else { + if (bsi.block_len) { + ret = btrfs_add_swap_extent(sis, &bsi); + if (ret) + goto out; + } + bsi.start = start; + bsi.block_start = physical_block_start; + bsi.block_len = len; + } + + start = end; + } + + if (bsi.block_len) + ret = btrfs_add_swap_extent(sis, &bsi); + +out: + if (!IS_ERR_OR_NULL(em)) + free_extent_map(em); + + unlock_extent_cached(io_tree, 0, isize - 1, &cached_state); + + if (ret) + btrfs_swap_deactivate(file); + + clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); + + if (ret) + return ret; + + if (device) + sis->bdev = device->bdev; + *span = bsi.highest_ppage - bsi.lowest_ppage + 1; + sis->max = bsi.nr_pages; + sis->pages = bsi.nr_pages - 1; + sis->highest_bit = bsi.nr_pages - 1; + return bsi.nr_extents; +} + static const struct inode_operations btrfs_dir_inode_operations = { .getattr = btrfs_getattr, .lookup = btrfs_lookup, @@ -10565,6 +10880,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 = {