]>
Commit | Line | Data |
---|---|---|
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 | ||
60147660 | 24 | void ceph_adjust_quota_realms_count(struct inode *inode, bool inc) |
68a16a23 | 25 | { |
60147660 LH |
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; | |
68a16a23 LH |
37 | } |
38 | ||
7370e8a4 LH |
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); | |
60147660 LH |
75 | __ceph_update_quota(ci, le64_to_cpu(h->max_bytes), |
76 | le64_to_cpu(h->max_files)); | |
7370e8a4 LH |
77 | spin_unlock(&ci->i_ceph_lock); |
78 | ||
0c0e5720 YZ |
79 | /* avoid calling iput_final() in dispatch thread */ |
80 | ceph_async_iput(inode); | |
7370e8a4 | 81 | } |
6aa007ee | 82 | |
68a16a23 LH |
83 | /* |
84 | * This function walks through the snaprealm for an inode and returns the | |
85 | * ceph_snap_realm for the first snaprealm that has quotas set (either max_files | |
86 | * or max_bytes). If the root is reached, return the root ceph_snap_realm | |
87 | * instead. | |
88 | * | |
89 | * Note that the caller is responsible for calling ceph_put_snap_realm() on the | |
90 | * returned realm. | |
91 | */ | |
92 | static struct ceph_snap_realm *get_quota_realm(struct ceph_mds_client *mdsc, | |
93 | struct inode *inode) | |
94 | { | |
95 | struct ceph_inode_info *ci = NULL; | |
96 | struct ceph_snap_realm *realm, *next; | |
97 | struct ceph_vino vino; | |
98 | struct inode *in; | |
780ac19f | 99 | bool has_quota; |
68a16a23 LH |
100 | |
101 | realm = ceph_inode(inode)->i_snap_realm; | |
102 | ceph_get_snap_realm(mdsc, realm); | |
103 | while (realm) { | |
104 | vino.ino = realm->ino; | |
105 | vino.snap = CEPH_NOSNAP; | |
106 | in = ceph_find_inode(inode->i_sb, vino); | |
107 | if (!in) { | |
108 | pr_warn("Failed to find inode for %llu\n", vino.ino); | |
109 | break; | |
110 | } | |
111 | ci = ceph_inode(in); | |
60147660 | 112 | has_quota = __ceph_has_any_quota(ci); |
0c0e5720 YZ |
113 | /* avoid calling iput_final() while holding mdsc->snap_rwsem */ |
114 | ceph_async_iput(in); | |
780ac19f | 115 | |
68a16a23 | 116 | next = realm->parent; |
780ac19f YZ |
117 | if (has_quota || !next) |
118 | return realm; | |
119 | ||
68a16a23 LH |
120 | ceph_get_snap_realm(mdsc, next); |
121 | ceph_put_snap_realm(mdsc, realm); | |
122 | realm = next; | |
123 | } | |
124 | if (realm) | |
125 | ceph_put_snap_realm(mdsc, realm); | |
126 | ||
127 | return NULL; | |
128 | } | |
129 | ||
130 | bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) | |
131 | { | |
132 | struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc; | |
133 | struct ceph_snap_realm *old_realm, *new_realm; | |
134 | bool is_same; | |
135 | ||
136 | down_read(&mdsc->snap_rwsem); | |
137 | old_realm = get_quota_realm(mdsc, old); | |
138 | new_realm = get_quota_realm(mdsc, new); | |
139 | is_same = (old_realm == new_realm); | |
140 | up_read(&mdsc->snap_rwsem); | |
141 | ||
142 | if (old_realm) | |
143 | ceph_put_snap_realm(mdsc, old_realm); | |
144 | if (new_realm) | |
145 | ceph_put_snap_realm(mdsc, new_realm); | |
146 | ||
147 | return is_same; | |
148 | } | |
149 | ||
6aa007ee | 150 | enum quota_check_op { |
0cb65d53 | 151 | QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */ |
763b0c3d LH |
152 | QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */ |
153 | QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files | |
154 | limit is approaching */ | |
6aa007ee LH |
155 | }; |
156 | ||
157 | /* | |
158 | * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each | |
159 | * realm, it will execute quota check operation defined by the 'op' parameter. | |
160 | * The snaprealm walk is interrupted if the quota check detects that the quota | |
161 | * is exceeded or if the root inode is reached. | |
162 | */ | |
163 | static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op, | |
164 | loff_t delta) | |
165 | { | |
166 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; | |
167 | struct ceph_inode_info *ci; | |
168 | struct ceph_snap_realm *realm, *next; | |
169 | struct ceph_vino vino; | |
170 | struct inode *in; | |
171 | u64 max, rvalue; | |
6aa007ee LH |
172 | bool exceeded = false; |
173 | ||
174 | down_read(&mdsc->snap_rwsem); | |
175 | realm = ceph_inode(inode)->i_snap_realm; | |
176 | ceph_get_snap_realm(mdsc, realm); | |
177 | while (realm) { | |
178 | vino.ino = realm->ino; | |
179 | vino.snap = CEPH_NOSNAP; | |
180 | in = ceph_find_inode(inode->i_sb, vino); | |
181 | if (!in) { | |
182 | pr_warn("Failed to find inode for %llu\n", vino.ino); | |
183 | break; | |
184 | } | |
185 | ci = ceph_inode(in); | |
186 | spin_lock(&ci->i_ceph_lock); | |
187 | if (op == QUOTA_CHECK_MAX_FILES_OP) { | |
188 | max = ci->i_max_files; | |
189 | rvalue = ci->i_rfiles + ci->i_rsubdirs; | |
0cb65d53 LH |
190 | } else { |
191 | max = ci->i_max_bytes; | |
192 | rvalue = ci->i_rbytes; | |
6aa007ee | 193 | } |
6aa007ee LH |
194 | spin_unlock(&ci->i_ceph_lock); |
195 | switch (op) { | |
196 | case QUOTA_CHECK_MAX_FILES_OP: | |
197 | exceeded = (max && (rvalue >= max)); | |
198 | break; | |
0cb65d53 LH |
199 | case QUOTA_CHECK_MAX_BYTES_OP: |
200 | exceeded = (max && (rvalue + delta > max)); | |
201 | break; | |
763b0c3d LH |
202 | case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP: |
203 | if (max) { | |
204 | if (rvalue >= max) | |
205 | exceeded = true; | |
206 | else { | |
207 | /* | |
208 | * when we're writing more that 1/16th | |
209 | * of the available space | |
210 | */ | |
211 | exceeded = | |
212 | (((max - rvalue) >> 4) < delta); | |
213 | } | |
214 | } | |
215 | break; | |
6aa007ee LH |
216 | default: |
217 | /* Shouldn't happen */ | |
218 | pr_warn("Invalid quota check op (%d)\n", op); | |
219 | exceeded = true; /* Just break the loop */ | |
220 | } | |
0c0e5720 YZ |
221 | /* avoid calling iput_final() while holding mdsc->snap_rwsem */ |
222 | ceph_async_iput(in); | |
6aa007ee | 223 | |
6aa007ee | 224 | next = realm->parent; |
780ac19f YZ |
225 | if (exceeded || !next) |
226 | break; | |
6aa007ee LH |
227 | ceph_get_snap_realm(mdsc, next); |
228 | ceph_put_snap_realm(mdsc, realm); | |
229 | realm = next; | |
230 | } | |
c7f8f766 LH |
231 | if (realm) |
232 | ceph_put_snap_realm(mdsc, realm); | |
6aa007ee LH |
233 | up_read(&mdsc->snap_rwsem); |
234 | ||
235 | return exceeded; | |
236 | } | |
237 | ||
238 | /* | |
239 | * ceph_quota_is_max_files_exceeded - check if we can create a new file | |
240 | * @inode: directory where a new file is being created | |
241 | * | |
242 | * This functions returns true is max_files quota allows a new file to be | |
243 | * created. It is necessary to walk through the snaprealm hierarchy (until the | |
244 | * FS root) to check all realms with quotas set. | |
245 | */ | |
246 | bool ceph_quota_is_max_files_exceeded(struct inode *inode) | |
247 | { | |
60147660 LH |
248 | if (!ceph_has_realms_with_quotas(inode)) |
249 | return false; | |
250 | ||
6aa007ee LH |
251 | WARN_ON(!S_ISDIR(inode->i_mode)); |
252 | ||
253 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0); | |
254 | } | |
0cb65d53 LH |
255 | |
256 | /* | |
257 | * ceph_quota_is_max_bytes_exceeded - check if we can write to a file | |
258 | * @inode: inode being written | |
259 | * @newsize: new size if write succeeds | |
260 | * | |
261 | * This functions returns true is max_bytes quota allows a file size to reach | |
262 | * @newsize; it returns false otherwise. | |
263 | */ | |
264 | bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize) | |
265 | { | |
266 | loff_t size = i_size_read(inode); | |
267 | ||
60147660 LH |
268 | if (!ceph_has_realms_with_quotas(inode)) |
269 | return false; | |
270 | ||
0cb65d53 LH |
271 | /* return immediately if we're decreasing file size */ |
272 | if (newsize <= size) | |
273 | return false; | |
274 | ||
275 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size)); | |
276 | } | |
763b0c3d LH |
277 | |
278 | /* | |
279 | * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes | |
280 | * @inode: inode being written | |
281 | * @newsize: new size if write succeeds | |
282 | * | |
283 | * This function returns true if the new file size @newsize will be consuming | |
284 | * more than 1/16th of the available quota space; it returns false otherwise. | |
285 | */ | |
286 | bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize) | |
287 | { | |
288 | loff_t size = ceph_inode(inode)->i_reported_size; | |
289 | ||
60147660 LH |
290 | if (!ceph_has_realms_with_quotas(inode)) |
291 | return false; | |
292 | ||
763b0c3d LH |
293 | /* return immediately if we're decreasing file size */ |
294 | if (newsize <= size) | |
295 | return false; | |
296 | ||
297 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP, | |
298 | (newsize - size)); | |
299 | } |