1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * quota.c - CephFS quota 4 * 5 * Copyright (C) 2017-2018 SUSE 6 */ 7 8 #include <linux/statfs.h> 9 10 #include "super.h" 11 #include "mds_client.h" 12 13 void ceph_adjust_quota_realms_count(struct inode *inode, bool inc) 14 { 15 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 16 if (inc) 17 atomic64_inc(&mdsc->quotarealms_count); 18 else 19 atomic64_dec(&mdsc->quotarealms_count); 20 } 21 22 static inline bool ceph_has_realms_with_quotas(struct inode *inode) 23 { 24 struct super_block *sb = inode->i_sb; 25 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb); 26 struct inode *root = d_inode(sb->s_root); 27 28 if (atomic64_read(&mdsc->quotarealms_count) > 0) 29 return true; 30 /* if root is the real CephFS root, we don't have quota realms */ 31 if (root && ceph_ino(root) == CEPH_INO_ROOT) 32 return false; 33 /* MDS stray dirs have no quota realms */ 34 if (ceph_vino_is_reserved(ceph_inode(inode)->i_vino)) 35 return false; 36 /* otherwise, we can't know for sure */ 37 return true; 38 } 39 40 void ceph_handle_quota(struct ceph_mds_client *mdsc, 41 struct ceph_mds_session *session, 42 struct ceph_msg *msg) 43 { 44 struct super_block *sb = mdsc->fsc->sb; 45 struct ceph_mds_quota *h = msg->front.iov_base; 46 struct ceph_vino vino; 47 struct inode *inode; 48 struct ceph_inode_info *ci; 49 50 if (!ceph_inc_mds_stopping_blocker(mdsc, session)) 51 return; 52 53 if (msg->front.iov_len < sizeof(*h)) { 54 pr_err("%s corrupt message mds%d len %d\n", __func__, 55 session->s_mds, (int)msg->front.iov_len); 56 ceph_msg_dump(msg); 57 goto out; 58 } 59 60 /* lookup inode */ 61 vino.ino = le64_to_cpu(h->ino); 62 vino.snap = CEPH_NOSNAP; 63 inode = ceph_find_inode(sb, vino); 64 if (!inode) { 65 pr_warn("Failed to find inode %llu\n", vino.ino); 66 goto out; 67 } 68 ci = ceph_inode(inode); 69 70 spin_lock(&ci->i_ceph_lock); 71 ci->i_rbytes = le64_to_cpu(h->rbytes); 72 ci->i_rfiles = le64_to_cpu(h->rfiles); 73 ci->i_rsubdirs = le64_to_cpu(h->rsubdirs); 74 __ceph_update_quota(ci, le64_to_cpu(h->max_bytes), 75 le64_to_cpu(h->max_files)); 76 spin_unlock(&ci->i_ceph_lock); 77 78 iput(inode); 79 out: 80 ceph_dec_mds_stopping_blocker(mdsc); 81 } 82 83 static struct ceph_quotarealm_inode * 84 find_quotarealm_inode(struct ceph_mds_client *mdsc, u64 ino) 85 { 86 struct ceph_quotarealm_inode *qri = NULL; 87 struct rb_node **node, *parent = NULL; 88 89 mutex_lock(&mdsc->quotarealms_inodes_mutex); 90 node = &(mdsc->quotarealms_inodes.rb_node); 91 while (*node) { 92 parent = *node; 93 qri = container_of(*node, struct ceph_quotarealm_inode, node); 94 95 if (ino < qri->ino) 96 node = &((*node)->rb_left); 97 else if (ino > qri->ino) 98 node = &((*node)->rb_right); 99 else 100 break; 101 } 102 if (!qri || (qri->ino != ino)) { 103 /* Not found, create a new one and insert it */ 104 qri = kmalloc(sizeof(*qri), GFP_KERNEL); 105 if (qri) { 106 qri->ino = ino; 107 qri->inode = NULL; 108 qri->timeout = 0; 109 mutex_init(&qri->mutex); 110 rb_link_node(&qri->node, parent, node); 111 rb_insert_color(&qri->node, &mdsc->quotarealms_inodes); 112 } else 113 pr_warn("Failed to alloc quotarealms_inode\n"); 114 } 115 mutex_unlock(&mdsc->quotarealms_inodes_mutex); 116 117 return qri; 118 } 119 120 /* 121 * This function will try to lookup a realm inode which isn't visible in the 122 * filesystem mountpoint. A list of these kind of inodes (not visible) is 123 * maintained in the mdsc and freed only when the filesystem is umounted. 124 * 125 * Note that these inodes are kept in this list even if the lookup fails, which 126 * allows to prevent useless lookup requests. 127 */ 128 static struct inode *lookup_quotarealm_inode(struct ceph_mds_client *mdsc, 129 struct super_block *sb, 130 struct ceph_snap_realm *realm) 131 { 132 struct ceph_quotarealm_inode *qri; 133 struct inode *in; 134 135 qri = find_quotarealm_inode(mdsc, realm->ino); 136 if (!qri) 137 return NULL; 138 139 mutex_lock(&qri->mutex); 140 if (qri->inode && ceph_is_any_caps(qri->inode)) { 141 /* A request has already returned the inode */ 142 mutex_unlock(&qri->mutex); 143 return qri->inode; 144 } 145 /* Check if this inode lookup has failed recently */ 146 if (qri->timeout && 147 time_before_eq(jiffies, qri->timeout)) { 148 mutex_unlock(&qri->mutex); 149 return NULL; 150 } 151 if (qri->inode) { 152 /* get caps */ 153 int ret = __ceph_do_getattr(qri->inode, NULL, 154 CEPH_STAT_CAP_INODE, true); 155 if (ret >= 0) 156 in = qri->inode; 157 else 158 in = ERR_PTR(ret); 159 } else { 160 in = ceph_lookup_inode(sb, realm->ino); 161 } 162 163 if (IS_ERR(in)) { 164 dout("Can't lookup inode %llx (err: %ld)\n", 165 realm->ino, PTR_ERR(in)); 166 qri->timeout = jiffies + msecs_to_jiffies(60 * 1000); /* XXX */ 167 } else { 168 qri->timeout = 0; 169 qri->inode = in; 170 } 171 mutex_unlock(&qri->mutex); 172 173 return in; 174 } 175 176 void ceph_cleanup_quotarealms_inodes(struct ceph_mds_client *mdsc) 177 { 178 struct ceph_quotarealm_inode *qri; 179 struct rb_node *node; 180 181 /* 182 * It should now be safe to clean quotarealms_inode tree without holding 183 * mdsc->quotarealms_inodes_mutex... 184 */ 185 mutex_lock(&mdsc->quotarealms_inodes_mutex); 186 while (!RB_EMPTY_ROOT(&mdsc->quotarealms_inodes)) { 187 node = rb_first(&mdsc->quotarealms_inodes); 188 qri = rb_entry(node, struct ceph_quotarealm_inode, node); 189 rb_erase(node, &mdsc->quotarealms_inodes); 190 iput(qri->inode); 191 kfree(qri); 192 } 193 mutex_unlock(&mdsc->quotarealms_inodes_mutex); 194 } 195 196 /* 197 * This function walks through the snaprealm for an inode and set the 198 * realmp with the first snaprealm that has quotas set (max_files, 199 * max_bytes, or any, depending on the 'which_quota' argument). If the root is 200 * reached, set the realmp with the root ceph_snap_realm instead. 201 * 202 * Note that the caller is responsible for calling ceph_put_snap_realm() on the 203 * returned realm. 204 * 205 * Callers of this function need to hold mdsc->snap_rwsem. However, if there's 206 * a need to do an inode lookup, this rwsem will be temporarily dropped. Hence 207 * the 'retry' argument: if rwsem needs to be dropped and 'retry' is 'false' 208 * this function will return -EAGAIN; otherwise, the snaprealms walk-through 209 * will be restarted. 210 */ 211 static int get_quota_realm(struct ceph_mds_client *mdsc, struct inode *inode, 212 enum quota_get_realm which_quota, 213 struct ceph_snap_realm **realmp, bool retry) 214 { 215 struct ceph_inode_info *ci = NULL; 216 struct ceph_snap_realm *realm, *next; 217 struct inode *in; 218 bool has_quota; 219 220 if (realmp) 221 *realmp = NULL; 222 if (ceph_snap(inode) != CEPH_NOSNAP) 223 return 0; 224 225 restart: 226 realm = ceph_inode(inode)->i_snap_realm; 227 if (realm) 228 ceph_get_snap_realm(mdsc, realm); 229 else 230 pr_err_ratelimited("get_quota_realm: ino (%llx.%llx) " 231 "null i_snap_realm\n", ceph_vinop(inode)); 232 while (realm) { 233 bool has_inode; 234 235 spin_lock(&realm->inodes_with_caps_lock); 236 has_inode = realm->inode; 237 in = has_inode ? igrab(realm->inode) : NULL; 238 spin_unlock(&realm->inodes_with_caps_lock); 239 if (has_inode && !in) 240 break; 241 if (!in) { 242 up_read(&mdsc->snap_rwsem); 243 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm); 244 down_read(&mdsc->snap_rwsem); 245 if (IS_ERR_OR_NULL(in)) 246 break; 247 ceph_put_snap_realm(mdsc, realm); 248 if (!retry) 249 return -EAGAIN; 250 goto restart; 251 } 252 253 ci = ceph_inode(in); 254 has_quota = __ceph_has_quota(ci, which_quota); 255 iput(in); 256 257 next = realm->parent; 258 if (has_quota || !next) { 259 if (realmp) 260 *realmp = realm; 261 return 0; 262 } 263 264 ceph_get_snap_realm(mdsc, next); 265 ceph_put_snap_realm(mdsc, realm); 266 realm = next; 267 } 268 if (realm) 269 ceph_put_snap_realm(mdsc, realm); 270 271 return 0; 272 } 273 274 bool ceph_quota_is_same_realm(struct inode *old, struct inode *new) 275 { 276 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old->i_sb); 277 struct ceph_snap_realm *old_realm, *new_realm; 278 bool is_same; 279 int ret; 280 281 restart: 282 /* 283 * We need to lookup 2 quota realms atomically, i.e. with snap_rwsem. 284 * However, get_quota_realm may drop it temporarily. By setting the 285 * 'retry' parameter to 'false', we'll get -EAGAIN if the rwsem was 286 * dropped and we can then restart the whole operation. 287 */ 288 down_read(&mdsc->snap_rwsem); 289 get_quota_realm(mdsc, old, QUOTA_GET_ANY, &old_realm, true); 290 ret = get_quota_realm(mdsc, new, QUOTA_GET_ANY, &new_realm, false); 291 if (ret == -EAGAIN) { 292 up_read(&mdsc->snap_rwsem); 293 if (old_realm) 294 ceph_put_snap_realm(mdsc, old_realm); 295 goto restart; 296 } 297 is_same = (old_realm == new_realm); 298 up_read(&mdsc->snap_rwsem); 299 300 if (old_realm) 301 ceph_put_snap_realm(mdsc, old_realm); 302 if (new_realm) 303 ceph_put_snap_realm(mdsc, new_realm); 304 305 return is_same; 306 } 307 308 enum quota_check_op { 309 QUOTA_CHECK_MAX_FILES_OP, /* check quota max_files limit */ 310 QUOTA_CHECK_MAX_BYTES_OP, /* check quota max_files limit */ 311 QUOTA_CHECK_MAX_BYTES_APPROACHING_OP /* check if quota max_files 312 limit is approaching */ 313 }; 314 315 /* 316 * check_quota_exceeded() will walk up the snaprealm hierarchy and, for each 317 * realm, it will execute quota check operation defined by the 'op' parameter. 318 * The snaprealm walk is interrupted if the quota check detects that the quota 319 * is exceeded or if the root inode is reached. 320 */ 321 static bool check_quota_exceeded(struct inode *inode, enum quota_check_op op, 322 loff_t delta) 323 { 324 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 325 struct ceph_inode_info *ci; 326 struct ceph_snap_realm *realm, *next; 327 struct inode *in; 328 u64 max, rvalue; 329 bool exceeded = false; 330 331 if (ceph_snap(inode) != CEPH_NOSNAP) 332 return false; 333 334 down_read(&mdsc->snap_rwsem); 335 restart: 336 realm = ceph_inode(inode)->i_snap_realm; 337 if (realm) 338 ceph_get_snap_realm(mdsc, realm); 339 else 340 pr_err_ratelimited("check_quota_exceeded: ino (%llx.%llx) " 341 "null i_snap_realm\n", ceph_vinop(inode)); 342 while (realm) { 343 bool has_inode; 344 345 spin_lock(&realm->inodes_with_caps_lock); 346 has_inode = realm->inode; 347 in = has_inode ? igrab(realm->inode) : NULL; 348 spin_unlock(&realm->inodes_with_caps_lock); 349 if (has_inode && !in) 350 break; 351 if (!in) { 352 up_read(&mdsc->snap_rwsem); 353 in = lookup_quotarealm_inode(mdsc, inode->i_sb, realm); 354 down_read(&mdsc->snap_rwsem); 355 if (IS_ERR_OR_NULL(in)) 356 break; 357 ceph_put_snap_realm(mdsc, realm); 358 goto restart; 359 } 360 ci = ceph_inode(in); 361 spin_lock(&ci->i_ceph_lock); 362 if (op == QUOTA_CHECK_MAX_FILES_OP) { 363 max = ci->i_max_files; 364 rvalue = ci->i_rfiles + ci->i_rsubdirs; 365 } else { 366 max = ci->i_max_bytes; 367 rvalue = ci->i_rbytes; 368 } 369 spin_unlock(&ci->i_ceph_lock); 370 switch (op) { 371 case QUOTA_CHECK_MAX_FILES_OP: 372 case QUOTA_CHECK_MAX_BYTES_OP: 373 exceeded = (max && (rvalue + delta > max)); 374 break; 375 case QUOTA_CHECK_MAX_BYTES_APPROACHING_OP: 376 if (max) { 377 if (rvalue >= max) 378 exceeded = true; 379 else { 380 /* 381 * when we're writing more that 1/16th 382 * of the available space 383 */ 384 exceeded = 385 (((max - rvalue) >> 4) < delta); 386 } 387 } 388 break; 389 default: 390 /* Shouldn't happen */ 391 pr_warn("Invalid quota check op (%d)\n", op); 392 exceeded = true; /* Just break the loop */ 393 } 394 iput(in); 395 396 next = realm->parent; 397 if (exceeded || !next) 398 break; 399 ceph_get_snap_realm(mdsc, next); 400 ceph_put_snap_realm(mdsc, realm); 401 realm = next; 402 } 403 if (realm) 404 ceph_put_snap_realm(mdsc, realm); 405 up_read(&mdsc->snap_rwsem); 406 407 return exceeded; 408 } 409 410 /* 411 * ceph_quota_is_max_files_exceeded - check if we can create a new file 412 * @inode: directory where a new file is being created 413 * 414 * This functions returns true is max_files quota allows a new file to be 415 * created. It is necessary to walk through the snaprealm hierarchy (until the 416 * FS root) to check all realms with quotas set. 417 */ 418 bool ceph_quota_is_max_files_exceeded(struct inode *inode) 419 { 420 if (!ceph_has_realms_with_quotas(inode)) 421 return false; 422 423 WARN_ON(!S_ISDIR(inode->i_mode)); 424 425 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_FILES_OP, 1); 426 } 427 428 /* 429 * ceph_quota_is_max_bytes_exceeded - check if we can write to a file 430 * @inode: inode being written 431 * @newsize: new size if write succeeds 432 * 433 * This functions returns true is max_bytes quota allows a file size to reach 434 * @newsize; it returns false otherwise. 435 */ 436 bool ceph_quota_is_max_bytes_exceeded(struct inode *inode, loff_t newsize) 437 { 438 loff_t size = i_size_read(inode); 439 440 if (!ceph_has_realms_with_quotas(inode)) 441 return false; 442 443 /* return immediately if we're decreasing file size */ 444 if (newsize <= size) 445 return false; 446 447 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_OP, (newsize - size)); 448 } 449 450 /* 451 * ceph_quota_is_max_bytes_approaching - check if we're reaching max_bytes 452 * @inode: inode being written 453 * @newsize: new size if write succeeds 454 * 455 * This function returns true if the new file size @newsize will be consuming 456 * more than 1/16th of the available quota space; it returns false otherwise. 457 */ 458 bool ceph_quota_is_max_bytes_approaching(struct inode *inode, loff_t newsize) 459 { 460 loff_t size = ceph_inode(inode)->i_reported_size; 461 462 if (!ceph_has_realms_with_quotas(inode)) 463 return false; 464 465 /* return immediately if we're decreasing file size */ 466 if (newsize <= size) 467 return false; 468 469 return check_quota_exceeded(inode, QUOTA_CHECK_MAX_BYTES_APPROACHING_OP, 470 (newsize - size)); 471 } 472 473 /* 474 * ceph_quota_update_statfs - if root has quota update statfs with quota status 475 * @fsc: filesystem client instance 476 * @buf: statfs to update 477 * 478 * If the mounted filesystem root has max_bytes quota set, update the filesystem 479 * statistics with the quota status. 480 * 481 * This function returns true if the stats have been updated, false otherwise. 482 */ 483 bool ceph_quota_update_statfs(struct ceph_fs_client *fsc, struct kstatfs *buf) 484 { 485 struct ceph_mds_client *mdsc = fsc->mdsc; 486 struct ceph_inode_info *ci; 487 struct ceph_snap_realm *realm; 488 struct inode *in; 489 u64 total = 0, used, free; 490 bool is_updated = false; 491 492 down_read(&mdsc->snap_rwsem); 493 get_quota_realm(mdsc, d_inode(fsc->sb->s_root), QUOTA_GET_MAX_BYTES, 494 &realm, true); 495 up_read(&mdsc->snap_rwsem); 496 if (!realm) 497 return false; 498 499 spin_lock(&realm->inodes_with_caps_lock); 500 in = realm->inode ? igrab(realm->inode) : NULL; 501 spin_unlock(&realm->inodes_with_caps_lock); 502 if (in) { 503 ci = ceph_inode(in); 504 spin_lock(&ci->i_ceph_lock); 505 if (ci->i_max_bytes) { 506 total = ci->i_max_bytes >> CEPH_BLOCK_SHIFT; 507 used = ci->i_rbytes >> CEPH_BLOCK_SHIFT; 508 /* For quota size less than 4MB, use 4KB block size */ 509 if (!total) { 510 total = ci->i_max_bytes >> CEPH_4K_BLOCK_SHIFT; 511 used = ci->i_rbytes >> CEPH_4K_BLOCK_SHIFT; 512 buf->f_frsize = 1 << CEPH_4K_BLOCK_SHIFT; 513 } 514 /* It is possible for a quota to be exceeded. 515 * Report 'zero' in that case 516 */ 517 free = total > used ? total - used : 0; 518 /* For quota size less than 4KB, report the 519 * total=used=4KB,free=0 when quota is full 520 * and total=free=4KB, used=0 otherwise */ 521 if (!total) { 522 total = 1; 523 free = ci->i_max_bytes > ci->i_rbytes ? 1 : 0; 524 buf->f_frsize = 1 << CEPH_4K_BLOCK_SHIFT; 525 } 526 } 527 spin_unlock(&ci->i_ceph_lock); 528 if (total) { 529 buf->f_blocks = total; 530 buf->f_bfree = free; 531 buf->f_bavail = free; 532 is_updated = true; 533 } 534 iput(in); 535 } 536 ceph_put_snap_realm(mdsc, realm); 537 538 return is_updated; 539 } 540 541