Skip to contentSkip to conversation

ceph: kernel client cephfs quota support

PATCH In review Luis Henriques 2 participants 11 messages Jan 5, 2018 – Jan 10, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover ceph: kernel client cephfs quota support

A cephfs-specific quota implementation has been available in the user-space fuse client for a while. This quota implementation allows an administrator to restrict the number of bytes and/or the number of files in a filesystem subtree. This quota implementation, however, is supported at the client-level only, which means that cooperation is required between different clients accessing the system.

This obviously assumes that all clients are trusted entities and will respect the quotas, preventing users from exceeding the quota limits. Since the kernel client doesn't support quotas, it has not been possible to use it in a cluster where quotas are a requirement.

This patchset adds kernel client support for cephfs quotas as it is currently implemented in the ceph fuse client. Note however that it relies on some still-to-be-merged changes to the MDS (see below, "Changes since v1" for details).

For further details on CephFS quota, see [1].

[1] http://docs.ceph.com/docs/master/cephfs/quota/

** Changes since v3 **

- Rework after review from Yan, Zheng:
* ceph_handle_quota(): Always increment message sequence number, even if inode isn't in cache
* renamed inode variables ino -> in
* get_quota_realm() now returns a ceph_snap_realm instead of ceph_inode_info

- Updated quota.c copyright and added SPDX identifier

- Added max_bytes quota implementation

- Updated Documentation/filesystems/ceph.txt to include reference to quota; also documented added a few more comments to the code.

** Changes since v2 **

Rework after review from Yan, Zheng:

- Dropped patch 0001 ("ceph: add seqlock for snaprealm hierarchy change detection") and use mdsc->snap_rwsem for walking the snaprealm hierarchy instead of adding a seqlock. This means that patches 0003 and 0004 needed to be reworked.

- Added a NULL check in ceph_handle_quota() after the inode lookup with ceph_find_inode().

** Changes since v1 **

Instead of trying to do a reverse path walk to find the "quota realm" for a given directory, this patchset is now using snaprealms. Thus, for testing it, a modified MDS is required:

https://github.com/ukernel/ceph/tree/wip-cephfs-quota-realm

This modified MDS creates a snaprealm when a quota is set in a directory. This means that a client needs only to walk up the snaprealm hierarchy to find a directory that has quotas instead of doing the full reverse path walking.

Note however that this requires an extra patch that adds a seqlock (1st patch in series) to detect changes in the snaprealm hierarchy.

Luis Henriques (6):
ceph: quota: add initial infrastructure to support cephfs quotas
ceph: quota: support for ceph.quota.max_files
ceph: quota: don't allow cross-quota renames
ceph: quota: support for ceph.quota.max_bytes
ceph: quota: update MDS when max_bytes is approaching
ceph: quota: add quotas to the in-tree cephfs documentation

 Documentation/filesystems/ceph.txt |  12 ++
 fs/ceph/Makefile                   |   2 +-
 fs/ceph/dir.c                      |  16 +++
 fs/ceph/file.c                     |  21 ++-
 fs/ceph/inode.c                    |  10 ++
 fs/ceph/mds_client.c               |  23 ++++
 fs/ceph/mds_client.h               |   2 +
 fs/ceph/quota.c                    | 276 +++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h                    |  14 ++
 fs/ceph/xattr.c                    |  44 ++++++
 include/linux/ceph/ceph_features.h |   3 +-
 include/linux/ceph/ceph_fs.h       |  17 +++
 12 files changed, 437 insertions(+), 3 deletions(-)
 create mode 100644 fs/ceph/quota.c
#1 lore raw

PATCH v4 1/6 ceph: quota: add initial infrastructure to support cephfs quotas

This patch adds the infrastructure required to support cephfs quotas as it is currently implemented in the ceph fuse client. Cephfs quotas can be set on any directory, and can restrict the number of bytes or the number of files stored beneath that point in the directory hierarchy.

Quotas are set using the extended attributes 'ceph.quota.max_files' and 'ceph.quota.max_bytes', and can be removed by setting these attributes to '0'.

