blob: 588744b4665fe7a66b8a8be7695fe37a0017ec21 [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
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
Luis Henriquesd557c482018-01-12 17:19:29 +000024void ceph_adjust_quota_realms_count(struct inode *inode, bool inc)
Luis Henriquescafe21a2018-01-05 10:47:20 +000025{
Luis Henriquesd557c482018-01-12 17:19:29 +000026 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
33static 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;
Luis Henriquescafe21a2018-01-05 10:47:20 +000037}
38
Luis Henriquesfb18a572018-01-05 10:47:18 +000039void 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);
Luis Henriquesd557c482018-01-12 17:19:29 +000075 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes),
76 le64_to_cpu(h->max_files));
Luis Henriquesfb18a572018-01-05 10:47:18 +000077 spin_unlock(&ci->i_ceph_lock);
78
79 iput(inode);
80}
Luis Henriquesb7a29212018-01-05 10:47:19 +000081
Luis Henriquescafe21a2018-01-05 10:47:20 +000082/*
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 */
91static 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;
Luis Henriquescafe21a2018-01-05 10:47:20 +000096 struct inode *in;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +080097 bool has_quota;
Luis Henriquescafe21a2018-01-05 10:47:20 +000098
Yan, Zheng25963662018-01-12 16:26:17 +080099 if (ceph_snap(inode) != CEPH_NOSNAP)
100 return NULL;
101
Luis Henriquescafe21a2018-01-05 10:47:20 +0000102 realm = ceph_inode(inode)->i_snap_realm;
Yan, Zheng25963662018-01-12 16:26:17 +0800103 if (realm)
104 ceph_get_snap_realm(mdsc, realm);
105 else
106 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) "
107 "null i_snap_realm\n", ceph_vinop(inode));
Luis Henriquescafe21a2018-01-05 10:47:20 +0000108 while (realm) {
Luis Henriquese3161f12018-01-12 17:19:28 +0000109 spin_lock(&realm->inodes_with_caps_lock);
110 in = realm->inode ? igrab(realm->inode) : NULL;
111 spin_unlock(&realm->inodes_with_caps_lock);
112 if (!in)
Luis Henriquescafe21a2018-01-05 10:47:20 +0000113 break;
Luis Henriquese3161f12018-01-12 17:19:28 +0000114
Luis Henriquescafe21a2018-01-05 10:47:20 +0000115 ci = ceph_inode(in);
Luis Henriquesd557c482018-01-12 17:19:29 +0000116 has_quota = __ceph_has_any_quota(ci);
Luis Henriquescafe21a2018-01-05 10:47:20 +0000117 iput(in);
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800118
Luis Henriquescafe21a2018-01-05 10:47:20 +0000119 next = realm->parent;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800120 if (has_quota || !next)
121 return realm;
122
Luis Henriquescafe21a2018-01-05 10:47:20 +0000123 ceph_get_snap_realm(mdsc, next);
124 ceph_put_snap_realm(mdsc, realm);
125 realm = next;
126 }
127 if (realm)
128 ceph_put_snap_realm(mdsc, realm);
129
130 return NULL;
131}
132
133bool ceph_quota_is_same_realm(struct inode *old, struct inode *new)
134{
135 struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc;
136 struct ceph_snap_realm *old_realm, *new_realm;
137 bool is_same;
138
139 down_read(&mdsc->snap_rwsem);
140 old_realm = get_quota_realm(mdsc, old);
141 new_realm = get_quota_realm(mdsc, new);
142 is_same = (old_realm == new_realm);
143 up_read(&mdsc->snap_rwsem);
144
145 if (old_realm)
146 ceph_put_snap_realm(mdsc, old_realm);
147 if (new_realm)
148 ceph_put_snap_realm(mdsc, new_realm);
149
150 return is_same;
151}
152
Luis Henriquesb7a29212018-01-05 10:47:19 +0000153enum quota_check_op {
Luis Henriques2b838452018-01-05 10:47:21 +0000154 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */
Luis Henriques1ab302a2018-01-05 10:47:22 +0000155 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */
156 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files
157 limit is approaching */
Luis Henriquesb7a29212018-01-05 10:47:19 +0000158};
159
160/*
161 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each
162 * realm, it will execute quota check operation defined by the 'op' parameter.
163 * The snaprealm walk is interrupted if the quota check detects that the quota
164 * is exceeded or if the root inode is reached.
165 */
166static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op,
167 loff_t delta)
168{
169 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
170 struct ceph_inode_info *ci;
171 struct ceph_snap_realm *realm, *next;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000172 struct inode *in;
173 u64 max, rvalue;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000174 bool exceeded = false;
175
Yan, Zheng25963662018-01-12 16:26:17 +0800176 if (ceph_snap(inode) != CEPH_NOSNAP)
177 return false;
178
Luis Henriquesb7a29212018-01-05 10:47:19 +0000179 down_read(&mdsc->snap_rwsem);
180 realm = ceph_inode(inode)->i_snap_realm;
Yan, Zheng25963662018-01-12 16:26:17 +0800181 if (realm)
182 ceph_get_snap_realm(mdsc, realm);
183 else
184 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) "
185 "null i_snap_realm\n", ceph_vinop(inode));
Luis Henriquesb7a29212018-01-05 10:47:19 +0000186 while (realm) {
Luis Henriquese3161f12018-01-12 17:19:28 +0000187 spin_lock(&realm->inodes_with_caps_lock);
188 in = realm->inode ? igrab(realm->inode) : NULL;
189 spin_unlock(&realm->inodes_with_caps_lock);
190 if (!in)
Luis Henriquesb7a29212018-01-05 10:47:19 +0000191 break;
Luis Henriquese3161f12018-01-12 17:19:28 +0000192
Luis Henriquesb7a29212018-01-05 10:47:19 +0000193 ci = ceph_inode(in);
194 spin_lock(&ci->i_ceph_lock);
195 if (op == QUOTA_CHECK_MAX_FILES_OP) {
196 max = ci->i_max_files;
197 rvalue = ci->i_rfiles + ci->i_rsubdirs;
Luis Henriques2b838452018-01-05 10:47:21 +0000198 } else {
199 max = ci->i_max_bytes;
200 rvalue = ci->i_rbytes;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000201 }
Luis Henriquesb7a29212018-01-05 10:47:19 +0000202 spin_unlock(&ci->i_ceph_lock);
203 switch (op) {
204 case QUOTA_CHECK_MAX_FILES_OP:
205 exceeded = (max && (rvalue >= max));
206 break;
Luis Henriques2b838452018-01-05 10:47:21 +0000207 case QUOTA_CHECK_MAX_BYTES_OP:
208 exceeded = (max && (rvalue + delta > max));
209 break;
Luis Henriques1ab302a2018-01-05 10:47:22 +0000210 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP:
211 if (max) {
212 if (rvalue >= max)
213 exceeded = true;
214 else {
215 /*
216 * when we're writing more that 1/16th
217 * of the available space
218 */
219 exceeded =
220 (((max - rvalue) >> 4) < delta);
221 }
222 }
223 break;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000224 default:
225 /* Shouldn't happen */
226 pr_warn("Invalid quota check op (%d)\n", op);
227 exceeded = true; /* Just break the loop */
228 }
229 iput(in);
230
Luis Henriquesb7a29212018-01-05 10:47:19 +0000231 next = realm->parent;
Yan, Zheng0eb6bbe2018-01-12 16:55:31 +0800232 if (exceeded || !next)
233 break;
Luis Henriquesb7a29212018-01-05 10:47:19 +0000234 ceph_get_snap_realm(mdsc, next);
235 ceph_put_snap_realm(mdsc, realm);
236 realm = next;
237 }
238 ceph_put_snap_realm(mdsc, realm);
239 up_read(&mdsc->snap_rwsem);
240
241 return exceeded;
242}
243
244/*
245 * ceph_quota_is_max_files_exceeded - check if we can create a new file
246 * @inode: directory where a new file is being created
247 *
248 * This functions returns true is max_files quota allows a new file to be
249 * created. It is necessary to walk through the snaprealm hierarchy (until the
250 * FS root) to check all realms with quotas set.
251 */
252bool ceph_quota_is_max_files_exceeded(struct inode *inode)
253{
Luis Henriquesd557c482018-01-12 17:19:29 +0000254 if (!ceph_has_realms_with_quotas(inode))
255 return false;
256
Luis Henriquesb7a29212018-01-05 10:47:19 +0000257 WARN_ON(!S_ISDIR(inode->i_mode));
258
259 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0);
260}
Luis Henriques2b838452018-01-05 10:47:21 +0000261
262/*
263 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file
264 * @inode: inode being written
265 * @newsize: new size if write succeeds
266 *
267 * This functions returns true is max_bytes quota allows a file size to reach
268 * @newsize; it returns false otherwise.
269 */
270bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize)
271{
272 loff_t size = i_size_read(inode);
273
Luis Henriquesd557c482018-01-12 17:19:29 +0000274 if (!ceph_has_realms_with_quotas(inode))
275 return false;
276
Luis Henriques2b838452018-01-05 10:47:21 +0000277 /* return immediately if we're decreasing file size */
278 if (newsize <= size)
279 return false;
280
281 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size));
282}
Luis Henriques1ab302a2018-01-05 10:47:22 +0000283
284/*
285 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes
286 * @inode: inode being written
287 * @newsize: new size if write succeeds
288 *
289 * This function returns true if the new file size @newsize will be consuming
290 * more than 1/16th of the available quota space; it returns false otherwise.
291 */
292bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize)
293{
294 loff_t size = ceph_inode(inode)->i_reported_size;
295
Luis Henriquesd557c482018-01-12 17:19:29 +0000296 if (!ceph_has_realms_with_quotas(inode))
297 return false;
298
Luis Henriques1ab302a2018-01-05 10:47:22 +0000299 /* return immediately if we're decreasing file size */
300 if (newsize <= size)
301 return false;
302
303 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP,
304 (newsize - size));
305}