From patchwork Fri Sep 7 07:39:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591777 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 9C018174C for ; Fri, 7 Sep 2018 07:39:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C2282A630 for ; Fri, 7 Sep 2018 07:39:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7FFB72A659; Fri, 7 Sep 2018 07:39:42 +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 C73662A630 for ; Fri, 7 Sep 2018 07:39:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3732E6B7D2D; Fri, 7 Sep 2018 03:39:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 256496B7D2E; Fri, 7 Sep 2018 03:39:39 -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 0A7296B7D2F; Fri, 7 Sep 2018 03:39:39 -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 B9EF86B7D2D for ; Fri, 7 Sep 2018 03:39:38 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id e15-v6so7192762pfi.5 for ; Fri, 07 Sep 2018 00:39:38 -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=nJdPUjC9Bf1blafYhiqq5pLe1RiCLFQle+k4KAN/0FKpbjYJ9BSVC9x0gfzWtp7/7p Wiu5JjUX3lx3Q+CvMVyAF66sLQ+LJcwZCUJnniDl3drsnhnCKMpKkAfu/UdnPTMifMbX y0GmN5eZkPZXqDESTnJxwUn7dDgKixEMy64JkJ+Mp4+x2F9mNhHleERPF3INRdQjnlYk 2oZa8BX+QInUA1WlJhVahI2oFiBcI8kRM2/QzH6+ka83RhDpEI2c7KyIqsc04Zsgenny OmzHwhgV+hhIgai+0ZMiZu+11cSt8zrHE1iP3bBW6tALBS/7YlcGyQkdidgB+Wa9gdXU NIZQ== X-Gm-Message-State: APzg51A7CWNN5js7PQdkNYz3anm2QifLEImY6Gx1v4f6wBjjcYSSqVte uod9RJx5AcxAphMqcG/OOFgMz0WXXD3wiRmJnfcFHF8NAhVskisGUMvVi/im7/rCS/bWdgYtc8o osC+QKTXnSn+XEz/MLLf0+R2557IqjX2SauMyx3niJXMLILhSHCewpmI0tnC3t4rbgvP7uJY1SE 3PH1r5F0BB11+gd69rIxU27g2JpM/iutfKScglCM2LGaW/00WkYyzV0eL2G5LhhBCZyUoH2NTrm OBz08bWs5k7B4aErB0+XIxo7lW0AHtIYiPBJpGnjzW7LBPn5qWcIwZSrDaEdFX14NRelQRGliOT ndsEf7eDhNC+YLHrwaX0Fzbc5Lrt2KYcI+DryslfPXcs0RvhMUjJgdkFaV/9I2mug0u7GLZGDjJ 5 X-Received: by 2002:a62:234d:: with SMTP id j74-v6mr7064101pfj.106.1536305978409; Fri, 07 Sep 2018 00:39:38 -0700 (PDT) X-Received: by 2002:a62:234d:: with SMTP id j74-v6mr7064064pfj.106.1536305977591; Fri, 07 Sep 2018 00:39:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305977; cv=none; d=google.com; s=arc-20160816; b=vNKul/fiEcs8NHzTYSi2UowxdU53MFKnS7tJCHX24Mg1Q7zVNQ29zbU3njv57Qvb5z mfK8p/f5Gs4kdIh5YVpkI2rruiW0PT8XXxYyjNBc+fRqRfpGcd294cOdgVg4DlPrXqz1 lRWITfR26NzSBHLJBjbqUksCthzbMHbewn9tEjMdqG11aLiULVkEBz+yXfSVK4d8KD8t 2O1vcdpg5ixgh4q0IDB39t4/2aJ+P58ldISyPUh+dw7jb1w5Tuk9VC8XbXXd7tDaklAZ 09fR3NbJN0IDtYTC2/Xz8IKV/sYi1VewdXvJIXlfpAKgZETJTbSTOBAurcLRvnUoYtlA l3Tw== 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=J753ll+wJeBewARKHgjA1QaFZUeCF1UfXVaq9PeyOAS9f2NDeSpCV2o/yPfeWxWBMi sbwpW2tR9nGzy0n78cPjXzcAp2vYZaqDben2Sdx/lOU4mVhA+9oSG3I6ow1Gf4O6JHmk XaUa30/lMKBEVS/dyFbaS9Ifzy9yZCjv/IuS2/loTiWbCdLthafjxFhE/biRf0wDcpS9 1OG7LEdlQoKpyeh7bpMKGAmUPg7mOrt1u4RuDAZ3CnnvesRKNW6TJ8JAtuG7tXpFyUmc AYD/fFmD36jAF8Uc36UKcy3x6U8FmHRFAqXJbk/aj4DpnEQjrsr3Gfkfv7KONA4Y1Uta VDGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=cI2xbv+h; 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 y4-v6sor1511189pfm.45.2018.09.07.00.39.37 for (Google Transport Security); Fri, 07 Sep 2018 00:39:37 -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=cI2xbv+h; 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=cI2xbv+hDJwZ0+3IBkv98TFp0Mq011U3+dhk6+LEGnZR2+yEgpSYJQTwMGOTYji41X VE2i1dqplfW2VJOWjWnktzuq0eUhF8pYJlItm2THrdSGBIgepankPwMYc4FKMB3nTkET CqzrqCei014oXVfMF97JEEESUHZEdqf7vdBSiYMV5YyQ25wRQt6n85LIMlv1xJ/EChMK tiVjt/M6HvTcs9GemKAJ9rgVd1fRHUYiHWBkg2VYXI7B+r2H/jQhvvjUqaUoRnSMAxBo SBwOh0C3JS897TbotD0daNfTTtMNX/1eCrfx/nAbqm1DCmmdB/Y9PNvWHlrtGgxD05LT ps2g== X-Google-Smtp-Source: ANB0VdYk4kVHUBK0zs1Tsax/yfZqaspTVo/Ra4qhRcrYT73esq49m3sue+qpeCwRHAfuS1AjmLyepw== X-Received: by 2002:a62:1e81:: with SMTP id e123-v6mr7107456pfe.24.1536305976984; Fri, 07 Sep 2018 00:39:36 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:36 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 1/6] mm: split SWP_FILE into SWP_ACTIVATED and SWP_FS Date: Fri, 7 Sep 2018 00:39:15 -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 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 --- 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 Fri Sep 7 07:39:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591787 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 EBCBD6CB for ; Fri, 7 Sep 2018 07:39:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF1352A63B for ; Fri, 7 Sep 2018 07:39:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3C3A2A659; Fri, 7 Sep 2018 07:39:44 +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=unavailable 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 79CFD2A63B for ; Fri, 7 Sep 2018 07:39:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0AFC6B7D2E; Fri, 7 Sep 2018 03:39:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C3E616B7D2F; Fri, 7 Sep 2018 03:39:39 -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 B08436B7D30; Fri, 7 Sep 2018 03:39:39 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by kanga.kvack.org (Postfix) with ESMTP id 604B46B7D2E for ; Fri, 7 Sep 2018 03:39:39 -0400 (EDT) Received: by mail-pg1-f200.google.com with SMTP id m4-v6so6821318pgq.19 for ; Fri, 07 Sep 2018 00:39:39 -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=HTv8kWz+Aqsu3n7jJeKsJU7NkIHrAlKnVL51ia3sq4/6CxZ4q1NFswIGktSIrHfudB 0B94qFHAV/n7djefO/KWgknX32wX0HTjOR6w9AnvUaZSX5rmcy0jGkfjv/9P3OCWyKt7 NKtEFDSmaumKddHF4nQqYFhRF1GpbWu/+/DC3wkM0799DKjNbcPK1j3ldpPpaTmjqPDH 6Ojirwqp0jEntdc/uBDWe92RTnkn6FkgnZ5wSxJOiUNHteD1rhhpBraHdNH7TfPKpNhw MW62oNTuioTRWgKDbm7yqJyS11uCtEi14ZIC2IVFzJt6rMKrzID7MzKSuZZ48pQs8CrY spnA== X-Gm-Message-State: APzg51Az/DHi0kMPcNM7KXZOIcucTsK5JO/GRlYsTBDLXg4jnRPIMG1D nv+ggkSY9FtBsMZpdd9dqJxlP3BD4QiKf5vqiS5PRWMKaB59aTyfNhNpXOsax59jmCB2zDm73sg WvFNpJuRv3ofX8mNRy3IFFgFznKg9FFPhb3IaMCi0In4tQBDdDC9lM2w3pzrZpJlqEMCJns21Lh yIAw6N9meNRkOdnrgWp9H/yfR0AkZo5goyQJ/nUGJxm9t2ePlt8bnRlp+TDb7E5qh8YO4aoHj21 NDSKbhn+DxE576JMGl3jRS7UoOeAizm6AANeStgPrxlKIebIhGR5q1fH2UACvqLFHwZ3w0bqxuj pjO96HLA6eGvjxyaFzLKsBgBEhUgqwpttEzasyjAQABCWp2C8grvAOgXLuUXLwpY1DvQy8/tEAE l X-Received: by 2002:a63:ab0c:: with SMTP id p12-v6mr7009335pgf.190.1536305979085; Fri, 07 Sep 2018 00:39:39 -0700 (PDT) X-Received: by 2002:a63:ab0c:: with SMTP id p12-v6mr7009319pgf.190.1536305978407; Fri, 07 Sep 2018 00:39:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305978; cv=none; d=google.com; s=arc-20160816; b=XdZJeUbQ55MTx3lq8LCWq5vd1SYEEDz2F+l9dKmX0orpKwBC+bdfPwt9irGz+h4lph 0XKRqReL/l+2Vw8DLNIZUpavjzqFK/HTv49xzLE8Cs1BeTQClosgqDrdWg9gwTc1aQeI 57XPCGekfe1dvXxhBOd85JepHS7Is565vTz72rVX2DF1GcIXLhlixHrfxqd0x9hsWQBG Glwi7LxrEBf3ErOv6ucKvGCmc4/bzCxe1n7JQTxcU0Q1UBWniEKXqx2wzxhifySnWjzn QHoLtT2L3V7NGFfde8DTfC9T0yFjYEeps3RYOpYwxY2tk3HIkl/oLQ+iu1RC0L83whRp xxxg== 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=m5EeKvzoXTK7QxcwmgruKOPRQZF+tn4LdnNCmuJO9xxa4YUNexIahoAvupFQKZUGNJ RYTPtP+ZOJEChjx73gcnk8u+C70mf4wl/eSLCpHX23knFskpoowDcxeXAnQ0BHKKCNS0 OwZ3ajNqnEWrLNvQMps1O9wS+BqNEKYHod8QbwHtB97lIGmDnoDYxdXyzPWBdXUMe5KF U7+gWUQQDqVEiyPu9CVWP2ECxTTqGyQm85JvSfM9ruCC0sDNs9PbUOnQAf/5JLLEmRhi 3Z8suf/MFr3sisHlye51mpA4dsLiOyPa1ItFAvCZCuNQnPWcHvbRP+OZpU/wQtXRaCRJ xlIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=SL0Ru4nA; 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-v6sor1634513pll.123.2018.09.07.00.39.38 for (Google Transport Security); Fri, 07 Sep 2018 00:39:38 -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=SL0Ru4nA; 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=SL0Ru4nAUG80M4Q6RuPg1eXSftKHhUa9quvYWPi96z6XJG1yr4A3ONsLtqZbw92WI9 V1V7MbXlMH54XMMiKPPcKiInZsgouBTuSeYal8/Gxcp/eKTVk5fcda3W2AXcuC8HkoAX yCqGnCxDdN/eZgiFYwI/A1VOpJ/VV7+L6QH0/0YT9MOky4pWohVMFLUY2By/syBadiL1 qId7PElcUxlBujBNjInosydETdU5Ln4iyCx5jS8Pn9nKGXnwAzLmdx1E82hxdLJdTaOF pwblyepayQ+yAcwVygjwbJVYWbBVMQkTWXuQ4sJj1Y+nyHOZHpmIuuRoujLYiWq/0fcn p5nA== X-Google-Smtp-Source: ANB0VdYKJZXmRnl+PeEmhotnNPgxwupVLhpFoLRHFhWS7YdCCdaVdtmoI8fjEjqYN5cvu68QzB0hew== X-Received: by 2002:a17:902:558f:: with SMTP id g15-v6mr6689435pli.38.1536305977909; Fri, 07 Sep 2018 00:39:37 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:37 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 2/6] mm: export add_swap_extent() Date: Fri, 7 Sep 2018 00:39:16 -0700 Message-Id: <6846192edded07fb0cccf11ef37aebd4822275a1.1536305017.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000019, 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 --- 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 Fri Sep 7 07:39:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591793 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 B064813AC for ; Fri, 7 Sep 2018 07:39:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A295F2A630 for ; Fri, 7 Sep 2018 07:39:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9634A2A64E; Fri, 7 Sep 2018 07:39:47 +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=unavailable 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 1F3E02A630 for ; Fri, 7 Sep 2018 07:39:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D6C36B7D2F; Fri, 7 Sep 2018 03:39:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7626B6B7D31; Fri, 7 Sep 2018 03:39:41 -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 5B1DD6B7D32; Fri, 7 Sep 2018 03:39:41 -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 13C6B6B7D2F for ; Fri, 7 Sep 2018 03:39:41 -0400 (EDT) Received: by mail-pl1-f197.google.com with SMTP id n4-v6so6708394plk.7 for ; Fri, 07 Sep 2018 00:39:41 -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=fGeS/RA0COb/MAZJABuUvj60bIdFSG2mdhyplz+1P03e68caGFcGBwSjXftcW7Hk7U 4NJBipLdKGDDXsJ/OGJphyGdRpMUHVgUgSu+hMMycsvSSFj6yp34Rpci11S4tMHOWBv8 o+4otGNPdtZibdi6ROJMaUCznHs6bJnm4zy72znCoj5c6fSSjU+vPANdqeo74+s3gKiv WtP62LEQFYyfAkmvUfXbW0p5HtWqvg/T9YmniQ7UJCTemmq3g9sIvU1a4bPYvOBGmEMT aQOKP9Bp8kvAfQpn3kQcLmRLzpnv57gs9LLMA3wiQfT+4q2qBd71W0jfN6aAw0PXdVdV xMAQ== X-Gm-Message-State: APzg51BXVOTnD8on9C+iUYMWuoDcGT4RuD7lKKDGeJJHk0g4U9FcYzzl KHLXpDJwlag6Dn69bGs9a7Kfs6mtA/AypNIhqtDNsmounXgA10d4olsmGYMypiempgJO5uVtfJT Ojnya4stQVBYpzxkVpBOgP1j2KHS9s06fhKfyTcDOQQW8DLCEU3bKIp+n0V3qTZ5sQiZdZGVPBZ MM1AT7qXyVqgW1gZd+84Am8j58cw1Jw2qRA3Clr7xBwxqmpuYGW+ROORwYnxjgSrxfbMFJDu6Qg UsHwN5iIolXu1z4Z1JfO15fBNM75toJNF9Id/RN8s1P66LUrPJA1DVE8kJSNz3rf1PG1l5dtjVx 7o3U2zHTiPWeaZEDQSG1i60totLQhCrb14E/G+aYFY5bpE3Csty1vCADym6rRHpdfP4zbeLC4me e X-Received: by 2002:a17:902:4001:: with SMTP id b1-v6mr6632213pld.312.1536305980696; Fri, 07 Sep 2018 00:39:40 -0700 (PDT) X-Received: by 2002:a17:902:4001:: with SMTP id b1-v6mr6632163pld.312.1536305979482; Fri, 07 Sep 2018 00:39:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305979; cv=none; d=google.com; s=arc-20160816; b=svxT8v8ylGP5gFBIUpKmsFxNbs+YP3du7/K8PhysIvmpmtXwfbZpBwuZ58fesd0neU 46GrUJTdQY6eO2oOv8vZ4JmEiV+9yVuPthWvreylHIZf1XfWNSM20AiCSdFDxgh6rylr rlUpXyzTqRL/65AYlv/aAqBaV/Abje1DvIO8KTgGBM+uocnbgRdpHWsisXWmipZrNV3v /2SNyYqGdBP88++kQTu/GpWzLAn8vPCKhVof0Xt1VN8dWPMe+kBkq7qpa/i0rMnsAE1c MR/xVqPFzB+ghZvmuMe0WHLUsG18HKiFYw8S67DFnUz595L8jL/5CY6Z0dp1w5/QBtMn pZXA== 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=Nbn4OtRkx+92hBjmQMOnLcnI2jt4v9tLFY43db5b7dC1EKqCuxOpjrwWrfrgcacALl 2mNd7VwxLr1jcvVCurry76D6tMm3db4FWqV9JtnTiRnQPUqKkbERtFfIlPdJtbrjbvWh A0Nei8BskPhWbWxNhKNJhOFVCx2PWNAOLGmy5Sg6RmX/pUxHbisGv4+/EpNGDWjCtFkA AF1UrIqn7wvKvAQKlpQM7O6v/4QDb3eu+dEkedad41xTweyqrzrChO4j3BrNsDfZ+xib 6Em2dxszDMetiFTL1xzjEuGqzjf8r0qmD4fws46teNCp1JWJiP9JUV0NBEFyXPjYDFM8 zrgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=RoRVEPPU; 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 q2-v6sor1652825plh.47.2018.09.07.00.39.39 for (Google Transport Security); Fri, 07 Sep 2018 00:39:39 -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=RoRVEPPU; 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=RoRVEPPUOEAvIxvPOl1OeAmw+mD7pTOuntBHzO1lpPqrDvJQsF3KRI/e1/FUjbZB5O uSqocwN93MugyqZWn5O0oU04KisGpZMrOHIbmM52LZH2GQBYPy0qezGr2WyB46+/mG4E 0TAcmH1I3YOuDLIDs4mEjc+UTumEvLQ1ncSn0epdzMIUDqTf+xvNw+UjbsAvmQxUxXBN BnemE0viWHvbslOX5s2xeblL+HXMVBU0o83U920zUAjS1PhEHKiY7DFwjJQPUA5YhhKk 3KJ+dGJnUdNblfLQQx1pMktUO/kD3PDkuqdEsgfjODwZjcw9aSeftE4ZcRAzZP64up4W banw== X-Google-Smtp-Source: ANB0VdbxxlwkXZ1lCGauNltUS/aaH8hbS2nFbc42eL3HhyOtujug8aQckD4zqeYM2kBTMquVV2NvVA== X-Received: by 2002:a17:902:7587:: with SMTP id j7-v6mr6707161pll.256.1536305978927; Fri, 07 Sep 2018 00:39:38 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:38 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 3/6] vfs: update swap_{,de}activate documentation Date: Fri, 7 Sep 2018 00:39:17 -0700 Message-Id: <1cb226723bbec23a8a8d4c05368188b3c16522da.1536305017.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 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 Fri Sep 7 07:39:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591797 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 0F8986CB for ; Fri, 7 Sep 2018 07:39:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 021852A630 for ; Fri, 7 Sep 2018 07:39:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA9A22A63B; Fri, 7 Sep 2018 07:39:50 +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=unavailable 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 2B5542A650 for ; Fri, 7 Sep 2018 07:39:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D8296B7D32; Fri, 7 Sep 2018 03:39:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 589416B7D33; Fri, 7 Sep 2018 03:39:42 -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 4552F6B7D34; Fri, 7 Sep 2018 03:39:42 -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 EE1286B7D32 for ; Fri, 7 Sep 2018 03:39:41 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id p5-v6so7127447pfh.11 for ; Fri, 07 Sep 2018 00:39:41 -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=xETjaS2qBDXBCnMLGIpNkv52DTAemV2cBklyWyJDvjI=; b=mfVQ/jGG8zVHHy/SnjV9/QrGeAHiGbC6RLji/w5XLGyBceOauNk3fchUedA1mJzAAZ lWDtd7o0Sln3goDU8wsfB13i3MiDBp/lNKUe37FxuGXDuwyM8GNCz49nrXGIomQghfHf nrcjz7cd7x0Kx/F48Wzn8wBLY283PlRSIqNd6Vkq3QPalZrwVcb8EfxOH/7LdhHNojTN KYj5CM5cnlRDHcDSn/WCuT0NWTh3c6c/F27oqZB4kp2QxuXeqzwKcKg7Gx8gRH2DLXoD 7qu0HQrd6MvobN4juF7MUdXjbfY7qYBkuX/bmuTenx697RZmutcnWVgN9HjoAT5pr6v5 yTuA== X-Gm-Message-State: APzg51DkUpgw+34oUDSXTp1GgsEW24jgymOz1RuFBjknKv0nLnjjUvS9 kdqfhQJnhOnMtNTcjBBOnGhmQGPmGRHMYzntZMVdLUTtUEpWC9xqVRc+BwS162bh67ECwgtaIuS uzYg2/JIvU5d4jh2avO6ai14M8/YLMjdFBO7VJAza4WCUcc5Wsat2l1ACa2RdzhWaAlS4DGIhwc BS/LDEoE3o5ELZPBMvXmw7a9aH46RTqZNUD+zILTPcLPDdj2snUFS4r/y/1iTkv9vH9nGBdX6e3 otsytmvG1n/fbaI1DneGG6wiiFZWFB5fFcfg1cG6rJZ+/FYSx2ddrwGc0L0vs8P6bziwt8foKqz t00SQnxj4/HJlKiD2tPZzLvUs0l+oqe8QXALCGJmqiTP/1l5Aa2REGpQ80cw15AoFiQbO/1SkcP p X-Received: by 2002:a65:4849:: with SMTP id i9-v6mr6706876pgs.350.1536305981609; Fri, 07 Sep 2018 00:39:41 -0700 (PDT) X-Received: by 2002:a65:4849:: with SMTP id i9-v6mr6706827pgs.350.1536305980507; Fri, 07 Sep 2018 00:39:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305980; cv=none; d=google.com; s=arc-20160816; b=vwsuFJBPhDFIu26jbw009wJt1XxNifT+ZGBFEULcewnYq49UUws/bHgxNh5wjZnDlq NmEwUumIUHmqvAV9JeAP+N4QXLutlLXbVID86dyh/SMYl0cTXOfy8Ktz+VRHZoCqR0Lz nRx6+T9dNucfFDwlg5JtwA9/Pjowo/He1VFccvrfG9NKu5Adu2Eib+lXiXYw3smULhzG 9TqgDbobpuxqcglm0bgmZImeF0OyYnfyv+w16CSVdhmzSzm7nE+qr+CiSQqv3Tx3ew51 U6GEA/bBc7wE5Q+23YhAm5iFGxY3QdWy9khCj3FzW4TzMKqkSUsw+DAFYK2pSWE3NulB AYWQ== 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=xETjaS2qBDXBCnMLGIpNkv52DTAemV2cBklyWyJDvjI=; b=JWRgT+QLZc0Ymy+5MGj+abekDiaEfh2W6DVZ93vYTTFYmJhrkvgENWSq7lVUY66ejf LDVUqSWSc2QE89kSp2tEBp+Kl+s/B9VbqFT1IofGSU8PEsnoK/6nuaFgfCVwaE9t5/f/ FdtwrYA4h+jN/r/o4t+/2EXHWiSIXfptjm+KzXa2vbm6jWOWqcnNhpHzgDO58ZmLfuIf qn6xFu6MF4wPJD+t1+AECpHkOOlToesXJxSRT3KrJzy1/jV3PHmyBiRHiqw8XOnXWEkY jr+y7tGKgj0CvzEHvNy7CSo0q8rcWmRT+ofMYjv72tbKC+mE50unkPTRL7bdAtjpOgNW b3Uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=So4pczDU; 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 u8-v6sor1660190plr.15.2018.09.07.00.39.40 for (Google Transport Security); Fri, 07 Sep 2018 00:39:40 -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=So4pczDU; 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=xETjaS2qBDXBCnMLGIpNkv52DTAemV2cBklyWyJDvjI=; b=So4pczDUtQdx1iOILabsHS8bXuPIH10HvELsDXQuvKc/hNGH96FvQt6KzGho2WXf0H /E+ymKH0QTPMi4dlN6yQOdy1QcKQPCBajSD79EBjQDkv1Y+UnOJ1SO7kbLc80iT2dKLH /C9OkbS7C0thfYospXoY49PSHTJxZPmFpC2qGYWFNGT/y9vJ7WKt300/z3fCdc7bxS1O 7QXE8AETdEKKJtzdy5VWBMIREtN0HldK+/CKI2dX1ZDy2rrXIrfWNRXcFcChnusrYrmH YzsO6blJmEKC7xXwIU8zVmQ7idDtuPKPZvkvcIVc92MBB9Fc/Y8xWsKvQ5xJKQtAu6EA 10/A== X-Google-Smtp-Source: ANB0VdZG7tp/NrFPMH3eZdxzJNklQ72GuIMdNUZCeDF5+boZvpCXr3XDg+T05Ydctx8jn9DV1Uu4rw== X-Received: by 2002:a17:902:c3:: with SMTP id a61-v6mr6493987pla.279.1536305979885; Fri, 07 Sep 2018 00:39:39 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:39 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 4/6] Btrfs: prevent ioctls from interfering with a swap file Date: Fri, 7 Sep 2018 00:39:18 -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 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 | 24 +++++++++++++++++++++ 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 | 49 +++++++++++++++++++++++++++++++++++++----- 6 files changed, 122 insertions(+), 12 deletions(-) diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 53af9f5253f4..e37ce40db380 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -716,6 +716,23 @@ 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. + */ +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 +1138,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; @@ -1285,6 +1306,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 5124c15705ce..0ab3527bedd1 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1187,6 +1187,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, atomic_set(&root->log_batch, 0); refcount_set(&root->refs, 1); atomic_set(&root->will_be_snapshotted, 0); + atomic_set(&root->nr_swapfiles, 0); root->log_transid = 0; root->log_transid_committed = -1; root->last_log_commit = 0; @@ -2781,6 +2782,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 63600dc2ac4c..d083bf21aabe 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; @@ -751,6 +756,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; @@ -1487,9 +1498,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; @@ -3538,6 +3553,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) @@ -4234,6 +4254,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 da86706123ff..207e36b70d9b 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 == -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) { @@ -7525,3 +7544,23 @@ int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) btrfs_free_path(path); return ret; } + +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 Fri Sep 7 07:39:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591799 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 C299913AC for ; Fri, 7 Sep 2018 07:39:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B5E5F2A630 for ; Fri, 7 Sep 2018 07:39:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA1692A64E; Fri, 7 Sep 2018 07:39:53 +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=unavailable 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 2F26A2A630 for ; Fri, 7 Sep 2018 07:39:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B5CA6B7D33; Fri, 7 Sep 2018 03:39:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3675C6B7D34; Fri, 7 Sep 2018 03:39:43 -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 147E26B7D35; Fri, 7 Sep 2018 03:39:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id C28C56B7D33 for ; Fri, 7 Sep 2018 03:39:42 -0400 (EDT) Received: by mail-pf1-f200.google.com with SMTP id x19-v6so7171569pfh.15 for ; Fri, 07 Sep 2018 00:39:42 -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=vODlK4PZBBKEJgi4ubv0xa/RKomiq2V+t41JD1y5z/4=; b=Po2NGKU85Rs6lsHBkNuKobs1CcXvEOrPHWdyku5+WA+kTkcjfR5BAY9fXGZHfm7Skc 5YsRnmbo7giOLbkCk7fVV94unvnsmBWe8naKuQq5cnrqe0KV98kHtWzH4LlcIIBJ19u+ bCi3Y3Tt6F+OuDQ7kb4Sf/tm+muxxAaoTlnjEKqCvCh1ZZIMJIGXbh7kwZeY6+9YqPcU DMuVj2GBDqdo2q63W58hR1cBO0e8dUz1MWIwwpwrBKZXQdCXC+WpwAlBa0tvPtjLSbvy Jvaro5RlELHIK4fNH8qL6MDcQA1SbEtE3q7wN/hbhtT5k77KI6EqT4vz2D1G6Rt7n29K kDpA== X-Gm-Message-State: APzg51BsWZ0tAUZW+QVq8A6S+FbhkCG7CnM4uCDAVjXG2Fs+dGbHglKc uuHu7UrAaQvNEGTrs8Ru1lu6Y7ITmeO/RXi0oZmMGR/EqsgR9pE0bIP87WZl/ta3HoQ1n4FxXUp GYPHjf6tq4ViXnt4bZdGZ+pp0vt8cBYREoQkXLjl69fzb9qfp2+IYsuiA/MNCFb0uTuZO3mKXDP +0YvMt5LdjhBALzNBFGENfgArZgdM7Q+Zosk/Aa1yde0TVY5Tg90fBWoswFww2tz3VpRR8P134E AXFbAetKpEbR9hPSFyXe7OVxS54OYGvmCfofvbdyajYj8F1UPFNd5aDCW1xZilLI9FFVVYsyfAR GqlgIZdrkzuCWPQJ0d9dNxYUqytRYcpx+YIlkg6xOS7mPMzvXdI1UjBvMbrSJGRYVq4HkvQtK7U d X-Received: by 2002:a65:4289:: with SMTP id j9-v6mr6887774pgp.284.1536305982450; Fri, 07 Sep 2018 00:39:42 -0700 (PDT) X-Received: by 2002:a65:4289:: with SMTP id j9-v6mr6887736pgp.284.1536305981622; Fri, 07 Sep 2018 00:39:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305981; cv=none; d=google.com; s=arc-20160816; b=XmSKp2SIG5mWwFJnCW7gRrEXARiR4BeUykl8yyqYQqLut3xlHOIkZM2XkBod+gSfx8 GkvagsR6AhEjnCMb9XUFz43xaogXAbBCEJqtnAYMJ9O+ddnhBBphqxp/8rPML0cs+YCH QIR/66pJixe2NGZd7pqgJAGvDvjOc1jJSH6pAiT90sKZ2LmmlpMUwdH73ZoAF+5IVFAd gT3CFJWu3DfUIH4hBfHdPxkmUiKO/szf1U9ZzwymETRbp+KLyfYRg0FQ6yQXEG8on9Hf WLsXRohO5D7FyShflSst+xAsNgAllFNBDms/BVvzwBbwiQGtW2PwiMqQb8BaeVdbdsQS Q7qA== 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=vODlK4PZBBKEJgi4ubv0xa/RKomiq2V+t41JD1y5z/4=; b=WUDc8hw/vatQydv5OiPvuHNykB4tk+Hr+R2yoEClEa3FX/YNTPTz7MZbFB9nW6s+Ia /ThSwnQGYkNI+jZElP5IvORz9R2AGw0wCUKJcVtikS8hwc+KWxdLl/BX0Yo4Ulh+jmDp rNk0sJyzVqNqf8DDj0+IuFWcgCBFunKxucmttcM8Gcsn1AeYi/wiI98AcdbhN7aK/YN0 gKWEZKcevV6QmLvcC4hTk3Ia4cYwnd2xmuFIvVir+WFQbpC9V1Oaelp9sjTBCMSSmaeY gwrNoSVX2LHf3mnY2pxTTKunCHSvfp+HVL5NjfBWRn6+O95qfUVerzexHkkfQQtebWoL 4R+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=nr1btmiW; 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 d4-v6sor1637967pla.118.2018.09.07.00.39.41 for (Google Transport Security); Fri, 07 Sep 2018 00:39:41 -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=nr1btmiW; 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=vODlK4PZBBKEJgi4ubv0xa/RKomiq2V+t41JD1y5z/4=; b=nr1btmiWU7s7pNSc57k45VCUIfg0bxRLVqLCmi7TOBwggGIDfGToKTcJLXK/be4MXe 7pZ36/mIGl7LLi7H6MwBVCKC6LkAstvK3ltC+Q6Q/w7mfzSuErEtIEZNaoaJM0nVe5RN G431CKiK5vJwFxm4aHcjgC566OgZIhpZHKwnrYdjvPdjnHjsF7isYNCtm4yjEG7xf1wv HhMq06dSWD+jDvJ/1uQPi7v46rlj3GuG4robNCFuh/pCZT5gbBTbwFr2eh8CVtHy3oSI VoQfEk0/lc514oqgA8g5x42gIHsQYvIB+P9dCno99NbTYlkNr1OuCJQ4aD0K99c6EeRe QxKQ== X-Google-Smtp-Source: ANB0VdZo9evVYUnPJ98lxgj6rDfr8L/ivwBG+aqa6gQF66fKsKXH8+tWhzTNTjNznm1IJt+l9CROmA== X-Received: by 2002:a17:902:b702:: with SMTP id d2-v6mr6724508pls.12.1536305981074; Fri, 07 Sep 2018 00:39:41 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:40 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 5/6] Btrfs: rename get_chunk_map() and make it non-static Date: Fri, 7 Sep 2018 00:39:19 -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 Btrfs swap code is going to need it, so give it a btrfs_ prefix and make it non-static. Signed-off-by: Omar Sandoval Reviewed-by: Nikolay Borisov --- fs/btrfs/volumes.c | 22 +++++++++++----------- fs/btrfs/volumes.h | 9 +++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 207e36b70d9b..514932c47bcd 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -2714,8 +2714,8 @@ 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) +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 +2752,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 @@ -4897,7 +4897,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); @@ -5039,7 +5039,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; @@ -5099,7 +5099,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 @@ -5145,7 +5145,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; @@ -5162,7 +5162,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; @@ -5321,7 +5321,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); @@ -5647,7 +5647,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); @@ -5946,7 +5946,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..d68c8a05a774 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -465,6 +465,15 @@ 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); +/** + * 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); static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) From patchwork Fri Sep 7 07:39:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10591801 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 2BDCB13AC for ; Fri, 7 Sep 2018 07:39:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E4372A630 for ; Fri, 7 Sep 2018 07:39:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 121152A64E; Fri, 7 Sep 2018 07:39:57 +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=unavailable 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 429782A630 for ; Fri, 7 Sep 2018 07:39:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89B7D6B7D35; Fri, 7 Sep 2018 03:39:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 820296B7D36; Fri, 7 Sep 2018 03:39:44 -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 672B36B7D37; Fri, 7 Sep 2018 03:39:44 -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 1952E6B7D35 for ; Fri, 7 Sep 2018 03:39:44 -0400 (EDT) Received: by mail-pl1-f198.google.com with SMTP id a10-v6so6663133pls.23 for ; Fri, 07 Sep 2018 00:39:44 -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=K4Y906AymHUAxdzlG6CbU0IU5tVvjC493iC6RSTqEvc=; b=RgbcpulSh4OnAhw7xoFNREJW4iwQFYXT/5VLo9D2oEbGfkALA8SLhCiM3ZD1Gnryan l4uEgD4pHtmEGm86l8vbKG1QbaPQ/kACwCkxpfz76i+5SJpPW7L6a7O29H7zm8xZJl3g /PDYqan7/MvOnKQxQrZ4DRMozWMcyzCObvkNBI0HAwPDu9Ml3Z4K9vhhvk5DaBwKPs3i P/rcr4x+x+QvvmkRjcAlO58zDhqPbQMmrHSba1mW4uvWH0ViHABUZaq+rSZ7f/2N8TiN IFk9JBV1qYpye3Zy2U/4KgnSRjcplvcIGOMZ1AOn1/DQAurDxRir635alGpGTdWMuoha WNdw== X-Gm-Message-State: APzg51ApLpjYQpIAomrVrO93J9nE0KxLyXf8ZAPOLwnrRsjmfo8NIylv nzDmZhUuEHptzc3Q0fZRF7dBfWWgDGPkl/FbRCRgz89W2XocPM7TNgC7f4/w9eBtrPKh4a3Ujhi 7Pt6fVfITQuHeQI+5z+34crYokXmp+NGkZa+M/jZPly3APGdl2bA2Ryjt4eAUkpQU8AZUbryXrj VXd0X/sXC71T/WhHWOhhu7YpmtdYO1k3+Yq8MyW+JCVXvpXxs0ogHIPOy4xHMdhhJB2TAAMrjOs noTliM9G/KKlPXKkiZxD1wLcTG717W5Hjnhe7a6YPPA4rPFhjiEWj8ewSAs9vjXVGQO2pE6Xljk ZHGdFUfQVLsm1RMRapbx8K9VH8kx8QAkmcR3tMlAeXxTBKm6ttK5EuAWWpiSitYKE2J2CuVfgBO C X-Received: by 2002:a63:f344:: with SMTP id t4-v6mr6921911pgj.428.1536305983736; Fri, 07 Sep 2018 00:39:43 -0700 (PDT) X-Received: by 2002:a63:f344:: with SMTP id t4-v6mr6921865pgj.428.1536305982697; Fri, 07 Sep 2018 00:39:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536305982; cv=none; d=google.com; s=arc-20160816; b=WImywkqIqayXb55Ih3HUrYUMaSsNyjbSAhfUISaZZs7c/HyST+admZIK2BNkmOXT3U YyQg27Y1iZeLNwDuYc1oZ0pryrXOqk+F4bMxiYSCKh6jCWhe6ucvrHpgwbEaEFWPrzy8 URBwdMAWSbE10IjxezAVVpdANcadxI+WiPs2owotpJjgzxhDD1dmcDmE+2eSsWRM9qrE mciKfnm62b4dCUcbB6ziaF/5B9tfiXtj2QpDiHbOUOhbC9iR4VHVS+S+DgJO5Hdc9yiZ VuvCD126GffKVkU8mezjrccSor/S2nBh6AbtFKljjbp+H8P5+wWQA+sxTu8LLDZ6uC/1 JYNA== 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=K4Y906AymHUAxdzlG6CbU0IU5tVvjC493iC6RSTqEvc=; b=KOsp2PuLCd5PxPaD/DiLUmoPbxTX6fQZM6SdtcrblXqKuRn45PXCFclHx9RRj5IlXl rXLJ3lChkPyKXTjMtqPPKjav+Vsqyt16tOs5l/1u/K9rjne60arDL8ld7eJ6E67I99Rm aT/A24cwyN1E54QfuVReTrHVYzRvrEAfX+GG6J5ySYLm/fnXRfhIWLOVI23q7FdUTjXv Spf7PCFAmPNOxUK9QuqpSkz31ahQNHbBLh5gAxJvR2h0jd0vhSdrqR+TK1mjb3vJBc1j cnNnl3WgP5o/SD+Khi5N3bEb3SMUhVEYjl6lZ1O4oty0xw4NfbEFU1rMoGkL6yW8MjKo ybCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=et3B1Rq1; 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 w127-v6sor1311359pgb.391.2018.09.07.00.39.42 for (Google Transport Security); Fri, 07 Sep 2018 00:39:42 -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=et3B1Rq1; 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=K4Y906AymHUAxdzlG6CbU0IU5tVvjC493iC6RSTqEvc=; b=et3B1Rq1KwzRrw/3dDEr9I2QvqzOerMHlAujXvJfF/yifS4jd2ACJMYPoUPOFgHirS TFHWMsqKpzaojl5bJFcs22lfrLdHey1gWGOIk16qqnLkIYW6lSMS1ux9AWNXwdzMBYCu GhtGWwkrqUthTBIndycP+vhgSrxVr8TDtfIJbv4bsA6z5/Xwh2Gosq1ki/QA1dOeByqw zHlAGAfbeuohQzUc5W8KMDFpLT1Qd8brv0/yzZkJml13CNsh65xM74lCvB7A/+CTPGQN 6KF3JCQ5xVtNtuZAz+6O/P+Ol398K5fm5R30k3ODZvBmOmXm6ftc8zFytUO3qF363ihu bukg== X-Google-Smtp-Source: ANB0VdYbj4iBr4kARLbU03pQkLKfnUknFdnuQv7/qNfj+tSfWqZnleC8X4YfjFj9nIhQDEub5tYCIw== X-Received: by 2002:a63:2906:: with SMTP id p6-v6mr6783393pgp.204.1536305982065; Fri, 07 Sep 2018 00:39:42 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:904c]) by smtp.gmail.com with ESMTPSA id i65-v6sm16276914pfk.43.2018.09.07.00.39.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 00:39:41 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, linux-mm@kvack.org Subject: [PATCH v6 6/6] Btrfs: support swap files Date: Fri, 7 Sep 2018 00:39:20 -0700 Message-Id: <77442bbbad9ebc37f3b72a47ca983a3a805e0718.1536305017.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 | 316 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 9357a19d2bff..55aba2d7074c 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" @@ -10437,6 +10438,319 @@ 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; +} + +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, @@ -10514,6 +10828,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 = {