blob: 4e32c9600ecc881472616436c988ab1ae960ebf8 [file] [log] [blame]
Luis Henriquesfb18a572018-01-05 10:47:18 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * quota.c - CephFS quota
4 *
5 * Copyright (C) 2017-2018 SUSE
Luis Henriquesfb18a572018-01-05 10:47:18 +00006 */
7
Luis Henriques9122eed2018-01-31 10:53:13 +00008#include <linux/statfs.h>
9
Luis Henriquesfb18a572018-01-05 10:47:18 +000010#include "super.h"
11#include "mds_client.h"
12
Luis Henriquesd557c482018-01-12 17:19:29 +000013void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
Luis Henriquescafe21a2018-01-05 10:47:20 +000014{
Xiubo Li2678da82020-09-03 09:01:39 -040015 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
Luis Henriquesd557c482018-01-12 17:19:29 +000016 if (inc)
17 atomic64_inc(&mdsc->quotarealms_count);
18 else
19 atomic64_dec(&mdsc->quotarealms_count);
20}
21
22static inline bool ceph_has_realms_with_quotas(struct inode *inode)
23{
Xiubo Li2678da82020-09-03 09:01:39 -040024 struct super_block *sb = inode->i_sb;
25 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
Jeff Laytonebce3eb2020-08-18 08:03:48 -040026 struct inode *root = d_inode(sb->s_root);
Luis Henriques0c44a8e2019-03-21 10:20:10 +000027
28 if (atomic64_read(&mdsc->quotarealms_count) > 0)
29 return true;
30 /* if root is the real CephFS root, we don't have quota realms */
Jeff Laytonebce3eb2020-08-18 08:03:48 -040031 if (root && ceph_ino(root) == CEPH_INO_ROOT)
Luis Henriques0c44a8e2019-03-21 10:20:10 +000032 return false;
33 /* otherwise, we can't know for sure */
34 return true;
Luis Henriquescafe21a2018-01-05 10:47:20 +000035}
36
Luis Henriquesfb18a572018-01-05 10:47:18 +000037void ceph_handle_quota(struct ceph_mds_client *mdsc,
38 struct ceph_mds_session *session,
39 struct ceph_msg *msg)
40{
41 struct super_block *sb = mdsc->fsc->sb;
42 struct ceph_mds_quota *h = msg->front.iov_base;
43 struct ceph_vino vino;
44 struct inode *inode;
45 struct ceph_inode_info *ci;
46
Yan, Zheng0fcf6c02018-08-03 16:24:49 +080047 if (msg->front.iov_len < sizeof(*h)) {
Luis Henriquesfb18a572018-01-05 10:47:18 +000048 pr_err("%s corrupt message mds%d len %d\n", __func__,
49 session->s_mds, (int)msg->front.iov_len);
50 ceph_msg_dump(msg);
51 return;
52 }
53
54 /* increment msg sequence number */
55 mutex_lock(&session->s_mutex);
Jeff Layton62575e22020-10-12 09:39:06 -040056 inc_session_sequence(session);
Luis Henriquesfb18a572018-01-05 10:47:18 +000057 mutex_unlock(&session->s_mutex);
58
59 /* lookup inode */
60 vino.ino = le64_to_cpu(h->ino);
61 vino.snap = CEPH_NOSNAP;
62 inode = ceph_find_inode(sb, vino);
63 if (!inode) {
64 pr_warn("Failed to find inode %llu\n", vino.ino);
65 return;
66 }
67 ci = ceph_inode(inode);
68
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);
Luis Henriquesd557c482018-01-12 17:19:29 +000073 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
74 le64_to_cpu(h->max_files));
Luis Henriquesfb18a572018-01-05 10:47:18 +000075 spin_unlock(&ci->i_ceph_lock);
76
Yan, Zheng3e1d0452019-05-18 20:39:55 +080077 /* avoid calling iput_final() in dispatch thread */
78 ceph_async_iput(inode);
Luis Henriquesfb18a572018-01-05 10:47:18 +000079}
Luis Henriquesb7a29212018-01-05 10:47:19 +000080
Luis Henriques0c44a8e2019-03-21 10:20:10 +000081static struct ceph_quotarealm_inode *
82find_quotarealm_inode(struct ceph_mds_client *mdsc, u64 ino)
83{
84 struct ceph_quotarealm_inode *qri = NULL;
85 struct rb_node **node, *parent = NULL;
86
87 mutex_lock(&mdsc->quotarealms_inodes_mutex);
88 node = &(mdsc->quotarealms_inodes.rb_node);
89 while (*node) {
90 parent = *node;
91 qri = container_of(*node, struct ceph_quotarealm_inode, node);
92
93 if (ino < qri->ino)
94 node = &((*node)->rb_left);
95 else if (ino > qri->ino)
96 node = &((*node)->rb_right);
97 else
98 break;
99 }
100 if (!qri || (qri->ino != ino)) {
101 /* Not found, create a new one and insert it */
102 qri = kmalloc(sizeof(*qri), GFP_KERNEL);
103 if (qri) {
104 qri->ino = ino;
105 qri->inode = NULL;
106 qri->timeout = 0;
107 mutex_init(&qri->mutex);
108 rb_link_node(&qri->node, parent, node);
109 rb_insert_color(&qri->node, &mdsc->quotarealms_inodes);
110 } else
111 pr_warn("Failed to alloc quotarealms_inode\n");
112 }
113 mutex_unlock(&mdsc->quotarealms_inodes_mutex);
114
115 return qri;
116}
117
118/*
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.
122 *
123 * Note that these inodes are kept in this list even if the lookup fails, which
124 * allows to prevent useless lookup requests.
125 */
126static struct inode *lookup_quotarealm_inode(struct ceph_mds_client *mdsc,
127 struct super_block *sb,
128 struct ceph_snap_realm *realm)
129{
130 struct ceph_quotarealm_inode *qri;
131 struct inode *in;
132
133 qri = find_quotarealm_inode(mdsc, realm->ino);
134 if (!qri)
135 return NULL;
136
137 mutex_lock(&qri->mutex);
Yan, Zheng2ef5df12019-05-31 20:19:18 +0800138 if (qri->inode && ceph_is_any_caps(qri->inode)) {
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000139 /* A request has already returned the inode */
140 mutex_unlock(&qri->mutex);
141 return qri->inode;
142 }
143 /* Check if this inode lookup has failed recently */
144 if (qri->timeout &&
145 time_before_eq(jiffies, qri->timeout)) {
146 mutex_unlock(&qri->mutex);
147 return NULL;
148 }
Yan, Zheng2ef5df12019-05-31 20:19:18 +0800149 if (qri->inode) {
150 /* get caps */
151 int ret = __ceph_do_getattr(qri->inode, NULL,
152 CEPH_STAT_CAP_INODE, true);
153 if (ret >= 0)
154 in = qri->inode;
155 else
156 in = ERR_PTR(ret);
157 } else {
158 in = ceph_lookup_inode(sb, realm->ino);
159 }
160
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000161 if (IS_ERR(in)) {
Luis Henriques12ae44a2020-05-05 13:59:02 +0100162 dout("Can't lookup inode %llx (err: %ld)\n",
163 realm->ino, PTR_ERR(in));
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000164 qri->timeout = jiffies + msecs_to_jiffies(60 * 1000); /* XXX */
165 } else {
166 qri->timeout = 0;
167 qri->inode = in;
168 }
169 mutex_unlock(&qri->mutex);
170
171 return in;
172}
173
174void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc)
175{
176 struct ceph_quotarealm_inode *qri;
177 struct rb_node *node;
178
179 /*
180 * It should now be safe to clean quotarealms_inode tree without holding
181 * mdsc->quotarealms_inodes_mutex...
182 */
183 mutex_lock(&mdsc->quotarealms_inodes_mutex);
184 while (!RB_EMPTY_ROOT(&mdsc->quotarealms_inodes)) {
185 node = rb_first(&mdsc->quotarealms_inodes);
186 qri = rb_entry(node, struct ceph_quotarealm_inode, node);
187 rb_erase(node, &mdsc->quotarealms_inodes);
188 iput(qri->inode);
189 kfree(qri);
190 }
191 mutex_unlock(&mdsc->quotarealms_inodes_mutex);
192}
193
Luis Henriquescafe21a2018-01-05 10:47:20 +0000194/*
195 * This function walks through the snaprealm for an inode and returns the
196 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
197 * or max_bytes). If the root is reached, return the root ceph_snap_realm
198 * instead.
199 *
200 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
201 * returned realm.
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000202 *
203 * Callers of this function need to hold mdsc->snap_rwsem. However, if there's
204 * a need to do an inode lookup, this rwsem will be temporarily dropped. Hence
205 * the 'retry' argument: if rwsem needs to be dropped and 'retry' is 'false'
206 * this function will return -EAGAIN; otherwise, the snaprealms walk-through
207 * will be restarted.
Luis Henriquescafe21a2018-01-05 10:47:20 +0000208 */
209static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000210 struct inode *inode, bool retry)
Luis Henriquescafe21a2018-01-05 10:47:20 +0000211{
212 struct ceph_inode_info *ci = NULL;
213 struct ceph_snap_realm *realm, *next;
Luis Henriquescafe21a2018-01-05 10:47:20 +0000214 struct inode *in;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800215 bool has_quota;
Luis Henriquescafe21a2018-01-05 10:47:20 +0000216
Yan, Zheng25963662018-01-12 16:26:17 +0800217 if (ceph_snap(inode) != CEPH_NOSNAP)
218 return NULL;
219
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000220restart:
Luis Henriquescafe21a2018-01-05 10:47:20 +0000221 realm = ceph_inode(inode)->i_snap_realm;
Yan, Zheng25963662018-01-12 16:26:17 +0800222 if (realm)
223 ceph_get_snap_realm(mdsc, realm);
224 else
225 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
226 "null i_snap_realm\n", ceph_vinop(inode));
Luis Henriquescafe21a2018-01-05 10:47:20 +0000227 while (realm) {
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000228 bool has_inode;
229
Luis Henriquese3161f12018-01-12 17:19:28 +0000230 spin_lock(&realm->inodes_with_caps_lock);
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000231 has_inode = realm->inode;
232 in = has_inode ? igrab(realm->inode) : NULL;
Luis Henriquese3161f12018-01-12 17:19:28 +0000233 spin_unlock(&realm->inodes_with_caps_lock);
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000234 if (has_inode && !in)
Luis Henriquescafe21a2018-01-05 10:47:20 +0000235 break;
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000236 if (!in) {
237 up_read(&mdsc->snap_rwsem);
238 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
239 down_read(&mdsc->snap_rwsem);
240 if (IS_ERR_OR_NULL(in))
241 break;
242 ceph_put_snap_realm(mdsc, realm);
243 if (!retry)
244 return ERR_PTR(-EAGAIN);
245 goto restart;
246 }
Luis Henriquese3161f12018-01-12 17:19:28 +0000247
Luis Henriquescafe21a2018-01-05 10:47:20 +0000248 ci = ceph_inode(in);
Luis Henriquesd557c482018-01-12 17:19:29 +0000249 has_quota = __ceph_has_any_quota(ci);
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800250 /* avoid calling iput_final() while holding mdsc->snap_rwsem */
251 ceph_async_iput(in);
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800252
Luis Henriquescafe21a2018-01-05 10:47:20 +0000253 next = realm->parent;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800254 if (has_quota || !next)
255 return realm;
256
Luis Henriquescafe21a2018-01-05 10:47:20 +0000257 ceph_get_snap_realm(mdsc, next);
258 ceph_put_snap_realm(mdsc, realm);
259 realm = next;
260 }
261 if (realm)
262 ceph_put_snap_realm(mdsc, realm);
263
264 return NULL;
265}
266
Luis Henriques6646ea12020-11-12 15:23:21 +0000267bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
Luis Henriquescafe21a2018-01-05 10:47:20 +0000268{
Xiubo Li2678da82020-09-03 09:01:39 -0400269 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old->i_sb);
Luis Henriquescafe21a2018-01-05 10:47:20 +0000270 struct ceph_snap_realm *old_realm, *new_realm;
271 bool is_same;
272
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000273restart:
274 /*
275 * We need to lookup 2 quota realms atomically, i.e. with snap_rwsem.
276 * However, get_quota_realm may drop it temporarily. By setting the
277 * 'retry' parameter to 'false', we'll get -EAGAIN if the rwsem was
278 * dropped and we can then restart the whole operation.
279 */
Luis Henriquescafe21a2018-01-05 10:47:20 +0000280 down_read(&mdsc->snap_rwsem);
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000281 old_realm = get_quota_realm(mdsc, old, true);
282 new_realm = get_quota_realm(mdsc, new, false);
283 if (PTR_ERR(new_realm) == -EAGAIN) {
284 up_read(&mdsc->snap_rwsem);
285 if (old_realm)
286 ceph_put_snap_realm(mdsc, old_realm);
287 goto restart;
288 }
Luis Henriquescafe21a2018-01-05 10:47:20 +0000289 is_same = (old_realm == new_realm);
290 up_read(&mdsc->snap_rwsem);
291
292 if (old_realm)
293 ceph_put_snap_realm(mdsc, old_realm);
294 if (new_realm)
295 ceph_put_snap_realm(mdsc, new_realm);
296
297 return is_same;
298}
299
Luis Henriquesb7a29212018-01-05 10:47:19 +0000300enum quota_check_op {
Luis Henriques2b838452018-01-05 10:47:21 +0000301 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
Luis Henriques1ab302a2018-01-05 10:47:22 +0000302 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
303 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
304 limit is approaching */
Luis Henriquesb7a29212018-01-05 10:47:19 +0000305};
306
307/*
308 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
309 * realm, it will execute quota check operation defined by the 'op' parameter.
310 * The snaprealm walk is interrupted if the quota check detects that the quota
311 * is exceeded or if the root inode is reached.
312 */
313static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
314 loff_t delta)
315{
Xiubo Li2678da82020-09-03 09:01:39 -0400316 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
Luis Henriquesb7a29212018-01-05 10:47:19 +0000317 struct ceph_inode_info *ci;
318 struct ceph_snap_realm *realm, *next;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000319 struct inode *in;
320 u64 max, rvalue;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000321 bool exceeded = false;
322
Yan, Zheng25963662018-01-12 16:26:17 +0800323 if (ceph_snap(inode) != CEPH_NOSNAP)
324 return false;
325
Luis Henriquesb7a29212018-01-05 10:47:19 +0000326 down_read(&mdsc->snap_rwsem);
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000327restart:
Luis Henriquesb7a29212018-01-05 10:47:19 +0000328 realm = ceph_inode(inode)->i_snap_realm;
Yan, Zheng25963662018-01-12 16:26:17 +0800329 if (realm)
330 ceph_get_snap_realm(mdsc, realm);
331 else
332 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
333 "null i_snap_realm\n", ceph_vinop(inode));
Luis Henriquesb7a29212018-01-05 10:47:19 +0000334 while (realm) {
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000335 bool has_inode;
Luis Henriquese3161f12018-01-12 17:19:28 +0000336
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000337 spin_lock(&realm->inodes_with_caps_lock);
338 has_inode = realm->inode;
339 in = has_inode ? igrab(realm->inode) : NULL;
340 spin_unlock(&realm->inodes_with_caps_lock);
341 if (has_inode && !in)
342 break;
343 if (!in) {
344 up_read(&mdsc->snap_rwsem);
345 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
346 down_read(&mdsc->snap_rwsem);
347 if (IS_ERR_OR_NULL(in))
348 break;
349 ceph_put_snap_realm(mdsc, realm);
350 goto restart;
351 }
Luis Henriquesb7a29212018-01-05 10:47:19 +0000352 ci = ceph_inode(in);
353 spin_lock(&ci->i_ceph_lock);
354 if (op == QUOTA_CHECK_MAX_FILES_OP) {
355 max = ci->i_max_files;
356 rvalue = ci->i_rfiles + ci->i_rsubdirs;
Luis Henriques2b838452018-01-05 10:47:21 +0000357 } else {
358 max = ci->i_max_bytes;
359 rvalue = ci->i_rbytes;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000360 }
Luis Henriquesb7a29212018-01-05 10:47:19 +0000361 spin_unlock(&ci->i_ceph_lock);
362 switch (op) {
363 case QUOTA_CHECK_MAX_FILES_OP:
Luis Henriques2b838452018-01-05 10:47:21 +0000364 case QUOTA_CHECK_MAX_BYTES_OP:
365 exceeded = (max && (rvalue + delta > max));
366 break;
Luis Henriques1ab302a2018-01-05 10:47:22 +0000367 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
368 if (max) {
369 if (rvalue >= max)
370 exceeded = true;
371 else {
372 /*
373 * when we're writing more that 1/16th
374 * of the available space
375 */
376 exceeded =
377 (((max - rvalue) >> 4) < delta);
378 }
379 }
380 break;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000381 default:
382 /* Shouldn't happen */
383 pr_warn("Invalid quota check op (%d)\n", op);
384 exceeded = true; /* Just break the loop */
385 }
Yan, Zheng3e1d0452019-05-18 20:39:55 +0800386 /* avoid calling iput_final() while holding mdsc->snap_rwsem */
387 ceph_async_iput(in);
Luis Henriquesb7a29212018-01-05 10:47:19 +0000388
Luis Henriquesb7a29212018-01-05 10:47:19 +0000389 next = realm->parent;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800390 if (exceeded || !next)
391 break;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000392 ceph_get_snap_realm(mdsc, next);
393 ceph_put_snap_realm(mdsc, realm);
394 realm = next;
395 }
Luis Henriques71f2cc62018-11-05 19:00:52 +0000396 if (realm)
397 ceph_put_snap_realm(mdsc, realm);
Luis Henriquesb7a29212018-01-05 10:47:19 +0000398 up_read(&mdsc->snap_rwsem);
399
400 return exceeded;
401}
402
403/*
404 * ceph_quota_is_max_files_exceeded - check if we can create a new file
405 * @inode: directory where a new file is being created
406 *
407 * This functions returns true is max_files quota allows a new file to be
408 * created. It is necessary to walk through the snaprealm hierarchy (until the
409 * FS root) to check all realms with quotas set.
410 */
411bool ceph_quota_is_max_files_exceeded(struct inode *inode)
412{
Luis Henriquesd557c482018-01-12 17:19:29 +0000413 if (!ceph_has_realms_with_quotas(inode))
414 return false;
415
Luis Henriquesb7a29212018-01-05 10:47:19 +0000416 WARN_ON(!S_ISDIR(inode->i_mode));
417
Luis Henriquesdaa668f2020-04-07 11:30:19 +0100418 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 1);
Luis Henriquesb7a29212018-01-05 10:47:19 +0000419}
Luis Henriques2b838452018-01-05 10:47:21 +0000420
421/*
422 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
423 * @inode: inode being written
424 * @newsize: new size if write succeeds
425 *
426 * This functions returns true is max_bytes quota allows a file size to reach
427 * @newsize; it returns false otherwise.
428 */
429bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
430{
431 loff_t size = i_size_read(inode);
432
Luis Henriquesd557c482018-01-12 17:19:29 +0000433 if (!ceph_has_realms_with_quotas(inode))
434 return false;
435
Luis Henriques2b838452018-01-05 10:47:21 +0000436 /* return immediately if we're decreasing file size */
437 if (newsize <= size)
438 return false;
439
440 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
441}
Luis Henriques1ab302a2018-01-05 10:47:22 +0000442
443/*
444 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
445 * @inode: inode being written
446 * @newsize: new size if write succeeds
447 *
448 * This function returns true if the new file size @newsize will be consuming
449 * more than 1/16th of the available quota space; it returns false otherwise.
450 */
451bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
452{
453 loff_t size = ceph_inode(inode)->i_reported_size;
454
Luis Henriquesd557c482018-01-12 17:19:29 +0000455 if (!ceph_has_realms_with_quotas(inode))
456 return false;
457
Luis Henriques1ab302a2018-01-05 10:47:22 +0000458 /* return immediately if we're decreasing file size */
459 if (newsize <= size)
460 return false;
461
462 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
463 (newsize - size));
464}
Luis Henriques9122eed2018-01-31 10:53:13 +0000465
466/*
467 * ceph_quota_update_statfs - if root has quota update statfs with quota status
468 * @fsc: filesystem client instance
469 * @buf: statfs to update
470 *
471 * If the mounted filesystem root has max_bytes quota set, update the filesystem
472 * statistics with the quota status.
473 *
474 * This function returns true if the stats have been updated, false otherwise.
475 */
476bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
477{
478 struct ceph_mds_client *mdsc = fsc->mdsc;
479 struct ceph_inode_info *ci;
480 struct ceph_snap_realm *realm;
481 struct inode *in;
482 u64 total = 0, used, free;
483 bool is_updated = false;
484
485 down_read(&mdsc->snap_rwsem);
Luis Henriques0c44a8e2019-03-21 10:20:10 +0000486 realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root), true);
Luis Henriques9122eed2018-01-31 10:53:13 +0000487 up_read(&mdsc->snap_rwsem);
488 if (!realm)
489 return false;
490
491 spin_lock(&realm->inodes_with_caps_lock);
492 in = realm->inode ? igrab(realm->inode) : NULL;
493 spin_unlock(&realm->inodes_with_caps_lock);
494 if (in) {
495 ci = ceph_inode(in);
496 spin_lock(&ci->i_ceph_lock);
497 if (ci->i_max_bytes) {
498 total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
499 used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
500 /* It is possible for a quota to be exceeded.
501 * Report 'zero' in that case
502 */
503 free = total > used ? total - used : 0;
504 }
505 spin_unlock(&ci->i_ceph_lock);
506 if (total) {
507 buf->f_blocks = total;
508 buf->f_bfree = free;
509 buf->f_bavail = free;
510 is_updated = true;
511 }
512 iput(in);
513 }
514 ceph_put_snap_realm(mdsc, realm);
515
516 return is_updated;
517}
518