---
 fs/ceph/Makefile                   |  2 +-
 fs/ceph/inode.c                    |  6 ++++
 fs/ceph/mds_client.c               | 23 ++++++++++++++
 fs/ceph/mds_client.h               |  2 ++
 fs/ceph/quota.c                    | 65 ++++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h                    |  8 +++++
 fs/ceph/xattr.c                    | 44 ++++++++++++++++++++++++++
 include/linux/ceph/ceph_features.h |  3 +-
 include/linux/ceph/ceph_fs.h       | 17 ++++++++++
 9 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 fs/ceph/quota.c
fs/ceph/Makefile +1 -1
index 174f5709e508..a699e320393f 100644
@@ -6,7 +6,7 @@
 obj-$(CONFIG_CEPH_FS) += ceph.o
 
 ceph-y := super.o inode.o dir.o file.o locks.o addr.o ioctl.o \
-	export.o caps.o snap.o xattr.o \
+	export.o caps.o snap.o xattr.o quota.o \
 	mds_client.o mdsmap.o strings.o ceph_frag.o \
 	debugfs.o
 
fs/ceph/inode.c +6 -0
index ab81652198c4..8a0ba96e105d 100644
@@ -441,6 +441,9 @@ struct inode *ceph_alloc_inode(struct super_block *sb)
 	atomic64_set(&ci->i_complete_seq[1], 0);
 	ci->i_symlink = NULL;
 
+	ci->i_max_bytes = 0;
+	ci->i_max_files = 0;
+
 	memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
 	RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
 
