linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Jan Kara <jack@suse.com>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	tytso@mit.edu, Andreas Dilger <adilger.kernel@dilger.ca>,
	linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: [PATCH 2/3] quota: Support using the page cache for quota files
Date: Sun,  5 Jun 2022 15:38:14 +0100	[thread overview]
Message-ID: <20220605143815.2330891-3-willy@infradead.org> (raw)
In-Reply-To: <20220605143815.2330891-1-willy@infradead.org>

Quota files are usually cached in the buffer cache of the block device.
These support functions allow a filesystem to cache quota files in their
page cache instead which is more efficient.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 fs/quota/dquot.c         | 68 ++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h       |  2 ++
 include/linux/quotaops.h |  1 +
 3 files changed, 71 insertions(+)

diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index cdb22d6d7488..ef9aeae802c7 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -59,6 +59,7 @@
 #include <linux/fs.h>
 #include <linux/mount.h>
 #include <linux/mm.h>
+#include <linux/pagemap.h>
 #include <linux/time.h>
 #include <linux/types.h>
 #include <linux/string.h>
@@ -73,6 +74,7 @@
 #include <linux/proc_fs.h>
 #include <linux/security.h>
 #include <linux/sched.h>
+#include <linux/sched/mm.h>
 #include <linux/cred.h>
 #include <linux/kmod.h>
 #include <linux/namei.h>
@@ -2161,6 +2163,72 @@ const struct dquot_operations dquot_operations = {
 };
 EXPORT_SYMBOL(dquot_operations);
 
+ssize_t generic_quota_read(struct super_block *sb, int type, char *data,
+			      size_t len, loff_t pos)
+{
+	struct inode *inode = sb_dqopt(sb)->files[type];
+	struct address_space *mapping = inode->i_mapping;
+	size_t toread;
+	pgoff_t index;
+	loff_t i_size = i_size_read(inode);
+
+	if (pos > i_size)
+		return 0;
+	if (pos + len > i_size)
+		len = i_size - pos;
+	toread = len;
+	index = pos / PAGE_SIZE;
+
+	while (toread > 0) {
+		struct folio *folio = read_mapping_folio(mapping, index, NULL);
+		size_t tocopy = min(toread, PAGE_SIZE - offset_in_page(pos));
+		void *src;
+
+		if (folio == ERR_PTR(-ENOMEM)) {
+			memalloc_retry_wait(GFP_NOFS);
+			continue;
+		} else if (IS_ERR(folio))
+			return PTR_ERR(folio);
+
+		src = kmap_local_folio(folio, offset_in_folio(folio, pos));
+		memcpy(data, src, tocopy);
+		kunmap_local(src);
+		folio_put(folio);
+
+		toread -= tocopy;
+		data += tocopy;
+		pos += tocopy;
+		index++;
+	}
+	return len;
+}
+EXPORT_SYMBOL(generic_quota_read);
+
+int generic_quota_sync(struct super_block *sb, int type)
+{
+	struct quota_info *dqopt = sb_dqopt(sb);
+	int i, ret;
+
+	ret = dquot_writeback_dquots(sb, type);
+	if (ret)
+		return ret;
+	if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
+		return 0;
+
+	for (i = 0; i < MAXQUOTAS; i++) {
+		if (type != -1 && type != i)
+			continue;
+		if (!sb_has_quota_active(sb, i))
+			continue;
+		ret = write_inode_now(dqopt->files[i], true);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(generic_quota_sync);
+
 /*
  * Generic helper for ->open on filesystems supporting disk quotas.
  */
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 9ad5e3520fae..2e798fc4c118 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2449,6 +2449,8 @@ struct super_block *sget(struct file_system_type *type,
 			int (*test)(struct super_block *,void *),
 			int (*set)(struct super_block *,void *),
 			int flags, void *data);
+ssize_t generic_quota_read(struct super_block *sb, int type, char *data,
+		size_t len, loff_t pos);
 
 /* Alas, no aliases. Too much hassle with bringing module.h everywhere */
 #define fops_get(fops) \
diff --git a/include/linux/quotaops.h b/include/linux/quotaops.h
index a0f6668924d3..fe12b04948f6 100644
--- a/include/linux/quotaops.h
+++ b/include/linux/quotaops.h
@@ -105,6 +105,7 @@ int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
 int dquot_quota_off(struct super_block *sb, int type);
 int dquot_writeback_dquots(struct super_block *sb, int type);
 int dquot_quota_sync(struct super_block *sb, int type);
+int generic_quota_sync(struct super_block *sb, int type);
 int dquot_get_state(struct super_block *sb, struct qc_state *state);
 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii);
 int dquot_get_dqblk(struct super_block *sb, struct kqid id,
-- 
2.35.1


  parent reply	other threads:[~2022-06-05 14:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-05 14:38 [PATCH 0/3] Cache quota files in the page cache Matthew Wilcox (Oracle)
2022-06-05 14:38 ` [PATCH 1/3] quota: Prevent memory allocation recursion while holding dq_lock Matthew Wilcox (Oracle)
2022-06-06  8:03   ` Jan Kara
2022-06-06 12:42     ` Matthew Wilcox
2022-06-06 13:08       ` Jan Kara
2022-06-05 14:38 ` Matthew Wilcox (Oracle) [this message]
2022-06-06  2:53   ` [PATCH 2/3] quota: Support using the page cache for quota files kernel test robot
2022-06-06  4:05   ` kernel test robot
2022-06-06  4:36   ` kernel test robot
2022-06-05 14:38 ` [PATCH 3/3] ext4: Use generic_quota_read() Matthew Wilcox (Oracle)
2022-06-06  8:38   ` Jan Kara
2022-06-08  1:42     ` Matthew Wilcox
2022-06-08 14:21       ` Jan Kara
2022-06-13  7:59   ` [ext4] fa96490369: WARNING:at_fs/ext4/inode.c:#ext4_invalidate_folio kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220605143815.2330891-3-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=adilger.kernel@dilger.ca \
    --cc=jack@suse.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).