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_inode *ip = GFS2_I(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(ip->i_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 566 spin_lock(&inode->i_lock); 567 if (ip->i_qadata == NULL) { 568 struct gfs2_qadata *tmp; 569 570 spin_unlock(&inode->i_lock); 571 tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS); 572 if (!tmp) 573 return -ENOMEM; 574 575 spin_lock(&inode->i_lock); 576 if (ip->i_qadata == NULL) 577 ip->i_qadata = tmp; 578 else 579 kmem_cache_free(gfs2_qadata_cachep, tmp); 580 } 581 ip->i_qadata->qa_ref++; 582 spin_unlock(&inode->i_lock); 583 return 0; 584 } 585 586 void gfs2_qa_put(struct gfs2_inode *ip) 587 { 588 struct inode *inode = &ip->i_inode; 589 590 spin_lock(&inode->i_lock); 591 if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) { 592 kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata); 593 ip->i_qadata = NULL; 594 } 595 spin_unlock(&inode->i_lock); 596 } 597 598 int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) 599 { 600 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 601 struct gfs2_quota_data **qd; 602 int error; 603 604 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 605 return 0; 606 607 error = gfs2_qa_get(ip); 608 if (error) 609 return error; 610 611 qd = ip->i_qadata->qa_qd; 612 613 if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) || 614 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) { 615 error = -EIO; 616 gfs2_qa_put(ip); 617 goto out; 618 } 619 620 error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd); 621 if (error) 622 goto out_unhold; 623 ip->i_qadata->qa_qd_num++; 624 qd++; 625 626 error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd); 627 if (error) 628 goto out_unhold; 629 ip->i_qadata->qa_qd_num++; 630 qd++; 631 632 if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) && 633 !uid_eq(uid, ip->i_inode.i_uid)) { 634 error = qdsb_get(sdp, make_kqid_uid(uid), qd); 635 if (error) 636 goto out_unhold; 637 ip->i_qadata->qa_qd_num++; 638 qd++; 639 } 640 641 if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) && 642 !gid_eq(gid, ip->i_inode.i_gid)) { 643 error = qdsb_get(sdp, make_kqid_gid(gid), qd); 644 if (error) 645 goto out_unhold; 646 ip->i_qadata->qa_qd_num++; 647 qd++; 648 } 649 650 out_unhold: 651 if (error) 652 gfs2_quota_unhold(ip); 653 out: 654 return error; 655 } 656 657 void gfs2_quota_unhold(struct gfs2_inode *ip) 658 { 659 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 660 u32 x; 661 662 if (ip->i_qadata == NULL) 663 return; 664 665 gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)); 666 667 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 668 qdsb_put(ip->i_qadata->qa_qd[x]); 669 ip->i_qadata->qa_qd[x] = NULL; 670 } 671 ip->i_qadata->qa_qd_num = 0; 672 gfs2_qa_put(ip); 673 } 674 675 static int sort_qd(const void *a, const void *b) 676 { 677 const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a; 678 const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b; 679 680 if (qid_lt(qd_a->qd_id, qd_b->qd_id)) 681 return -1; 682 if (qid_lt(qd_b->qd_id, qd_a->qd_id)) 683 return 1; 684 return 0; 685 } 686 687 static void do_qc(struct gfs2_quota_data *qd, s64 change) 688 { 689 struct gfs2_sbd *sdp = qd->qd_sbd; 690 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); 691 struct gfs2_quota_change *qc = qd->qd_bh_qc; 692 bool needs_put = false; 693 s64 x; 694 695 gfs2_trans_add_meta(ip->i_gl, qd->qd_bh); 696 697 /* 698 * The QDF_CHANGE flag indicates that the slot in the quota change file 699 * is used. Here, we use the value of qc->qc_change when the slot is 700 * used, and we assume a value of 0 otherwise. 701 */ 702 703 spin_lock(&qd->qd_lockref.lock); 704 705 x = 0; 706 if (test_bit(QDF_CHANGE, &qd->qd_flags)) 707 x = be64_to_cpu(qc->qc_change); 708 x += change; 709 qd->qd_change += change; 710 711 if (!x && test_bit(QDF_CHANGE, &qd->qd_flags)) { 712 /* The slot in the quota change file becomes unused. */ 713 clear_bit(QDF_CHANGE, &qd->qd_flags); 714 qc->qc_flags = 0; 715 qc->qc_id = 0; 716 needs_put = true; 717 } else if (x && !test_bit(QDF_CHANGE, &qd->qd_flags)) { 718 /* The slot in the quota change file becomes used. */ 719 set_bit(QDF_CHANGE, &qd->qd_flags); 720 __qd_hold(qd); 721 slot_hold(qd); 722 723 qc->qc_flags = 0; 724 if (qd->qd_id.type == USRQUOTA) 725 qc->qc_flags = cpu_to_be32(GFS2_QCF_USER); 726 qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id)); 727 } 728 qc->qc_change = cpu_to_be64(x); 729 730 spin_unlock(&qd->qd_lockref.lock); 731 732 if (needs_put) { 733 slot_put(qd); 734 qd_put(qd); 735 } 736 if (change < 0) /* Reset quiet flag if we freed some blocks */ 737 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags); 738 } 739 740 static int gfs2_write_buf_to_page(struct gfs2_sbd *sdp, unsigned long index, 741 unsigned off, void *buf, unsigned bytes) 742 { 743 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 744 struct inode *inode = &ip->i_inode; 745 struct address_space *mapping = inode->i_mapping; 746 struct folio *folio; 747 struct buffer_head *bh; 748 u64 blk; 749 unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0; 750 unsigned to_write = bytes, pg_off = off; 751 752 blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift); 753 boff = off % bsize; 754 755 folio = filemap_grab_folio(mapping, index); 756 if (IS_ERR(folio)) 757 return PTR_ERR(folio); 758 bh = folio_buffers(folio); 759 if (!bh) 760 bh = create_empty_buffers(folio, bsize, 0); 761 762 for (;;) { 763 /* Find the beginning block within the folio */ 764 if (pg_off >= ((bnum * bsize) + bsize)) { 765 bh = bh->b_this_page; 766 bnum++; 767 blk++; 768 continue; 769 } 770 if (!buffer_mapped(bh)) { 771 gfs2_block_map(inode, blk, bh, 1); 772 if (!buffer_mapped(bh)) 773 goto unlock_out; 774 /* If it's a newly allocated disk block, zero it */ 775 if (buffer_new(bh)) 776 folio_zero_range(folio, bnum * bsize, 777 bh->b_size); 778 } 779 if (folio_test_uptodate(folio)) 780 set_buffer_uptodate(bh); 781 if (bh_read(bh, REQ_META | REQ_PRIO) < 0) 782 goto unlock_out; 783 gfs2_trans_add_data(ip->i_gl, bh); 784 785 /* If we need to write to the next block as well */ 786 if (to_write > (bsize - boff)) { 787 pg_off += (bsize - boff); 788 to_write -= (bsize - boff); 789 boff = pg_off % bsize; 790 continue; 791 } 792 break; 793 } 794 795 /* Write to the folio, now that we have setup the buffer(s) */ 796 memcpy_to_folio(folio, off, buf, bytes); 797 flush_dcache_folio(folio); 798 folio_unlock(folio); 799 folio_put(folio); 800 801 return 0; 802 803 unlock_out: 804 folio_unlock(folio); 805 folio_put(folio); 806 return -EIO; 807 } 808 809 static int gfs2_write_disk_quota(struct gfs2_sbd *sdp, struct gfs2_quota *qp, 810 loff_t loc) 811 { 812 unsigned long pg_beg; 813 unsigned pg_off, nbytes, overflow = 0; 814 int error; 815 void *ptr; 816 817 nbytes = sizeof(struct gfs2_quota); 818 819 pg_beg = loc >> PAGE_SHIFT; 820 pg_off = offset_in_page(loc); 821 822 /* If the quota straddles a page boundary, split the write in two */ 823 if ((pg_off + nbytes) > PAGE_SIZE) 824 overflow = (pg_off + nbytes) - PAGE_SIZE; 825 826 ptr = qp; 827 error = gfs2_write_buf_to_page(sdp, pg_beg, pg_off, ptr, 828 nbytes - overflow); 829 /* If there's an overflow, write the remaining bytes to the next page */ 830 if (!error && overflow) 831 error = gfs2_write_buf_to_page(sdp, pg_beg + 1, 0, 832 ptr + nbytes - overflow, 833 overflow); 834 return error; 835 } 836 837 /** 838 * gfs2_adjust_quota - adjust record of current block usage 839 * @sdp: The superblock 840 * @loc: Offset of the entry in the quota file 841 * @change: The amount of usage change to record 842 * @qd: The quota data 843 * @fdq: The updated limits to record 844 * 845 * This function was mostly borrowed from gfs2_block_truncate_page which was 846 * in turn mostly borrowed from ext3 847 * 848 * Returns: 0 or -ve on error 849 */ 850 851 static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc, 852 s64 change, struct gfs2_quota_data *qd, 853 struct qc_dqblk *fdq) 854 { 855 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 856 struct inode *inode = &ip->i_inode; 857 struct gfs2_quota q; 858 int err; 859 u64 size; 860 861 if (gfs2_is_stuffed(ip)) { 862 err = gfs2_unstuff_dinode(ip); 863 if (err) 864 return err; 865 } 866 867 memset(&q, 0, sizeof(struct gfs2_quota)); 868 err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q)); 869 if (err < 0) 870 return err; 871 872 loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */ 873 be64_add_cpu(&q.qu_value, change); 874 if (((s64)be64_to_cpu(q.qu_value)) < 0) 875 q.qu_value = 0; /* Never go negative on quota usage */ 876 spin_lock(&qd->qd_lockref.lock); 877 qd->qd_qb.qb_value = q.qu_value; 878 if (fdq) { 879 if (fdq->d_fieldmask & QC_SPC_SOFT) { 880 q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift); 881 qd->qd_qb.qb_warn = q.qu_warn; 882 } 883 if (fdq->d_fieldmask & QC_SPC_HARD) { 884 q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift); 885 qd->qd_qb.qb_limit = q.qu_limit; 886 } 887 if (fdq->d_fieldmask & QC_SPACE) { 888 q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift); 889 qd->qd_qb.qb_value = q.qu_value; 890 } 891 } 892 spin_unlock(&qd->qd_lockref.lock); 893 894 err = gfs2_write_disk_quota(sdp, &q, loc); 895 if (!err) { 896 size = loc + sizeof(struct gfs2_quota); 897 if (size > inode->i_size) 898 i_size_write(inode, size); 899 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 900 mark_inode_dirty(inode); 901 set_bit(QDF_REFRESH, &qd->qd_flags); 902 } 903 904 return err; 905 } 906 907 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda, 908 u64 sync_gen) 909 { 910 struct gfs2_sbd *sdp = (*qda)->qd_sbd; 911 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 912 struct gfs2_alloc_parms ap = {}; 913 unsigned int data_blocks, ind_blocks; 914 struct gfs2_holder *ghs, i_gh; 915 unsigned int qx, x; 916 struct gfs2_quota_data *qd; 917 unsigned reserved; 918 loff_t offset; 919 unsigned int nalloc = 0, blocks; 920 int error; 921 922 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), 923 &data_blocks, &ind_blocks); 924 925 ghs = kmalloc_objs(struct gfs2_holder, num_qd, GFP_NOFS); 926 if (!ghs) 927 return -ENOMEM; 928 929 sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL); 930 inode_lock(&ip->i_inode); 931 for (qx = 0; qx < num_qd; qx++) { 932 error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE, 933 GL_NOCACHE, &ghs[qx]); 934 if (error) 935 goto out_dq; 936 } 937 938 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); 939 if (error) 940 goto out_dq; 941 942 for (x = 0; x < num_qd; x++) { 943 offset = qd2offset(qda[x]); 944 if (gfs2_write_alloc_required(ip, offset, 945 sizeof(struct gfs2_quota))) 946 nalloc++; 947 } 948 949 /* 950 * 1 blk for unstuffing inode if stuffed. We add this extra 951 * block to the reservation unconditionally. If the inode 952 * doesn't need unstuffing, the block will be released to the 953 * rgrp since it won't be allocated during the transaction 954 */ 955 /* +3 in the end for unstuffing block, inode size update block 956 * and another block in case quota straddles page boundary and 957 * two blocks need to be updated instead of 1 */ 958 blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3; 959 960 reserved = 1 + (nalloc * (data_blocks + ind_blocks)); 961 ap.target = reserved; 962 error = gfs2_inplace_reserve(ip, &ap); 963 if (error) 964 goto out_alloc; 965 966 if (nalloc) 967 blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS; 968 969 error = gfs2_trans_begin(sdp, blocks, 0); 970 if (error) 971 goto out_ipres; 972 973 for (x = 0; x < num_qd; x++) { 974 qd = qda[x]; 975 offset = qd2offset(qd); 976 error = gfs2_adjust_quota(sdp, offset, qd->qd_change_sync, qd, 977 NULL); 978 if (error) 979 goto out_end_trans; 980 981 do_qc(qd, -qd->qd_change_sync); 982 set_bit(QDF_REFRESH, &qd->qd_flags); 983 } 984 985 out_end_trans: 986 gfs2_trans_end(sdp); 987 out_ipres: 988 gfs2_inplace_release(ip); 989 out_alloc: 990 gfs2_glock_dq_uninit(&i_gh); 991 out_dq: 992 while (qx--) 993 gfs2_glock_dq_uninit(&ghs[qx]); 994 inode_unlock(&ip->i_inode); 995 kfree(ghs); 996 gfs2_log_flush(glock_sbd(ip->i_gl), ip->i_gl, 997 GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC); 998 if (!error) { 999 for (x = 0; x < num_qd; x++) { 1000 qd = qda[x]; 1001 spin_lock(&qd->qd_lockref.lock); 1002 if (qd->qd_sync_gen < sync_gen) 1003 qd->qd_sync_gen = sync_gen; 1004 spin_unlock(&qd->qd_lockref.lock); 1005 } 1006 } 1007 return error; 1008 } 1009 1010 static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd) 1011 { 1012 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 1013 struct gfs2_quota q; 1014 struct gfs2_quota_lvb *qlvb; 1015 loff_t pos; 1016 int error; 1017 1018 memset(&q, 0, sizeof(struct gfs2_quota)); 1019 pos = qd2offset(qd); 1020 error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q)); 1021 if (error < 0) 1022 return error; 1023 1024 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1025 qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC); 1026 qlvb->__pad = 0; 1027 qlvb->qb_limit = q.qu_limit; 1028 qlvb->qb_warn = q.qu_warn; 1029 qlvb->qb_value = q.qu_value; 1030 spin_lock(&qd->qd_lockref.lock); 1031 qd->qd_qb = *qlvb; 1032 spin_unlock(&qd->qd_lockref.lock); 1033 1034 return 0; 1035 } 1036 1037 static int do_glock(struct gfs2_quota_data *qd, int force_refresh, 1038 struct gfs2_holder *q_gh) 1039 { 1040 struct gfs2_sbd *sdp = qd->qd_sbd; 1041 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 1042 struct gfs2_holder i_gh; 1043 int error; 1044 1045 gfs2_assert_warn(sdp, sdp == glock_sbd(qd->qd_gl)); 1046 restart: 1047 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh); 1048 if (error) 1049 return error; 1050 1051 if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags)) 1052 force_refresh = FORCE; 1053 1054 spin_lock(&qd->qd_lockref.lock); 1055 qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1056 spin_unlock(&qd->qd_lockref.lock); 1057 1058 if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) { 1059 gfs2_glock_dq_uninit(q_gh); 1060 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 1061 GL_NOCACHE, q_gh); 1062 if (error) 1063 return error; 1064 1065 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); 1066 if (error) 1067 goto fail; 1068 1069 error = update_qd(sdp, qd); 1070 if (error) 1071 goto fail_gunlock; 1072 1073 gfs2_glock_dq_uninit(&i_gh); 1074 gfs2_glock_dq_uninit(q_gh); 1075 force_refresh = 0; 1076 goto restart; 1077 } 1078 1079 return 0; 1080 1081 fail_gunlock: 1082 gfs2_glock_dq_uninit(&i_gh); 1083 fail: 1084 gfs2_glock_dq_uninit(q_gh); 1085 return error; 1086 } 1087 1088 int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid) 1089 { 1090 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1091 struct gfs2_quota_data *qd; 1092 u32 x; 1093 int error; 1094 1095 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1096 return 0; 1097 1098 error = gfs2_quota_hold(ip, uid, gid); 1099 if (error) 1100 return error; 1101 1102 sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num, 1103 sizeof(struct gfs2_quota_data *), sort_qd, NULL); 1104 1105 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1106 qd = ip->i_qadata->qa_qd[x]; 1107 error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]); 1108 if (error) 1109 break; 1110 } 1111 1112 if (!error) 1113 set_bit(GIF_QD_LOCKED, &ip->i_flags); 1114 else { 1115 while (x--) 1116 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]); 1117 gfs2_quota_unhold(ip); 1118 } 1119 1120 return error; 1121 } 1122 1123 static bool need_sync(struct gfs2_quota_data *qd) 1124 { 1125 struct gfs2_sbd *sdp = qd->qd_sbd; 1126 struct gfs2_tune *gt = &sdp->sd_tune; 1127 s64 value, change, limit; 1128 unsigned int num, den; 1129 int ret = false; 1130 1131 spin_lock(&qd->qd_lockref.lock); 1132 if (!qd->qd_qb.qb_limit) 1133 goto out; 1134 1135 change = qd->qd_change; 1136 if (change <= 0) 1137 goto out; 1138 value = (s64)be64_to_cpu(qd->qd_qb.qb_value); 1139 limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit); 1140 if (value >= limit) 1141 goto out; 1142 1143 spin_lock(>->gt_spin); 1144 num = gt->gt_quota_scale_num; 1145 den = gt->gt_quota_scale_den; 1146 spin_unlock(>->gt_spin); 1147 1148 change *= gfs2_jindex_size(sdp) * num; 1149 change = div_s64(change, den); 1150 if (value + change < limit) 1151 goto out; 1152 1153 ret = true; 1154 out: 1155 spin_unlock(&qd->qd_lockref.lock); 1156 return ret; 1157 } 1158 1159 void gfs2_quota_unlock(struct gfs2_inode *ip) 1160 { 1161 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1162 struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS]; 1163 unsigned int count = 0; 1164 u32 x; 1165 1166 if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags)) 1167 return; 1168 1169 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1170 struct gfs2_quota_data *qd; 1171 bool sync; 1172 int error; 1173 1174 qd = ip->i_qadata->qa_qd[x]; 1175 sync = need_sync(qd); 1176 1177 gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]); 1178 if (!sync) 1179 continue; 1180 1181 spin_lock(&qd_lock); 1182 sync = qd_grab_sync(sdp, qd, U64_MAX); 1183 spin_unlock(&qd_lock); 1184 1185 if (!sync) 1186 continue; 1187 1188 gfs2_assert_warn(sdp, qd->qd_change_sync); 1189 error = bh_get(qd); 1190 if (error) { 1191 qd_ungrab_sync(qd); 1192 continue; 1193 } 1194 1195 qda[count++] = qd; 1196 } 1197 1198 if (count) { 1199 u64 sync_gen = READ_ONCE(sdp->sd_quota_sync_gen); 1200 1201 do_sync(count, qda, sync_gen); 1202 for (x = 0; x < count; x++) 1203 qd_unlock(qda[x]); 1204 } 1205 1206 gfs2_quota_unhold(ip); 1207 } 1208 1209 #define MAX_LINE 256 1210 1211 static void print_message(struct gfs2_quota_data *qd, char *type) 1212 { 1213 struct gfs2_sbd *sdp = qd->qd_sbd; 1214 1215 if (sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET) { 1216 fs_info(sdp, "quota %s for %s %u\n", 1217 type, 1218 (qd->qd_id.type == USRQUOTA) ? "user" : "group", 1219 from_kqid(&init_user_ns, qd->qd_id)); 1220 } 1221 } 1222 1223 /** 1224 * gfs2_quota_check - check if allocating new blocks will exceed quota 1225 * @ip: The inode for which this check is being performed 1226 * @uid: The uid to check against 1227 * @gid: The gid to check against 1228 * @ap: The allocation parameters. ap->target contains the requested 1229 * blocks. ap->min_target, if set, contains the minimum blks 1230 * requested. 1231 * 1232 * Returns: 0 on success. 1233 * min_req = ap->min_target ? ap->min_target : ap->target; 1234 * quota must allow at least min_req blks for success and 1235 * ap->allowed is set to the number of blocks allowed 1236 * 1237 * -EDQUOT otherwise, quota violation. ap->allowed is set to number 1238 * of blocks available. 1239 */ 1240 int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid, 1241 struct gfs2_alloc_parms *ap) 1242 { 1243 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1244 struct gfs2_quota_data *qd; 1245 s64 value, warn, limit; 1246 u32 x; 1247 int error = 0; 1248 1249 ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */ 1250 if (!test_bit(GIF_QD_LOCKED, &ip->i_flags)) 1251 return 0; 1252 1253 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1254 qd = ip->i_qadata->qa_qd[x]; 1255 1256 if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) || 1257 qid_eq(qd->qd_id, make_kqid_gid(gid)))) 1258 continue; 1259 1260 spin_lock(&qd->qd_lockref.lock); 1261 warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn); 1262 limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit); 1263 value = (s64)be64_to_cpu(qd->qd_qb.qb_value); 1264 value += qd->qd_change; 1265 spin_unlock(&qd->qd_lockref.lock); 1266 1267 if (limit > 0 && (limit - value) < ap->allowed) 1268 ap->allowed = limit - value; 1269 /* If we can't meet the target */ 1270 if (limit && limit < (value + (s64)ap->target)) { 1271 /* If no min_target specified or we don't meet 1272 * min_target, return -EDQUOT */ 1273 if (!ap->min_target || ap->min_target > ap->allowed) { 1274 if (!test_and_set_bit(QDF_QMSG_QUIET, 1275 &qd->qd_flags)) { 1276 print_message(qd, "exceeded"); 1277 quota_send_warning(qd->qd_id, 1278 sdp->sd_vfs->s_dev, 1279 QUOTA_NL_BHARDWARN); 1280 } 1281 error = -EDQUOT; 1282 break; 1283 } 1284 } else if (warn && warn < value && 1285 time_after_eq(jiffies, qd->qd_last_warn + 1286 gfs2_tune_get(sdp, gt_quota_warn_period) 1287 * HZ)) { 1288 quota_send_warning(qd->qd_id, 1289 sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN); 1290 print_message(qd, "warning"); 1291 error = 0; 1292 qd->qd_last_warn = jiffies; 1293 } 1294 } 1295 return error; 1296 } 1297 1298 void gfs2_quota_change(struct gfs2_inode *ip, s64 change, 1299 kuid_t uid, kgid_t gid) 1300 { 1301 struct gfs2_quota_data *qd; 1302 u32 x; 1303 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1304 1305 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF || 1306 gfs2_assert_warn(sdp, change)) 1307 return; 1308 if (ip->i_diskflags & GFS2_DIF_SYSTEM) 1309 return; 1310 1311 if (gfs2_assert_withdraw(sdp, ip->i_qadata && 1312 ip->i_qadata->qa_ref > 0)) 1313 return; 1314 for (x = 0; x < ip->i_qadata->qa_qd_num; x++) { 1315 qd = ip->i_qadata->qa_qd[x]; 1316 1317 if (qid_eq(qd->qd_id, make_kqid_uid(uid)) || 1318 qid_eq(qd->qd_id, make_kqid_gid(gid))) { 1319 do_qc(qd, change); 1320 } 1321 } 1322 } 1323 1324 int gfs2_quota_sync(struct super_block *sb, int type) 1325 { 1326 struct gfs2_sbd *sdp = sb->s_fs_info; 1327 struct gfs2_quota_data **qda; 1328 unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder); 1329 u64 sync_gen; 1330 int error = 0; 1331 1332 if (sb_rdonly(sdp->sd_vfs)) 1333 return 0; 1334 1335 qda = kzalloc_objs(struct gfs2_quota_data *, max_qd); 1336 if (!qda) 1337 return -ENOMEM; 1338 1339 mutex_lock(&sdp->sd_quota_sync_mutex); 1340 sync_gen = sdp->sd_quota_sync_gen + 1; 1341 1342 do { 1343 struct gfs2_quota_data *iter; 1344 unsigned int num_qd = 0; 1345 unsigned int x; 1346 1347 spin_lock(&qd_lock); 1348 list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) { 1349 if (qd_grab_sync(sdp, iter, sync_gen)) { 1350 qda[num_qd++] = iter; 1351 if (num_qd == max_qd) 1352 break; 1353 } 1354 } 1355 spin_unlock(&qd_lock); 1356 1357 if (!num_qd) 1358 break; 1359 1360 for (x = 0; x < num_qd; x++) { 1361 error = bh_get(qda[x]); 1362 if (!error) 1363 continue; 1364 1365 while (x < num_qd) 1366 qd_ungrab_sync(qda[--num_qd]); 1367 break; 1368 } 1369 1370 if (!error) { 1371 WRITE_ONCE(sdp->sd_quota_sync_gen, sync_gen); 1372 error = do_sync(num_qd, qda, sync_gen); 1373 } 1374 1375 for (x = 0; x < num_qd; x++) 1376 qd_unlock(qda[x]); 1377 } while (!error); 1378 1379 mutex_unlock(&sdp->sd_quota_sync_mutex); 1380 kfree(qda); 1381 1382 return error; 1383 } 1384 1385 int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid) 1386 { 1387 struct gfs2_quota_data *qd; 1388 struct gfs2_holder q_gh; 1389 int error; 1390 1391 error = qd_get(sdp, qid, &qd); 1392 if (error) 1393 return error; 1394 1395 error = do_glock(qd, FORCE, &q_gh); 1396 if (!error) 1397 gfs2_glock_dq_uninit(&q_gh); 1398 1399 qd_put(qd); 1400 return error; 1401 } 1402 1403 int gfs2_quota_init(struct gfs2_sbd *sdp) 1404 { 1405 struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode); 1406 u64 size = i_size_read(sdp->sd_qc_inode); 1407 unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift; 1408 unsigned int x, slot = 0; 1409 unsigned int found = 0; 1410 unsigned int hash; 1411 unsigned int bm_size; 1412 struct buffer_head *bh; 1413 u64 dblock; 1414 u32 extlen = 0; 1415 int error; 1416 1417 if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20)) 1418 return -EIO; 1419 1420 sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block; 1421 bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long)); 1422 bm_size *= sizeof(unsigned long); 1423 error = -ENOMEM; 1424 sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN); 1425 if (sdp->sd_quota_bitmap == NULL) 1426 sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS | 1427 __GFP_ZERO); 1428 if (!sdp->sd_quota_bitmap) 1429 return error; 1430 1431 for (x = 0; x < blocks; x++) { 1432 struct gfs2_quota_change *qc; 1433 unsigned int y; 1434 1435 if (!extlen) { 1436 extlen = 32; 1437 error = gfs2_get_extent(&ip->i_inode, x, &dblock, &extlen); 1438 if (error) 1439 goto fail; 1440 } 1441 error = -EIO; 1442 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); 1443 if (!bh) 1444 goto fail; 1445 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) 1446 goto fail_brelse; 1447 1448 qc = (struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header)); 1449 for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots; 1450 y++, slot++, qc++) { 1451 struct gfs2_quota_data *old_qd, *qd; 1452 s64 qc_change = be64_to_cpu(qc->qc_change); 1453 u32 qc_flags = be32_to_cpu(qc->qc_flags); 1454 enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ? 1455 USRQUOTA : GRPQUOTA; 1456 struct kqid qc_id = make_kqid(&init_user_ns, qtype, 1457 be32_to_cpu(qc->qc_id)); 1458 if (!qc_change) 1459 continue; 1460 1461 hash = gfs2_qd_hash(sdp, qc_id); 1462 qd = qd_alloc(hash, sdp, qc_id); 1463 if (qd == NULL) 1464 goto fail_brelse; 1465 1466 qd->qd_lockref.count = 0; 1467 set_bit(QDF_CHANGE, &qd->qd_flags); 1468 qd->qd_change = qc_change; 1469 qd->qd_slot = slot; 1470 qd->qd_slot_ref = 1; 1471 1472 spin_lock(&qd_lock); 1473 spin_lock_bucket(hash); 1474 old_qd = gfs2_qd_search_bucket_noref(hash, sdp, qc_id); 1475 if (old_qd) { 1476 fs_err(sdp, "Corruption found in quota_change%u" 1477 "file: duplicate identifier in " 1478 "slot %u\n", 1479 sdp->sd_jdesc->jd_jid, slot); 1480 1481 spin_unlock_bucket(hash); 1482 spin_unlock(&qd_lock); 1483 1484 gfs2_glock_put(qd->qd_gl); 1485 kmem_cache_free(gfs2_quotad_cachep, qd); 1486 1487 /* zero out the duplicate slot */ 1488 lock_buffer(bh); 1489 memset(qc, 0, sizeof(*qc)); 1490 mark_buffer_dirty(bh); 1491 unlock_buffer(bh); 1492 1493 continue; 1494 } 1495 BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap)); 1496 list_add(&qd->qd_list, &sdp->sd_quota_list); 1497 atomic_inc(&sdp->sd_quota_count); 1498 hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]); 1499 spin_unlock_bucket(hash); 1500 spin_unlock(&qd_lock); 1501 1502 found++; 1503 } 1504 1505 if (buffer_dirty(bh)) 1506 sync_dirty_buffer(bh); 1507 brelse(bh); 1508 dblock++; 1509 extlen--; 1510 } 1511 1512 if (found) 1513 fs_info(sdp, "found %u quota changes\n", found); 1514 1515 return 0; 1516 1517 fail_brelse: 1518 if (buffer_dirty(bh)) 1519 sync_dirty_buffer(bh); 1520 brelse(bh); 1521 fail: 1522 gfs2_quota_cleanup(sdp); 1523 return error; 1524 } 1525 1526 void gfs2_quota_cleanup(struct gfs2_sbd *sdp) 1527 { 1528 struct gfs2_quota_data *qd; 1529 LIST_HEAD(dispose); 1530 int count; 1531 1532 BUG_ON(!test_bit(SDF_NORECOVERY, &sdp->sd_flags) && 1533 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); 1534 1535 spin_lock(&qd_lock); 1536 list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) { 1537 spin_lock(&qd->qd_lockref.lock); 1538 if (qd->qd_lockref.count != 0) { 1539 spin_unlock(&qd->qd_lockref.lock); 1540 continue; 1541 } 1542 lockref_mark_dead(&qd->qd_lockref); 1543 spin_unlock(&qd->qd_lockref.lock); 1544 1545 list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru); 1546 list_add(&qd->qd_lru, &dispose); 1547 } 1548 spin_unlock(&qd_lock); 1549 1550 gfs2_qd_list_dispose(&dispose); 1551 1552 wait_event_timeout(sdp->sd_kill_wait, 1553 (count = atomic_read(&sdp->sd_quota_count)) == 0, 1554 HZ * 60); 1555 1556 if (count != 0) 1557 fs_err(sdp, "%d left-over quota data objects\n", count); 1558 1559 kvfree(sdp->sd_quota_bitmap); 1560 sdp->sd_quota_bitmap = NULL; 1561 } 1562 1563 static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error) 1564 { 1565 if (error == 0 || error == -EROFS) 1566 return; 1567 if (!gfs2_withdrawn(sdp)) { 1568 if (!cmpxchg(&sdp->sd_log_error, 0, error)) 1569 fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error); 1570 wake_up(&sdp->sd_logd_waitq); 1571 } 1572 } 1573 1574 void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) { 1575 if (!sdp->sd_statfs_force_sync) { 1576 sdp->sd_statfs_force_sync = 1; 1577 wake_up(&sdp->sd_quota_wait); 1578 } 1579 } 1580 1581 1582 /** 1583 * gfs2_quotad - Write cached quota changes into the quota file 1584 * @data: Pointer to GFS2 superblock 1585 * 1586 */ 1587 1588 int gfs2_quotad(void *data) 1589 { 1590 struct gfs2_sbd *sdp = data; 1591 unsigned long now = jiffies; 1592 unsigned long statfs_deadline = now; 1593 unsigned long quotad_deadline = now; 1594 1595 set_freezable(); 1596 while (!kthread_should_stop()) { 1597 unsigned long t; 1598 1599 if (gfs2_withdrawn(sdp)) 1600 break; 1601 1602 now = jiffies; 1603 if (sdp->sd_statfs_force_sync || 1604 time_after(now, statfs_deadline)) { 1605 unsigned int quantum; 1606 int error; 1607 1608 /* Update the master statfs file */ 1609 error = gfs2_statfs_sync(sdp->sd_vfs, 0); 1610 quotad_error(sdp, "statfs", error); 1611 1612 quantum = gfs2_tune_get(sdp, gt_statfs_quantum); 1613 statfs_deadline = now + quantum * HZ; 1614 } 1615 if (time_after(now, quotad_deadline)) { 1616 unsigned int quantum; 1617 int error; 1618 1619 /* Update the quota file */ 1620 error = gfs2_quota_sync(sdp->sd_vfs, 0); 1621 quotad_error(sdp, "sync", error); 1622 1623 quantum = gfs2_tune_get(sdp, gt_quota_quantum); 1624 quotad_deadline = now + quantum * HZ; 1625 } 1626 1627 t = min(statfs_deadline - now, quotad_deadline - now); 1628 wait_event_freezable_timeout(sdp->sd_quota_wait, 1629 sdp->sd_statfs_force_sync || 1630 gfs2_withdrawn(sdp) || 1631 kthread_should_stop(), 1632 t); 1633 1634 if (sdp->sd_statfs_force_sync) 1635 t = 0; 1636 } 1637 1638 return 0; 1639 } 1640 1641 static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state) 1642 { 1643 struct gfs2_sbd *sdp = sb->s_fs_info; 1644 1645 memset(state, 0, sizeof(*state)); 1646 1647 switch (sdp->sd_args.ar_quota) { 1648 case GFS2_QUOTA_QUIET: 1649 fallthrough; 1650 case GFS2_QUOTA_ON: 1651 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED; 1652 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED; 1653 fallthrough; 1654 case GFS2_QUOTA_ACCOUNT: 1655 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED | 1656 QCI_SYSFILE; 1657 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED | 1658 QCI_SYSFILE; 1659 break; 1660 case GFS2_QUOTA_OFF: 1661 break; 1662 } 1663 if (sdp->sd_quota_inode) { 1664 state->s_state[USRQUOTA].ino = 1665 GFS2_I(sdp->sd_quota_inode)->i_no_addr; 1666 state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks; 1667 } 1668 state->s_state[USRQUOTA].nextents = 1; /* unsupported */ 1669 state->s_state[GRPQUOTA] = state->s_state[USRQUOTA]; 1670 state->s_incoredqs = list_lru_count(&gfs2_qd_lru); 1671 return 0; 1672 } 1673 1674 static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid, 1675 struct qc_dqblk *fdq) 1676 { 1677 struct gfs2_sbd *sdp = sb->s_fs_info; 1678 struct gfs2_quota_lvb *qlvb; 1679 struct gfs2_quota_data *qd; 1680 struct gfs2_holder q_gh; 1681 int error; 1682 1683 memset(fdq, 0, sizeof(*fdq)); 1684 1685 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1686 return -ESRCH; /* Crazy XFS error code */ 1687 1688 if ((qid.type != USRQUOTA) && 1689 (qid.type != GRPQUOTA)) 1690 return -EINVAL; 1691 1692 error = qd_get(sdp, qid, &qd); 1693 if (error) 1694 return error; 1695 error = do_glock(qd, FORCE, &q_gh); 1696 if (error) 1697 goto out; 1698 1699 qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr; 1700 fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift; 1701 fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift; 1702 fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift; 1703 1704 gfs2_glock_dq_uninit(&q_gh); 1705 out: 1706 qd_put(qd); 1707 return error; 1708 } 1709 1710 /* GFS2 only supports a subset of the XFS fields */ 1711 #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE) 1712 1713 static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid, 1714 struct qc_dqblk *fdq) 1715 { 1716 struct gfs2_sbd *sdp = sb->s_fs_info; 1717 struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode); 1718 struct gfs2_quota_data *qd; 1719 struct gfs2_holder q_gh, i_gh; 1720 unsigned int data_blocks, ind_blocks; 1721 unsigned int blocks = 0; 1722 int alloc_required; 1723 loff_t offset; 1724 int error; 1725 1726 if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF) 1727 return -ESRCH; /* Crazy XFS error code */ 1728 1729 if ((qid.type != USRQUOTA) && 1730 (qid.type != GRPQUOTA)) 1731 return -EINVAL; 1732 1733 if (fdq->d_fieldmask & ~GFS2_FIELDMASK) 1734 return -EINVAL; 1735 1736 error = qd_get(sdp, qid, &qd); 1737 if (error) 1738 return error; 1739 1740 error = gfs2_qa_get(ip); 1741 if (error) 1742 goto out_put; 1743 1744 inode_lock(&ip->i_inode); 1745 error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh); 1746 if (error) 1747 goto out_unlockput; 1748 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); 1749 if (error) 1750 goto out_q; 1751 1752 /* Check for existing entry, if none then alloc new blocks */ 1753 error = update_qd(sdp, qd); 1754 if (error) 1755 goto out_i; 1756 1757 /* If nothing has changed, this is a no-op */ 1758 if ((fdq->d_fieldmask & QC_SPC_SOFT) && 1759 ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn))) 1760 fdq->d_fieldmask ^= QC_SPC_SOFT; 1761 1762 if ((fdq->d_fieldmask & QC_SPC_HARD) && 1763 ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit))) 1764 fdq->d_fieldmask ^= QC_SPC_HARD; 1765 1766 if ((fdq->d_fieldmask & QC_SPACE) && 1767 ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value))) 1768 fdq->d_fieldmask ^= QC_SPACE; 1769 1770 if (fdq->d_fieldmask == 0) 1771 goto out_i; 1772 1773 offset = qd2offset(qd); 1774 alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota)); 1775 if (gfs2_is_stuffed(ip)) 1776 alloc_required = 1; 1777 if (alloc_required) { 1778 struct gfs2_alloc_parms ap = {}; 1779 gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota), 1780 &data_blocks, &ind_blocks); 1781 blocks = 1 + data_blocks + ind_blocks; 1782 ap.target = blocks; 1783 error = gfs2_inplace_reserve(ip, &ap); 1784 if (error) 1785 goto out_i; 1786 blocks += gfs2_rg_blocks(ip, blocks); 1787 } 1788 1789 /* Some quotas span block boundaries and can update two blocks, 1790 adding an extra block to the transaction to handle such quotas */ 1791 error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0); 1792 if (error) 1793 goto out_release; 1794 1795 /* Apply changes */ 1796 error = gfs2_adjust_quota(sdp, offset, 0, qd, fdq); 1797 if (!error) 1798 clear_bit(QDF_QMSG_QUIET, &qd->qd_flags); 1799 1800 gfs2_trans_end(sdp); 1801 out_release: 1802 if (alloc_required) 1803 gfs2_inplace_release(ip); 1804 out_i: 1805 gfs2_glock_dq_uninit(&i_gh); 1806 out_q: 1807 gfs2_glock_dq_uninit(&q_gh); 1808 out_unlockput: 1809 gfs2_qa_put(ip); 1810 inode_unlock(&ip->i_inode); 1811 out_put: 1812 qd_put(qd); 1813 return error; 1814 } 1815 1816 const struct quotactl_ops gfs2_quotactl_ops = { 1817 .quota_sync = gfs2_quota_sync, 1818 .get_state = gfs2_quota_get_state, 1819 .get_dqblk = gfs2_get_dqblk, 1820 .set_dqblk = gfs2_set_dqblk, 1821 }; 1822 1823 void __init gfs2_quota_hash_init(void) 1824 { 1825 unsigned i; 1826 1827 for(i = 0; i < GFS2_QD_HASH_SIZE; i++) 1828 INIT_HLIST_BL_HEAD(&qd_hash_table[i]); 1829 } 1830