@@ -790,6 +793,9 @@ static int fill_inode(struct inode *inode, struct page *locked_page,
 	inode->i_rdev = le32_to_cpu(info->rdev);
 	inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
 
+	ci->i_max_bytes = iinfo->max_bytes;
+	ci->i_max_files = iinfo->max_files;
+
 	if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
 	    (issued & CEPH_CAP_AUTH_EXCL) == 0) {
 		inode->i_mode = le32_to_cpu(info->mode);
fs/ceph/mds_client.c +23 -0
index 1b468250e947..2290056d13fc 100644
@@ -100,6 +100,26 @@ static int parse_reply_info_in(void **p, void *end,
 	} else
 		info->inline_version = CEPH_INLINE_NONE;
 
+	if (features & CEPH_FEATURE_MDS_QUOTA) {
+		u8 struct_v, struct_compat;
+		u32 struct_len;
+
+		/*
+		 * both struct_v and struct_compat are expected to be >= 1
+		 */
+		ceph_decode_8_safe(p, end, struct_v, bad);
+		ceph_decode_8_safe(p, end, struct_compat, bad);
+		if (!struct_v || !struct_compat)
+			goto bad;
+		ceph_decode_32_safe(p, end, struct_len, bad);
+		ceph_decode_need(p, end, struct_len, bad);
+		ceph_decode_64_safe(p, end, info->max_bytes, bad);
+		ceph_decode_64_safe(p, end, info->max_files, bad);
+	} else {
+		info->max_bytes = 0;
+		info->max_files = 0;
+	}
+
 	info->pool_ns_len = 0;
 	info->pool_ns_data = NULL;
 	if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
@@ -4064,6 +4084,9 @@ static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
 	case CEPH_MSG_CLIENT_LEASE:
 		handle_lease(mdsc, s, msg);
 		break;
+	case CEPH_MSG_CLIENT_QUOTA:
+		ceph_handle_quota(mdsc, s, msg);
+		break;
 
 	default:
 		pr_err("received unknown message type %d %s\n", type,
fs/ceph/mds_client.h +2 -0
index 837ac4b087a0..7af576733948 100644
@@ -49,6 +49,8 @@ struct ceph_mds_reply_info_in {
 	char *inline_data;
 	u32 pool_ns_len;
 	char *pool_ns_data;
+	u64 max_bytes;
+	u64 max_files;
 };
 
 struct ceph_mds_reply_dir_entry {
fs/ceph/quota.c new file +65 -0

65 lines not on this page · raw message

fs/ceph/super.h +8 -0
index 2beeec07fa76..f998b7f076cf 100644
@@ -309,6 +309,9 @@ struct ceph_inode_info {
 	u64 i_rbytes, i_rfiles, i_rsubdirs;
 	u64 i_files, i_subdirs;
 
+	/* quotas */
+	u64 i_max_bytes, i_max_files;
+
 	struct rb_root i_fragtree;
 	int i_fragtree_nsplits;
 	struct mutex i_fragtree_mutex;
@@ -1019,4 +1022,9 @@ extern int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
 extern int ceph_fs_debugfs_init(struct ceph_fs_client *client);
 extern void ceph_fs_debugfs_cleanup(struct ceph_fs_client *client);
 
+/* quota.c */
+extern void ceph_handle_quota(struct ceph_mds_client *mdsc,
+			      struct ceph_mds_session *session,
+			      struct ceph_msg *msg);
+
 #endif /* _FS_CEPH_SUPER_H */
fs/ceph/xattr.c +44 -0

62 lines not on this page · raw message

include/linux/ceph/ceph_features.h +2 -1
index 59042d5ac520..6acd46c36271 100644
@@ -209,7 +209,8 @@ DEFINE_CEPH_FEATURE_DEPRECATED(63, 1, RESERVED_BROKEN, LUMINOUS) // client-facin
 	 CEPH_FEATURE_SERVER_JEWEL |		\
 	 CEPH_FEATURE_MON_STATEFUL_SUB |	\
 	 CEPH_FEATURE_CRUSH_TUNABLES5 |		\
-	 CEPH_FEATURE_NEW_OSDOPREPLY_ENCODING)
+	 CEPH_FEATURE_NEW_OSDOPREPLY_ENCODING |	\
+	 CEPH_FEATURE_MDS_QUOTA)
 
 #define CEPH_FEATURES_REQUIRED_DEFAULT   \
 	(CEPH_FEATURE_NOSRCADDR |	 \
include/linux/ceph/ceph_fs.h +17 -0
index 88dd51381aaf..98bdcc0eda3f 100644
@@ -134,6 +134,7 @@ struct ceph_dir_layout {
 #define CEPH_MSG_CLIENT_LEASE           0x311
 #define CEPH_MSG_CLIENT_SNAP            0x312
 #define CEPH_MSG_CLIENT_CAPRELEASE      0x313
+#define CEPH_MSG_CLIENT_QUOTA		0x314
 
 /* pool ops */
 #define CEPH_MSG_POOLOP_REPLY           48
@@ -807,4 +808,20 @@ struct ceph_mds_snap_realm {
 } __attribute__ ((packed));
 /* followed by my snap list, then prior parent snap list */
 
+/*
+ * quotas
+ */
+struct ceph_mds_quota {
+	__le64 ino;		/* ino */
+	struct ceph_timespec rctime;
+	__le64 rbytes;		/* dir stats */
+	__le64 rfiles;
+	__le64 rsubdirs;
+	__u8 struct_v;		/* compat */
+	__u8 struct_compat;
+	__le32 struct_len;
+	__le64 max_bytes;	/* quota max. bytes */
+	__le64 max_files;	/* quota max. files */
+} __attribute__ ((packed));
+
 #endif
#2 lore raw

PATCH v4 2/6 ceph: quota: support for ceph.quota.max_files

In reply to Luis Henriques

This patch adds support for the max_files quota. It hooks into all the ceph functions that add new filesystem objects that need to be checked against the quota limits. When these limits are hit, -EDQUOT is returned.

Note that we're not checking quotas on ceph_link(). ceph_link doesn't really create a new inode, and since the MDS doesn't update the directory statistics when a new (hard) link is created (only with symlinks), they are not accounted as a new file.

---
 fs/ceph/dir.c   | 11 ++++++++
 fs/ceph/file.c  |  4 ++-
 fs/ceph/quota.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h |  1 +
 4 files changed, 95 insertions(+), 1 deletion(-)
fs/ceph/dir.c +11 -0
index 8a5266699b67..66550d92b1ac 100644
@@ -818,6 +818,9 @@ static int ceph_mknod(struct inode *dir, struct dentry *dentry,
 	if (ceph_snap(dir) != CEPH_NOSNAP)
 		return -EROFS;
 
+	if (ceph_quota_is_max_files_exceeded(dir))
+		return -EDQUOT;
+
 	err = ceph_pre_init_acls(dir, &mode, &acls);
 	if (err < 0)
 		return err;
@@ -871,6 +874,9 @@ static int ceph_symlink(struct inode *dir, struct dentry *dentry,
 	if (ceph_snap(dir) != CEPH_NOSNAP)
 		return -EROFS;
 
+	if (ceph_quota_is_max_files_exceeded(dir))
+		return -EDQUOT;
+
 	dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
 	if (IS_ERR(req)) {
@@ -920,6 +926,11 @@ static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
 		goto out;
 	}
 
+	if (ceph_quota_is_max_files_exceeded(dir)) {
+		err = -EDQUOT;
+		goto out;
+	}
+
 	mode |= S_IFDIR;
 	err = ceph_pre_init_acls(dir, &mode, &acls);
 	if (err < 0)
fs/ceph/file.c +3 -1
index 5c17125f45c7..5a77a66e3d6b 100644
@@ -371,7 +371,7 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
 	struct ceph_mds_request *req;
 	struct dentry *dn;
 	struct ceph_acls_info acls = {};
-       int mask;
+	int mask;
 	int err;
 
 	dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
@@ -382,6 +382,8 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
 		return -ENAMETOOLONG;
 
 	if (flags & O_CREAT) {
+		if (ceph_quota_is_max_files_exceeded(dir))
+			return -EDQUOT;
 		err = ceph_pre_init_acls(dir, &mode, &acls);
 		if (err < 0)
 			return err;
fs/ceph/quota.c +80 -0

83 lines not on this page · raw message

fs/ceph/super.h +1 -0
index f998b7f076cf..20197e29a7f0 100644
@@ -1026,5 +1026,6 @@ extern void ceph_fs_debugfs_cleanup(struct ceph_fs_client *client);
 extern void ceph_handle_quota(struct ceph_mds_client *mdsc,
 			      struct ceph_mds_session *session,
 			      struct ceph_msg *msg);
+extern bool ceph_quota_is_max_files_exceeded(struct inode *inode);
 
 #endif /* _FS_CEPH_SUPER_H */
#3 lore raw

PATCH v4 3/6 ceph: quota: don't allow cross-quota renames

In reply to Luis Henriques

This patch changes ceph_rename so that -EXDEV is returned if an attempt is made to mv a file between two different dir trees with different quotas setup.

---
 fs/ceph/dir.c   |  5 +++++
 fs/ceph/quota.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h |  1 +
 3 files changed, 75 insertions(+)
fs/ceph/dir.c +5 -0
index 66550d92b1ac..f6ac16caa1e9 100644
@@ -1090,6 +1090,11 @@ static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
 		else
 			return -EROFS;
 	}
+	/* don't allow cross-quota renames */
+	if ((old_dir != new_dir) &&
+	    (!ceph_quota_is_same_realm(old_dir, new_dir)))
+		return -EXDEV;
+
 	dout("rename dir %p dentry %p to dir %p dentry %p\n",
 	     old_dir, old_dentry, new_dir, new_dentry);
 	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
fs/ceph/quota.c +69 -0

81 lines not on this page · raw message

fs/ceph/super.h +1 -0
index 20197e29a7f0..a66e73338386 100644
@@ -1027,5 +1027,6 @@ extern void ceph_handle_quota(struct ceph_mds_client *mdsc,
 			      struct ceph_mds_session *session,
 			      struct ceph_msg *msg);
 extern bool ceph_quota_is_max_files_exceeded(struct inode *inode);
+extern bool ceph_quota_is_same_realm(struct inode *old, struct inode *new);
 
 #endif /* _FS_CEPH_SUPER_H */
#4 lore raw

PATCH v4 4/6 ceph: quota: support for ceph.quota.max_bytes

In reply to Luis Henriques
---
 fs/ceph/file.c  | 11 +++++++++++
 fs/ceph/inode.c |  4 ++++
 fs/ceph/quota.c | 28 +++++++++++++++++++++++++++-
 fs/ceph/super.h |  2 ++
 4 files changed, 44 insertions(+), 1 deletion(-)
fs/ceph/file.c +11 -0
index 5a77a66e3d6b..762402d323a6 100644
@@ -1331,6 +1331,11 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 
 	pos = iocb->ki_pos;
 	count = iov_iter_count(from);
+	if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
+		err = -EDQUOT;
+		goto out;
+	}
+
 	err = file_remove_privs(file);
 	if (err)
 		goto out;
@@ -1661,6 +1666,12 @@ static long ceph_fallocate(struct file *file, int mode,
 		goto unlock;
 	}
 
+	if (!(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)) &&
+	    ceph_quota_is_max_bytes_exceeded(inode, offset + length)) {
+		ret = -EDQUOT;
+		goto unlock;
+	}
+
 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) &&
 	    !(mode & FALLOC_FL_PUNCH_HOLE)) {
 		ret = -ENOSPC;
fs/ceph/inode.c +4 -0
index 8a0ba96e105d..342ba26f6232 100644
@@ -2130,6 +2130,10 @@ int ceph_setattr(struct dentry *dentry, struct iattr *attr)
 	if (err != 0)
 		return err;
 
+	if ((attr->ia_valid & ATTR_SIZE) &&
+	    ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
+		return -EDQUOT;
+
 	err = __ceph_setattr(inode, attr);
 
 	if (err >= 0 && (attr->ia_valid & ATTR_MODE))
fs/ceph/quota.c +27 -1
index 5d7dada91a57..745f9f47027b 100644
@@ -134,7 +134,8 @@ bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
 }
 
 enum quota_check_op {
-	QUOTA_CHECK_MAX_FILES_OP	/* check quota max_files limit */
+	QUOTA_CHECK_MAX_FILES_OP,	/* check quota max_files limit */
+	QUOTA_CHECK_MAX_BYTES_OP	/* check quota max_files limit */
 };
 
 /*
@@ -171,6 +172,9 @@ static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
 		if (op == QUOTA_CHECK_MAX_FILES_OP) {
 			max = ci->i_max_files;
 			rvalue = ci->i_rfiles + ci->i_rsubdirs;
+		} else {
+			max = ci->i_max_bytes;
+			rvalue = ci->i_rbytes;
 		}
 		is_root = (ci->i_vino.ino == CEPH_INO_ROOT);
 		spin_unlock(&ci->i_ceph_lock);
@@ -178,6 +182,9 @@ static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
 		case QUOTA_CHECK_MAX_FILES_OP:
 			exceeded = (max && (rvalue >= max));
 			break;
+		case QUOTA_CHECK_MAX_BYTES_OP:
+			exceeded = (max && (rvalue + delta > max));
+			break;
 		default:
 			/* Shouldn't happen */
 			pr_warn("Invalid quota check op (%d)\n", op);
@@ -212,3 +219,22 @@ bool ceph_quota_is_max_files_exceeded(struct inode *inode)
 
 	return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
 }
+
+/*
+ * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
+ * @inode:	inode being written
+ * @newsize:	new size if write succeeds
+ *
+ * This functions returns true is max_bytes quota allows a file size to reach
+ * @newsize; it returns false otherwise.
+ */
+bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
+{
+	loff_t size = i_size_read(inode);
+
+	/* return immediately if we're decreasing file size */
+	if (newsize <= size)
+		return false;
+
+	return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
+}
fs/ceph/super.h +2 -0
index a66e73338386..60ace9ce5a94 100644
@@ -1028,5 +1028,7 @@ extern void ceph_handle_quota(struct ceph_mds_client *mdsc,
 			      struct ceph_msg *msg);
 extern bool ceph_quota_is_max_files_exceeded(struct inode *inode);
 extern bool ceph_quota_is_same_realm(struct inode *old, struct inode *new);
+extern bool ceph_quota_is_max_bytes_exceeded(struct inode *inode,
+					     loff_t newlen);
 
 #endif /* _FS_CEPH_SUPER_H */
#5 lore raw

PATCH v4 5/6 ceph: quota: update MDS when max_bytes is approaching

In reply to Luis Henriques

When we're reaching the ceph.quota.max_bytes limit, i.e., when writing more than 1/16th of the space left in a quota realm, update the MDS with the new file size.

This mirrors the fuse-client approach with commit 122c50315ed1 ("client:
Inform mds file size when approaching quota limit"), in the ceph git tree.

---
 fs/ceph/file.c  |  6 ++++++
 fs/ceph/quota.c | 38 +++++++++++++++++++++++++++++++++++++-
 fs/ceph/super.h |  2 ++
 3 files changed, 45 insertions(+), 1 deletion(-)
fs/ceph/file.c +6 -0
index 762402d323a6..4a8fe87edfa3 100644
@@ -1417,6 +1417,7 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 
 	if (written >= 0) {
 		int dirty;
+
 		spin_lock(&ci->i_ceph_lock);
 		ci->i_inline_version = CEPH_INLINE_NONE;
 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
@@ -1424,6 +1425,8 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		spin_unlock(&ci->i_ceph_lock);
 		if (dirty)
 			__mark_inode_dirty(inode, dirty);
+		if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
+			ceph_check_caps(ci, CHECK_CAPS_NODELAY, NULL);
 	}
 
 	dout("aio_write %p %llx.%llx %llu~%u  dropping cap refs on %s\n",
@@ -1720,6 +1723,9 @@ static long ceph_fallocate(struct file *file, int mode,
 		spin_unlock(&ci->i_ceph_lock);
 		if (dirty)
 			__mark_inode_dirty(inode, dirty);
+		if ((endoff > size) &&
+		    ceph_quota_is_max_bytes_approaching(inode, endoff))
+			ceph_check_caps(ci, CHECK_CAPS_NODELAY, NULL);
 	}
 
 	ceph_put_cap_refs(ci, got);
fs/ceph/quota.c +37 -1
index 745f9f47027b..caa79fda4c5d 100644
@@ -135,7 +135,9 @@ bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
 
 enum quota_check_op {
 	QUOTA_CHECK_MAX_FILES_OP,	/* check quota max_files limit */
-	QUOTA_CHECK_MAX_BYTES_OP	/* check quota max_files limit */
+	QUOTA_CHECK_MAX_BYTES_OP,	/* check quota max_files limit */
+	QUOTA_CHECK_MAX_BYTES_APPROACHING_OP	/* check if quota max_files
+						   limit is approaching */
 };
 
 /*
@@ -185,6 +187,20 @@ static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
 		case QUOTA_CHECK_MAX_BYTES_OP:
 			exceeded = (max && (rvalue + delta > max));
 			break;
+		case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
+			if (max) {
+				if (rvalue >= max)
+					exceeded = true;
+				else {
+					/*
+					 * when we're writing more that 1/16th
+					 * of the available space
+					 */
+					exceeded =
+						(((max - rvalue) >> 4) < delta);
+				}
+			}
+			break;
 		default:
 			/* Shouldn't happen */
 			pr_warn("Invalid quota check op (%d)\n", op);
@@ -238,3 +254,23 @@ bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
 
 	return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
 }
+
+/*
+ * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
+ * @inode:	inode being written
+ * @newsize:	new size if write succeeds
+ *
+ * This function returns true if the new file size @newsize will be consuming
+ * more than 1/16th of the available quota space; it returns false otherwise.
+ */
+bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
+{
+	loff_t size = ceph_inode(inode)->i_reported_size;
+
+	/* return immediately if we're decreasing file size */
+	if (newsize <= size)
+		return false;
+
+	return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
+				    (newsize - size));
+}
fs/ceph/super.h +2 -0
index 60ace9ce5a94..13623dac0e1b 100644
@@ -1030,5 +1030,7 @@ extern bool ceph_quota_is_max_files_exceeded(struct inode *inode);
 extern bool ceph_quota_is_same_realm(struct inode *old, struct inode *new);
 extern bool ceph_quota_is_max_bytes_exceeded(struct inode *inode,
 					     loff_t newlen);
