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