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 struct super_block *sb = mdsc->fsc->sb;
27 if (atomic64_read(&mdsc->quotarealms_count) > 0)
29 /* if root is the real CephFS root, we don't have quota realms */
30 if (sb->s_root->d_inode &&
31 (sb->s_root->d_inode->i_ino == CEPH_INO_ROOT))
33 /* otherwise, we can't know for sure */
37 void ceph_handle_quota(struct ceph_mds_client *mdsc,
38 struct ceph_mds_session *session,
41 struct super_block *sb = mdsc->fsc->sb;
42 struct ceph_mds_quota *h = msg->front.iov_base;
43 struct ceph_vino vino;
45 struct ceph_inode_info *ci;
47 if (msg->front.iov_len < sizeof(*h)) {
48 pr_err("%s corrupt message mds%d len %d\n", __func__,
49 session->s_mds, (int)msg->front.iov_len);
54 /* increment msg sequence number */
55 mutex_lock(&session->s_mutex);
57 mutex_unlock(&session->s_mutex);
60 vino.ino = le64_to_cpu(h->ino);
61 vino.snap = CEPH_NOSNAP;
62 inode = ceph_find_inode(sb, vino);
64 pr_warn("Failed to find inode %llu\n", vino.ino);
67 ci = ceph_inode(inode);
69 spin_lock(&ci->i_ceph_lock);
70 ci->i_rbytes = le64_to_cpu(h->rbytes);
71 ci->i_rfiles = le64_to_cpu(h->rfiles);
72 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
73 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
74 le64_to_cpu(h->max_files));
75 spin_unlock(&ci->i_ceph_lock);
77 /* avoid calling iput_final() in dispatch thread */
78 ceph_async_iput(inode);
81 static struct ceph_quotarealm_inode *
82 find_quotarealm_inode(struct ceph_mds_client *mdsc, u64 ino)
84 struct ceph_quotarealm_inode *qri = NULL;
85 struct rb_node **node, *parent = NULL;
87 mutex_lock(&mdsc->quotarealms_inodes_mutex);
88 node = &(mdsc->quotarealms_inodes.rb_node);
91 qri = container_of(*node, struct ceph_quotarealm_inode, node);
94 node = &((*node)->rb_left);
95 else if (ino > qri->ino)
96 node = &((*node)->rb_right);
100 if (!qri || (qri->ino != ino)) {
101 /* Not found, create a new one and insert it */
102 qri = kmalloc(sizeof(*qri), GFP_KERNEL);
107 mutex_init(&qri->mutex);
108 rb_link_node(&qri->node, parent, node);
109 rb_insert_color(&qri->node, &mdsc->quotarealms_inodes);
111 pr_warn("Failed to alloc quotarealms_inode\n");
113 mutex_unlock(&mdsc->quotarealms_inodes_mutex);
119 * This function will try to lookup a realm inode which isn't visible in the
120 * filesystem mountpoint. A list of these kind of inodes (not visible) is
121 * maintained in the mdsc and freed only when the filesystem is umounted.
123 * Note that these inodes are kept in this list even if the lookup fails, which
124 * allows to prevent useless lookup requests.
126 static struct inode *lookup_quotarealm_inode(struct ceph_mds_client *mdsc,
127 struct super_block *sb,
128 struct ceph_snap_realm *realm)
130 struct ceph_quotarealm_inode *qri;
133 qri = find_quotarealm_inode(mdsc, realm->ino);
137 mutex_lock(&qri->mutex);
139 /* A request has already returned the inode */
140 mutex_unlock(&qri->mutex);
143 /* Check if this inode lookup has failed recently */
145 time_before_eq(jiffies, qri->timeout)) {
146 mutex_unlock(&qri->mutex);
149 in = ceph_lookup_inode(sb, realm->ino);
151 pr_warn("Can't lookup inode %llx (err: %ld)\n",
152 realm->ino, PTR_ERR(in));
153 qri->timeout = jiffies + msecs_to_jiffies(60 * 1000); /* XXX */
158 mutex_unlock(&qri->mutex);
163 void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc)
165 struct ceph_quotarealm_inode *qri;
166 struct rb_node *node;
169 * It should now be safe to clean quotarealms_inode tree without holding
170 * mdsc->quotarealms_inodes_mutex...
172 mutex_lock(&mdsc->quotarealms_inodes_mutex);
173 while (!RB_EMPTY_ROOT(&mdsc->quotarealms_inodes)) {
174 node = rb_first(&mdsc->quotarealms_inodes);
175 qri = rb_entry(node, struct ceph_quotarealm_inode, node);
176 rb_erase(node, &mdsc->quotarealms_inodes);
180 mutex_unlock(&mdsc->quotarealms_inodes_mutex);
184 * This function walks through the snaprealm for an inode and returns the
185 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
186 * or max_bytes). If the root is reached, return the root ceph_snap_realm
189 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
192 * Callers of this function need to hold mdsc->snap_rwsem. However, if there's
193 * a need to do an inode lookup, this rwsem will be temporarily dropped. Hence
194 * the 'retry' argument: if rwsem needs to be dropped and 'retry' is 'false'
195 * this function will return -EAGAIN; otherwise, the snaprealms walk-through
198 static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
199 struct inode *inode, bool retry)
201 struct ceph_inode_info *ci = NULL;
202 struct ceph_snap_realm *realm, *next;
206 if (ceph_snap(inode) != CEPH_NOSNAP)
210 realm = ceph_inode(inode)->i_snap_realm;
212 ceph_get_snap_realm(mdsc, realm);
214 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
215 "null i_snap_realm\n", ceph_vinop(inode));
219 spin_lock(&realm->inodes_with_caps_lock);
220 has_inode = realm->inode;
221 in = has_inode ? igrab(realm->inode) : NULL;
222 spin_unlock(&realm->inodes_with_caps_lock);
223 if (has_inode && !in)
226 up_read(&mdsc->snap_rwsem);
227 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
228 down_read(&mdsc->snap_rwsem);
229 if (IS_ERR_OR_NULL(in))
231 ceph_put_snap_realm(mdsc, realm);
233 return ERR_PTR(-EAGAIN);
238 has_quota = __ceph_has_any_quota(ci);
239 /* avoid calling iput_final() while holding mdsc->snap_rwsem */
242 next = realm->parent;
243 if (has_quota || !next)
246 ceph_get_snap_realm(mdsc, next);
247 ceph_put_snap_realm(mdsc, realm);
251 ceph_put_snap_realm(mdsc, realm);
256 bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
258 struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
259 struct ceph_snap_realm *old_realm, *new_realm;
264 * We need to lookup 2 quota realms atomically, i.e. with snap_rwsem.
265 * However, get_quota_realm may drop it temporarily. By setting the
266 * 'retry' parameter to 'false', we'll get -EAGAIN if the rwsem was
267 * dropped and we can then restart the whole operation.
269 down_read(&mdsc->snap_rwsem);
270 old_realm = get_quota_realm(mdsc, old, true);
271 new_realm = get_quota_realm(mdsc, new, false);
272 if (PTR_ERR(new_realm) == -EAGAIN) {
273 up_read(&mdsc->snap_rwsem);
275 ceph_put_snap_realm(mdsc, old_realm);
278 is_same = (old_realm == new_realm);
279 up_read(&mdsc->snap_rwsem);
282 ceph_put_snap_realm(mdsc, old_realm);
284 ceph_put_snap_realm(mdsc, new_realm);
289 enum quota_check_op {
290 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
291 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
292 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
293 limit is approaching */
297 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
298 * realm, it will execute quota check operation defined by the 'op' parameter.
299 * The snaprealm walk is interrupted if the quota check detects that the quota
300 * is exceeded or if the root inode is reached.
302 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
305 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
306 struct ceph_inode_info *ci;
307 struct ceph_snap_realm *realm, *next;
310 bool exceeded = false;
312 if (ceph_snap(inode) != CEPH_NOSNAP)
315 down_read(&mdsc->snap_rwsem);
317 realm = ceph_inode(inode)->i_snap_realm;
319 ceph_get_snap_realm(mdsc, realm);
321 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
322 "null i_snap_realm\n", ceph_vinop(inode));
326 spin_lock(&realm->inodes_with_caps_lock);
327 has_inode = realm->inode;
328 in = has_inode ? igrab(realm->inode) : NULL;
329 spin_unlock(&realm->inodes_with_caps_lock);
330 if (has_inode && !in)
333 up_read(&mdsc->snap_rwsem);
334 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
335 down_read(&mdsc->snap_rwsem);
336 if (IS_ERR_OR_NULL(in))
338 ceph_put_snap_realm(mdsc, realm);
342 spin_lock(&ci->i_ceph_lock);
343 if (op == QUOTA_CHECK_MAX_FILES_OP) {
344 max = ci->i_max_files;
345 rvalue = ci->i_rfiles + ci->i_rsubdirs;
347 max = ci->i_max_bytes;
348 rvalue = ci->i_rbytes;
350 spin_unlock(&ci->i_ceph_lock);
352 case QUOTA_CHECK_MAX_FILES_OP:
353 exceeded = (max && (rvalue >= max));
355 case QUOTA_CHECK_MAX_BYTES_OP:
356 exceeded = (max && (rvalue + delta > max));
358 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
364 * when we're writing more that 1/16th
365 * of the available space
368 (((max - rvalue) >> 4) < delta);
373 /* Shouldn't happen */
374 pr_warn("Invalid quota check op (%d)\n", op);
375 exceeded = true; /* Just break the loop */
377 /* avoid calling iput_final() while holding mdsc->snap_rwsem */
380 next = realm->parent;
381 if (exceeded || !next)
383 ceph_get_snap_realm(mdsc, next);
384 ceph_put_snap_realm(mdsc, realm);
388 ceph_put_snap_realm(mdsc, realm);
389 up_read(&mdsc->snap_rwsem);
395 * ceph_quota_is_max_files_exceeded - check if we can create a new file
396 * @inode: directory where a new file is being created
398 * This functions returns true is max_files quota allows a new file to be
399 * created. It is necessary to walk through the snaprealm hierarchy (until the
400 * FS root) to check all realms with quotas set.
402 bool ceph_quota_is_max_files_exceeded(struct inode *inode)
404 if (!ceph_has_realms_with_quotas(inode))
407 WARN_ON(!S_ISDIR(inode->i_mode));
409 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
413 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
414 * @inode: inode being written
415 * @newsize: new size if write succeeds
417 * This functions returns true is max_bytes quota allows a file size to reach
418 * @newsize; it returns false otherwise.
420 bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
422 loff_t size = i_size_read(inode);
424 if (!ceph_has_realms_with_quotas(inode))
427 /* return immediately if we're decreasing file size */
431 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
435 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
436 * @inode: inode being written
437 * @newsize: new size if write succeeds
439 * This function returns true if the new file size @newsize will be consuming
440 * more than 1/16th of the available quota space; it returns false otherwise.
442 bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
444 loff_t size = ceph_inode(inode)->i_reported_size;
446 if (!ceph_has_realms_with_quotas(inode))
449 /* return immediately if we're decreasing file size */
453 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
458 * ceph_quota_update_statfs - if root has quota update statfs with quota status
459 * @fsc: filesystem client instance
460 * @buf: statfs to update
462 * If the mounted filesystem root has max_bytes quota set, update the filesystem
463 * statistics with the quota status.
465 * This function returns true if the stats have been updated, false otherwise.
467 bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
469 struct ceph_mds_client *mdsc = fsc->mdsc;
470 struct ceph_inode_info *ci;
471 struct ceph_snap_realm *realm;
473 u64 total = 0, used, free;
474 bool is_updated = false;
476 down_read(&mdsc->snap_rwsem);
477 realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root), true);
478 up_read(&mdsc->snap_rwsem);
482 spin_lock(&realm->inodes_with_caps_lock);
483 in = realm->inode ? igrab(realm->inode) : NULL;
484 spin_unlock(&realm->inodes_with_caps_lock);
487 spin_lock(&ci->i_ceph_lock);
488 if (ci->i_max_bytes) {
489 total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
490 used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
491 /* It is possible for a quota to be exceeded.
492 * Report 'zero' in that case
494 free = total > used ? total - used : 0;
496 spin_unlock(&ci->i_ceph_lock);
498 buf->f_blocks = total;
500 buf->f_bavail = free;
505 ceph_put_snap_realm(mdsc, realm);