+extern bool ceph_quota_is_max_bytes_approaching(struct inode *inode,
+						loff_t newlen);
 
 #endif /* _FS_CEPH_SUPER_H */
#6 lore raw

PATCH v4 6/6 ceph: quota: add quotas to the in-tree cephfs documentation

In reply to Luis Henriques

Update documentation to include a high-level description of ceph quotas.

---
 Documentation/filesystems/ceph.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Documentation/filesystems/ceph.txt +12 -0
index 0b302a11718a..094772481263 100644
@@ -62,6 +62,18 @@ subdirectories, and a summation of all nested file sizes.  This makes
 the identification of large disk space consumers relatively quick, as
 no 'du' or similar recursive scan of the file system is required.
 
+Finally, Ceph also allows quotas to be set on any directory in the system.
+The quota can restrict the number of bytes or the number of files stored
+beneath that point in the directory hierarchy.  Quotas can be set using
+extended attributes 'ceph.quota.max_files' and 'ceph.quota.max_bytes', eg:
+
+ setfattr -n ceph.quota.max_bytes -v 100000000 /some/dir
+ getfattr -n ceph.quota.max_bytes /some/dir
+
+A limitation of the current quotas implementation is that it relies on the
+cooperation of the client mounting the file system to stop writers when a
+limit is reached.  A modified or adversarial client cannot be prevented
+from writing as much data as it needs.
 
 Mount Syntax
 ============
