]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ceph/quota.c
ceph: quota: update MDS when max_bytes is approaching
[mirror_ubuntu-bionic-kernel.git] / fs / ceph / quota.c
CommitLineData
7370e8a4
LH
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * quota.c - CephFS quota
4 *
5 * Copyright (C) 2017-2018 SUSE
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "super.h"
22#include "mds_client.h"
23
68a16a23
LH
24static inline bool ceph_has_quota(struct ceph_inode_info *ci)
25{
26 return (ci && (ci->i_max_files || ci->i_max_bytes));
27}
28
7370e8a4
LH
29void ceph_handle_quota(struct ceph_mds_client *mdsc,
30 struct ceph_mds_session *session,
31 struct ceph_msg *msg)
32{
33 struct super_block *sb = mdsc->fsc->sb;
34 struct ceph_mds_quota *h = msg->front.iov_base;
35 struct ceph_vino vino;
36 struct inode *inode;
37 struct ceph_inode_info *ci;
38
39 if (msg->front.iov_len != sizeof(*h)) {
40 pr_err("%s corrupt message mds%d len %d\n", __func__,
41 session->s_mds, (int)msg->front.iov_len);
42 ceph_msg_dump(msg);
43 return;
44 }
45
46 /* increment msg sequence number */
47 mutex_lock(&session->s_mutex);
48 session->s_seq++;
49 mutex_unlock(&session->s_mutex);
50
51 /* lookup inode */
52 vino.ino = le64_to_cpu(h->ino);
53 vino.snap = CEPH_NOSNAP;
54 inode = ceph_find_inode(sb, vino);
55 if (!inode) {
56 pr_warn("Failed to find inode %llu\n", vino.ino);
57 return;
58 }
59 ci = ceph_inode(inode);
60
61 spin_lock(&ci->i_ceph_lock);
62 ci->i_rbytes = le64_to_cpu(h->rbytes);
63 ci->i_rfiles = le64_to_cpu(h->rfiles);
64 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
65 ci->i_max_bytes = le64_to_cpu(h->max_bytes);
66 ci->i_max_files = le64_to_cpu(h->max_files);
67 spin_unlock(&ci->i_ceph_lock);
68
69 iput(inode);
70}
6aa007ee 71
68a16a23
LH
72/*
73 * This function walks through the snaprealm for an inode and returns the
74 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
75 * or max_bytes). If the root is reached, return the root ceph_snap_realm
76 * instead.
77 *
78 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
79 * returned realm.
80 */
81static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
82 struct inode *inode)
83{
84 struct ceph_inode_info *ci = NULL;
85 struct ceph_snap_realm *realm, *next;
86 struct ceph_vino vino;
87 struct inode *in;
780ac19f 88 bool has_quota;
68a16a23
LH
89
90 realm = ceph_inode(inode)->i_snap_realm;
91 ceph_get_snap_realm(mdsc, realm);
92 while (realm) {
93 vino.ino = realm->ino;
94 vino.snap = CEPH_NOSNAP;
95 in = ceph_find_inode(inode->i_sb, vino);
96 if (!in) {
97 pr_warn("Failed to find inode for %llu\n", vino.ino);
98 break;
99 }
100 ci = ceph_inode(in);
780ac19f 101 has_quota = ceph_has_quota(ci);
68a16a23 102 iput(in);
780ac19f 103
68a16a23 104 next = realm->parent;
780ac19f
YZ
105 if (has_quota || !next)
106 return realm;
107
68a16a23
LH
108 ceph_get_snap_realm(mdsc, next);
109 ceph_put_snap_realm(mdsc, realm);
110 realm = next;
111 }
112 if (realm)
113 ceph_put_snap_realm(mdsc, realm);
114
115 return NULL;
116}
117
118bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
119{
120 struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
121 struct ceph_snap_realm *old_realm, *new_realm;
122 bool is_same;
123
124 down_read(&mdsc->snap_rwsem);
125 old_realm = get_quota_realm(mdsc, old);
126 new_realm = get_quota_realm(mdsc, new);
127 is_same = (old_realm == new_realm);
128 up_read(&mdsc->snap_rwsem);
129
130 if (old_realm)
131 ceph_put_snap_realm(mdsc, old_realm);
132 if (new_realm)
133 ceph_put_snap_realm(mdsc, new_realm);
134
135 return is_same;
136}
137
6aa007ee 138enum quota_check_op {
0cb65d53 139 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
763b0c3d
LH
140 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
141 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
142 limit is approaching */
6aa007ee
LH
143};
144
145/*
146 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
147 * realm, it will execute quota check operation defined by the 'op' parameter.
148 * The snaprealm walk is interrupted if the quota check detects that the quota
149 * is exceeded or if the root inode is reached.
150 */
151static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
152 loff_t delta)
153{
154 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
155 struct ceph_inode_info *ci;
156 struct ceph_snap_realm *realm, *next;
157 struct ceph_vino vino;
158 struct inode *in;
159 u64 max, rvalue;
6aa007ee
LH
160 bool exceeded = false;
161
162 down_read(&mdsc->snap_rwsem);
163 realm = ceph_inode(inode)->i_snap_realm;
164 ceph_get_snap_realm(mdsc, realm);
165 while (realm) {
166 vino.ino = realm->ino;
167 vino.snap = CEPH_NOSNAP;
168 in = ceph_find_inode(inode->i_sb, vino);
169 if (!in) {
170 pr_warn("Failed to find inode for %llu\n", vino.ino);
171 break;
172 }
173 ci = ceph_inode(in);
174 spin_lock(&ci->i_ceph_lock);
175 if (op == QUOTA_CHECK_MAX_FILES_OP) {
176 max = ci->i_max_files;
177 rvalue = ci->i_rfiles + ci->i_rsubdirs;
0cb65d53
LH
178 } else {
179 max = ci->i_max_bytes;
180 rvalue = ci->i_rbytes;
6aa007ee 181 }
6aa007ee
LH
182 spin_unlock(&ci->i_ceph_lock);
183 switch (op) {
184 case QUOTA_CHECK_MAX_FILES_OP:
185 exceeded = (max && (rvalue >= max));
186 break;
0cb65d53
LH
187 case QUOTA_CHECK_MAX_BYTES_OP:
188 exceeded = (max && (rvalue + delta > max));
189 break;
763b0c3d
LH
190 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
191 if (max) {
192 if (rvalue >= max)
193 exceeded = true;
194 else {
195 /*
196 * when we're writing more that 1/16th
197 * of the available space
198 */
199 exceeded =
200 (((max - rvalue) >> 4) < delta);
201 }
202 }
203 break;
6aa007ee
LH
204 default:
205 /* Shouldn't happen */
206 pr_warn("Invalid quota check op (%d)\n", op);
207 exceeded = true; /* Just break the loop */
208 }
209 iput(in);
210
6aa007ee 211 next = realm->parent;
780ac19f
YZ
212 if (exceeded || !next)
213 break;
6aa007ee
LH
214 ceph_get_snap_realm(mdsc, next);
215 ceph_put_snap_realm(mdsc, realm);
216 realm = next;
217 }
218 ceph_put_snap_realm(mdsc, realm);
219 up_read(&mdsc->snap_rwsem);
220
221 return exceeded;
222}
223
224/*
225 * ceph_quota_is_max_files_exceeded - check if we can create a new file
226 * @inode: directory where a new file is being created
227 *
228 * This functions returns true is max_files quota allows a new file to be
229 * created. It is necessary to walk through the snaprealm hierarchy (until the
230 * FS root) to check all realms with quotas set.
231 */
232bool ceph_quota_is_max_files_exceeded(struct inode *inode)
233{
234 WARN_ON(!S_ISDIR(inode->i_mode));
235
236 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
237}
0cb65d53
LH
238
239/*
240 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
241 * @inode: inode being written
242 * @newsize: new size if write succeeds
243 *
244 * This functions returns true is max_bytes quota allows a file size to reach
245 * @newsize; it returns false otherwise.
246 */
247bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
248{
249 loff_t size = i_size_read(inode);
250
251 /* return immediately if we're decreasing file size */
252 if (newsize <= size)
253 return false;
254
255 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
256}
763b0c3d
LH
257
258/*
259 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
260 * @inode: inode being written
261 * @newsize: new size if write succeeds
262 *
263 * This function returns true if the new file size @newsize will be consuming
264 * more than 1/16th of the available quota space; it returns false otherwise.
265 */
266bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
267{
268 loff_t size = ceph_inode(inode)->i_reported_size;
269
270 /* return immediately if we're decreasing file size */
271 if (newsize <= size)
272 return false;
273
274 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
275 (newsize - size));
276}