1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 5 */ 6 7 /* 8 * Quota change tags are associated with each transaction that allocates or 9 * deallocates space. Those changes are accumulated locally to each node (in a 10 * per-node file) and then are periodically synced to the quota file. This 11 * avoids the bottleneck of constantly touching the quota file, but introduces 12 * fuzziness in the current usage value of IDs that are being used on different 13 * nodes in the cluster simultaneously. So, it is possible for a user on 14 * multiple nodes to overrun their quota, but that overrun is controlable. 15 * Since quota tags are part of transactions, there is no need for a quota check 16 * program to be run on node crashes or anything like that. 17 * 18 * There are couple of knobs that let the administrator manage the quota 19 * fuzziness. "quota_quantum" sets the maximum time a quota change can be 20 * sitting on one node before being synced to the quota file. (The default is 21 * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency 22 * of quota file syncs increases as the user moves closer to their limit. The 23 * more frequent the syncs, the more accurate the quota enforcement, but that 24 * means that there is more contention between the nodes for the quota file. 25 * The default value is one. This sets the maximum theoretical quota overrun 26 * (with infinite node with infinite bandwidth) to twice the user's limit. (In 27 * practice, the maximum overrun you see should be much less.) A "quota_scale" 28 * number greater than one makes quota syncs more frequent and reduces the 29 * maximum overrun. Numbers less than one (but greater than zero) make quota 30 * syncs less frequent. 31 * 32 * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of 33 * the quota file, so it is not being constantly read. 34 */ 35 36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 37 38 #include <linux/sched.h> 39 #include <linux/slab.h> 40 #include <linux/mm.h> 41 #include <linux/spinlock.h> 42 #include <linux/completion.h> 43 #include <linux/buffer_head.h> 44 #include <linux/sort.h> 45 #include <linux/fs.h> 46 #include <linux/bio.h> 47 #include <linux/gfs2_ondisk.h> 48 #include <linux/kthread.h> 49 #include <linux/freezer.h> 50 #include <linux/quota.h> 51 #include <linux/dqblk_xfs.h> 52 #include <linux/lockref.h> 53 #include <linux/list_lru.h> 54 #include <linux/rcupdate.h> 55 #include <linux/rculist_bl.h> 56 #include <linux/bit_spinlock.h> 57 #include <linux/jhash.h> 58 #include <linux/vmalloc.h> 59 60 #include "gfs2.h" 61 #include "incore.h" 62 #include "bmap.h" 63 #include "glock.h" 64 #include "glops.h" 65 #include "log.h" 66 #include "meta_io.h" 67 #include "quota.h" 68 #include "rgrp.h" 69 #include "super.h" 70 #include "trans.h" 71 #include "inode.h" 72 #include "util.h" 73 74 #define GFS2_QD_HASH_SHIFT 12 75 #define GFS2_QD_HASH_SIZE BIT(GFS2_QD_HASH_SHIFT) 76 #define GFS2_QD_HASH_MASK (GFS2_QD_HASH_SIZE - 1) 77 78 /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */ 79 /* -> sd_bitmap_lock */ 80 static DEFINE_SPINLOCK(qd_lock); 81 struct list_lru gfs2_qd_lru; 82 83 static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE]; 84 85 static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp, 86 const struct kqid qid) 87 { 88 unsigned int h; 89 90 h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0); 91 h = jhash(&qid, sizeof(struct kqid), h); 92 93 return h & GFS2_QD_HASH_MASK; 94 } 95 96 static inline void spin_lock_bucket(unsigned int hash) 97 { 98 hlist_bl_lock(&qd_hash_table[hash]); 99 } 100 101 static inline void spin_unlock_bucket(unsigned int hash) 102 { 103 hlist_bl_unlock(&qd_hash_table[hash]); 104 } 105 106 static void gfs2_qd_dealloc(struct rcu_head *rcu) 107 { 108 struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu); 109 struct gfs2_sbd *sdp = qd->qd_sbd; 110 111 kmem_cache_free(gfs2_quotad_cachep, qd); 112 if (atomic_dec_and_test(&sdp->sd_quota_count)) 113 wake_up(&sdp->sd_kill_wait); 114 } 115 116 static void gfs2_qd_dispose(struct gfs2_quota_data *qd) 117 { 118 struct gfs2_sbd *sdp = qd->qd_sbd; 119 120 spin_lock(&qd_lock); 121 list_del(&qd->qd_list); 122 spin_unlock(&qd_lock); 123 124 spin_lock_bucket(qd->qd_hash); 125 hlist_bl_del_rcu(&qd->qd_hlist); 126 spin_unlock_bucket(qd->qd_hash); 127 128 if (!gfs2_withdrawn(sdp)) { 129 gfs2_assert_warn(sdp, !qd->qd_change); 130 gfs2_assert_warn(sdp, !qd->qd_slot_ref); 131 gfs2_assert_warn(sdp, !qd->qd_bh_count); 132 } 133 134 gfs2_glock_put(qd->qd_gl); 135 call_rcu(&qd->qd_rcu, gfs2_qd_dealloc); 136 } 137 138 static void gfs2_qd_list_dispose(struct list_head *list) 139 { 140 struct gfs2_quota_data *qd; 141 142 while (!list_empty(list)) { 143 qd = list_first_entry(list, struct gfs2_quota_data, qd_lru); 144 list_del(&qd->qd_lru); 145 146 gfs2_qd_dispose(qd); 147 } 148 } 149 150 151 static enum lru_status gfs2_qd_isolate(struct list_head *item, 152 struct list_lru_one *lru, void *arg) 153 { 154 struct list_head *dispose = arg; 155 struct gfs2_quota_data *qd = 156 list_entry(item, struct gfs2_quota_data, qd_lru); 157 enum lru_status status; 158 159 if (!spin_trylock(&qd->qd_lockref.lock)) 160 return LRU_SKIP; 161 162 status = LRU_SKIP; 163 if (qd->qd_lockref.count == 0) { 164 lockref_mark_dead(&qd->qd_lockref); 165 list_lru_isolate_move(lru, &qd->qd_lru, dispose); 166 status = LRU_REMOVED; 167 } 168 169 spin_unlock(&qd->qd_lockref.lock); 170 return status; 171 } 172 173 static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink, 174 struct shrink_control *sc) 175 { 176 LIST_HEAD(dispose); 177 unsigned long freed; 178 179 if (!(sc->gfp_mask & __GFP_FS)) 180 return SHRINK_STOP; 181 182 freed = list_lru_shrink_walk(&gfs2_qd_lru, sc, 183 gfs2_qd_isolate, &dispose); 184 185 gfs2_qd_list_dispose(&dispose); 186 187 return freed; 188 } 189 190 static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink, 191 struct shrink_control *sc) 192 { 193 return vfs_pressure_ratio(list_lru_shrink_count(&gfs2_qd_lru, sc)); 194 } 195 196 static struct shrinker *gfs2_qd_shrinker; 197 198 int __init gfs2_qd_shrinker_init(void) 199 { 200 gfs2_qd_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "gfs2-qd"); 201 if (!gfs2_qd_shrinker) 202 return -ENOMEM; 203 204 gfs2_qd_shrinker->count_objects = gfs2_qd_shrink_count; 205 gfs2_qd_shrinker->scan_objects = gfs2_qd_shrink_scan; 206 207 shrinker_register(gfs2_qd_shrinker); 208 209 return 0; 210 } 211 212 void gfs2_qd_shrinker_exit(void) 213 { 214 shrinker_free(gfs2_qd_shrinker); 215 } 216 217 static u64 qd2index(struct gfs2_quota_data *qd) 218 { 219 struct kqid qid = qd->qd_id; 220 return (2 * (u64)from_kqid(&init_user_ns, qid)) + 221 ((qid.type == USRQUOTA) ? 0 : 1); 222 } 223 224 static u64 qd2offset(struct gfs2_quota_data *qd) 225 { 226 return qd2index(qd) * sizeof(struct gfs2_quota); 227 } 228 229 static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid) 230 { 231 struct gfs2_quota_data *qd; 232 int error; 233 234 qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS); 235 if (!qd) 236 return NULL; 237 238 qd->qd_sbd = sdp; 239 lockref_init(&qd->qd_lockref); 240 qd->qd_id = qid; 241 qd->qd_slot = -1; 242 INIT_LIST_HEAD(&qd->qd_lru); 243 qd->qd_hash = hash; 244 245 error = gfs2_glock_get(sdp, qd2index(qd), 246 &gfs2_quota_glops, CREATE, &qd->qd_gl); 247 if (error) 248 goto fail; 249 250 return qd; 251 252 fail: 253 kmem_cache_free(gfs2_quotad_cachep, qd); 254 return NULL; 255 } 256 257 /* 258 * Lookup variant for callers which already hold qd_lock + bucket lock. 259 */ 260 static struct gfs2_quota_data * 261 gfs2_qd_search_bucket_noref(unsigned int hash, 262 const struct gfs2_sbd *sdp, 263 struct kqid qid) 264 { 265 struct gfs2_quota_data *qd; 266 struct hlist_bl_node *h; 267 268 hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) { 269 if (!qid_eq(qd->qd_id, qid)) 270 continue; 271 if (qd->qd_sbd == sdp) 272 return qd; 273 } 274 275 return NULL; 276 } 277 278 static struct gfs2_quota_data * 279 gfs2_qd_search_bucket(unsigned int hash, const struct gfs2_sbd *sdp, struct kqid qid) 280 { 281 struct gfs2_quota_data *qd; 282 283 qd = gfs2_qd_search_bucket_noref(hash, sdp, qid); 284 if (qd && lockref_get_not_dead(&qd->qd_lockref)) { 285 list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); 286 return qd; 287 } 288 289 return NULL; 290 } 291 292 293 static int qd_get(struct gfs2_sbd *sdp, struct kqid qid, 294 struct gfs2_quota_data **qdp) 295 { 296 struct gfs2_quota_data *qd, *new_qd; 297 unsigned int hash = gfs2_qd_hash(sdp, qid); 298 299 rcu_read_lock(); 300 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid); 301 rcu_read_unlock(); 302 303 if (qd) 304 return 0; 305 306 new_qd = qd_alloc(hash, sdp, qid); 307 if (!new_qd) 308 return -ENOMEM; 309 310 spin_lock(&qd_lock); 311 spin_lock_bucket(hash); 312 *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid); 313 if (qd == NULL) { 314 *qdp = new_qd; 315 list_add(&new_qd->qd_list, &sdp->sd_quota_list); 316 hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]); 317 atomic_inc(&sdp->sd_quota_count); 318 } 319 spin_unlock_bucket(hash); 320 spin_unlock(&qd_lock); 321 322 if (qd) { 323 gfs2_glock_put(new_qd->qd_gl); 324 kmem_cache_free(gfs2_quotad_cachep, new_qd); 325 } 326 327 return 0; 328 } 329 330 331 static void __qd_hold(struct gfs2_quota_data *qd) 332 { 333 struct gfs2_sbd *sdp = qd->qd_sbd; 334 gfs2_assert(sdp, qd->qd_lockref.count > 0); 335 qd->qd_lockref.count++; 336 } 337 338 static void qd_put(struct gfs2_quota_data *qd) 339 { 340 struct gfs2_sbd *sdp; 341 342 if (lockref_put_or_lock(&qd->qd_lockref)) 343 return; 344 345 BUG_ON(lockref_is_dead(&qd->qd_lockref)); 346 sdp = qd->qd_sbd; 347 if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { 348 lockref_mark_dead(&qd->qd_lockref); 349 spin_unlock(&qd->qd_lockref.lock); 350 351 list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); 352 gfs2_qd_dispose(qd); 353 return; 354 } 355 356 qd->qd_lockref.count = 0; 357 list_lru_add_obj(&gfs2_qd_lru, &qd->qd_lru); 358 spin_unlock(&qd->qd_lockref.lock); 359 } 360 361 static int slot_get(struct gfs2_quota_data *qd) 362 { 363 struct gfs2_sbd *sdp = qd->qd_sbd; 364 unsigned int bit; 365 int error = 0; 366 367 spin_lock(&sdp->sd_bitmap_lock); 368 if (qd->qd_slot_ref == 0) { 369 bit = find_first_zero_bit(sdp->sd_quota_bitmap, 370 sdp->sd_quota_slots); 371 if (bit >= sdp->sd_quota_slots) { 372 error = -ENOSPC; 373 goto out; 374 } 375 set_bit(bit, sdp->sd_quota_bitmap); 376 qd->qd_slot = bit; 377 } 378 qd->qd_slot_ref++; 379 out: 380 spin_unlock(&sdp->sd_bitmap_lock); 381 return error; 382 } 383 384 static void slot_hold(struct gfs2_quota_data *qd) 385 { 386 struct gfs2_sbd *sdp = qd->qd_sbd; 387 388 spin_lock(&sdp->sd_bitmap_lock); 389 gfs2_assert(sdp, qd->qd_slot_ref); 390 qd->qd_slot_ref++; 391 spin_unlock(&sdp->sd_bitmap_lock); 392 } 393 394 static void slot_put(struct gfs2_quota_data *qd) 395 { 396 struct gfs2_sbd *sdp = qd->qd_sbd; 397 398 spin_lock(&sdp->sd_bitmap_lock); 399 gfs2_assert(sdp, qd->qd_slot_ref); 400 if (!--qd->qd_slot_ref) { 401 BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap)); 402 qd->qd_slot = -1; 403 } 404 spin_unlock(&sdp->sd_bitmap_lock); 405 } 406 407 static int bh_get(struct gfs2_quota_data *qd) 408 { 409 struct gfs2_sbd *sdp = qd->qd_sbd; 410 struct inode *inode = sdp->sd_qc_inode; 411 struct gfs2_glock *gl = gfs2_inode_glock(inode); 412 unsigned int block, offset; 413 struct buffer_head *bh = NULL; 414 struct iomap iomap = { }; 415 int error; 416 417 spin_lock(&qd->qd_lockref.lock); 418 if (qd->qd_bh_count) { 419 qd->qd_bh_count++; 420 spin_unlock(&qd->qd_lockref.lock); 421 return 0; 422 } 423 spin_unlock(&qd->qd_lockref.lock); 424 425 block = qd->qd_slot / sdp->sd_qc_per_block; 426 offset = qd->qd_slot % sdp->sd_qc_per_block; 427 428 error = gfs2_iomap_get(inode, 429 (loff_t)block << inode->i_blkbits, 430 i_blocksize(inode), &iomap); 431 if (error) 432 return error; 433 error = -ENOENT; 434 if (iomap.type != IOMAP_MAPPED) 435 return error; 436 437 error = gfs2_meta_read(gl, iomap.addr >> inode->i_blkbits, 438 DIO_WAIT, 0, &bh); 439 if (error) 440 return error; 441 error = -EIO; 442 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) 443 goto out; 444 445 spin_lock(&qd->qd_lockref.lock); 446 if (qd->qd_bh == NULL) { 447 qd->qd_bh = bh; 448 qd->qd_bh_qc = (struct gfs2_quota_change *) 449 (bh->b_data + sizeof(struct gfs2_meta_header) + 450 offset * sizeof(struct gfs2_quota_change)); 451 bh = NULL; 452 } 453 qd->qd_bh_count++; 454 spin_unlock(&qd->qd_lockref.lock); 455 error = 0; 456 457 out: 458 brelse(bh); 459 return error; 460 } 461 462 static void bh_put(struct gfs2_quota_data *qd) 463 { 464 struct gfs2_sbd *sdp = qd->qd_sbd; 465 struct buffer_head *bh = NULL; 466 467 spin_lock(&qd->qd_lockref.lock); 468 gfs2_assert(sdp, qd->qd_bh_count); 469 if (!--qd->qd_bh_count) { 470 bh = qd->qd_bh; 471 qd->qd_bh = NULL; 472 qd->qd_bh_qc = NULL; 473 } 474 spin_unlock(&qd->qd_lockref.lock); 475 brelse(bh); 476 } 477 478 static bool qd_grab_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd, 479 u64 sync_gen) 480 { 481 bool ret = false; 482 483 spin_lock(&qd->qd_lockref.lock); 484 if (test_bit(QDF_LOCKED, &qd->qd_flags) || 485 !test_bit(QDF_CHANGE, &qd->qd_flags) || 486 qd->qd_sync_gen >= sync_gen) 487 goto out; 488 489 if (lockref_is_dead(&qd->qd_lockref)) 490 goto out; 491 qd->qd_lockref.count++; 492 493 list_move_tail(&qd->qd_list, &sdp->sd_quota_list); 494 set_bit(QDF_LOCKED, &qd->qd_flags); 495 qd->qd_change_sync = qd->qd_change; 496 slot_hold(qd); 497 ret = true; 498 499 out: 500 spin_unlock(&qd->qd_lockref.lock); 501 return ret; 502 } 503 504 static void qd_ungrab_sync(struct gfs2_quota_data *qd) 505 { 506 clear_bit(QDF_LOCKED, &qd->qd_flags); 507 slot_put(qd); 508 qd_put(qd); 509 } 510 511 static void qdsb_put(struct gfs2_quota_data *qd) 512 { 513 bh_put(qd); 514 slot_put(qd); 515 qd_put(qd); 516 } 517 518 static void qd_unlock(struct gfs2_quota_data *qd) 519 { 520 spin_lock(&qd->qd_lockref.lock); 521 gfs2_assert_warn(qd->qd_sbd, test_bit(QDF_LOCKED, &qd->qd_flags)); 522 clear_bit(QDF_LOCKED, &qd->qd_flags); 523 spin_unlock(&qd->qd_lockref.lock); 524 qdsb_put(qd); 525 } 526 527 static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid, 528 struct gfs2_quota_data **qdp) 529 { 530 int error; 531 532 error = qd_get(sdp, qid, qdp); 533 if (error) 534 return error; 535 536 error = slot_get(*qdp); 537 if (error) 538 goto fail; 539 540 error = bh_get(*qdp); 541 if (error) 542 goto fail_slot; 543 544 return 0; 545 546 fail_slot: 547 slot_put(*qdp); 548 fail: 549 qd_put(*qdp); 550 return error; 551 } 552 553 /** 554 * gfs2_qa_get - make sure we have a quota allocations data structure, 555 * if necessary 556 * @ip: the inode for this reservation 557 */ 558 int gfs2_qa_get(struct gfs2_inode *ip) 559 { 560 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 561 struct inode *inode = &ip->i_inode; 562 563 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 564 return 0; 565 if (ip->i_diskflags & GFS2_DIF_SYSTEM) 566 return 0; 567 568 spin_lock(&inode->i_lock); 569 if (ip->i_qadata == NULL) { 570 struct gfs2_qadata *tmp; 571 572 spin_unlock(&inode->i_lock); 573 tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS); 574 if (!tmp) 575 return -ENOMEM; 576 577 spin_lock(&inode->i_lock); 578 if (ip->i_qadata == NULL) 579 ip->i_qadata = tmp; 580 else 581 kmem_cache_free(gfs2_qadata_cachep, tmp); 582 } 583 ip->i_qadata->qa_ref++; 584 spin_unlock(&inode->i_lock); 585 return 0; 586 } 587 588 void gfs2_qa_put(struct gfs2_inode *ip) 589 { 590 struct inode *inode = &ip->i_inode; 591 592 spin_lock(&inode->i_lock); 593 if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) { 594 kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata); 595 ip->i_qadata = NULL; 596 } 597 spin_unlock(&inode->i_lock); 598 } 599 600 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) 601 { 602 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 603 struct gfs2_quota_data **qd; 604 int error; 605 606 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 607 return 0; 608 609 error = gfs2_qa_get(ip); 610 if (error || !ip->i_qadata) 611 return error; 612 613 qd = ip->i_qadata->qa_qd; 614 615 if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) || 616 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) { 617 error = -EIO; 618 gfs2_qa_put(ip); 619 goto out; 620 } 621 622 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd); 623 if (error) 624 goto out_unhold; 625 ip->i_qadata->qa_qd_num++; 626 qd++; 627 628 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd); 629 if (error) 630 goto out_unhold; 631 ip->i_qadata->qa_qd_num++; 632 qd++; 633 634 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) && 635 !uid_eq(uid, ip->i_inode.i_uid)) { 636 error = qdsb_get(sdp, make_kqid_uid(uid), qd); 637 if (error) 638 goto out_unhold; 639 ip->i_qadata->qa_qd_num++; 640 qd++; 641 } 642 643 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) && 644 !gid_eq(gid, ip->i_inode.i_gid)) { 645 error = qdsb_get(sdp, make_kqid_gid(gid), qd); 646 if (error) 647 goto out_unhold; 648 ip->i_qadata->qa_qd_num++; 649 qd++; 650 } 651 652 out_unhold: 653 if (error) 654 gfs2_quota_unhold(ip); 655 out: 656 return error; 657 } 658 659 void gfs2_quota_unhold(struct gfs2_inode *ip) 660 { 661 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 662 u32 x; 663 664 if (ip->i_qadata == NULL) 665 return; 666 667 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)); 668 669 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 670 qdsb_put(ip->i_qadata->qa_qd[x]); 671 ip->i_qadata->qa_qd[x] = NULL; 672 } 673 ip->i_qadata->qa_qd_num = 0; 674 gfs2_qa_put(ip); 675 } 676 677 static int sort_qd(const void *a, const void *b) 678 { 679 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a; 680 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b; 681 682 if (qid_lt(qd_a->qd_id, qd_b->qd_id)) 683 return -1; 684 if (qid_lt(qd_b->qd_id, qd_a->qd_id)) 685 return 1; 686 return 0; 687 } 688 689 static void do_qc(struct gfs2_quota_data *qd, s64 change) 690 { 691 struct gfs2_sbd *sdp = qd->qd_sbd; 692 struct gfs2_glock *gl = gfs2_inode_glock(sdp->sd_qc_inode); 693 struct gfs2_quota_change *qc = qd->qd_bh_qc; 694 bool needs_put = false; 695 s64 x; 696 697 gfs2_trans_add_meta(gl, qd->qd_bh); 698 699 /* 700 * The QDF_CHANGE flag indicates that the slot in the quota change file 701 * is used. Here, we use the value of qc->qc_change when the slot is 702 * used, and we assume a value of 0 otherwise. 703 */ 704 705 spin_lock(&qd->qd_lockref.lock); 706 707 x = 0; 708 if (test_bit(QDF_CHANGE, &qd->qd_flags)) 709 x = be64_to_cpu(qc->qc_change); 710 x += change; 711 qd->qd_change += change; 712 713 if (!x && test_bit(QDF_CHANGE, &qd->qd_flags)) { 714 /* The slot in the quota change file becomes unused. */ 715 clear_bit(QDF_CHANGE, &qd->qd_flags); 716 qc->qc_flags = 0; 717 qc->qc_id = 0; 718 needs_put = true; 719 } else if (x && !test_bit(QDF_CHANGE, &qd->qd_flags)) { 720 /* The slot in the quota change file becomes used. */ 721 set_bit(QDF_CHANGE, &qd->qd_flags); 722 __qd_hold(qd); 723 slot_hold(qd); 724 725 qc->qc_flags = 0; 726 if (qd->qd_id.type == USRQUOTA) 727 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER); 728 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id)); 729 } 730 qc->qc_change = cpu_to_be64(x); 731 732 spin_unlock(&qd->qd_lockref.lock); 733 734 if (needs_put) { 735 slot_put(qd); 736 qd_put(qd); 737 } 738 if (change < 0) /* Reset quiet flag if we freed some blocks */ 739 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags); 740 } 741 742 static int gfs2_write_buf_to_page(struct gfs2_sbd *sdp, unsigned long index, 743 unsigned off, void *buf, unsigned bytes) 744 { 745 struct inode *inode = sdp->sd_quota_inode; 746 struct gfs2_glock *gl = gfs2_inode_glock(inode); 747 struct address_space *mapping = inode->i_mapping; 748 struct folio *folio; 749 struct buffer_head *bh; 750 u64 blk; 751 unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0; 752 unsigned to_write = bytes, pg_off = off; 753 754 blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift); 755 boff = off % bsize; 756 757 folio = filemap_grab_folio(mapping, index); 758 if (IS_ERR(folio)) 759 return PTR_ERR(folio); 760 bh = folio_buffers(folio); 761 if (!bh) 762 bh = create_empty_buffers(folio, bsize, 0); 763 764 for (;;) { 765 /* Find the beginning block within the folio */ 766 if (pg_off >= ((bnum * bsize) + bsize)) { 767 bh = bh->b_this_page; 768 bnum++; 769 blk++; 770 continue; 771 } 772 if (!buffer_mapped(bh)) { 773 gfs2_block_map(inode, blk, bh, 1); 774 if (!buffer_mapped(bh)) 775 goto unlock_out; 776 /* If it's a newly allocated disk block, zero it */ 777 if (buffer_new(bh)) 778 folio_zero_range(folio, bnum * bsize, 779 bh->b_size); 780 } 781 if (folio_test_uptodate(folio)) 782 set_buffer_uptodate(bh); 783 if (bh_read(bh, REQ_META | REQ_PRIO) < 0) 784 goto unlock_out; 785 gfs2_trans_add_data(gl, bh); 786 787 /* If we need to write to the next block as well */ 788 if (to_write > (bsize - boff)) { 789 pg_off += (bsize - boff); 790 to_write -= (bsize - boff); 791 boff = pg_off % bsize; 792 continue; 793 } 794 break; 795 } 796 797 /* Write to the folio, now that we have setup the buffer(s) */ 798 memcpy_to_folio(folio, off, buf, bytes); 799 flush_dcache_folio(folio); 800 folio_unlock(folio); 801 folio_put(folio); 802 803 return 0; 804 805 unlock_out: 806 folio_unlock(folio); 807 folio_put(folio); 808 return -EIO; 809 } 810 811 static int gfs2_write_disk_quota(struct gfs2_sbd *sdp, struct gfs2_quota *qp, 812 loff_t loc) 813 { 814 unsigned long pg_beg; 815 unsigned pg_off, nbytes, overflow = 0; 816 int error; 817 void *ptr; 818 819 nbytes = sizeof(struct gfs2_quota); 820 821 pg_beg = loc >> PAGE_SHIFT; 822 pg_off = offset_in_page(loc); 823 824 /* If the quota straddles a page boundary, split the write in two */ 825 if ((pg_off + nbytes) > PAGE_SIZE) 826 overflow = (pg_off + nbytes) - PAGE_SIZE; 827 828 ptr = qp; 829 error = gfs2_write_buf_to_page(sdp, pg_beg, pg_off, ptr, 830 nbytes - overflow); 831 /* If there's an overflow, write the remaining bytes to the next page */ 832 if (!error && overflow) 833 error = gfs2_write_buf_to_page(sdp, pg_beg + 1, 0, 834 ptr + nbytes - overflow, 835 overflow); 836 return error; 837 } 838 839 /** 840 * gfs2_adjust_quota - adjust record of current block usage 841 * @sdp: The superblock 842 * @loc: Offset of the entry in the quota file 843 * @change: The amount of usage change to record 844 * @qd: The quota data 845 * @fdq: The updated limits to record 846 * 847 * This function was mostly borrowed from gfs2_block_truncate_page which was 848 * in turn mostly borrowed from ext3 849 * 850 * Returns: 0 or -ve on error 851 */ 852 853 static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc, 854 s64 change, struct gfs2_quota_data *qd, 855 struct qc_dqblk *fdq) 856 { 857 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 858 struct inode *inode = &ip->i_inode; 859 struct gfs2_quota q; 860 int err; 861 u64 size; 862 863 if (gfs2_is_stuffed(ip)) { 864 err = gfs2_unstuff_dinode(ip); 865 if (err) 866 return err; 867 } 868 869 memset(&q, 0, sizeof(struct gfs2_quota)); 870 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q)); 871 if (err < 0) 872 return err; 873 874 loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */ 875 be64_add_cpu(&q.qu_value, change); 876 if (((s64)be64_to_cpu(q.qu_value)) < 0) 877 q.qu_value = 0; /* Never go negative on quota usage */ 878 spin_lock(&qd->qd_lockref.lock); 879 qd->qd_qb.qb_value = q.qu_value; 880 if (fdq) { 881 if (fdq->d_fieldmask & QC_SPC_SOFT) { 882 q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift); 883 qd->qd_qb.qb_warn = q.qu_warn; 884 } 885 if (fdq->d_fieldmask & QC_SPC_HARD) { 886 q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift); 887 qd->qd_qb.qb_limit = q.qu_limit; 888 } 889 if (fdq->d_fieldmask & QC_SPACE) { 890 q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift); 891 qd->qd_qb.qb_value = q.qu_value; 892 } 893 } 894 spin_unlock(&qd->qd_lockref.lock); 895 896 err = gfs2_write_disk_quota(sdp, &q, loc); 897 if (!err) { 898 size = loc + sizeof(struct gfs2_quota); 899 if (size > inode->i_size) 900 i_size_write(inode, size); 901 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 902 mark_inode_dirty(inode); 903 set_bit(QDF_REFRESH, &qd->qd_flags); 904 } 905 906 return err; 907 } 908 909 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda, 910 u64 sync_gen) 911 { 912 struct gfs2_sbd *sdp = (*qda)->qd_sbd; 913 struct inode *inode = sdp->sd_quota_inode; 914 struct gfs2_glock *gl = gfs2_inode_glock(inode); 915 struct gfs2_inode *ip = GFS2_I(inode); 916 struct gfs2_alloc_parms ap = {}; 917 unsigned int data_blocks, ind_blocks; 918 struct gfs2_holder *ghs, i_gh; 919 unsigned int qx, x; 920 struct gfs2_quota_data *qd; 921 unsigned reserved; 922 loff_t offset; 923 unsigned int nalloc = 0, blocks; 924 int error; 925 926 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), 927 &data_blocks, &ind_blocks); 928 929 ghs = kmalloc_objs(struct gfs2_holder, num_qd, GFP_NOFS); 930 if (!ghs) 931 return -ENOMEM; 932 933 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL); 934 inode_lock(inode); 935 for (qx = 0; qx < num_qd; qx++) { 936 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE, 937 GL_NOCACHE, &ghs[qx]); 938 if (error) 939 goto out_dq; 940 } 941 942 error = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, 0, &i_gh); 943 if (error) 944 goto out_dq; 945 946 for (x = 0; x < num_qd; x++) { 947 offset = qd2offset(qda[x]); 948 if (gfs2_write_alloc_required(ip, offset, 949 sizeof(struct gfs2_quota))) 950 nalloc++; 951 } 952 953 /* 954 * 1 blk for unstuffing inode if stuffed. We add this extra 955 * block to the reservation unconditionally. If the inode 956 * doesn't need unstuffing, the block will be released to the 957 * rgrp since it won't be allocated during the transaction 958 */ 959 /* +3 in the end for unstuffing block, inode size update block 960 * and another block in case quota straddles page boundary and 961 * two blocks need to be updated instead of 1 */ 962 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3; 963 964 reserved = 1 + (nalloc * (data_blocks + ind_blocks)); 965 ap.target = reserved; 966 error = gfs2_inplace_reserve(ip, &ap); 967 if (error) 968 goto out_alloc; 969 970 if (nalloc) 971 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS; 972 973 error = gfs2_trans_begin(sdp, blocks, 0); 974 if (error) 975 goto out_ipres; 976 977 for (x = 0; x < num_qd; x++) { 978 qd = qda[x]; 979 offset = qd2offset(qd); 980 error = gfs2_adjust_quota(sdp, offset, qd->qd_change_sync, qd, 981 NULL); 982 if (error) 983 goto out_end_trans; 984 985 do_qc(qd, -qd->qd_change_sync); 986 set_bit(QDF_REFRESH, &qd->qd_flags); 987 } 988 989 out_end_trans: 990 gfs2_trans_end(sdp); 991 out_ipres: 992 gfs2_inplace_release(ip); 993 out_alloc: 994 gfs2_glock_dq_uninit(&i_gh); 995 out_dq: 996 while (qx--) 997 gfs2_glock_dq_uninit(&ghs[qx]); 998 inode_unlock(inode); 999 kfree(ghs); 1000 gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC); 1001 if (!error) { 1002 for (x = 0; x < num_qd; x++) { 1003 qd = qda[x]; 1004 spin_lock(&qd->qd_lockref.lock); 1005 if (qd->qd_sync_gen < sync_gen) 1006 qd->qd_sync_gen = sync_gen; 1007 spin_unlock(&qd->qd_lockref.lock); 1008 } 1009 } 1010 return error; 1011 } 1012 1013 static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd) 1014 { 1015 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 1016 struct gfs2_quota q; 1017 struct gfs2_quota_lvb *qlvb; 1018 loff_t pos; 1019 int error; 1020 1021 memset(&q, 0, sizeof(struct gfs2_quota)); 1022 pos = qd2offset(qd); 1023 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q)); 1024 if (error < 0) 1025 return error; 1026 1027 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1028 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC); 1029 qlvb->__pad = 0; 1030 qlvb->qb_limit = q.qu_limit; 1031 qlvb->qb_warn = q.qu_warn; 1032 qlvb->qb_value = q.qu_value; 1033 spin_lock(&qd->qd_lockref.lock); 1034 qd->qd_qb = *qlvb; 1035 spin_unlock(&qd->qd_lockref.lock); 1036 1037 return 0; 1038 } 1039 1040 static int do_glock(struct gfs2_quota_data *qd, int force_refresh, 1041 struct gfs2_holder *q_gh) 1042 { 1043 struct gfs2_sbd *sdp = qd->qd_sbd; 1044 struct gfs2_glock *gl = gfs2_inode_glock(sdp->sd_quota_inode); 1045 struct gfs2_holder i_gh; 1046 int error; 1047 1048 gfs2_assert_warn(sdp, sdp == glock_sbd(qd->qd_gl)); 1049 restart: 1050 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh); 1051 if (error) 1052 return error; 1053 1054 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags)) 1055 force_refresh = FORCE; 1056 1057 spin_lock(&qd->qd_lockref.lock); 1058 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1059 spin_unlock(&qd->qd_lockref.lock); 1060 1061 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) { 1062 gfs2_glock_dq_uninit(q_gh); 1063 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 1064 GL_NOCACHE, q_gh); 1065 if (error) 1066 return error; 1067 1068 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &i_gh); 1069 if (error) 1070 goto fail; 1071 1072 error = update_qd(sdp, qd); 1073 if (error) 1074 goto fail_gunlock; 1075 1076 gfs2_glock_dq_uninit(&i_gh); 1077 gfs2_glock_dq_uninit(q_gh); 1078 force_refresh = 0; 1079 goto restart; 1080 } 1081 1082 return 0; 1083 1084 fail_gunlock: 1085 gfs2_glock_dq_uninit(&i_gh); 1086 fail: 1087 gfs2_glock_dq_uninit(q_gh); 1088 return error; 1089 } 1090 1091 int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) 1092 { 1093 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1094 struct gfs2_quota_data *qd; 1095 u32 x; 1096 int error; 1097 1098 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1099 return 0; 1100 1101 error = gfs2_quota_hold(ip, uid, gid); 1102 if (error) 1103 return error; 1104 /* Meta inodes never get quota data (see gfs2_qa_get()); nothing to lock. */ 1105 if (!ip->i_qadata) 1106 return 0; 1107 1108 sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num, 1109 sizeof(struct gfs2_quota_data *), sort_qd, NULL); 1110 1111 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1112 qd = ip->i_qadata->qa_qd[x]; 1113 error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]); 1114 if (error) 1115 break; 1116 } 1117 1118 if (!error) 1119 set_bit(GIF_QD_LOCKED, &ip->i_flags); 1120 else { 1121 while (x--) 1122 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]); 1123 gfs2_quota_unhold(ip); 1124 } 1125 1126 return error; 1127 } 1128 1129 static bool need_sync(struct gfs2_quota_data *qd) 1130 { 1131 struct gfs2_sbd *sdp = qd->qd_sbd; 1132 struct gfs2_tune *gt = &sdp->sd_tune; 1133 s64 value, change, limit; 1134 unsigned int num, den; 1135 int ret = false; 1136 1137 spin_lock(&qd->qd_lockref.lock); 1138 if (!qd->qd_qb.qb_limit) 1139 goto out; 1140 1141 change = qd->qd_change; 1142 if (change <= 0) 1143 goto out; 1144 value = (s64)be64_to_cpu(qd->qd_qb.qb_value); 1145 limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit); 1146 if (value >= limit) 1147 goto out; 1148 1149 spin_lock(>->gt_spin); 1150 num = gt->gt_quota_scale_num; 1151 den = gt->gt_quota_scale_den; 1152 spin_unlock(>->gt_spin); 1153 1154 change *= gfs2_jindex_size(sdp) * num; 1155 change = div_s64(change, den); 1156 if (value + change < limit) 1157 goto out; 1158 1159 ret = true; 1160 out: 1161 spin_unlock(&qd->qd_lockref.lock); 1162 return ret; 1163 } 1164 1165 void gfs2_quota_unlock(struct gfs2_inode *ip) 1166 { 1167 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1168 struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS]; 1169 unsigned int count = 0; 1170 u32 x; 1171 1172 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags)) 1173 return; 1174 1175 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1176 struct gfs2_quota_data *qd; 1177 bool sync; 1178 int error; 1179 1180 qd = ip->i_qadata->qa_qd[x]; 1181 sync = need_sync(qd); 1182 1183 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]); 1184 if (!sync) 1185 continue; 1186 1187 spin_lock(&qd_lock); 1188 sync = qd_grab_sync(sdp, qd, U64_MAX); 1189 spin_unlock(&qd_lock); 1190 1191 if (!sync) 1192 continue; 1193 1194 gfs2_assert_warn(sdp, qd->qd_change_sync); 1195 error = bh_get(qd); 1196 if (error) { 1197 qd_ungrab_sync(qd); 1198 continue; 1199 } 1200 1201 qda[count++] = qd; 1202 } 1203 1204 if (count) { 1205 u64 sync_gen = READ_ONCE(sdp->sd_quota_sync_gen); 1206 1207 do_sync(count, qda, sync_gen); 1208 for (x = 0; x < count; x++) 1209 qd_unlock(qda[x]); 1210 } 1211 1212 gfs2_quota_unhold(ip); 1213 } 1214 1215 #define MAX_LINE 256 1216 1217 static void print_message(struct gfs2_quota_data *qd, char *type) 1218 { 1219 struct gfs2_sbd *sdp = qd->qd_sbd; 1220 1221 if (sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET) { 1222 fs_info(sdp, "quota %s for %s %u\n", 1223 type, 1224 (qd->qd_id.type == USRQUOTA) ? "user" : "group", 1225 from_kqid(&init_user_ns, qd->qd_id)); 1226 } 1227 } 1228 1229 /** 1230 * gfs2_quota_check - check if allocating new blocks will exceed quota 1231 * @ip: The inode for which this check is being performed 1232 * @uid: The uid to check against 1233 * @gid: The gid to check against 1234 * @ap: The allocation parameters. ap->target contains the requested 1235 * blocks. ap->min_target, if set, contains the minimum blks 1236 * requested. 1237 * 1238 * Returns: 0 on success. 1239 * min_req = ap->min_target ? ap->min_target : ap->target; 1240 * quota must allow at least min_req blks for success and 1241 * ap->allowed is set to the number of blocks allowed 1242 * 1243 * -EDQUOT otherwise, quota violation. ap->allowed is set to number 1244 * of blocks available. 1245 */ 1246 int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid, 1247 struct gfs2_alloc_parms *ap) 1248 { 1249 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1250 struct gfs2_quota_data *qd; 1251 s64 value, warn, limit; 1252 u32 x; 1253 int error = 0; 1254 1255 ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */ 1256 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags)) 1257 return 0; 1258 1259 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1260 qd = ip->i_qadata->qa_qd[x]; 1261 1262 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) || 1263 qid_eq(qd->qd_id, make_kqid_gid(gid)))) 1264 continue; 1265 1266 spin_lock(&qd->qd_lockref.lock); 1267 warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn); 1268 limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit); 1269 value = (s64)be64_to_cpu(qd->qd_qb.qb_value); 1270 value += qd->qd_change; 1271 spin_unlock(&qd->qd_lockref.lock); 1272 1273 if (limit > 0 && (limit - value) < ap->allowed) 1274 ap->allowed = limit - value; 1275 /* If we can't meet the target */ 1276 if (limit && limit < (value + (s64)ap->target)) { 1277 /* If no min_target specified or we don't meet 1278 * min_target, return -EDQUOT */ 1279 if (!ap->min_target || ap->min_target > ap->allowed) { 1280 if (!test_and_set_bit(QDF_QMSG_QUIET, 1281 &qd->qd_flags)) { 1282 print_message(qd, "exceeded"); 1283 quota_send_warning(qd->qd_id, 1284 sdp->sd_vfs->s_dev, 1285 QUOTA_NL_BHARDWARN); 1286 } 1287 error = -EDQUOT; 1288 break; 1289 } 1290 } else if (warn && warn < value && 1291 time_after_eq(jiffies, qd->qd_last_warn + 1292 gfs2_tune_get(sdp, gt_quota_warn_period) 1293 * HZ)) { 1294 quota_send_warning(qd->qd_id, 1295 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN); 1296 print_message(qd, "warning"); 1297 error = 0; 1298 qd->qd_last_warn = jiffies; 1299 } 1300 } 1301 return error; 1302 } 1303 1304 void gfs2_quota_change(struct gfs2_inode *ip, s64 change, 1305 kuid_t uid, kgid_t gid) 1306 { 1307 struct gfs2_quota_data *qd; 1308 u32 x; 1309 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1310 1311 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF || 1312 gfs2_assert_warn(sdp, change)) 1313 return; 1314 if (ip->i_diskflags & GFS2_DIF_SYSTEM) 1315 return; 1316 1317 if (gfs2_assert_withdraw(sdp, ip->i_qadata && 1318 ip->i_qadata->qa_ref > 0)) 1319 return; 1320 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1321 qd = ip->i_qadata->qa_qd[x]; 1322 1323 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) || 1324 qid_eq(qd->qd_id, make_kqid_gid(gid))) { 1325 do_qc(qd, change); 1326 } 1327 } 1328 } 1329 1330 int gfs2_quota_sync(struct super_block *sb, int type) 1331 { 1332 struct gfs2_sbd *sdp = sb->s_fs_info; 1333 struct gfs2_quota_data **qda; 1334 unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder); 1335 u64 sync_gen; 1336 int error = 0; 1337 1338 if (sb_rdonly(sdp->sd_vfs)) 1339 return 0; 1340 1341 qda = kzalloc_objs(struct gfs2_quota_data *, max_qd); 1342 if (!qda) 1343 return -ENOMEM; 1344 1345 mutex_lock(&sdp->sd_quota_sync_mutex); 1346 sync_gen = sdp->sd_quota_sync_gen + 1; 1347 1348 do { 1349 struct gfs2_quota_data *iter; 1350 unsigned int num_qd = 0; 1351 unsigned int x; 1352 1353 spin_lock(&qd_lock); 1354 list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) { 1355 if (qd_grab_sync(sdp, iter, sync_gen)) { 1356 qda[num_qd++] = iter; 1357 if (num_qd == max_qd) 1358 break; 1359 } 1360 } 1361 spin_unlock(&qd_lock); 1362 1363 if (!num_qd) 1364 break; 1365 1366 for (x = 0; x < num_qd; x++) { 1367 error = bh_get(qda[x]); 1368 if (!error) 1369 continue; 1370 1371 while (x < num_qd) 1372 qd_ungrab_sync(qda[--num_qd]); 1373 break; 1374 } 1375 1376 if (!error) { 1377 WRITE_ONCE(sdp->sd_quota_sync_gen, sync_gen); 1378 error = do_sync(num_qd, qda, sync_gen); 1379 } 1380 1381 for (x = 0; x < num_qd; x++) 1382 qd_unlock(qda[x]); 1383 } while (!error); 1384 1385 mutex_unlock(&sdp->sd_quota_sync_mutex); 1386 kfree(qda); 1387 1388 return error; 1389 } 1390 1391 int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid) 1392 { 1393 struct gfs2_quota_data *qd; 1394 struct gfs2_holder q_gh; 1395 int error; 1396 1397 error = qd_get(sdp, qid, &qd); 1398 if (error) 1399 return error; 1400 1401 error = do_glock(qd, FORCE, &q_gh); 1402 if (!error) 1403 gfs2_glock_dq_uninit(&q_gh); 1404 1405 qd_put(qd); 1406 return error; 1407 } 1408 1409 int gfs2_quota_init(struct gfs2_sbd *sdp) 1410 { 1411 struct inode *inode = sdp->sd_qc_inode; 1412 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1413 u64 size = i_size_read(sdp->sd_qc_inode); 1414 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift; 1415 unsigned int x, slot = 0; 1416 unsigned int found = 0; 1417 unsigned int hash; 1418 unsigned int bm_size; 1419 struct buffer_head *bh; 1420 u64 dblock; 1421 u32 extlen = 0; 1422 int error; 1423 1424 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20)) 1425 return -EIO; 1426 1427 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; 1428 bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long)); 1429 bm_size *= sizeof(unsigned long); 1430 error = -ENOMEM; 1431 sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN); 1432 if (sdp->sd_quota_bitmap == NULL) 1433 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS | 1434 __GFP_ZERO); 1435 if (!sdp->sd_quota_bitmap) 1436 return error; 1437 1438 for (x = 0; x < blocks; x++) { 1439 struct gfs2_quota_change *qc; 1440 unsigned int y; 1441 1442 if (!extlen) { 1443 extlen = 32; 1444 error = gfs2_get_extent(inode, x, &dblock, &extlen); 1445 if (error) 1446 goto fail; 1447 } 1448 error = -EIO; 1449 bh = gfs2_meta_ra(gl, dblock, extlen); 1450 if (!bh) 1451 goto fail; 1452 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) 1453 goto fail_brelse; 1454 1455 qc = (struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header)); 1456 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots; 1457 y++, slot++, qc++) { 1458 struct gfs2_quota_data *old_qd, *qd; 1459 s64 qc_change = be64_to_cpu(qc->qc_change); 1460 u32 qc_flags = be32_to_cpu(qc->qc_flags); 1461 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ? 1462 USRQUOTA : GRPQUOTA; 1463 struct kqid qc_id = make_kqid(&init_user_ns, qtype, 1464 be32_to_cpu(qc->qc_id)); 1465 if (!qc_change) 1466 continue; 1467 1468 hash = gfs2_qd_hash(sdp, qc_id); 1469 qd = qd_alloc(hash, sdp, qc_id); 1470 if (qd == NULL) 1471 goto fail_brelse; 1472 1473 qd->qd_lockref.count = 0; 1474 set_bit(QDF_CHANGE, &qd->qd_flags); 1475 qd->qd_change = qc_change; 1476 qd->qd_slot = slot; 1477 qd->qd_slot_ref = 1; 1478 1479 spin_lock(&qd_lock); 1480 spin_lock_bucket(hash); 1481 old_qd = gfs2_qd_search_bucket_noref(hash, sdp, qc_id); 1482 if (old_qd) { 1483 fs_err(sdp, "Corruption found in quota_change%u " 1484 "file: duplicate identifier in " 1485 "slot %u\n", 1486 sdp->sd_jdesc->jd_jid, slot); 1487 1488 spin_unlock_bucket(hash); 1489 spin_unlock(&qd_lock); 1490 1491 gfs2_glock_put(qd->qd_gl); 1492 kmem_cache_free(gfs2_quotad_cachep, qd); 1493 1494 /* zero out the duplicate slot */ 1495 lock_buffer(bh); 1496 memset(qc, 0, sizeof(*qc)); 1497 mark_buffer_dirty(bh); 1498 unlock_buffer(bh); 1499 1500 continue; 1501 } 1502 BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap)); 1503 list_add(&qd->qd_list, &sdp->sd_quota_list); 1504 atomic_inc(&sdp->sd_quota_count); 1505 hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]); 1506 spin_unlock_bucket(hash); 1507 spin_unlock(&qd_lock); 1508 1509 found++; 1510 } 1511 1512 if (buffer_dirty(bh)) 1513 sync_dirty_buffer(bh); 1514 brelse(bh); 1515 dblock++; 1516 extlen--; 1517 } 1518 1519 if (found) 1520 fs_info(sdp, "found %u quota changes\n", found); 1521 1522 return 0; 1523 1524 fail_brelse: 1525 if (buffer_dirty(bh)) 1526 sync_dirty_buffer(bh); 1527 brelse(bh); 1528 fail: 1529 gfs2_quota_cleanup(sdp); 1530 return error; 1531 } 1532 1533 void gfs2_quota_cleanup(struct gfs2_sbd *sdp) 1534 { 1535 struct gfs2_quota_data *qd; 1536 LIST_HEAD(dispose); 1537 int count; 1538 1539 BUG_ON(!test_bit(SDF_NORECOVERY, &sdp->sd_flags) && 1540 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); 1541 1542 spin_lock(&qd_lock); 1543 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { 1544 spin_lock(&qd->qd_lockref.lock); 1545 if (qd->qd_lockref.count != 0) { 1546 spin_unlock(&qd->qd_lockref.lock); 1547 continue; 1548 } 1549 lockref_mark_dead(&qd->qd_lockref); 1550 spin_unlock(&qd->qd_lockref.lock); 1551 1552 list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); 1553 list_add(&qd->qd_lru, &dispose); 1554 } 1555 spin_unlock(&qd_lock); 1556 1557 gfs2_qd_list_dispose(&dispose); 1558 1559 wait_event_timeout(sdp->sd_kill_wait, 1560 (count = atomic_read(&sdp->sd_quota_count)) == 0, 1561 HZ * 60); 1562 1563 if (count != 0) 1564 fs_err(sdp, "%d left-over quota data objects\n", count); 1565 1566 kvfree(sdp->sd_quota_bitmap); 1567 sdp->sd_quota_bitmap = NULL; 1568 } 1569 1570 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error) 1571 { 1572 if (error == 0 || error == -EROFS) 1573 return; 1574 if (!gfs2_withdrawn(sdp)) { 1575 if (!cmpxchg(&sdp->sd_log_error, 0, error)) 1576 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error); 1577 wake_up(&sdp->sd_logd_waitq); 1578 } 1579 } 1580 1581 void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) { 1582 if (!sdp->sd_statfs_force_sync) { 1583 sdp->sd_statfs_force_sync = 1; 1584 wake_up(&sdp->sd_quota_wait); 1585 } 1586 } 1587 1588 1589 /** 1590 * gfs2_quotad - Write cached quota changes into the quota file 1591 * @data: Pointer to GFS2 superblock 1592 * 1593 */ 1594 1595 int gfs2_quotad(void *data) 1596 { 1597 struct gfs2_sbd *sdp = data; 1598 unsigned long now = jiffies; 1599 unsigned long statfs_deadline = now; 1600 unsigned long quotad_deadline = now; 1601 1602 set_freezable(); 1603 while (!kthread_should_stop()) { 1604 unsigned long t; 1605 1606 if (gfs2_withdrawn(sdp)) 1607 break; 1608 1609 now = jiffies; 1610 if (sdp->sd_statfs_force_sync || 1611 time_after(now, statfs_deadline)) { 1612 unsigned int quantum; 1613 int error; 1614 1615 /* Update the master statfs file */ 1616 error = gfs2_statfs_sync(sdp->sd_vfs, 0); 1617 quotad_error(sdp, "statfs", error); 1618 1619 quantum = gfs2_tune_get(sdp, gt_statfs_quantum); 1620 statfs_deadline = now + quantum * HZ; 1621 } 1622 if (time_after(now, quotad_deadline)) { 1623 unsigned int quantum; 1624 int error; 1625 1626 /* Update the quota file */ 1627 error = gfs2_quota_sync(sdp->sd_vfs, 0); 1628 quotad_error(sdp, "sync", error); 1629 1630 quantum = gfs2_tune_get(sdp, gt_quota_quantum); 1631 quotad_deadline = now + quantum * HZ; 1632 } 1633 1634 t = min(statfs_deadline - now, quotad_deadline - now); 1635 wait_event_freezable_timeout(sdp->sd_quota_wait, 1636 sdp->sd_statfs_force_sync || 1637 gfs2_withdrawn(sdp) || 1638 kthread_should_stop(), 1639 t); 1640 1641 if (sdp->sd_statfs_force_sync) 1642 t = 0; 1643 } 1644 1645 return 0; 1646 } 1647 1648 static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state) 1649 { 1650 struct gfs2_sbd *sdp = sb->s_fs_info; 1651 1652 memset(state, 0, sizeof(*state)); 1653 1654 switch (sdp->sd_args.ar_quota) { 1655 case GFS2_QUOTA_QUIET: 1656 fallthrough; 1657 case GFS2_QUOTA_ON: 1658 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED; 1659 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED; 1660 fallthrough; 1661 case GFS2_QUOTA_ACCOUNT: 1662 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED | 1663 QCI_SYSFILE; 1664 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED | 1665 QCI_SYSFILE; 1666 break; 1667 case GFS2_QUOTA_OFF: 1668 break; 1669 } 1670 if (sdp->sd_quota_inode) { 1671 state->s_state[USRQUOTA].ino = 1672 GFS2_I(sdp->sd_quota_inode)->i_no_addr; 1673 state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks; 1674 } 1675 state->s_state[USRQUOTA].nextents = 1; /* unsupported */ 1676 state->s_state[GRPQUOTA] = state->s_state[USRQUOTA]; 1677 state->s_incoredqs = list_lru_count(&gfs2_qd_lru); 1678 return 0; 1679 } 1680 1681 static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid, 1682 struct qc_dqblk *fdq) 1683 { 1684 struct gfs2_sbd *sdp = sb->s_fs_info; 1685 struct gfs2_quota_lvb *qlvb; 1686 struct gfs2_quota_data *qd; 1687 struct gfs2_holder q_gh; 1688 int error; 1689 1690 memset(fdq, 0, sizeof(*fdq)); 1691 1692 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1693 return -ESRCH; /* Crazy XFS error code */ 1694 1695 if ((qid.type != USRQUOTA) && 1696 (qid.type != GRPQUOTA)) 1697 return -EINVAL; 1698 1699 error = qd_get(sdp, qid, &qd); 1700 if (error) 1701 return error; 1702 error = do_glock(qd, FORCE, &q_gh); 1703 if (error) 1704 goto out; 1705 1706 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1707 fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift; 1708 fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift; 1709 fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift; 1710 1711 gfs2_glock_dq_uninit(&q_gh); 1712 out: 1713 qd_put(qd); 1714 return error; 1715 } 1716 1717 /* GFS2 only supports a subset of the XFS fields */ 1718 #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE) 1719 1720 static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid, 1721 struct qc_dqblk *fdq) 1722 { 1723 struct gfs2_sbd *sdp = sb->s_fs_info; 1724 struct inode *inode = sdp->sd_quota_inode; 1725 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1726 struct gfs2_inode *ip = GFS2_I(inode); 1727 struct gfs2_quota_data *qd; 1728 struct gfs2_holder q_gh, i_gh; 1729 unsigned int data_blocks, ind_blocks; 1730 unsigned int blocks = 0; 1731 int alloc_required; 1732 loff_t offset; 1733 int error; 1734 1735 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1736 return -ESRCH; /* Crazy XFS error code */ 1737 1738 if ((qid.type != USRQUOTA) && 1739 (qid.type != GRPQUOTA)) 1740 return -EINVAL; 1741 1742 if (fdq->d_fieldmask & ~GFS2_FIELDMASK) 1743 return -EINVAL; 1744 1745 error = qd_get(sdp, qid, &qd); 1746 if (error) 1747 return error; 1748 1749 error = gfs2_qa_get(ip); 1750 if (error) 1751 goto out_put; 1752 1753 inode_lock(inode); 1754 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh); 1755 if (error) 1756 goto out_unlockput; 1757 error = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, 0, &i_gh); 1758 if (error) 1759 goto out_q; 1760 1761 /* Check for existing entry, if none then alloc new blocks */ 1762 error = update_qd(sdp, qd); 1763 if (error) 1764 goto out_i; 1765 1766 /* If nothing has changed, this is a no-op */ 1767 if ((fdq->d_fieldmask & QC_SPC_SOFT) && 1768 ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn))) 1769 fdq->d_fieldmask ^= QC_SPC_SOFT; 1770 1771 if ((fdq->d_fieldmask & QC_SPC_HARD) && 1772 ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit))) 1773 fdq->d_fieldmask ^= QC_SPC_HARD; 1774 1775 if ((fdq->d_fieldmask & QC_SPACE) && 1776 ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value))) 1777 fdq->d_fieldmask ^= QC_SPACE; 1778 1779 if (fdq->d_fieldmask == 0) 1780 goto out_i; 1781 1782 offset = qd2offset(qd); 1783 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota)); 1784 if (gfs2_is_stuffed(ip)) 1785 alloc_required = 1; 1786 if (alloc_required) { 1787 struct gfs2_alloc_parms ap = {}; 1788 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), 1789 &data_blocks, &ind_blocks); 1790 blocks = 1 + data_blocks + ind_blocks; 1791 ap.target = blocks; 1792 error = gfs2_inplace_reserve(ip, &ap); 1793 if (error) 1794 goto out_i; 1795 blocks += gfs2_rg_blocks(ip, blocks); 1796 } 1797 1798 /* Some quotas span block boundaries and can update two blocks, 1799 adding an extra block to the transaction to handle such quotas */ 1800 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0); 1801 if (error) 1802 goto out_release; 1803 1804 /* Apply changes */ 1805 error = gfs2_adjust_quota(sdp, offset, 0, qd, fdq); 1806 if (!error) 1807 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags); 1808 1809 gfs2_trans_end(sdp); 1810 out_release: 1811 if (alloc_required) 1812 gfs2_inplace_release(ip); 1813 out_i: 1814 gfs2_glock_dq_uninit(&i_gh); 1815 out_q: 1816 gfs2_glock_dq_uninit(&q_gh); 1817 out_unlockput: 1818 gfs2_qa_put(ip); 1819 inode_unlock(inode); 1820 out_put: 1821 qd_put(qd); 1822 return error; 1823 } 1824 1825 const struct quotactl_ops gfs2_quotactl_ops = { 1826 .quota_sync = gfs2_quota_sync, 1827 .get_state = gfs2_quota_get_state, 1828 .get_dqblk = gfs2_get_dqblk, 1829 .set_dqblk = gfs2_set_dqblk, 1830 }; 1831 1832 void __init gfs2_quota_hash_init(void) 1833 { 1834 unsigned i; 1835 1836 for(i = 0; i < GFS2_QD_HASH_SIZE; i++) 1837 INIT_HLIST_BL_HEAD(&qd_hash_table[i]); 1838 } 1839