#7 lore raw
In reply to Luis Henriques
Quoted 73 lines · Luis Henriques

A cephfs-specific quota implementation has been available in the user-space fuse client for a while. This quota implementation allows an administrator to restrict the number of bytes and/or the number of files in a filesystem subtree. This quota implementation, however, is supported at the client-level only, which means that cooperation is required between different clients accessing the system.

This obviously assumes that all clients are trusted entities and will respect the quotas, preventing users from exceeding the quota limits. Since the kernel client doesn't support quotas, it has not been possible to use it in a cluster where quotas are a requirement.

This patchset adds kernel client support for cephfs quotas as it is currently implemented in the ceph fuse client. Note however that it relies on some still-to-be-merged changes to the MDS (see below, "Changes since v1" for details).

For further details on CephFS quota, see [1].

[1] http://docs.ceph.com/docs/master/cephfs/quota/

** Changes since v3 **

- Rework after review from Yan, Zheng:
* ceph_handle_quota(): Always increment message sequence number, even if inode isn't in cache
* renamed inode variables ino -> in
* get_quota_realm() now returns a ceph_snap_realm instead of ceph_inode_info

- Updated quota.c copyright and added SPDX identifier

- Added max_bytes quota implementation

- Updated Documentation/filesystems/ceph.txt to include reference to quota; also documented added a few more comments to the code.

