Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 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 | |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 24 | static inline bool ceph_has_quota(struct ceph_inode_info *ci) |
| 25 | { |
| 26 | return (ci && (ci->i_max_files || ci->i_max_bytes)); |
| 27 | } |
| 28 | |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 29 | void 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 | } |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 71 | |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 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 | */ |
| 81 | static 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; |
Yan, Zheng | 0eb6bbe | 2018-01-12 16:55:31 +0800 | [diff] [blame^] | 88 | bool has_quota; |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 89 | |
Yan, Zheng | 2596366 | 2018-01-12 16:26:17 +0800 | [diff] [blame] | 90 | if (ceph_snap(inode) != CEPH_NOSNAP) |
| 91 | return NULL; |
| 92 | |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 93 | realm = ceph_inode(inode)->i_snap_realm; |
Yan, Zheng | 2596366 | 2018-01-12 16:26:17 +0800 | [diff] [blame] | 94 | if (realm) |
| 95 | ceph_get_snap_realm(mdsc, realm); |
| 96 | else |
| 97 | pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) " |
| 98 | "null i_snap_realm\n", ceph_vinop(inode)); |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 99 | while (realm) { |
| 100 | vino.ino = realm->ino; |
| 101 | vino.snap = CEPH_NOSNAP; |
| 102 | in = ceph_find_inode(inode->i_sb, vino); |
| 103 | if (!in) { |
| 104 | pr_warn("Failed to find inode for %llu\n", vino.ino); |
| 105 | break; |
| 106 | } |
| 107 | ci = ceph_inode(in); |
Yan, Zheng | 0eb6bbe | 2018-01-12 16:55:31 +0800 | [diff] [blame^] | 108 | has_quota = ceph_has_quota(ci); |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 109 | iput(in); |
Yan, Zheng | 0eb6bbe | 2018-01-12 16:55:31 +0800 | [diff] [blame^] | 110 | |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 111 | next = realm->parent; |
Yan, Zheng | 0eb6bbe | 2018-01-12 16:55:31 +0800 | [diff] [blame^] | 112 | if (has_quota || !next) |
| 113 | return realm; |
| 114 | |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 115 | ceph_get_snap_realm(mdsc, next); |
| 116 | ceph_put_snap_realm(mdsc, realm); |
| 117 | realm = next; |
| 118 | } |
| 119 | if (realm) |
| 120 | ceph_put_snap_realm(mdsc, realm); |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) |
| 126 | { |
| 127 | struct ceph_mds_client *mdsc = ceph_inode_to_client(old)->mdsc; |
| 128 | struct ceph_snap_realm *old_realm, *new_realm; |
| 129 | bool is_same; |
| 130 | |
| 131 | down_read(&mdsc->snap_rwsem); |
| 132 | old_realm = get_quota_realm(mdsc, old); |
| 133 | new_realm = get_quota_realm(mdsc, new); |
| 134 | is_same = (old_realm == new_realm); |
| 135 | up_read(&mdsc->snap_rwsem); |
| 136 | |
| 137 | if (old_realm) |
| 138 | ceph_put_snap_realm(mdsc, old_realm); |
| 139 | if (new_realm) |
| 140 | ceph_put_snap_realm(mdsc, new_realm); |
| 141 | |
| 142 | return is_same; |
| 143 | } |
| 144 | |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 145 | enum quota_check_op { |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 146 | QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */ |
Luis Henriques | 1ab302a | 2018-01-05 10:47:22 +0000 | [diff] [blame] | 147 | QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */ |
| 148 | QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files |
| 149 | limit is approaching */ |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each |
| 154 | * realm, it will execute quota check operation defined by the 'op' parameter. |
| 155 | * The snaprealm walk is interrupted if the quota check detects that the quota |
| 156 | * is exceeded or if the root inode is reached. |
| 157 | */ |
| 158 | static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op, |
| 159 | loff_t delta) |
| 160 | { |
| 161 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
| 162 | struct ceph_inode_info *ci; |
| 163 | struct ceph_snap_realm *realm, *next; |
| 164 | struct ceph_vino vino; |
| 165 | struct inode *in; |
| 166 | u64 max, rvalue; |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 167 | bool exceeded = false; |
| 168 | |
Yan, Zheng | 2596366 | 2018-01-12 16:26:17 +0800 | [diff] [blame] | 169 | if (ceph_snap(inode) != CEPH_NOSNAP) |
| 170 | return false; |
| 171 | |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 172 | down_read(&mdsc->snap_rwsem); |
| 173 | realm = ceph_inode(inode)->i_snap_realm; |
Yan, Zheng | 2596366 | 2018-01-12 16:26:17 +0800 | [diff] [blame] | 174 | if (realm) |
| 175 | ceph_get_snap_realm(mdsc, realm); |
| 176 | else |
| 177 | pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) " |
| 178 | "null i_snap_realm\n", ceph_vinop(inode)); |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 179 | while (realm) { |
| 180 | vino.ino = realm->ino; |
| 181 | vino.snap = CEPH_NOSNAP; |
| 182 | in = ceph_find_inode(inode->i_sb, vino); |
| 183 | if (!in) { |
| 184 | pr_warn("Failed to find inode for %llu\n", vino.ino); |
| 185 | break; |
| 186 | } |
| 187 | ci = ceph_inode(in); |
| 188 | spin_lock(&ci->i_ceph_lock); |
| 189 | if (op == QUOTA_CHECK_MAX_FILES_OP) { |
| 190 | max = ci->i_max_files; |
| 191 | rvalue = ci->i_rfiles + ci->i_rsubdirs; |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 192 | } else { |
| 193 | max = ci->i_max_bytes; |
| 194 | rvalue = ci->i_rbytes; |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 195 | } |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 196 | spin_unlock(&ci->i_ceph_lock); |
| 197 | switch (op) { |
| 198 | case QUOTA_CHECK_MAX_FILES_OP: |
| 199 | exceeded = (max && (rvalue >= max)); |
| 200 | break; |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 201 | case QUOTA_CHECK_MAX_BYTES_OP: |
| 202 | exceeded = (max && (rvalue + delta > max)); |
| 203 | break; |
Luis Henriques | 1ab302a | 2018-01-05 10:47:22 +0000 | [diff] [blame] | 204 | case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP: |
| 205 | if (max) { |
| 206 | if (rvalue >= max) |
| 207 | exceeded = true; |
| 208 | else { |
| 209 | /* |
| 210 | * when we're writing more that 1/16th |
| 211 | * of the available space |
| 212 | */ |
| 213 | exceeded = |
| 214 | (((max - rvalue) >> 4) < delta); |
| 215 | } |
| 216 | } |
| 217 | break; |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 218 | default: |
| 219 | /* Shouldn't happen */ |
| 220 | pr_warn("Invalid quota check op (%d)\n", op); |
| 221 | exceeded = true; /* Just break the loop */ |
| 222 | } |
| 223 | iput(in); |
| 224 | |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 225 | next = realm->parent; |
Yan, Zheng | 0eb6bbe | 2018-01-12 16:55:31 +0800 | [diff] [blame^] | 226 | if (exceeded || !next) |
| 227 | break; |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 228 | ceph_get_snap_realm(mdsc, next); |
| 229 | ceph_put_snap_realm(mdsc, realm); |
| 230 | realm = next; |
| 231 | } |
| 232 | ceph_put_snap_realm(mdsc, realm); |
| 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 | { |
| 248 | WARN_ON(!S_ISDIR(inode->i_mode)); |
| 249 | |
| 250 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 0); |
| 251 | } |
Luis Henriques | 2b83845 | 2018-01-05 10:47:21 +0000 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * ceph_quota_is_max_bytes_exceeded - check if we can write to a file |
| 255 | * @inode: inode being written |
| 256 | * @newsize: new size if write succeeds |
| 257 | * |
| 258 | * This functions returns true is max_bytes quota allows a file size to reach |
| 259 | * @newsize; it returns false otherwise. |
| 260 | */ |
| 261 | bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize) |
| 262 | { |
| 263 | loff_t size = i_size_read(inode); |
| 264 | |
| 265 | /* return immediately if we're decreasing file size */ |
| 266 | if (newsize <= size) |
| 267 | return false; |
| 268 | |
| 269 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size)); |
| 270 | } |
Luis Henriques | 1ab302a | 2018-01-05 10:47:22 +0000 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes |
| 274 | * @inode: inode being written |
| 275 | * @newsize: new size if write succeeds |
| 276 | * |
| 277 | * This function returns true if the new file size @newsize will be consuming |
| 278 | * more than 1/16th of the available quota space; it returns false otherwise. |
| 279 | */ |
| 280 | bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize) |
| 281 | { |
| 282 | loff_t size = ceph_inode(inode)->i_reported_size; |
| 283 | |
| 284 | /* return immediately if we're decreasing file size */ |
| 285 | if (newsize <= size) |
| 286 | return false; |
| 287 | |
| 288 | return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP, |
| 289 | (newsize - size)); |
| 290 | } |