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