]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/ceph/quota.c
Merge tag 'clk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
[linux.git] / fs / ceph / quota.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * quota.c - CephFS quota
4  *
5  * Copyright (C) 2017-2018 SUSE
6  */
7
8 #include <linux/statfs.h>
9
10 #include "super.h"
11 #include "mds_client.h"
12
13 void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
14 {
15         struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
16         if (inc)
17                 atomic64_inc(&mdsc->quotarealms_count);
18         else
19                 atomic64_dec(&mdsc->quotarealms_count);
20 }
21
22 static inline bool ceph_has_realms_with_quotas(struct inode *inode)
23 {
24         struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
25         struct super_block *sb = mdsc->fsc->sb;
26
27         if (atomic64_read(&mdsc->quotarealms_count) > 0)
28                 return true;
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))
32                 return false;
33         /* otherwise, we can't know for sure */
34         return true;
35 }
36
37 void 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
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);
50                 ceph_msg_dump(msg);
51                 return;
52         }
53
54         /* increment msg sequence number */
55         mutex_lock(&session->s_mutex);
56         session->s_seq++;
57         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);
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);
76
77         iput(inode);
78 }
79
80 static struct ceph_quotarealm_inode *
81 find_quotarealm_inode(struct ceph_mds_client *mdsc, u64 ino)
82 {
83         struct ceph_quotarealm_inode *qri = NULL;
84         struct rb_node **node, *parent = NULL;
85
86         mutex_lock(&mdsc->quotarealms_inodes_mutex);
87         node = &(mdsc->quotarealms_inodes.rb_node);
88         while (*node) {
89                 parent = *node;
90                 qri = container_of(*node, struct ceph_quotarealm_inode, node);
91
92                 if (ino < qri->ino)
93                         node = &((*node)->rb_left);
94                 else if (ino > qri->ino)
95                         node = &((*node)->rb_right);
96                 else
97                         break;
98         }
99         if (!qri || (qri->ino != ino)) {
100                 /* Not found, create a new one and insert it */
101                 qri = kmalloc(sizeof(*qri), GFP_KERNEL);
102                 if (qri) {
103                         qri->ino = ino;
104                         qri->inode = NULL;
105                         qri->timeout = 0;
106                         mutex_init(&qri->mutex);
107                         rb_link_node(&qri->node, parent, node);
108                         rb_insert_color(&qri->node, &mdsc->quotarealms_inodes);
109                 } else
110                         pr_warn("Failed to alloc quotarealms_inode\n");
111         }
112         mutex_unlock(&mdsc->quotarealms_inodes_mutex);
113
114         return qri;
115 }
116
117 /*
118  * This function will try to lookup a realm inode which isn't visible in the
119  * filesystem mountpoint.  A list of these kind of inodes (not visible) is
120  * maintained in the mdsc and freed only when the filesystem is umounted.
121  *
122  * Note that these inodes are kept in this list even if the lookup fails, which
123  * allows to prevent useless lookup requests.
124  */
125 static struct inode *lookup_quotarealm_inode(struct ceph_mds_client *mdsc,
126                                              struct super_block *sb,
127                                              struct ceph_snap_realm *realm)
128 {
129         struct ceph_quotarealm_inode *qri;
130         struct inode *in;
131
132         qri = find_quotarealm_inode(mdsc, realm->ino);
133         if (!qri)
134                 return NULL;
135
136         mutex_lock(&qri->mutex);
137         if (qri->inode) {
138                 /* A request has already returned the inode */
139                 mutex_unlock(&qri->mutex);
140                 return qri->inode;
141         }
142         /* Check if this inode lookup has failed recently */
143         if (qri->timeout &&
144             time_before_eq(jiffies, qri->timeout)) {
145                 mutex_unlock(&qri->mutex);
146                 return NULL;
147         }
148         in = ceph_lookup_inode(sb, realm->ino);
149         if (IS_ERR(in)) {
150                 pr_warn("Can't lookup inode %llx (err: %ld)\n",
151                         realm->ino, PTR_ERR(in));
152                 qri->timeout = jiffies + msecs_to_jiffies(60 * 1000); /* XXX */
153         } else {
154                 qri->timeout = 0;
155                 qri->inode = in;
156         }
157         mutex_unlock(&qri->mutex);
158
159         return in;
160 }
161
162 void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc)
163 {
164         struct ceph_quotarealm_inode *qri;
165         struct rb_node *node;
166
167         /*
168          * It should now be safe to clean quotarealms_inode tree without holding
169          * mdsc->quotarealms_inodes_mutex...
170          */
171         mutex_lock(&mdsc->quotarealms_inodes_mutex);
172         while (!RB_EMPTY_ROOT(&mdsc->quotarealms_inodes)) {
173                 node = rb_first(&mdsc->quotarealms_inodes);
174                 qri = rb_entry(node, struct ceph_quotarealm_inode, node);
175                 rb_erase(node, &mdsc->quotarealms_inodes);
176                 iput(qri->inode);
177                 kfree(qri);
178         }
179         mutex_unlock(&mdsc->quotarealms_inodes_mutex);
180 }
181
182 /*
183  * This function walks through the snaprealm for an inode and returns the
184  * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
185  * or max_bytes).  If the root is reached, return the root ceph_snap_realm
186  * instead.
187  *
188  * Note that the caller is responsible for calling ceph_put_snap_realm() on the
189  * returned realm.
190  *
191  * Callers of this function need to hold mdsc->snap_rwsem.  However, if there's
192  * a need to do an inode lookup, this rwsem will be temporarily dropped.  Hence
193  * the 'retry' argument: if rwsem needs to be dropped and 'retry' is 'false'
194  * this function will return -EAGAIN; otherwise, the snaprealms walk-through
195  * will be restarted.
196  */
197 static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
198                                                struct inode *inode, bool retry)
199 {
200         struct ceph_inode_info *ci = NULL;
201         struct ceph_snap_realm *realm, *next;
202         struct inode *in;
203         bool has_quota;
204
205         if (ceph_snap(inode) != CEPH_NOSNAP)
206                 return NULL;
207
208 restart:
209         realm = ceph_inode(inode)->i_snap_realm;
210         if (realm)
211                 ceph_get_snap_realm(mdsc, realm);
212         else
213                 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
214                                    "null i_snap_realm\n", ceph_vinop(inode));
215         while (realm) {
216                 bool has_inode;
217
218                 spin_lock(&realm->inodes_with_caps_lock);
219                 has_inode = realm->inode;
220                 in = has_inode ? igrab(realm->inode) : NULL;
221                 spin_unlock(&realm->inodes_with_caps_lock);
222                 if (has_inode && !in)
223                         break;
224                 if (!in) {
225                         up_read(&mdsc->snap_rwsem);
226                         in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
227                         down_read(&mdsc->snap_rwsem);
228                         if (IS_ERR_OR_NULL(in))
229                                 break;
230                         ceph_put_snap_realm(mdsc, realm);
231                         if (!retry)
232                                 return ERR_PTR(-EAGAIN);
233                         goto restart;
234                 }
235
236                 ci = ceph_inode(in);
237                 has_quota = __ceph_has_any_quota(ci);
238                 iput(in);
239
240                 next = realm->parent;
241                 if (has_quota || !next)
242                        return realm;
243
244                 ceph_get_snap_realm(mdsc, next);
245                 ceph_put_snap_realm(mdsc, realm);
246                 realm = next;
247         }
248         if (realm)
249                 ceph_put_snap_realm(mdsc, realm);
250
251         return NULL;
252 }
253
254 bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
255 {
256         struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
257         struct ceph_snap_realm *old_realm, *new_realm;
258         bool is_same;
259
260 restart:
261         /*
262          * We need to lookup 2 quota realms atomically, i.e. with snap_rwsem.
263          * However, get_quota_realm may drop it temporarily.  By setting the
264          * 'retry' parameter to 'false', we'll get -EAGAIN if the rwsem was
265          * dropped and we can then restart the whole operation.
266          */
267         down_read(&mdsc->snap_rwsem);
268         old_realm = get_quota_realm(mdsc, old, true);
269         new_realm = get_quota_realm(mdsc, new, false);
270         if (PTR_ERR(new_realm) == -EAGAIN) {
271                 up_read(&mdsc->snap_rwsem);
272                 if (old_realm)
273                         ceph_put_snap_realm(mdsc, old_realm);
274                 goto restart;
275         }
276         is_same = (old_realm == new_realm);
277         up_read(&mdsc->snap_rwsem);
278
279         if (old_realm)
280                 ceph_put_snap_realm(mdsc, old_realm);
281         if (new_realm)
282                 ceph_put_snap_realm(mdsc, new_realm);
283
284         return is_same;
285 }
286
287 enum quota_check_op {
288         QUOTA_CHECK_MAX_FILES_OP,       /* check quota max_files limit */
289         QUOTA_CHECK_MAX_BYTES_OP,       /* check quota max_files limit */
290         QUOTA_CHECK_MAX_BYTES_APPROACHING_OP    /* check if quota max_files
291                                                    limit is approaching */
292 };
293
294 /*
295  * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
296  * realm, it will execute quota check operation defined by the 'op' parameter.
297  * The snaprealm walk is interrupted if the quota check detects that the quota
298  * is exceeded or if the root inode is reached.
299  */
300 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
301                                  loff_t delta)
302 {
303         struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
304         struct ceph_inode_info *ci;
305         struct ceph_snap_realm *realm, *next;
306         struct inode *in;
307         u64 max, rvalue;
308         bool exceeded = false;
309
310         if (ceph_snap(inode) != CEPH_NOSNAP)
311                 return false;
312
313         down_read(&mdsc->snap_rwsem);
314 restart:
315         realm = ceph_inode(inode)->i_snap_realm;
316         if (realm)
317                 ceph_get_snap_realm(mdsc, realm);
318         else
319                 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
320                                    "null i_snap_realm\n", ceph_vinop(inode));
321         while (realm) {
322                 bool has_inode;
323
324                 spin_lock(&realm->inodes_with_caps_lock);
325                 has_inode = realm->inode;
326                 in = has_inode ? igrab(realm->inode) : NULL;
327                 spin_unlock(&realm->inodes_with_caps_lock);
328                 if (has_inode && !in)
329                         break;
330                 if (!in) {
331                         up_read(&mdsc->snap_rwsem);
332                         in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm);
333                         down_read(&mdsc->snap_rwsem);
334                         if (IS_ERR_OR_NULL(in))
335                                 break;
336                         ceph_put_snap_realm(mdsc, realm);
337                         goto restart;
338                 }
339                 ci = ceph_inode(in);
340                 spin_lock(&ci->i_ceph_lock);
341                 if (op == QUOTA_CHECK_MAX_FILES_OP) {
342                         max = ci->i_max_files;
343                         rvalue = ci->i_rfiles + ci->i_rsubdirs;
344                 } else {
345                         max = ci->i_max_bytes;
346                         rvalue = ci->i_rbytes;
347                 }
348                 spin_unlock(&ci->i_ceph_lock);
349                 switch (op) {
350                 case QUOTA_CHECK_MAX_FILES_OP:
351                         exceeded = (max && (rvalue >= max));
352                         break;
353                 case QUOTA_CHECK_MAX_BYTES_OP:
354                         exceeded = (max && (rvalue + delta > max));
355                         break;
356                 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
357                         if (max) {
358                                 if (rvalue >= max)
359                                         exceeded = true;
360                                 else {
361                                         /*
362                                          * when we're writing more that 1/16th
363                                          * of the available space
364                                          */
365                                         exceeded =
366                                                 (((max - rvalue) >> 4) < delta);
367                                 }
368                         }
369                         break;
370                 default:
371                         /* Shouldn't happen */
372                         pr_warn("Invalid quota check op (%d)\n", op);
373                         exceeded = true; /* Just break the loop */
374                 }
375                 iput(in);
376
377                 next = realm->parent;
378                 if (exceeded || !next)
379                         break;
380                 ceph_get_snap_realm(mdsc, next);
381                 ceph_put_snap_realm(mdsc, realm);
382                 realm = next;
383         }
384         if (realm)
385                 ceph_put_snap_realm(mdsc, realm);
386         up_read(&mdsc->snap_rwsem);
387
388         return exceeded;
389 }
390
391 /*
392  * ceph_quota_is_max_files_exceeded - check if we can create a new file
393  * @inode:      directory where a new file is being created
394  *
395  * This functions returns true is max_files quota allows a new file to be
396  * created.  It is necessary to walk through the snaprealm hierarchy (until the
397  * FS root) to check all realms with quotas set.
398  */
399 bool ceph_quota_is_max_files_exceeded(struct inode *inode)
400 {
401         if (!ceph_has_realms_with_quotas(inode))
402                 return false;
403
404         WARN_ON(!S_ISDIR(inode->i_mode));
405
406         return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
407 }
408
409 /*
410  * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
411  * @inode:      inode being written
412  * @newsize:    new size if write succeeds
413  *
414  * This functions returns true is max_bytes quota allows a file size to reach
415  * @newsize; it returns false otherwise.
416  */
417 bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
418 {
419         loff_t size = i_size_read(inode);
420
421         if (!ceph_has_realms_with_quotas(inode))
422                 return false;
423
424         /* return immediately if we're decreasing file size */
425         if (newsize <= size)
426                 return false;
427
428         return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
429 }
430
431 /*
432  * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
433  * @inode:      inode being written
434  * @newsize:    new size if write succeeds
435  *
436  * This function returns true if the new file size @newsize will be consuming
437  * more than 1/16th of the available quota space; it returns false otherwise.
438  */
439 bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
440 {
441         loff_t size = ceph_inode(inode)->i_reported_size;
442
443         if (!ceph_has_realms_with_quotas(inode))
444                 return false;
445
446         /* return immediately if we're decreasing file size */
447         if (newsize <= size)
448                 return false;
449
450         return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
451                                     (newsize - size));
452 }
453
454 /*
455  * ceph_quota_update_statfs - if root has quota update statfs with quota status
456  * @fsc:        filesystem client instance
457  * @buf:        statfs to update
458  *
459  * If the mounted filesystem root has max_bytes quota set, update the filesystem
460  * statistics with the quota status.
461  *
462  * This function returns true if the stats have been updated, false otherwise.
463  */
464 bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf)
465 {
466         struct ceph_mds_client *mdsc = fsc->mdsc;
467         struct ceph_inode_info *ci;
468         struct ceph_snap_realm *realm;
469         struct inode *in;
470         u64 total = 0, used, free;
471         bool is_updated = false;
472
473         down_read(&mdsc->snap_rwsem);
474         realm = get_quota_realm(mdsc, d_inode(fsc->sb->s_root), true);
475         up_read(&mdsc->snap_rwsem);
476         if (!realm)
477                 return false;
478
479         spin_lock(&realm->inodes_with_caps_lock);
480         in = realm->inode ? igrab(realm->inode) : NULL;
481         spin_unlock(&realm->inodes_with_caps_lock);
482         if (in) {
483                 ci = ceph_inode(in);
484                 spin_lock(&ci->i_ceph_lock);
485                 if (ci->i_max_bytes) {
486                         total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT;
487                         used = ci->i_rbytes >> CEPH_BLOCK_SHIFT;
488                         /* It is possible for a quota to be exceeded.
489                          * Report 'zero' in that case
490                          */
491                         free = total > used ? total - used : 0;
492                 }
493                 spin_unlock(&ci->i_ceph_lock);
494                 if (total) {
495                         buf->f_blocks = total;
496                         buf->f_bfree = free;
497                         buf->f_bavail = free;
498                         is_updated = true;
499                 }
500                 iput(in);
501         }
502         ceph_put_snap_realm(mdsc, realm);
503
504         return is_updated;
505 }
506