]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/ceph/quota.c
ceph: quota: add counter for snaprealms with quota
[mirror_ubuntu-bionic-kernel.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 * 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
24 void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
25 {
26 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
27 if (inc)
28 atomic64_inc(&mdsc->quotarealms_count);
29 else
30 atomic64_dec(&mdsc->quotarealms_count);
31 }
32
33 static inline bool ceph_has_realms_with_quotas(struct inode *inode)
34 {
35 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
36 return atomic64_read(&mdsc->quotarealms_count) > 0;
37 }
38
39 void ceph_handle_quota(struct ceph_mds_client *mdsc,
40 struct ceph_mds_session *session,
41 struct ceph_msg *msg)
42 {
43 struct super_block *sb = mdsc->fsc->sb;
44 struct ceph_mds_quota *h = msg->front.iov_base;
45 struct ceph_vino vino;
46 struct inode *inode;
47 struct ceph_inode_info *ci;
48
49 if (msg->front.iov_len != sizeof(*h)) {
50 pr_err("%s corrupt message mds%d len %d\n", __func__,
51 session->s_mds, (int)msg->front.iov_len);
52 ceph_msg_dump(msg);
53 return;
54 }
55
56 /* increment msg sequence number */
57 mutex_lock(&session->s_mutex);
58 session->s_seq++;
59 mutex_unlock(&session->s_mutex);
60
61 /* lookup inode */
62 vino.ino = le64_to_cpu(h->ino);
63 vino.snap = CEPH_NOSNAP;
64 inode = ceph_find_inode(sb, vino);
65 if (!inode) {
66 pr_warn("Failed to find inode %llu\n", vino.ino);
67 return;
68 }
69 ci = ceph_inode(inode);
70
71 spin_lock(&ci->i_ceph_lock);
72 ci->i_rbytes = le64_to_cpu(h->rbytes);
73 ci->i_rfiles = le64_to_cpu(h->rfiles);
74 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs);
75 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
76 le64_to_cpu(h->max_files));
77 spin_unlock(&ci->i_ceph_lock);
78
79 iput(inode);
80 }
81
82 /*
83 * This function walks through the snaprealm for an inode and returns the
84 * ceph_snap_realm for the first snaprealm that has quotas set (either max_files
85 * or max_bytes). If the root is reached, return the root ceph_snap_realm
86 * instead.
87 *
88 * Note that the caller is responsible for calling ceph_put_snap_realm() on the
89 * returned realm.
90 */
91 static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc,
92 struct inode *inode)
93 {
94 struct ceph_inode_info *ci = NULL;
95 struct ceph_snap_realm *realm, *next;
96 struct ceph_vino vino;
97 struct inode *in;
98 bool has_quota;
99
100 realm = ceph_inode(inode)->i_snap_realm;
101 ceph_get_snap_realm(mdsc, realm);
102 while (realm) {
103 vino.ino = realm->ino;
104 vino.snap = CEPH_NOSNAP;
105 in = ceph_find_inode(inode->i_sb, vino);
106 if (!in) {
107 pr_warn("Failed to find inode for %llu\n", vino.ino);
108 break;
109 }
110 ci = ceph_inode(in);
111 has_quota = __ceph_has_any_quota(ci);
112 iput(in);
113
114 next = realm->parent;
115 if (has_quota || !next)
116 return realm;
117
118 ceph_get_snap_realm(mdsc, next);
119 ceph_put_snap_realm(mdsc, realm);
120 realm = next;
121 }
122 if (realm)
123 ceph_put_snap_realm(mdsc, realm);
124
125 return NULL;
126 }
127
128 bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
129 {
130 struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
131 struct ceph_snap_realm *old_realm, *new_realm;
132 bool is_same;
133
134 down_read(&mdsc->snap_rwsem);
135 old_realm = get_quota_realm(mdsc, old);
136 new_realm = get_quota_realm(mdsc, new);
137 is_same = (old_realm == new_realm);
138 up_read(&mdsc->snap_rwsem);
139
140 if (old_realm)
141 ceph_put_snap_realm(mdsc, old_realm);
142 if (new_realm)
143 ceph_put_snap_realm(mdsc, new_realm);
144
145 return is_same;
146 }
147
148 enum quota_check_op {
149 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
150 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
151 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
152 limit is approaching */
153 };
154
155 /*
156 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
157 * realm, it will execute quota check operation defined by the 'op' parameter.
158 * The snaprealm walk is interrupted if the quota check detects that the quota
159 * is exceeded or if the root inode is reached.
160 */
161 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
162 loff_t delta)
163 {
164 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
165 struct ceph_inode_info *ci;
166 struct ceph_snap_realm *realm, *next;
167 struct ceph_vino vino;
168 struct inode *in;
169 u64 max, rvalue;
170 bool exceeded = false;
171
172 down_read(&mdsc->snap_rwsem);
173 realm = ceph_inode(inode)->i_snap_realm;
174 ceph_get_snap_realm(mdsc, realm);
175 while (realm) {
176 vino.ino = realm->ino;
177 vino.snap = CEPH_NOSNAP;
178 in = ceph_find_inode(inode->i_sb, vino);
179 if (!in) {
180 pr_warn("Failed to find inode for %llu\n", vino.ino);
181 break;
182 }
183 ci = ceph_inode(in);
184 spin_lock(&ci->i_ceph_lock);
185 if (op == QUOTA_CHECK_MAX_FILES_OP) {
186 max = ci->i_max_files;
187 rvalue = ci->i_rfiles + ci->i_rsubdirs;
188 } else {
189 max = ci->i_max_bytes;
190 rvalue = ci->i_rbytes;
191 }
192 spin_unlock(&ci->i_ceph_lock);
193 switch (op) {
194 case QUOTA_CHECK_MAX_FILES_OP:
195 exceeded = (max && (rvalue >= max));
196 break;
197 case QUOTA_CHECK_MAX_BYTES_OP:
198 exceeded = (max && (rvalue + delta > max));
199 break;
200 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
201 if (max) {
202 if (rvalue >= max)
203 exceeded = true;
204 else {
205 /*
206 * when we're writing more that 1/16th
207 * of the available space
208 */
209 exceeded =
210 (((max - rvalue) >> 4) < delta);
211 }
212 }
213 break;
214 default:
215 /* Shouldn't happen */
216 pr_warn("Invalid quota check op (%d)\n", op);
217 exceeded = true; /* Just break the loop */
218 }
219 iput(in);
220
221 next = realm->parent;
222 if (exceeded || !next)
223 break;
224 ceph_get_snap_realm(mdsc, next);
225 ceph_put_snap_realm(mdsc, realm);
226 realm = next;
227 }
228 ceph_put_snap_realm(mdsc, realm);
229 up_read(&mdsc->snap_rwsem);
230
231 return exceeded;
232 }
233
234 /*
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
237 *
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.
241 */
242 bool ceph_quota_is_max_files_exceeded(struct inode *inode)
243 {
244 if (!ceph_has_realms_with_quotas(inode))
245 return false;
246
247 WARN_ON(!S_ISDIR(inode->i_mode));
248
249 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
250 }
251
252 /*
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
256 *
257 * This functions returns true is max_bytes quota allows a file size to reach
258 * @newsize; it returns false otherwise.
259 */
260 bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
261 {
262 loff_t size = i_size_read(inode);
263
264 if (!ceph_has_realms_with_quotas(inode))
265 return false;
266
267 /* return immediately if we're decreasing file size */
268 if (newsize <= size)
269 return false;
270
271 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
272 }
273
274 /*
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
278 *
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.
281 */
282 bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
283 {
284 loff_t size = ceph_inode(inode)->i_reported_size;
285
286 if (!ceph_has_realms_with_quotas(inode))
287 return false;
288
289 /* return immediately if we're decreasing file size */
290 if (newsize <= size)
291 return false;
292
293 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
294 (newsize - size));
295 }