** Changes since v2 **

Rework after review from Yan, Zheng:

- Dropped patch 0001 ("ceph: add seqlock for snaprealm hierarchy change detection") and use mdsc->snap_rwsem for walking the snaprealm hierarchy instead of adding a seqlock. This means that patches 0003 and 0004 needed to be reworked.

- Added a NULL check in ceph_handle_quota() after the inode lookup with ceph_find_inode().

** Changes since v1 **

Instead of trying to do a reverse path walk to find the "quota realm" for a given directory, this patchset is now using snaprealms. Thus, for testing it, a modified MDS is required:

https://github.com/ukernel/ceph/tree/wip-cephfs-quota-realm

This modified MDS creates a snaprealm when a quota is set in a directory. This means that a client needs only to walk up the snaprealm hierarchy to find a directory that has quotas instead of doing the full reverse path walking.

Note however that this requires an extra patch that adds a seqlock (1st patch in series) to detect changes in the snaprealm hierarchy.

Luis Henriques (6):
ceph: quota: add initial infrastructure to support cephfs quotas
ceph: quota: support for ceph.quota.max_files
ceph: quota: don't allow cross-quota renames
ceph: quota: support for ceph.quota.max_bytes
ceph: quota: update MDS when max_bytes is approaching
ceph: quota: add quotas to the in-tree cephfs documentation

