1 // SPDX-License-Identifier: GPL-2.0
3 * quota.c - CephFS quota
5 * Copyright (C) 2017-2018 SUSE
8 #include <linux/statfs.h>
11 #include "mds_client.h"
13 void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
15 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
17 atomic64_inc(&mdsc->quotarealms_count);
19 atomic64_dec(&mdsc->quotarealms_count);
22 static inline bool ceph_has_realms_with_quotas(struct inode *inode)
24 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
25 return atomic64_read(&mdsc->quotarealms_count) > 0;
28 void ceph_handle_quota(struct ceph_mds_client *mdsc,
29 struct ceph_mds_session *session,
32 struct super_block *sb = mdsc->fsc->sb;
33 struct ceph_mds_quota *h = msg->front.iov_base;
34 struct ceph_vino vino;
36 struct ceph_inode_info *ci;
38 if (msg->front.iov_len < sizeof(*h)) {
39 pr_err("%s corrupt message mds%d len %d\n", __func__,
40 session->s_mds, (int)msg->front.iov_len);
45 /* increment msg sequence number */
46 mutex_lock(&session->s_mutex);
48 mutex_unlock(&session->s_mutex);
51 vino.ino = le64_to_cpu(h->ino);
52 vino.snap = CEPH_NOSNAP;
53 inode = ceph_find_inode(sb, vino);
55 pr_warn("Failed to find inode %llu\n", vino.ino);
58 ci = ceph_inode(inode);
60 spin_lock(&ci->i_ceph_lock);
61 ci->i_rbytes = le64_to_cpu(h->rbytes);
62 ci->i_rfiles = le64_to_cpu(h->rfiles);
63 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
64 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
65 le64_to_cpu(h->max_files));
66 spin_unlock(&ci->i_ceph_lock);
72 * This function walks through the snaprealm for an inode and returns the
73 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
74 * or max_bytes). If the root is reached, return the root ceph_snap_realm
77 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
80 static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
83 struct ceph_inode_info *ci = NULL;
84 struct ceph_snap_realm *realm, *next;
88 if (ceph_snap(inode) != CEPH_NOSNAP)
91 realm = ceph_inode(inode)->i_snap_realm;
93 ceph_get_snap_realm(mdsc, realm);
95 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
96 "null i_snap_realm\n", ceph_vinop(inode));
98 spin_lock(&realm->inodes_with_caps_lock);
99 in = realm->inode ? igrab(realm->inode) : NULL;
100 spin_unlock(&realm->inodes_with_caps_lock);
105 has_quota = __ceph_has_any_quota(ci);
108 next = realm->parent;
109 if (has_quota || !next)
112 ceph_get_snap_realm(mdsc, next);
113 ceph_put_snap_realm(mdsc, realm);
117 ceph_put_snap_realm(mdsc, realm);
122 bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
124 struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
125 struct ceph_snap_realm *old_realm, *new_realm;
128 down_read(&mdsc->snap_rwsem);
129 old_realm = get_quota_realm(mdsc, old);
130 new_realm = get_quota_realm(mdsc, new);
131 is_same = (old_realm == new_realm);
132 up_read(&mdsc->snap_rwsem);
135 ceph_put_snap_realm(mdsc, old_realm);
137 ceph_put_snap_realm(mdsc, new_realm);
142 enum quota_check_op {
143 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
144 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
145 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
146 limit is approaching */
150 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
151 * realm, it will execute quota check operation defined by the 'op' parameter.
152 * The snaprealm walk is interrupted if the quota check detects that the quota
153 * is exceeded or if the root inode is reached.
155 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
158 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
159 struct ceph_inode_info *ci;
160 struct ceph_snap_realm *realm, *next;
163 bool exceeded = false;
165 if (ceph_snap(inode) != CEPH_NOSNAP)
168 down_read(&mdsc->snap_rwsem);
169 realm = ceph_inode(inode)->i_snap_realm;
171 ceph_get_snap_realm(mdsc, realm);
173 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
174 "null i_snap_realm\n", ceph_vinop(inode));
176 spin_lock(&realm->inodes_with_caps_lock);
177 in = realm->inode ? igrab(realm->inode) : NULL;
178 spin_unlock(&realm->inodes_with_caps_lock);
183 spin_lock(&ci->i_ceph_lock);
184 if (op == QUOTA_CHECK_MAX_FILES_OP) {
185 max = ci->i_max_files;
186 rvalue = ci->i_rfiles + ci->i_rsubdirs;
188 max = ci->i_max_bytes;
189 rvalue = ci->i_rbytes;
191 spin_unlock(&ci->i_ceph_lock);
193 case QUOTA_CHECK_MAX_FILES_OP:
194 exceeded = (max && (rvalue >= max));
196 case QUOTA_CHECK_MAX_BYTES_OP:
197 exceeded = (max && (rvalue + delta > max));
199 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
205 * when we're writing more that 1/16th
206 * of the available space
209 (((max - rvalue) >> 4) < delta);
214 /* Shouldn't happen */
215 pr_warn("Invalid quota check op (%d)\n", op);
216 exceeded = true; /* Just break the loop */
220 next = realm->parent;
221 if (exceeded || !next)
223 ceph_get_snap_realm(mdsc, next);
224 ceph_put_snap_realm(mdsc, realm);
228 ceph_put_snap_realm(mdsc, realm);
229 up_read(&mdsc->snap_rwsem);
235 * ceph_quota_is_max_files_exceeded - check if we can create a new file
236 * @inode: directory where a new file is being created
238 * This functions returns true is max_files quota allows a new file to be
239 * created. It is necessary to walk through the snaprealm hierarchy (until the
240 * FS root) to check all realms with quotas set.
242 bool ceph_quota_is_max_files_exceeded(struct inode *inode)
244 if (!ceph_has_realms_with_quotas(inode))
247 WARN_ON(!S_ISDIR(inode->i_mode));
249 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
253 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
254 * @inode: inode being written
255 * @newsize: new size if write succeeds
257 * This functions returns true is max_bytes quota allows a file size to reach
258 * @newsize; it returns false otherwise.
260 bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
262 loff_t size = i_size_read(inode);
264 if (!ceph_has_realms_with_quotas(inode))
267 /* return immediately if we're decreasing file size */
271 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
275 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
276 * @inode: inode being written
277 * @newsize: new size if write succeeds
279 * This function returns true if the new file size @newsize will be consuming
280 * more than 1/16th of the available quota space; it returns false otherwise.
282 bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
284 loff_t size = ceph_inode(inode)->i_reported_size;
286 if (!ceph_has_realms_with_quotas(inode))
289 /* return immediately if we're decreasing file size */
293 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
298 * ceph_quota_update_statfs - if root has quota update statfs with quota status
299 * @fsc: filesystem client instance
300 * @buf: statfs to update
302 * If the mounted filesystem root has max_bytes quota set, update the filesystem
303 * statistics with the quota status.
305 * This function returns true if the stats have been updated, false otherwise.
307 bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
309 struct ceph_mds_client *mdsc = fsc->mdsc;
310 struct ceph_inode_info *ci;
311 struct ceph_snap_realm *realm;
313 u64 total = 0, used, free;
314 bool is_updated = false;
316 down_read(&mdsc->snap_rwsem);
317 realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root));
318 up_read(&mdsc->snap_rwsem);
322 spin_lock(&realm->inodes_with_caps_lock);
323 in = realm->inode ? igrab(realm->inode) : NULL;
324 spin_unlock(&realm->inodes_with_caps_lock);
327 spin_lock(&ci->i_ceph_lock);
328 if (ci->i_max_bytes) {
329 total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
330 used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
331 /* It is possible for a quota to be exceeded.
332 * Report 'zero' in that case
334 free = total > used ? total - used : 0;
336 spin_unlock(&ci->i_ceph_lock);
338 buf->f_blocks = total;
340 buf->f_bavail = free;
345 ceph_put_snap_realm(mdsc, realm);