Quoted 15 lines
 Documentation/filesystems/ceph.txt |  12 ++
 fs/ceph/Makefile                   |   2 +-
 fs/ceph/dir.c                      |  16 +++
 fs/ceph/file.c                     |  21 ++-
 fs/ceph/inode.c                    |  10 ++
 fs/ceph/mds_client.c               |  23 ++++
 fs/ceph/mds_client.h               |   2 +
 fs/ceph/quota.c                    | 276 +++++++++++++++++++++++++++++++++++++
 fs/ceph/super.h                    |  14 ++
 fs/ceph/xattr.c                    |  44 ++++++
 include/linux/ceph/ceph_features.h |   3 +-
 include/linux/ceph/ceph_fs.h       |  17 +++
 12 files changed, 437 insertions(+), 3 deletions(-)
 create mode 100644 fs/ceph/quota.c

Hi Luis.

This series looks good. I will add them to our testing branch if my local test goes well.

I also wish to do some optimization (incremental patches) to the code

1. avoid quota check if there is no quota in the filesystem. we can use a variable to tracker how many snaprealm have quota enabled.
2. save inode pointer (not increase inode's reference count) in ceph_snap_realm data structure. so we can avoid calling ceph_find_inode() in check_quota_exceeded()

Regards
Yan, Zheng

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
Quoted 1 line · Zheng

On Fri, Jan 5, 2018 at 6:47 PM, Luis Henriques <[email protected]> wrote:

<snip>

Quoted 4 lines

Hi Luis.

This series looks good. I will add them to our testing branch if my local test goes well.

Awesome, thanks a lot for your review. Please let me know if you find any issues during testing.

Quoted 8 lines

I also wish to do some optimization (incremental patches) to the code

1. avoid quota check if there is no quota in the filesystem. we can use a variable to tracker how many snaprealm have quota enabled.
2. save inode pointer (not increase inode's reference count) in ceph_snap_realm data structure. so we can avoid calling ceph_find_inode() in check_quota_exceeded()

Yeah, these changes seem to make sense. I'll have a look at them and see what I can come up with.

Cheers,

Signature · 1 line
Luis
#9 lore raw
In reply to Yan, Zheng
Quoted 4 lines · Zheng

I also wish to do some optimization (incremental patches) to the code

1. avoid quota check if there is no quota in the filesystem. we can use a variable to tracker how many snaprealm have quota enabled.

I was thinking this should probably be an atomic64_t in struct ceph_mds_client. Does this make sense?

Also, it would probably make sense to have the following functions as inline, defined in super.h:

ceph_quota_is_max_files_exceeded
ceph_quota_is_max_bytes_exceeded
ceph_quota_is_max_bytes_approaching

This way they could simply check the mdsc and return immediately if the counter is 0.

Quoted 3 lines

2. save inode pointer (not increase inode's reference count) in ceph_snap_realm data structure. so we can avoid calling ceph_find_inode() in check_quota_exceeded()

Would you like me to work on v5 to include these changes, or would you rather keep v4 and simply have 2 extra patches on top implementing these optimisations?

Cheers,

Signature · 1 line
Luis
#10 lore raw
Quoted 10 lines · Luis Henriques
Quoted 4 lines · Zheng

I also wish to do some optimization (incremental patches) to the code

1. avoid quota check if there is no quota in the filesystem. we can use a variable to tracker how many snaprealm have quota enabled.

I was thinking this should probably be an atomic64_t in struct ceph_mds_client. Does this make sense?

yes

Quoted 10 lines

Also, it would probably make sense to have the following functions as inline, defined in super.h:

ceph_quota_is_max_files_exceeded
ceph_quota_is_max_bytes_exceeded
ceph_quota_is_max_bytes_approaching

This way they could simply check the mdsc and return immediately if the counter is 0.

Quoted 7 lines
Quoted 3 lines

2. save inode pointer (not increase inode's reference count) in ceph_snap_realm data structure. so we can avoid calling ceph_find_inode() in check_quota_exceeded()

Would you like me to work on v5 to include these changes, or would you rather keep v4 and simply have 2 extra patches on top implementing these optimisations?

I'd like to keep v2 and have extra patches

Regards
Yan, Zheng

Quoted 4 lines

Cheers,
--
Luis