1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Implementation of the diskquota system for the LINUX operating system. QUOTA 4 * is implemented using the BSD system call interface as the means of 5 * communication with the user level. This file contains the generic routines 6 * called by the different filesystems on allocation of an inode or block. 7 * These routines take care of the administration needed to have a consistent 8 * diskquota tracking system. The ideas of both user and group quotas are based 9 * on the Melbourne quota system as used on BSD derived systems. The internal 10 * implementation is based on one of the several variants of the LINUX 11 * inode-subsystem with added complexity of the diskquota system. 12 * 13 * Author: Marco van Wieringen <mvw@planets.elm.net> 14 * 15 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96 16 * 17 * Revised list management to avoid races 18 * -- Bill Hawes, <whawes@star.net>, 9/98 19 * 20 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...(). 21 * As the consequence the locking was moved from dquot_decr_...(), 22 * dquot_incr_...() to calling functions. 23 * invalidate_dquots() now writes modified dquots. 24 * Serialized quota_off() and quota_on() for mount point. 25 * Fixed a few bugs in grow_dquots(). 26 * Fixed deadlock in write_dquot() - we no longer account quotas on 27 * quota files 28 * remove_dquot_ref() moved to inode.c - it now traverses through inodes 29 * add_dquot_ref() restarts after blocking 30 * Added check for bogus uid and fixed check for group in quotactl. 31 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99 32 * 33 * Used struct list_head instead of own list struct 34 * Invalidation of referenced dquots is no longer possible 35 * Improved free_dquots list management 36 * Quota and i_blocks are now updated in one place to avoid races 37 * Warnings are now delayed so we won't block in critical section 38 * Write updated not to require dquot lock 39 * Jan Kara, <jack@suse.cz>, 9/2000 40 * 41 * Added dynamic quota structure allocation 42 * Jan Kara <jack@suse.cz> 12/2000 43 * 44 * Rewritten quota interface. Implemented new quota format and 45 * formats registering. 46 * Jan Kara, <jack@suse.cz>, 2001,2002 47 * 48 * New SMP locking. 49 * Jan Kara, <jack@suse.cz>, 10/2002 50 * 51 * Added journalled quota support, fix lock inversion problems 52 * Jan Kara, <jack@suse.cz>, 2003,2004 53 * 54 * (C) Copyright 1994 - 1997 Marco van Wieringen 55 */ 56 57 #include <linux/errno.h> 58 #include <linux/kernel.h> 59 #include <linux/fs.h> 60 #include <linux/mount.h> 61 #include <linux/mm.h> 62 #include <linux/time.h> 63 #include <linux/types.h> 64 #include <linux/string.h> 65 #include <linux/fcntl.h> 66 #include <linux/stat.h> 67 #include <linux/tty.h> 68 #include <linux/file.h> 69 #include <linux/slab.h> 70 #include <linux/sysctl.h> 71 #include <linux/init.h> 72 #include <linux/module.h> 73 #include <linux/proc_fs.h> 74 #include <linux/security.h> 75 #include <linux/sched.h> 76 #include <linux/cred.h> 77 #include <linux/kmod.h> 78 #include <linux/namei.h> 79 #include <linux/capability.h> 80 #include <linux/quotaops.h> 81 #include <linux/blkdev.h> 82 #include <linux/sched/mm.h> 83 84 #include <linux/uaccess.h> 85 86 /* 87 * There are five quota SMP locks: 88 * * dq_list_lock protects all lists with quotas and quota formats. 89 * * dquot->dq_dqb_lock protects data from dq_dqb 90 * * inode->i_lock protects inode->i_blocks, i_bytes and also guards 91 * consistency of dquot->dq_dqb with inode->i_blocks, i_bytes so that 92 * dquot_transfer() can stabilize amount it transfers 93 * * dq_data_lock protects mem_dqinfo structures and modifications of dquot 94 * pointers in the inode 95 * * dq_state_lock protects modifications of quota state (on quotaon and 96 * quotaoff) and readers who care about latest values take it as well. 97 * 98 * The spinlock ordering is hence: 99 * dq_data_lock > dq_list_lock > i_lock > dquot->dq_dqb_lock, 100 * dq_list_lock > dq_state_lock 101 * 102 * Note that some things (eg. sb pointer, type, id) doesn't change during 103 * the life of the dquot structure and so needn't to be protected by a lock 104 * 105 * Operation accessing dquots via inode pointers are protected by dquot_srcu. 106 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and 107 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from 108 * inode and before dropping dquot references to avoid use of dquots after 109 * they are freed. dq_data_lock is used to serialize the pointer setting and 110 * clearing operations. 111 * Special care needs to be taken about S_NOQUOTA inode flag (marking that 112 * inode is a quota file). Functions adding pointers from inode to dquots have 113 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they 114 * have to do all pointer modifications before dropping dq_data_lock. This makes 115 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and 116 * then drops all pointers to dquots from an inode. 117 * 118 * Each dquot has its dq_lock mutex. Dquot is locked when it is being read to 119 * memory (or space for it is being allocated) on the first dqget(), when it is 120 * being written out, and when it is being released on the last dqput(). The 121 * allocation and release operations are serialized by the dq_lock and by 122 * checking the use count in dquot_release(). 123 * 124 * Lock ordering (including related VFS locks) is the following: 125 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem 126 */ 127 128 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock); 129 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock); 130 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock); 131 EXPORT_SYMBOL(dq_data_lock); 132 DEFINE_STATIC_SRCU(dquot_srcu); 133 134 static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq); 135 136 void __quota_error(struct super_block *sb, const char *func, 137 const char *fmt, ...) 138 { 139 if (printk_ratelimit()) { 140 va_list args; 141 struct va_format vaf; 142 143 va_start(args, fmt); 144 145 vaf.fmt = fmt; 146 vaf.va = &args; 147 148 printk(KERN_ERR "Quota error (device %s): %s: %pV\n", 149 sb->s_id, func, &vaf); 150 151 va_end(args); 152 } 153 } 154 EXPORT_SYMBOL(__quota_error); 155 156 #ifdef CONFIG_QUOTA_DEBUG 157 static char *quotatypes[] = INITQFNAMES; 158 #endif 159 static struct quota_format_type *quota_formats; /* List of registered formats */ 160 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES; 161 162 /* SLAB cache for dquot structures */ 163 static struct kmem_cache *dquot_cachep; 164 165 /* workqueue for work quota_release_work*/ 166 static struct workqueue_struct *quota_unbound_wq; 167 168 void register_quota_format(struct quota_format_type *fmt) 169 { 170 spin_lock(&dq_list_lock); 171 fmt->qf_next = quota_formats; 172 quota_formats = fmt; 173 spin_unlock(&dq_list_lock); 174 } 175 EXPORT_SYMBOL(register_quota_format); 176 177 void unregister_quota_format(struct quota_format_type *fmt) 178 { 179 struct quota_format_type **actqf; 180 181 spin_lock(&dq_list_lock); 182 for (actqf = "a_formats; *actqf && *actqf != fmt; 183 actqf = &(*actqf)->qf_next) 184 ; 185 if (*actqf) 186 *actqf = (*actqf)->qf_next; 187 spin_unlock(&dq_list_lock); 188 } 189 EXPORT_SYMBOL(unregister_quota_format); 190 191 static struct quota_format_type *find_quota_format(int id) 192 { 193 struct quota_format_type *actqf; 194 195 spin_lock(&dq_list_lock); 196 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; 197 actqf = actqf->qf_next) 198 ; 199 if (!actqf || !try_module_get(actqf->qf_owner)) { 200 int qm; 201 202 spin_unlock(&dq_list_lock); 203 204 for (qm = 0; module_names[qm].qm_fmt_id && 205 module_names[qm].qm_fmt_id != id; qm++) 206 ; 207 if (!module_names[qm].qm_fmt_id || 208 request_module(module_names[qm].qm_mod_name)) 209 return NULL; 210 211 spin_lock(&dq_list_lock); 212 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; 213 actqf = actqf->qf_next) 214 ; 215 if (actqf && !try_module_get(actqf->qf_owner)) 216 actqf = NULL; 217 } 218 spin_unlock(&dq_list_lock); 219 return actqf; 220 } 221 222 static void put_quota_format(struct quota_format_type *fmt) 223 { 224 module_put(fmt->qf_owner); 225 } 226 227 /* 228 * Dquot List Management: 229 * The quota code uses five lists for dquot management: the inuse_list, 230 * releasing_dquots, free_dquots, dqi_dirty_list, and dquot_hash[] array. 231 * A single dquot structure may be on some of those lists, depending on 232 * its current state. 233 * 234 * All dquots are placed to the end of inuse_list when first created, and this 235 * list is used for invalidate operation, which must look at every dquot. 236 * 237 * When the last reference of a dquot is dropped, the dquot is added to 238 * releasing_dquots. We'll then queue work item which will call 239 * synchronize_srcu() and after that perform the final cleanup of all the 240 * dquots on the list. Each cleaned up dquot is moved to free_dquots list. 241 * Both releasing_dquots and free_dquots use the dq_free list_head in the dquot 242 * struct. 243 * 244 * Unused and cleaned up dquots are in the free_dquots list and this list is 245 * searched whenever we need an available dquot. Dquots are removed from the 246 * list as soon as they are used again and dqstats.free_dquots gives the number 247 * of dquots on the list. When dquot is invalidated it's completely released 248 * from memory. 249 * 250 * Dirty dquots are added to the dqi_dirty_list of quota_info when mark 251 * dirtied, and this list is searched when writing dirty dquots back to 252 * quota file. Note that some filesystems do dirty dquot tracking on their 253 * own (e.g. in a journal) and thus don't use dqi_dirty_list. 254 * 255 * Dquots with a specific identity (device, type and id) are placed on 256 * one of the dquot_hash[] hash chains. The provides an efficient search 257 * mechanism to locate a specific dquot. 258 */ 259 260 static LIST_HEAD(inuse_list); 261 static LIST_HEAD(free_dquots); 262 static LIST_HEAD(releasing_dquots); 263 static unsigned int dq_hash_bits, dq_hash_mask; 264 static struct hlist_head *dquot_hash; 265 266 struct dqstats dqstats; 267 EXPORT_SYMBOL(dqstats); 268 269 static qsize_t inode_get_rsv_space(struct inode *inode); 270 static qsize_t __inode_get_rsv_space(struct inode *inode); 271 static int __dquot_initialize(struct inode *inode, int type); 272 273 static void quota_release_workfn(struct work_struct *work); 274 static DECLARE_DELAYED_WORK(quota_release_work, quota_release_workfn); 275 276 static inline unsigned int 277 hashfn(const struct super_block *sb, struct kqid qid) 278 { 279 unsigned int id = from_kqid(&init_user_ns, qid); 280 int type = qid.type; 281 unsigned long tmp; 282 283 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type); 284 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask; 285 } 286 287 /* 288 * Following list functions expect dq_list_lock to be held 289 */ 290 static inline void insert_dquot_hash(struct dquot *dquot) 291 { 292 struct hlist_head *head; 293 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id); 294 hlist_add_head(&dquot->dq_hash, head); 295 } 296 297 static inline void remove_dquot_hash(struct dquot *dquot) 298 { 299 hlist_del_init(&dquot->dq_hash); 300 } 301 302 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, 303 struct kqid qid) 304 { 305 struct dquot *dquot; 306 307 hlist_for_each_entry(dquot, dquot_hash+hashent, dq_hash) 308 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid)) 309 return dquot; 310 311 return NULL; 312 } 313 314 /* Add a dquot to the tail of the free list */ 315 static inline void put_dquot_last(struct dquot *dquot) 316 { 317 list_add_tail(&dquot->dq_free, &free_dquots); 318 dqstats_inc(DQST_FREE_DQUOTS); 319 } 320 321 static inline void put_releasing_dquots(struct dquot *dquot) 322 { 323 list_add_tail(&dquot->dq_free, &releasing_dquots); 324 set_bit(DQ_RELEASING_B, &dquot->dq_flags); 325 } 326 327 static inline void remove_free_dquot(struct dquot *dquot) 328 { 329 if (list_empty(&dquot->dq_free)) 330 return; 331 list_del_init(&dquot->dq_free); 332 if (!test_bit(DQ_RELEASING_B, &dquot->dq_flags)) 333 dqstats_dec(DQST_FREE_DQUOTS); 334 else 335 clear_bit(DQ_RELEASING_B, &dquot->dq_flags); 336 } 337 338 static inline void put_inuse(struct dquot *dquot) 339 { 340 /* We add to the back of inuse list so we don't have to restart 341 * when traversing this list and we block */ 342 list_add_tail(&dquot->dq_inuse, &inuse_list); 343 dqstats_inc(DQST_ALLOC_DQUOTS); 344 } 345 346 static inline void remove_inuse(struct dquot *dquot) 347 { 348 dqstats_dec(DQST_ALLOC_DQUOTS); 349 list_del(&dquot->dq_inuse); 350 } 351 /* 352 * End of list functions needing dq_list_lock 353 */ 354 355 static void wait_on_dquot(struct dquot *dquot) 356 { 357 mutex_lock(&dquot->dq_lock); 358 mutex_unlock(&dquot->dq_lock); 359 } 360 361 static inline int dquot_active(struct dquot *dquot) 362 { 363 return test_bit(DQ_ACTIVE_B, &dquot->dq_flags); 364 } 365 366 static struct dquot *__dqgrab(struct dquot *dquot) 367 { 368 lockdep_assert_held(&dq_list_lock); 369 if (!atomic_read(&dquot->dq_count)) 370 remove_free_dquot(dquot); 371 atomic_inc(&dquot->dq_count); 372 return dquot; 373 } 374 375 /* 376 * Get reference to dquot when we got pointer to it by some other means. The 377 * dquot has to be active and the caller has to make sure it cannot get 378 * deactivated under our hands. 379 */ 380 struct dquot *dqgrab(struct dquot *dquot) 381 { 382 spin_lock(&dq_list_lock); 383 WARN_ON_ONCE(!dquot_active(dquot)); 384 dquot = __dqgrab(dquot); 385 spin_unlock(&dq_list_lock); 386 387 return dquot; 388 } 389 EXPORT_SYMBOL_GPL(dqgrab); 390 391 static inline int dquot_dirty(struct dquot *dquot) 392 { 393 return test_bit(DQ_MOD_B, &dquot->dq_flags); 394 } 395 396 static inline int mark_dquot_dirty(struct dquot *dquot) 397 { 398 return dquot->dq_sb->dq_op->mark_dirty(dquot); 399 } 400 401 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */ 402 int dquot_mark_dquot_dirty(struct dquot *dquot) 403 { 404 int ret = 1; 405 406 if (!dquot_active(dquot)) 407 return 0; 408 409 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY) 410 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags); 411 412 /* If quota is dirty already, we don't have to acquire dq_list_lock */ 413 if (dquot_dirty(dquot)) 414 return 1; 415 416 spin_lock(&dq_list_lock); 417 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) { 418 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)-> 419 info[dquot->dq_id.type].dqi_dirty_list); 420 ret = 0; 421 } 422 spin_unlock(&dq_list_lock); 423 return ret; 424 } 425 EXPORT_SYMBOL(dquot_mark_dquot_dirty); 426 427 /* Dirtify all the dquots - this can block when journalling */ 428 static inline int mark_all_dquot_dirty(struct dquot __rcu * const *dquots) 429 { 430 int ret, err, cnt; 431 struct dquot *dquot; 432 433 ret = err = 0; 434 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 435 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 436 if (dquot) 437 /* Even in case of error we have to continue */ 438 ret = mark_dquot_dirty(dquot); 439 if (!err && ret < 0) 440 err = ret; 441 } 442 return err; 443 } 444 445 static inline void dqput_all(struct dquot **dquot) 446 { 447 unsigned int cnt; 448 449 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 450 dqput(dquot[cnt]); 451 } 452 453 static inline int clear_dquot_dirty(struct dquot *dquot) 454 { 455 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY) 456 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags); 457 458 spin_lock(&dq_list_lock); 459 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) { 460 spin_unlock(&dq_list_lock); 461 return 0; 462 } 463 list_del_init(&dquot->dq_dirty); 464 spin_unlock(&dq_list_lock); 465 return 1; 466 } 467 468 void mark_info_dirty(struct super_block *sb, int type) 469 { 470 spin_lock(&dq_data_lock); 471 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY; 472 spin_unlock(&dq_data_lock); 473 } 474 EXPORT_SYMBOL(mark_info_dirty); 475 476 /* 477 * Read dquot from disk and alloc space for it 478 */ 479 480 int dquot_acquire(struct dquot *dquot) 481 { 482 int ret = 0, ret2 = 0; 483 unsigned int memalloc; 484 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); 485 486 mutex_lock(&dquot->dq_lock); 487 memalloc = memalloc_nofs_save(); 488 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) { 489 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot); 490 if (ret < 0) 491 goto out_iolock; 492 } 493 /* Make sure flags update is visible after dquot has been filled */ 494 smp_mb__before_atomic(); 495 set_bit(DQ_READ_B, &dquot->dq_flags); 496 /* Instantiate dquot if needed */ 497 if (!dquot_active(dquot) && !dquot->dq_off) { 498 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot); 499 /* Write the info if needed */ 500 if (info_dirty(&dqopt->info[dquot->dq_id.type])) { 501 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info( 502 dquot->dq_sb, dquot->dq_id.type); 503 } 504 if (ret < 0) 505 goto out_iolock; 506 if (ret2 < 0) { 507 ret = ret2; 508 goto out_iolock; 509 } 510 } 511 /* 512 * Make sure flags update is visible after on-disk struct has been 513 * allocated. Paired with smp_rmb() in dqget(). 514 */ 515 smp_mb__before_atomic(); 516 set_bit(DQ_ACTIVE_B, &dquot->dq_flags); 517 out_iolock: 518 memalloc_nofs_restore(memalloc); 519 mutex_unlock(&dquot->dq_lock); 520 return ret; 521 } 522 EXPORT_SYMBOL(dquot_acquire); 523 524 /* 525 * Write dquot to disk 526 */ 527 int dquot_commit(struct dquot *dquot) 528 { 529 int ret = 0; 530 unsigned int memalloc; 531 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); 532 533 mutex_lock(&dquot->dq_lock); 534 memalloc = memalloc_nofs_save(); 535 if (!clear_dquot_dirty(dquot)) 536 goto out_lock; 537 /* Inactive dquot can be only if there was error during read/init 538 * => we have better not writing it */ 539 if (dquot_active(dquot)) 540 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot); 541 else 542 ret = -EIO; 543 out_lock: 544 memalloc_nofs_restore(memalloc); 545 mutex_unlock(&dquot->dq_lock); 546 return ret; 547 } 548 EXPORT_SYMBOL(dquot_commit); 549 550 /* 551 * Release dquot 552 */ 553 int dquot_release(struct dquot *dquot) 554 { 555 int ret = 0, ret2 = 0; 556 unsigned int memalloc; 557 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); 558 559 mutex_lock(&dquot->dq_lock); 560 memalloc = memalloc_nofs_save(); 561 /* Check whether we are not racing with some other dqget() */ 562 if (dquot_is_busy(dquot)) 563 goto out_dqlock; 564 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) { 565 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot); 566 /* Write the info */ 567 if (info_dirty(&dqopt->info[dquot->dq_id.type])) { 568 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info( 569 dquot->dq_sb, dquot->dq_id.type); 570 } 571 if (ret >= 0) 572 ret = ret2; 573 } 574 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); 575 out_dqlock: 576 memalloc_nofs_restore(memalloc); 577 mutex_unlock(&dquot->dq_lock); 578 return ret; 579 } 580 EXPORT_SYMBOL(dquot_release); 581 582 void dquot_destroy(struct dquot *dquot) 583 { 584 kmem_cache_free(dquot_cachep, dquot); 585 } 586 EXPORT_SYMBOL(dquot_destroy); 587 588 static inline void do_destroy_dquot(struct dquot *dquot) 589 { 590 dquot->dq_sb->dq_op->destroy_dquot(dquot); 591 } 592 593 /* Invalidate all dquots on the list. Note that this function is called after 594 * quota is disabled and pointers from inodes removed so there cannot be new 595 * quota users. There can still be some users of quotas due to inodes being 596 * just deleted or pruned by prune_icache() (those are not attached to any 597 * list) or parallel quotactl call. We have to wait for such users. 598 */ 599 static void invalidate_dquots(struct super_block *sb, int type) 600 { 601 struct dquot *dquot, *tmp; 602 603 restart: 604 flush_delayed_work("a_release_work); 605 606 spin_lock(&dq_list_lock); 607 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) { 608 if (dquot->dq_sb != sb) 609 continue; 610 if (dquot->dq_id.type != type) 611 continue; 612 /* Wait for dquot users */ 613 if (atomic_read(&dquot->dq_count)) { 614 atomic_inc(&dquot->dq_count); 615 spin_unlock(&dq_list_lock); 616 /* 617 * Once dqput() wakes us up, we know it's time to free 618 * the dquot. 619 * IMPORTANT: we rely on the fact that there is always 620 * at most one process waiting for dquot to free. 621 * Otherwise dq_count would be > 1 and we would never 622 * wake up. 623 */ 624 wait_event(dquot_ref_wq, 625 atomic_read(&dquot->dq_count) == 1); 626 dqput(dquot); 627 /* At this moment dquot() need not exist (it could be 628 * reclaimed by prune_dqcache(). Hence we must 629 * restart. */ 630 goto restart; 631 } 632 /* 633 * The last user already dropped its reference but dquot didn't 634 * get fully cleaned up yet. Restart the scan which flushes the 635 * work cleaning up released dquots. 636 */ 637 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) { 638 spin_unlock(&dq_list_lock); 639 goto restart; 640 } 641 /* 642 * Quota now has no users and it has been written on last 643 * dqput() 644 */ 645 remove_dquot_hash(dquot); 646 remove_free_dquot(dquot); 647 remove_inuse(dquot); 648 do_destroy_dquot(dquot); 649 } 650 spin_unlock(&dq_list_lock); 651 } 652 653 /* Call callback for every active dquot on given filesystem */ 654 int dquot_scan_active(struct super_block *sb, 655 int (*fn)(struct dquot *dquot, unsigned long priv), 656 unsigned long priv) 657 { 658 struct dquot *dquot, *old_dquot = NULL; 659 int ret = 0; 660 661 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount)); 662 663 spin_lock(&dq_list_lock); 664 list_for_each_entry(dquot, &inuse_list, dq_inuse) { 665 if (!dquot_active(dquot)) 666 continue; 667 if (dquot->dq_sb != sb) 668 continue; 669 __dqgrab(dquot); 670 spin_unlock(&dq_list_lock); 671 dqput(old_dquot); 672 old_dquot = dquot; 673 /* 674 * ->release_dquot() can be racing with us. Our reference 675 * protects us from dquot_release() proceeding so just wait for 676 * any outstanding call and recheck the DQ_ACTIVE_B after that. 677 */ 678 wait_on_dquot(dquot); 679 if (dquot_active(dquot)) { 680 ret = fn(dquot, priv); 681 if (ret < 0) 682 goto out; 683 } 684 spin_lock(&dq_list_lock); 685 /* We are safe to continue now because our dquot could not 686 * be moved out of the inuse list while we hold the reference */ 687 } 688 spin_unlock(&dq_list_lock); 689 out: 690 dqput(old_dquot); 691 return ret; 692 } 693 EXPORT_SYMBOL(dquot_scan_active); 694 695 static inline int dquot_write_dquot(struct dquot *dquot) 696 { 697 int ret = dquot->dq_sb->dq_op->write_dquot(dquot); 698 if (ret < 0) { 699 quota_error(dquot->dq_sb, "Can't write quota structure " 700 "(error %d). Quota may get out of sync!", ret); 701 /* Clear dirty bit anyway to avoid infinite loop. */ 702 clear_dquot_dirty(dquot); 703 } 704 return ret; 705 } 706 707 /* Write all dquot structures to quota files */ 708 int dquot_writeback_dquots(struct super_block *sb, int type) 709 { 710 struct list_head dirty; 711 struct dquot *dquot; 712 struct quota_info *dqopt = sb_dqopt(sb); 713 int cnt; 714 int err, ret = 0; 715 716 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount)); 717 718 flush_delayed_work("a_release_work); 719 720 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 721 if (type != -1 && cnt != type) 722 continue; 723 if (!sb_has_quota_active(sb, cnt)) 724 continue; 725 spin_lock(&dq_list_lock); 726 /* Move list away to avoid livelock. */ 727 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty); 728 while (!list_empty(&dirty)) { 729 dquot = list_first_entry(&dirty, struct dquot, 730 dq_dirty); 731 732 WARN_ON(!dquot_active(dquot)); 733 /* If the dquot is releasing we should not touch it */ 734 if (test_bit(DQ_RELEASING_B, &dquot->dq_flags)) { 735 spin_unlock(&dq_list_lock); 736 flush_delayed_work("a_release_work); 737 spin_lock(&dq_list_lock); 738 continue; 739 } 740 741 /* Now we have active dquot from which someone is 742 * holding reference so we can safely just increase 743 * use count */ 744 __dqgrab(dquot); 745 spin_unlock(&dq_list_lock); 746 err = dquot_write_dquot(dquot); 747 if (err && !ret) 748 ret = err; 749 dqput(dquot); 750 spin_lock(&dq_list_lock); 751 } 752 spin_unlock(&dq_list_lock); 753 } 754 755 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 756 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt) 757 && info_dirty(&dqopt->info[cnt])) 758 sb->dq_op->write_info(sb, cnt); 759 dqstats_inc(DQST_SYNCS); 760 761 return ret; 762 } 763 EXPORT_SYMBOL(dquot_writeback_dquots); 764 765 /* Write all dquot structures to disk and make them visible from userspace */ 766 int dquot_quota_sync(struct super_block *sb, int type) 767 { 768 struct quota_info *dqopt = sb_dqopt(sb); 769 int cnt; 770 int ret; 771 772 ret = dquot_writeback_dquots(sb, type); 773 if (ret) 774 return ret; 775 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) 776 return 0; 777 778 /* This is not very clever (and fast) but currently I don't know about 779 * any other simple way of getting quota data to disk and we must get 780 * them there for userspace to be visible... */ 781 if (sb->s_op->sync_fs) { 782 ret = sb->s_op->sync_fs(sb, 1); 783 if (ret) 784 return ret; 785 } 786 ret = sync_blockdev(sb->s_bdev); 787 if (ret) 788 return ret; 789 790 /* 791 * Now when everything is written we can discard the pagecache so 792 * that userspace sees the changes. 793 */ 794 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 795 if (type != -1 && cnt != type) 796 continue; 797 if (!sb_has_quota_active(sb, cnt)) 798 continue; 799 inode_lock(dqopt->files[cnt]); 800 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0); 801 inode_unlock(dqopt->files[cnt]); 802 } 803 804 return 0; 805 } 806 EXPORT_SYMBOL(dquot_quota_sync); 807 808 static unsigned long 809 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) 810 { 811 struct dquot *dquot; 812 unsigned long orig_nr_to_scan = sc->nr_to_scan; 813 unsigned long freed = 0; 814 815 spin_lock(&dq_list_lock); 816 while (!list_empty(&free_dquots) && sc->nr_to_scan) { 817 dquot = list_first_entry(&free_dquots, struct dquot, dq_free); 818 remove_dquot_hash(dquot); 819 remove_free_dquot(dquot); 820 remove_inuse(dquot); 821 do_destroy_dquot(dquot); 822 sc->nr_to_scan--; 823 freed++; 824 } 825 spin_unlock(&dq_list_lock); 826 827 sc->nr_scanned = orig_nr_to_scan - sc->nr_to_scan; 828 829 /* 830 * DQST_FREE_DQUOTS is a percpu counter, so count_objects() can 831 * report a stale/approximate value that is still positive even 832 * though free_dquots is actually empty by the time we get here. 833 * When that happens sc->nr_scanned comes back 0 and we have 834 * nothing further to offer this reclaim pass, so tell 835 * do_shrink_slab() to stop calling us instead of letting it burn 836 * through its one-shot scan budget against an empty list. 837 */ 838 if (sc->nr_scanned == 0) 839 return SHRINK_STOP; 840 841 return freed; 842 } 843 844 static unsigned long 845 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc) 846 { 847 return vfs_pressure_ratio( 848 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])); 849 } 850 851 /* 852 * Safely release dquot and put reference to dquot. 853 */ 854 static void quota_release_workfn(struct work_struct *work) 855 { 856 struct dquot *dquot; 857 struct list_head rls_head; 858 859 spin_lock(&dq_list_lock); 860 /* Exchange the list head to avoid livelock. */ 861 list_replace_init(&releasing_dquots, &rls_head); 862 spin_unlock(&dq_list_lock); 863 synchronize_srcu(&dquot_srcu); 864 865 restart: 866 spin_lock(&dq_list_lock); 867 while (!list_empty(&rls_head)) { 868 dquot = list_first_entry(&rls_head, struct dquot, dq_free); 869 WARN_ON_ONCE(atomic_read(&dquot->dq_count)); 870 /* 871 * Note that DQ_RELEASING_B protects us from racing with 872 * invalidate_dquots() calls so we are safe to work with the 873 * dquot even after we drop dq_list_lock. 874 */ 875 if (dquot_dirty(dquot)) { 876 spin_unlock(&dq_list_lock); 877 /* Commit dquot before releasing */ 878 dquot_write_dquot(dquot); 879 goto restart; 880 } 881 if (dquot_active(dquot)) { 882 spin_unlock(&dq_list_lock); 883 dquot->dq_sb->dq_op->release_dquot(dquot); 884 goto restart; 885 } 886 /* Dquot is inactive and clean, now move it to free list */ 887 remove_free_dquot(dquot); 888 put_dquot_last(dquot); 889 } 890 spin_unlock(&dq_list_lock); 891 } 892 893 /* 894 * Put reference to dquot 895 */ 896 void dqput(struct dquot *dquot) 897 { 898 if (!dquot) 899 return; 900 #ifdef CONFIG_QUOTA_DEBUG 901 if (!atomic_read(&dquot->dq_count)) { 902 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d", 903 quotatypes[dquot->dq_id.type], 904 from_kqid(&init_user_ns, dquot->dq_id)); 905 BUG(); 906 } 907 #endif 908 dqstats_inc(DQST_DROPS); 909 910 spin_lock(&dq_list_lock); 911 if (atomic_read(&dquot->dq_count) > 1) { 912 /* We have more than one user... nothing to do */ 913 atomic_dec(&dquot->dq_count); 914 /* Releasing dquot during quotaoff phase? */ 915 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) && 916 atomic_read(&dquot->dq_count) == 1) 917 wake_up(&dquot_ref_wq); 918 spin_unlock(&dq_list_lock); 919 return; 920 } 921 922 /* Need to release dquot? */ 923 WARN_ON_ONCE(!list_empty(&dquot->dq_free)); 924 put_releasing_dquots(dquot); 925 atomic_dec(&dquot->dq_count); 926 spin_unlock(&dq_list_lock); 927 queue_delayed_work(quota_unbound_wq, "a_release_work, 1); 928 } 929 EXPORT_SYMBOL(dqput); 930 931 struct dquot *dquot_alloc(struct super_block *sb, int type) 932 { 933 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS); 934 } 935 EXPORT_SYMBOL(dquot_alloc); 936 937 static struct dquot *get_empty_dquot(struct super_block *sb, int type) 938 { 939 struct dquot *dquot; 940 941 dquot = sb->dq_op->alloc_dquot(sb, type); 942 if(!dquot) 943 return NULL; 944 945 mutex_init(&dquot->dq_lock); 946 INIT_LIST_HEAD(&dquot->dq_free); 947 INIT_LIST_HEAD(&dquot->dq_inuse); 948 INIT_HLIST_NODE(&dquot->dq_hash); 949 INIT_LIST_HEAD(&dquot->dq_dirty); 950 dquot->dq_sb = sb; 951 dquot->dq_id = make_kqid_invalid(type); 952 atomic_set(&dquot->dq_count, 1); 953 spin_lock_init(&dquot->dq_dqb_lock); 954 955 return dquot; 956 } 957 958 /* 959 * Get reference to dquot 960 * 961 * Locking is slightly tricky here. We are guarded from parallel quotaoff() 962 * destroying our dquot by: 963 * a) checking for quota flags under dq_list_lock and 964 * b) getting a reference to dquot before we release dq_list_lock 965 */ 966 struct dquot *dqget(struct super_block *sb, struct kqid qid) 967 { 968 unsigned int hashent = hashfn(sb, qid); 969 struct dquot *dquot, *empty = NULL; 970 971 if (!qid_has_mapping(sb->s_user_ns, qid)) 972 return ERR_PTR(-EINVAL); 973 974 if (!sb_has_quota_active(sb, qid.type)) 975 return ERR_PTR(-ESRCH); 976 we_slept: 977 spin_lock(&dq_list_lock); 978 spin_lock(&dq_state_lock); 979 if (!sb_has_quota_active(sb, qid.type)) { 980 spin_unlock(&dq_state_lock); 981 spin_unlock(&dq_list_lock); 982 dquot = ERR_PTR(-ESRCH); 983 goto out; 984 } 985 spin_unlock(&dq_state_lock); 986 987 dquot = find_dquot(hashent, sb, qid); 988 if (!dquot) { 989 if (!empty) { 990 spin_unlock(&dq_list_lock); 991 empty = get_empty_dquot(sb, qid.type); 992 if (!empty) 993 schedule(); /* Try to wait for a moment... */ 994 goto we_slept; 995 } 996 dquot = empty; 997 empty = NULL; 998 dquot->dq_id = qid; 999 /* all dquots go on the inuse_list */ 1000 put_inuse(dquot); 1001 /* hash it first so it can be found */ 1002 insert_dquot_hash(dquot); 1003 spin_unlock(&dq_list_lock); 1004 dqstats_inc(DQST_LOOKUPS); 1005 } else { 1006 __dqgrab(dquot); 1007 spin_unlock(&dq_list_lock); 1008 dqstats_inc(DQST_CACHE_HITS); 1009 dqstats_inc(DQST_LOOKUPS); 1010 } 1011 /* Wait for dq_lock - after this we know that either dquot_release() is 1012 * already finished or it will be canceled due to dq_count > 0 test */ 1013 wait_on_dquot(dquot); 1014 /* Read the dquot / allocate space in quota file */ 1015 if (!dquot_active(dquot)) { 1016 int err; 1017 1018 err = sb->dq_op->acquire_dquot(dquot); 1019 if (err < 0) { 1020 dqput(dquot); 1021 dquot = ERR_PTR(err); 1022 goto out; 1023 } 1024 } 1025 /* 1026 * Make sure following reads see filled structure - paired with 1027 * smp_mb__before_atomic() in dquot_acquire(). 1028 */ 1029 smp_rmb(); 1030 /* Has somebody invalidated entry under us? */ 1031 WARN_ON_ONCE(hlist_unhashed(&dquot->dq_hash)); 1032 out: 1033 if (empty) 1034 do_destroy_dquot(empty); 1035 1036 return dquot; 1037 } 1038 EXPORT_SYMBOL(dqget); 1039 1040 static inline struct dquot __rcu **i_dquot(struct inode *inode) 1041 { 1042 return inode->i_sb->s_op->get_dquots(inode); 1043 } 1044 1045 static int dqinit_needed(struct inode *inode, int type) 1046 { 1047 struct dquot __rcu * const *dquots; 1048 int cnt; 1049 1050 if (IS_NOQUOTA(inode)) 1051 return 0; 1052 1053 dquots = i_dquot(inode); 1054 if (type != -1) 1055 return !dquots[type]; 1056 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1057 if (!dquots[cnt]) 1058 return 1; 1059 return 0; 1060 } 1061 1062 /* This routine is guarded by s_umount semaphore */ 1063 static int add_dquot_ref(struct super_block *sb, int type) 1064 { 1065 struct inode *inode, *old_inode = NULL; 1066 #ifdef CONFIG_QUOTA_DEBUG 1067 int reserved = 0; 1068 #endif 1069 int err = 0; 1070 1071 spin_lock(&sb->s_inode_list_lock); 1072 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1073 spin_lock(&inode->i_lock); 1074 if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) || 1075 !atomic_read(&inode->i_writecount) || 1076 !dqinit_needed(inode, type)) { 1077 spin_unlock(&inode->i_lock); 1078 continue; 1079 } 1080 __iget(inode); 1081 spin_unlock(&inode->i_lock); 1082 spin_unlock(&sb->s_inode_list_lock); 1083 1084 #ifdef CONFIG_QUOTA_DEBUG 1085 if (unlikely(inode_get_rsv_space(inode) > 0)) 1086 reserved = 1; 1087 #endif 1088 iput(old_inode); 1089 err = __dquot_initialize(inode, type); 1090 if (err) { 1091 iput(inode); 1092 goto out; 1093 } 1094 1095 /* 1096 * We hold a reference to 'inode' so it couldn't have been 1097 * removed from s_inodes list while we dropped the 1098 * s_inode_list_lock. We cannot iput the inode now as we can be 1099 * holding the last reference and we cannot iput it under 1100 * s_inode_list_lock. So we keep the reference and iput it 1101 * later. 1102 */ 1103 old_inode = inode; 1104 cond_resched(); 1105 spin_lock(&sb->s_inode_list_lock); 1106 } 1107 spin_unlock(&sb->s_inode_list_lock); 1108 iput(old_inode); 1109 out: 1110 #ifdef CONFIG_QUOTA_DEBUG 1111 if (reserved) { 1112 quota_error(sb, "Writes happened before quota was turned on " 1113 "thus quota information is probably inconsistent. " 1114 "Please run quotacheck(8)"); 1115 } 1116 #endif 1117 return err; 1118 } 1119 1120 static void remove_dquot_ref(struct super_block *sb, int type) 1121 { 1122 struct inode *inode; 1123 #ifdef CONFIG_QUOTA_DEBUG 1124 int reserved = 0; 1125 #endif 1126 1127 spin_lock(&sb->s_inode_list_lock); 1128 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 1129 /* 1130 * We have to scan also I_NEW inodes because they can already 1131 * have quota pointer initialized. Luckily, we need to touch 1132 * only quota pointers and these have separate locking 1133 * (dq_data_lock). 1134 */ 1135 spin_lock(&dq_data_lock); 1136 if (!IS_NOQUOTA(inode)) { 1137 struct dquot __rcu **dquots = i_dquot(inode); 1138 struct dquot *dquot = srcu_dereference_check( 1139 dquots[type], &dquot_srcu, 1140 lockdep_is_held(&dq_data_lock)); 1141 1142 #ifdef CONFIG_QUOTA_DEBUG 1143 if (unlikely(inode_get_rsv_space(inode) > 0)) 1144 reserved = 1; 1145 #endif 1146 rcu_assign_pointer(dquots[type], NULL); 1147 if (dquot) 1148 dqput(dquot); 1149 } 1150 spin_unlock(&dq_data_lock); 1151 } 1152 spin_unlock(&sb->s_inode_list_lock); 1153 #ifdef CONFIG_QUOTA_DEBUG 1154 if (reserved) { 1155 printk(KERN_WARNING "VFS (%s): Writes happened after quota" 1156 " was disabled thus quota information is probably " 1157 "inconsistent. Please run quotacheck(8).\n", sb->s_id); 1158 } 1159 #endif 1160 } 1161 1162 /* Gather all references from inodes and drop them */ 1163 static void drop_dquot_ref(struct super_block *sb, int type) 1164 { 1165 if (sb->dq_op) 1166 remove_dquot_ref(sb, type); 1167 } 1168 1169 static inline 1170 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number) 1171 { 1172 if (dquot->dq_dqb.dqb_rsvspace >= number) 1173 dquot->dq_dqb.dqb_rsvspace -= number; 1174 else { 1175 WARN_ON_ONCE(1); 1176 dquot->dq_dqb.dqb_rsvspace = 0; 1177 } 1178 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <= 1179 dquot->dq_dqb.dqb_bsoftlimit) 1180 dquot->dq_dqb.dqb_btime = (time64_t) 0; 1181 clear_bit(DQ_BLKS_B, &dquot->dq_flags); 1182 } 1183 1184 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number) 1185 { 1186 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE || 1187 dquot->dq_dqb.dqb_curinodes >= number) 1188 dquot->dq_dqb.dqb_curinodes -= number; 1189 else 1190 dquot->dq_dqb.dqb_curinodes = 0; 1191 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit) 1192 dquot->dq_dqb.dqb_itime = (time64_t) 0; 1193 clear_bit(DQ_INODES_B, &dquot->dq_flags); 1194 } 1195 1196 static void dquot_decr_space(struct dquot *dquot, qsize_t number) 1197 { 1198 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE || 1199 dquot->dq_dqb.dqb_curspace >= number) 1200 dquot->dq_dqb.dqb_curspace -= number; 1201 else 1202 dquot->dq_dqb.dqb_curspace = 0; 1203 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <= 1204 dquot->dq_dqb.dqb_bsoftlimit) 1205 dquot->dq_dqb.dqb_btime = (time64_t) 0; 1206 clear_bit(DQ_BLKS_B, &dquot->dq_flags); 1207 } 1208 1209 struct dquot_warn { 1210 struct super_block *w_sb; 1211 struct kqid w_dq_id; 1212 short w_type; 1213 }; 1214 1215 static int warning_issued(struct dquot *dquot, const int warntype) 1216 { 1217 int flag = (warntype == QUOTA_NL_BHARDWARN || 1218 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B : 1219 ((warntype == QUOTA_NL_IHARDWARN || 1220 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0); 1221 1222 if (!flag) 1223 return 0; 1224 return test_and_set_bit(flag, &dquot->dq_flags); 1225 } 1226 1227 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot, 1228 int warntype) 1229 { 1230 if (warning_issued(dquot, warntype)) 1231 return; 1232 warn->w_type = warntype; 1233 warn->w_sb = dquot->dq_sb; 1234 warn->w_dq_id = dquot->dq_id; 1235 } 1236 1237 /* 1238 * Write warnings to the console and send warning messages over netlink. 1239 * 1240 * Note that this function can call into tty and networking code. 1241 */ 1242 static void flush_warnings(struct dquot_warn *warn) 1243 { 1244 int i; 1245 1246 for (i = 0; i < MAXQUOTAS; i++) { 1247 if (warn[i].w_type == QUOTA_NL_NOWARN) 1248 continue; 1249 1250 quota_send_warning(warn[i].w_dq_id, 1251 warn[i].w_sb->s_dev, warn[i].w_type); 1252 } 1253 } 1254 1255 static int ignore_hardlimit(struct dquot *dquot) 1256 { 1257 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type]; 1258 1259 return capable_noaudit(CAP_SYS_RESOURCE) && 1260 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || 1261 !(info->dqi_flags & DQF_ROOT_SQUASH)); 1262 } 1263 1264 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes, 1265 struct dquot_warn *warn) 1266 { 1267 qsize_t newinodes; 1268 int ret = 0; 1269 1270 spin_lock(&dquot->dq_dqb_lock); 1271 newinodes = dquot->dq_dqb.dqb_curinodes + inodes; 1272 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) || 1273 test_bit(DQ_FAKE_B, &dquot->dq_flags)) 1274 goto add; 1275 1276 if (dquot->dq_dqb.dqb_ihardlimit && 1277 newinodes > dquot->dq_dqb.dqb_ihardlimit && 1278 !ignore_hardlimit(dquot)) { 1279 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN); 1280 ret = -EDQUOT; 1281 goto out; 1282 } 1283 1284 if (dquot->dq_dqb.dqb_isoftlimit && 1285 newinodes > dquot->dq_dqb.dqb_isoftlimit && 1286 dquot->dq_dqb.dqb_itime && 1287 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime && 1288 !ignore_hardlimit(dquot)) { 1289 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN); 1290 ret = -EDQUOT; 1291 goto out; 1292 } 1293 1294 if (dquot->dq_dqb.dqb_isoftlimit && 1295 newinodes > dquot->dq_dqb.dqb_isoftlimit && 1296 dquot->dq_dqb.dqb_itime == 0) { 1297 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN); 1298 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() + 1299 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace; 1300 } 1301 add: 1302 dquot->dq_dqb.dqb_curinodes = newinodes; 1303 1304 out: 1305 spin_unlock(&dquot->dq_dqb_lock); 1306 return ret; 1307 } 1308 1309 static int dquot_add_space(struct dquot *dquot, qsize_t space, 1310 qsize_t rsv_space, unsigned int flags, 1311 struct dquot_warn *warn) 1312 { 1313 qsize_t tspace; 1314 struct super_block *sb = dquot->dq_sb; 1315 int ret = 0; 1316 1317 spin_lock(&dquot->dq_dqb_lock); 1318 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) || 1319 test_bit(DQ_FAKE_B, &dquot->dq_flags)) 1320 goto finish; 1321 1322 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace 1323 + space + rsv_space; 1324 1325 if (dquot->dq_dqb.dqb_bhardlimit && 1326 tspace > dquot->dq_dqb.dqb_bhardlimit && 1327 !ignore_hardlimit(dquot)) { 1328 if (flags & DQUOT_SPACE_WARN) 1329 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN); 1330 ret = -EDQUOT; 1331 goto finish; 1332 } 1333 1334 if (dquot->dq_dqb.dqb_bsoftlimit && 1335 tspace > dquot->dq_dqb.dqb_bsoftlimit && 1336 dquot->dq_dqb.dqb_btime && 1337 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime && 1338 !ignore_hardlimit(dquot)) { 1339 if (flags & DQUOT_SPACE_WARN) 1340 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN); 1341 ret = -EDQUOT; 1342 goto finish; 1343 } 1344 1345 if (dquot->dq_dqb.dqb_bsoftlimit && 1346 tspace > dquot->dq_dqb.dqb_bsoftlimit && 1347 dquot->dq_dqb.dqb_btime == 0) { 1348 if (flags & DQUOT_SPACE_WARN) { 1349 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN); 1350 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() + 1351 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace; 1352 } else { 1353 /* 1354 * We don't allow preallocation to exceed softlimit so exceeding will 1355 * be always printed 1356 */ 1357 ret = -EDQUOT; 1358 goto finish; 1359 } 1360 } 1361 finish: 1362 /* 1363 * We have to be careful and go through warning generation & grace time 1364 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it 1365 * only here... 1366 */ 1367 if (flags & DQUOT_SPACE_NOFAIL) 1368 ret = 0; 1369 if (!ret) { 1370 dquot->dq_dqb.dqb_rsvspace += rsv_space; 1371 dquot->dq_dqb.dqb_curspace += space; 1372 } 1373 spin_unlock(&dquot->dq_dqb_lock); 1374 return ret; 1375 } 1376 1377 static int info_idq_free(struct dquot *dquot, qsize_t inodes) 1378 { 1379 qsize_t newinodes; 1380 1381 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || 1382 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit || 1383 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type)) 1384 return QUOTA_NL_NOWARN; 1385 1386 newinodes = dquot->dq_dqb.dqb_curinodes - inodes; 1387 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit) 1388 return QUOTA_NL_ISOFTBELOW; 1389 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit && 1390 newinodes < dquot->dq_dqb.dqb_ihardlimit) 1391 return QUOTA_NL_IHARDBELOW; 1392 return QUOTA_NL_NOWARN; 1393 } 1394 1395 static int info_bdq_free(struct dquot *dquot, qsize_t space) 1396 { 1397 qsize_t tspace; 1398 1399 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace; 1400 1401 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) || 1402 tspace <= dquot->dq_dqb.dqb_bsoftlimit) 1403 return QUOTA_NL_NOWARN; 1404 1405 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit) 1406 return QUOTA_NL_BSOFTBELOW; 1407 if (tspace >= dquot->dq_dqb.dqb_bhardlimit && 1408 tspace - space < dquot->dq_dqb.dqb_bhardlimit) 1409 return QUOTA_NL_BHARDBELOW; 1410 return QUOTA_NL_NOWARN; 1411 } 1412 1413 static int inode_quota_active(const struct inode *inode) 1414 { 1415 struct super_block *sb = inode->i_sb; 1416 1417 if (IS_NOQUOTA(inode)) 1418 return 0; 1419 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb); 1420 } 1421 1422 /* 1423 * Initialize quota pointers in inode 1424 * 1425 * It is better to call this function outside of any transaction as it 1426 * might need a lot of space in journal for dquot structure allocation. 1427 */ 1428 static int __dquot_initialize(struct inode *inode, int type) 1429 { 1430 int cnt, init_needed = 0; 1431 struct dquot __rcu **dquots; 1432 struct dquot *got[MAXQUOTAS] = {}; 1433 struct super_block *sb = inode->i_sb; 1434 qsize_t rsv; 1435 int ret = 0; 1436 1437 if (!inode_quota_active(inode)) 1438 return 0; 1439 1440 dquots = i_dquot(inode); 1441 1442 /* First get references to structures we might need. */ 1443 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1444 struct kqid qid; 1445 kprojid_t projid; 1446 int rc; 1447 struct dquot *dquot; 1448 1449 if (type != -1 && cnt != type) 1450 continue; 1451 /* 1452 * The i_dquot should have been initialized in most cases, 1453 * we check it without locking here to avoid unnecessary 1454 * dqget()/dqput() calls. 1455 */ 1456 if (dquots[cnt]) 1457 continue; 1458 1459 if (!sb_has_quota_active(sb, cnt)) 1460 continue; 1461 1462 init_needed = 1; 1463 1464 switch (cnt) { 1465 case USRQUOTA: 1466 qid = make_kqid_uid(inode->i_uid); 1467 break; 1468 case GRPQUOTA: 1469 qid = make_kqid_gid(inode->i_gid); 1470 break; 1471 case PRJQUOTA: 1472 rc = inode->i_sb->dq_op->get_projid(inode, &projid); 1473 if (rc) 1474 continue; 1475 qid = make_kqid_projid(projid); 1476 break; 1477 } 1478 dquot = dqget(sb, qid); 1479 if (IS_ERR(dquot)) { 1480 /* We raced with somebody turning quotas off... */ 1481 if (PTR_ERR(dquot) != -ESRCH) { 1482 ret = PTR_ERR(dquot); 1483 goto out_put; 1484 } 1485 dquot = NULL; 1486 } 1487 got[cnt] = dquot; 1488 } 1489 1490 /* All required i_dquot has been initialized */ 1491 if (!init_needed) 1492 return 0; 1493 1494 spin_lock(&dq_data_lock); 1495 if (IS_NOQUOTA(inode)) 1496 goto out_lock; 1497 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1498 if (type != -1 && cnt != type) 1499 continue; 1500 /* Avoid races with quotaoff() */ 1501 if (!sb_has_quota_active(sb, cnt)) 1502 continue; 1503 /* We could race with quotaon or dqget() could have failed */ 1504 if (!got[cnt]) 1505 continue; 1506 if (!dquots[cnt]) { 1507 rcu_assign_pointer(dquots[cnt], got[cnt]); 1508 got[cnt] = NULL; 1509 /* 1510 * Make quota reservation system happy if someone 1511 * did a write before quota was turned on 1512 */ 1513 rsv = inode_get_rsv_space(inode); 1514 if (unlikely(rsv)) { 1515 struct dquot *dquot = srcu_dereference_check( 1516 dquots[cnt], &dquot_srcu, 1517 lockdep_is_held(&dq_data_lock)); 1518 1519 spin_lock(&inode->i_lock); 1520 /* Get reservation again under proper lock */ 1521 rsv = __inode_get_rsv_space(inode); 1522 spin_lock(&dquot->dq_dqb_lock); 1523 dquot->dq_dqb.dqb_rsvspace += rsv; 1524 spin_unlock(&dquot->dq_dqb_lock); 1525 spin_unlock(&inode->i_lock); 1526 } 1527 } 1528 } 1529 out_lock: 1530 spin_unlock(&dq_data_lock); 1531 out_put: 1532 /* Drop unused references */ 1533 dqput_all(got); 1534 1535 return ret; 1536 } 1537 1538 int dquot_initialize(struct inode *inode) 1539 { 1540 return __dquot_initialize(inode, -1); 1541 } 1542 EXPORT_SYMBOL(dquot_initialize); 1543 1544 bool dquot_initialize_needed(struct inode *inode) 1545 { 1546 struct dquot __rcu **dquots; 1547 int i; 1548 1549 if (!inode_quota_active(inode)) 1550 return false; 1551 1552 dquots = i_dquot(inode); 1553 for (i = 0; i < MAXQUOTAS; i++) 1554 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i)) 1555 return true; 1556 return false; 1557 } 1558 EXPORT_SYMBOL(dquot_initialize_needed); 1559 1560 /* 1561 * Release all quotas referenced by inode. 1562 * 1563 * This function only be called on inode free or converting 1564 * a file to quota file, no other users for the i_dquot in 1565 * both cases, so we needn't call synchronize_srcu() after 1566 * clearing i_dquot. 1567 */ 1568 static void __dquot_drop(struct inode *inode) 1569 { 1570 int cnt; 1571 struct dquot __rcu **dquots = i_dquot(inode); 1572 struct dquot *put[MAXQUOTAS]; 1573 1574 spin_lock(&dq_data_lock); 1575 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1576 put[cnt] = srcu_dereference_check(dquots[cnt], &dquot_srcu, 1577 lockdep_is_held(&dq_data_lock)); 1578 rcu_assign_pointer(dquots[cnt], NULL); 1579 } 1580 spin_unlock(&dq_data_lock); 1581 dqput_all(put); 1582 } 1583 1584 void dquot_drop(struct inode *inode) 1585 { 1586 struct dquot __rcu * const *dquots; 1587 int cnt; 1588 1589 if (IS_NOQUOTA(inode)) 1590 return; 1591 1592 /* 1593 * Test before calling to rule out calls from proc and such 1594 * where we are not allowed to block. Note that this is 1595 * actually reliable test even without the lock - the caller 1596 * must assure that nobody can come after the DQUOT_DROP and 1597 * add quota pointers back anyway. 1598 */ 1599 dquots = i_dquot(inode); 1600 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1601 if (dquots[cnt]) 1602 break; 1603 } 1604 1605 if (cnt < MAXQUOTAS) 1606 __dquot_drop(inode); 1607 } 1608 EXPORT_SYMBOL(dquot_drop); 1609 1610 /* 1611 * inode_reserved_space is managed internally by quota, and protected by 1612 * i_lock similar to i_blocks+i_bytes. 1613 */ 1614 static qsize_t *inode_reserved_space(struct inode * inode) 1615 { 1616 /* Filesystem must explicitly define it's own method in order to use 1617 * quota reservation interface */ 1618 BUG_ON(!inode->i_sb->dq_op->get_reserved_space); 1619 return inode->i_sb->dq_op->get_reserved_space(inode); 1620 } 1621 1622 static qsize_t __inode_get_rsv_space(struct inode *inode) 1623 { 1624 if (!inode->i_sb->dq_op->get_reserved_space) 1625 return 0; 1626 return *inode_reserved_space(inode); 1627 } 1628 1629 static qsize_t inode_get_rsv_space(struct inode *inode) 1630 { 1631 qsize_t ret; 1632 1633 if (!inode->i_sb->dq_op->get_reserved_space) 1634 return 0; 1635 spin_lock(&inode->i_lock); 1636 ret = __inode_get_rsv_space(inode); 1637 spin_unlock(&inode->i_lock); 1638 return ret; 1639 } 1640 1641 /* 1642 * This functions updates i_blocks+i_bytes fields and quota information 1643 * (together with appropriate checks). 1644 * 1645 * NOTE: We absolutely rely on the fact that caller dirties the inode 1646 * (usually helpers in quotaops.h care about this) and holds a handle for 1647 * the current transaction so that dquot write and inode write go into the 1648 * same transaction. 1649 */ 1650 1651 /* 1652 * This operation can block, but only after everything is updated 1653 */ 1654 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags) 1655 { 1656 int cnt, ret = 0, index; 1657 struct dquot_warn warn[MAXQUOTAS]; 1658 int reserve = flags & DQUOT_SPACE_RESERVE; 1659 struct dquot __rcu **dquots; 1660 struct dquot *dquot; 1661 1662 if (!inode_quota_active(inode)) { 1663 if (reserve) { 1664 spin_lock(&inode->i_lock); 1665 *inode_reserved_space(inode) += number; 1666 spin_unlock(&inode->i_lock); 1667 } else { 1668 inode_add_bytes(inode, number); 1669 } 1670 goto out; 1671 } 1672 1673 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1674 warn[cnt].w_type = QUOTA_NL_NOWARN; 1675 1676 dquots = i_dquot(inode); 1677 index = srcu_read_lock(&dquot_srcu); 1678 spin_lock(&inode->i_lock); 1679 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1680 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1681 if (!dquot) 1682 continue; 1683 if (reserve) { 1684 ret = dquot_add_space(dquot, 0, number, flags, &warn[cnt]); 1685 } else { 1686 ret = dquot_add_space(dquot, number, 0, flags, &warn[cnt]); 1687 } 1688 if (ret) { 1689 /* Back out changes we already did */ 1690 for (cnt--; cnt >= 0; cnt--) { 1691 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1692 if (!dquot) 1693 continue; 1694 spin_lock(&dquot->dq_dqb_lock); 1695 if (reserve) 1696 dquot_free_reserved_space(dquot, number); 1697 else 1698 dquot_decr_space(dquot, number); 1699 spin_unlock(&dquot->dq_dqb_lock); 1700 } 1701 spin_unlock(&inode->i_lock); 1702 goto out_flush_warn; 1703 } 1704 } 1705 if (reserve) 1706 *inode_reserved_space(inode) += number; 1707 else 1708 __inode_add_bytes(inode, number); 1709 spin_unlock(&inode->i_lock); 1710 1711 if (reserve) 1712 goto out_flush_warn; 1713 ret = mark_all_dquot_dirty(dquots); 1714 out_flush_warn: 1715 srcu_read_unlock(&dquot_srcu, index); 1716 flush_warnings(warn); 1717 out: 1718 return ret; 1719 } 1720 EXPORT_SYMBOL(__dquot_alloc_space); 1721 1722 /* 1723 * This operation can block, but only after everything is updated 1724 */ 1725 int dquot_alloc_inode(struct inode *inode) 1726 { 1727 int cnt, ret = 0, index; 1728 struct dquot_warn warn[MAXQUOTAS]; 1729 struct dquot __rcu * const *dquots; 1730 struct dquot *dquot; 1731 1732 if (!inode_quota_active(inode)) 1733 return 0; 1734 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1735 warn[cnt].w_type = QUOTA_NL_NOWARN; 1736 1737 dquots = i_dquot(inode); 1738 index = srcu_read_lock(&dquot_srcu); 1739 spin_lock(&inode->i_lock); 1740 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1741 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1742 if (!dquot) 1743 continue; 1744 ret = dquot_add_inodes(dquot, 1, &warn[cnt]); 1745 if (ret) { 1746 for (cnt--; cnt >= 0; cnt--) { 1747 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1748 if (!dquot) 1749 continue; 1750 /* Back out changes we already did */ 1751 spin_lock(&dquot->dq_dqb_lock); 1752 dquot_decr_inodes(dquot, 1); 1753 spin_unlock(&dquot->dq_dqb_lock); 1754 } 1755 goto warn_put_all; 1756 } 1757 } 1758 1759 warn_put_all: 1760 spin_unlock(&inode->i_lock); 1761 if (ret == 0) 1762 ret = mark_all_dquot_dirty(dquots); 1763 srcu_read_unlock(&dquot_srcu, index); 1764 flush_warnings(warn); 1765 return ret; 1766 } 1767 EXPORT_SYMBOL(dquot_alloc_inode); 1768 1769 /* 1770 * Convert in-memory reserved quotas to real consumed quotas 1771 */ 1772 void dquot_claim_space_nodirty(struct inode *inode, qsize_t number) 1773 { 1774 struct dquot __rcu **dquots; 1775 struct dquot *dquot; 1776 int cnt, index; 1777 1778 if (!inode_quota_active(inode)) { 1779 spin_lock(&inode->i_lock); 1780 *inode_reserved_space(inode) -= number; 1781 __inode_add_bytes(inode, number); 1782 spin_unlock(&inode->i_lock); 1783 return; 1784 } 1785 1786 dquots = i_dquot(inode); 1787 index = srcu_read_lock(&dquot_srcu); 1788 spin_lock(&inode->i_lock); 1789 /* Claim reserved quotas to allocated quotas */ 1790 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1791 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1792 if (dquot) { 1793 spin_lock(&dquot->dq_dqb_lock); 1794 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number)) 1795 number = dquot->dq_dqb.dqb_rsvspace; 1796 dquot->dq_dqb.dqb_curspace += number; 1797 dquot->dq_dqb.dqb_rsvspace -= number; 1798 spin_unlock(&dquot->dq_dqb_lock); 1799 } 1800 } 1801 /* Update inode bytes */ 1802 *inode_reserved_space(inode) -= number; 1803 __inode_add_bytes(inode, number); 1804 spin_unlock(&inode->i_lock); 1805 mark_all_dquot_dirty(dquots); 1806 srcu_read_unlock(&dquot_srcu, index); 1807 } 1808 EXPORT_SYMBOL(dquot_claim_space_nodirty); 1809 1810 /* 1811 * Convert allocated space back to in-memory reserved quotas 1812 */ 1813 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number) 1814 { 1815 struct dquot __rcu **dquots; 1816 struct dquot *dquot; 1817 int cnt, index; 1818 1819 if (!inode_quota_active(inode)) { 1820 spin_lock(&inode->i_lock); 1821 *inode_reserved_space(inode) += number; 1822 __inode_sub_bytes(inode, number); 1823 spin_unlock(&inode->i_lock); 1824 return; 1825 } 1826 1827 dquots = i_dquot(inode); 1828 index = srcu_read_lock(&dquot_srcu); 1829 spin_lock(&inode->i_lock); 1830 /* Claim reserved quotas to allocated quotas */ 1831 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1832 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1833 if (dquot) { 1834 spin_lock(&dquot->dq_dqb_lock); 1835 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number)) 1836 number = dquot->dq_dqb.dqb_curspace; 1837 dquot->dq_dqb.dqb_rsvspace += number; 1838 dquot->dq_dqb.dqb_curspace -= number; 1839 spin_unlock(&dquot->dq_dqb_lock); 1840 } 1841 } 1842 /* Update inode bytes */ 1843 *inode_reserved_space(inode) += number; 1844 __inode_sub_bytes(inode, number); 1845 spin_unlock(&inode->i_lock); 1846 mark_all_dquot_dirty(dquots); 1847 srcu_read_unlock(&dquot_srcu, index); 1848 } 1849 EXPORT_SYMBOL(dquot_reclaim_space_nodirty); 1850 1851 /* 1852 * This operation can block, but only after everything is updated 1853 */ 1854 void __dquot_free_space(struct inode *inode, qsize_t number, int flags) 1855 { 1856 unsigned int cnt; 1857 struct dquot_warn warn[MAXQUOTAS]; 1858 struct dquot __rcu **dquots; 1859 struct dquot *dquot; 1860 int reserve = flags & DQUOT_SPACE_RESERVE, index; 1861 1862 if (!inode_quota_active(inode)) { 1863 if (reserve) { 1864 spin_lock(&inode->i_lock); 1865 *inode_reserved_space(inode) -= number; 1866 spin_unlock(&inode->i_lock); 1867 } else { 1868 inode_sub_bytes(inode, number); 1869 } 1870 return; 1871 } 1872 1873 dquots = i_dquot(inode); 1874 index = srcu_read_lock(&dquot_srcu); 1875 spin_lock(&inode->i_lock); 1876 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1877 int wtype; 1878 1879 warn[cnt].w_type = QUOTA_NL_NOWARN; 1880 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1881 if (!dquot) 1882 continue; 1883 spin_lock(&dquot->dq_dqb_lock); 1884 wtype = info_bdq_free(dquot, number); 1885 if (wtype != QUOTA_NL_NOWARN) 1886 prepare_warning(&warn[cnt], dquot, wtype); 1887 if (reserve) 1888 dquot_free_reserved_space(dquot, number); 1889 else 1890 dquot_decr_space(dquot, number); 1891 spin_unlock(&dquot->dq_dqb_lock); 1892 } 1893 if (reserve) 1894 *inode_reserved_space(inode) -= number; 1895 else 1896 __inode_sub_bytes(inode, number); 1897 spin_unlock(&inode->i_lock); 1898 1899 if (reserve) 1900 goto out_unlock; 1901 mark_all_dquot_dirty(dquots); 1902 out_unlock: 1903 srcu_read_unlock(&dquot_srcu, index); 1904 flush_warnings(warn); 1905 } 1906 EXPORT_SYMBOL(__dquot_free_space); 1907 1908 /* 1909 * This operation can block, but only after everything is updated 1910 */ 1911 void dquot_free_inode(struct inode *inode) 1912 { 1913 unsigned int cnt; 1914 struct dquot_warn warn[MAXQUOTAS]; 1915 struct dquot __rcu * const *dquots; 1916 struct dquot *dquot; 1917 int index; 1918 1919 if (!inode_quota_active(inode)) 1920 return; 1921 1922 dquots = i_dquot(inode); 1923 index = srcu_read_lock(&dquot_srcu); 1924 spin_lock(&inode->i_lock); 1925 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1926 int wtype; 1927 warn[cnt].w_type = QUOTA_NL_NOWARN; 1928 dquot = srcu_dereference(dquots[cnt], &dquot_srcu); 1929 if (!dquot) 1930 continue; 1931 spin_lock(&dquot->dq_dqb_lock); 1932 wtype = info_idq_free(dquot, 1); 1933 if (wtype != QUOTA_NL_NOWARN) 1934 prepare_warning(&warn[cnt], dquot, wtype); 1935 dquot_decr_inodes(dquot, 1); 1936 spin_unlock(&dquot->dq_dqb_lock); 1937 } 1938 spin_unlock(&inode->i_lock); 1939 mark_all_dquot_dirty(dquots); 1940 srcu_read_unlock(&dquot_srcu, index); 1941 flush_warnings(warn); 1942 } 1943 EXPORT_SYMBOL(dquot_free_inode); 1944 1945 /* 1946 * Transfer the number of inode and blocks from one diskquota to an other. 1947 * On success, dquot references in transfer_to are consumed and references 1948 * to original dquots that need to be released are placed there. On failure, 1949 * references are kept untouched. 1950 * 1951 * This operation can block, but only after everything is updated 1952 * A transaction must be started when entering this function. 1953 * 1954 * We are holding reference on transfer_from & transfer_to, no need to 1955 * protect them by srcu_read_lock(). 1956 */ 1957 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to) 1958 { 1959 qsize_t cur_space; 1960 qsize_t rsv_space = 0; 1961 qsize_t inode_usage = 1; 1962 struct dquot __rcu **dquots; 1963 struct dquot *transfer_from[MAXQUOTAS] = {}; 1964 int cnt, index, ret = 0, err; 1965 char is_valid[MAXQUOTAS] = {}; 1966 struct dquot_warn warn_to[MAXQUOTAS]; 1967 struct dquot_warn warn_from_inodes[MAXQUOTAS]; 1968 struct dquot_warn warn_from_space[MAXQUOTAS]; 1969 1970 if (IS_NOQUOTA(inode)) 1971 return 0; 1972 1973 if (inode->i_sb->dq_op->get_inode_usage) { 1974 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage); 1975 if (ret) 1976 return ret; 1977 } 1978 1979 /* Initialize the arrays */ 1980 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1981 warn_to[cnt].w_type = QUOTA_NL_NOWARN; 1982 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN; 1983 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN; 1984 } 1985 1986 spin_lock(&dq_data_lock); 1987 spin_lock(&inode->i_lock); 1988 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */ 1989 spin_unlock(&inode->i_lock); 1990 spin_unlock(&dq_data_lock); 1991 return 0; 1992 } 1993 cur_space = __inode_get_bytes(inode); 1994 rsv_space = __inode_get_rsv_space(inode); 1995 dquots = i_dquot(inode); 1996 /* 1997 * Build the transfer_from list, check limits, and update usage in 1998 * the target structures. 1999 */ 2000 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 2001 /* 2002 * Skip changes for same uid or gid or for turned off quota-type. 2003 */ 2004 if (!transfer_to[cnt]) 2005 continue; 2006 /* Avoid races with quotaoff() */ 2007 if (!sb_has_quota_active(inode->i_sb, cnt)) 2008 continue; 2009 is_valid[cnt] = 1; 2010 transfer_from[cnt] = srcu_dereference_check(dquots[cnt], 2011 &dquot_srcu, lockdep_is_held(&dq_data_lock)); 2012 ret = dquot_add_inodes(transfer_to[cnt], inode_usage, 2013 &warn_to[cnt]); 2014 if (ret) 2015 goto over_quota; 2016 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space, 2017 DQUOT_SPACE_WARN, &warn_to[cnt]); 2018 if (ret) { 2019 spin_lock(&transfer_to[cnt]->dq_dqb_lock); 2020 dquot_decr_inodes(transfer_to[cnt], inode_usage); 2021 spin_unlock(&transfer_to[cnt]->dq_dqb_lock); 2022 goto over_quota; 2023 } 2024 } 2025 2026 /* Decrease usage for source structures and update quota pointers */ 2027 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 2028 if (!is_valid[cnt]) 2029 continue; 2030 /* Due to IO error we might not have transfer_from[] structure */ 2031 if (transfer_from[cnt]) { 2032 int wtype; 2033 2034 spin_lock(&transfer_from[cnt]->dq_dqb_lock); 2035 wtype = info_idq_free(transfer_from[cnt], inode_usage); 2036 if (wtype != QUOTA_NL_NOWARN) 2037 prepare_warning(&warn_from_inodes[cnt], 2038 transfer_from[cnt], wtype); 2039 wtype = info_bdq_free(transfer_from[cnt], 2040 cur_space + rsv_space); 2041 if (wtype != QUOTA_NL_NOWARN) 2042 prepare_warning(&warn_from_space[cnt], 2043 transfer_from[cnt], wtype); 2044 dquot_decr_inodes(transfer_from[cnt], inode_usage); 2045 dquot_decr_space(transfer_from[cnt], cur_space); 2046 dquot_free_reserved_space(transfer_from[cnt], 2047 rsv_space); 2048 spin_unlock(&transfer_from[cnt]->dq_dqb_lock); 2049 } 2050 rcu_assign_pointer(dquots[cnt], transfer_to[cnt]); 2051 } 2052 spin_unlock(&inode->i_lock); 2053 spin_unlock(&dq_data_lock); 2054 2055 /* 2056 * These arrays are local and we hold dquot references so we don't need 2057 * the srcu protection but still take dquot_srcu to avoid warning in 2058 * mark_all_dquot_dirty(). 2059 */ 2060 index = srcu_read_lock(&dquot_srcu); 2061 err = mark_all_dquot_dirty((struct dquot __rcu **)transfer_from); 2062 if (err < 0) 2063 ret = err; 2064 err = mark_all_dquot_dirty((struct dquot __rcu **)transfer_to); 2065 if (err < 0) 2066 ret = err; 2067 srcu_read_unlock(&dquot_srcu, index); 2068 2069 flush_warnings(warn_to); 2070 flush_warnings(warn_from_inodes); 2071 flush_warnings(warn_from_space); 2072 /* Pass back references to put */ 2073 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 2074 if (is_valid[cnt]) 2075 transfer_to[cnt] = transfer_from[cnt]; 2076 return ret; 2077 over_quota: 2078 /* Back out changes we already did */ 2079 for (cnt--; cnt >= 0; cnt--) { 2080 if (!is_valid[cnt]) 2081 continue; 2082 spin_lock(&transfer_to[cnt]->dq_dqb_lock); 2083 dquot_decr_inodes(transfer_to[cnt], inode_usage); 2084 dquot_decr_space(transfer_to[cnt], cur_space); 2085 dquot_free_reserved_space(transfer_to[cnt], rsv_space); 2086 spin_unlock(&transfer_to[cnt]->dq_dqb_lock); 2087 } 2088 spin_unlock(&inode->i_lock); 2089 spin_unlock(&dq_data_lock); 2090 flush_warnings(warn_to); 2091 return ret; 2092 } 2093 EXPORT_SYMBOL(__dquot_transfer); 2094 2095 /* Wrapper for transferring ownership of an inode for uid/gid only 2096 * Called from FSXXX_setattr() 2097 */ 2098 int dquot_transfer(const struct mnt_idmap *idmap, struct inode *inode, 2099 struct iattr *iattr) 2100 { 2101 struct dquot *transfer_to[MAXQUOTAS] = {}; 2102 struct dquot *dquot; 2103 struct super_block *sb = inode->i_sb; 2104 int ret; 2105 2106 if (!inode_quota_active(inode)) 2107 return 0; 2108 2109 if (i_uid_needs_update(idmap, iattr, inode)) { 2110 kuid_t kuid = from_vfsuid(idmap, i_user_ns(inode), 2111 iattr->ia_vfsuid); 2112 2113 dquot = dqget(sb, make_kqid_uid(kuid)); 2114 if (IS_ERR(dquot)) { 2115 if (PTR_ERR(dquot) != -ESRCH) { 2116 ret = PTR_ERR(dquot); 2117 goto out_put; 2118 } 2119 dquot = NULL; 2120 } 2121 transfer_to[USRQUOTA] = dquot; 2122 } 2123 if (i_gid_needs_update(idmap, iattr, inode)) { 2124 kgid_t kgid = from_vfsgid(idmap, i_user_ns(inode), 2125 iattr->ia_vfsgid); 2126 2127 dquot = dqget(sb, make_kqid_gid(kgid)); 2128 if (IS_ERR(dquot)) { 2129 if (PTR_ERR(dquot) != -ESRCH) { 2130 ret = PTR_ERR(dquot); 2131 goto out_put; 2132 } 2133 dquot = NULL; 2134 } 2135 transfer_to[GRPQUOTA] = dquot; 2136 } 2137 ret = __dquot_transfer(inode, transfer_to); 2138 out_put: 2139 dqput_all(transfer_to); 2140 return ret; 2141 } 2142 EXPORT_SYMBOL(dquot_transfer); 2143 2144 /* 2145 * Write info of quota file to disk 2146 */ 2147 int dquot_commit_info(struct super_block *sb, int type) 2148 { 2149 struct quota_info *dqopt = sb_dqopt(sb); 2150 2151 return dqopt->ops[type]->write_file_info(sb, type); 2152 } 2153 EXPORT_SYMBOL(dquot_commit_info); 2154 2155 int dquot_get_next_id(struct super_block *sb, struct kqid *qid) 2156 { 2157 struct quota_info *dqopt = sb_dqopt(sb); 2158 2159 if (!sb_has_quota_active(sb, qid->type)) 2160 return -ESRCH; 2161 if (!dqopt->ops[qid->type]->get_next_id) 2162 return -ENOSYS; 2163 return dqopt->ops[qid->type]->get_next_id(sb, qid); 2164 } 2165 EXPORT_SYMBOL(dquot_get_next_id); 2166 2167 /* 2168 * Definitions of diskquota operations. 2169 */ 2170 const struct dquot_operations dquot_operations = { 2171 .write_dquot = dquot_commit, 2172 .acquire_dquot = dquot_acquire, 2173 .release_dquot = dquot_release, 2174 .mark_dirty = dquot_mark_dquot_dirty, 2175 .write_info = dquot_commit_info, 2176 .alloc_dquot = dquot_alloc, 2177 .destroy_dquot = dquot_destroy, 2178 .get_next_id = dquot_get_next_id, 2179 }; 2180 EXPORT_SYMBOL(dquot_operations); 2181 2182 /* 2183 * Generic helper for ->open on filesystems supporting disk quotas. 2184 */ 2185 int dquot_file_open(struct inode *inode, struct file *file) 2186 { 2187 int error; 2188 2189 error = generic_file_open(inode, file); 2190 if (!error && (file->f_mode & FMODE_WRITE)) 2191 error = dquot_initialize(inode); 2192 return error; 2193 } 2194 EXPORT_SYMBOL(dquot_file_open); 2195 2196 static void vfs_cleanup_quota_inode(struct super_block *sb, int type) 2197 { 2198 struct quota_info *dqopt = sb_dqopt(sb); 2199 struct inode *inode = dqopt->files[type]; 2200 2201 if (!inode) 2202 return; 2203 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) { 2204 inode_lock(inode); 2205 inode->i_flags &= ~S_NOQUOTA; 2206 inode_unlock(inode); 2207 } 2208 dqopt->files[type] = NULL; 2209 iput(inode); 2210 } 2211 2212 /* 2213 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount) 2214 */ 2215 int dquot_disable(struct super_block *sb, int type, unsigned int flags) 2216 { 2217 int cnt; 2218 struct quota_info *dqopt = sb_dqopt(sb); 2219 2220 rwsem_assert_held_write(&sb->s_umount); 2221 2222 /* Cannot turn off usage accounting without turning off limits, or 2223 * suspend quotas and simultaneously turn quotas off. */ 2224 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED)) 2225 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED | 2226 DQUOT_USAGE_ENABLED))) 2227 return -EINVAL; 2228 2229 /* 2230 * Skip everything if there's nothing to do. We have to do this because 2231 * sometimes we are called when fill_super() failed and calling 2232 * sync_fs() in such cases does no good. 2233 */ 2234 if (!sb_any_quota_loaded(sb)) 2235 return 0; 2236 2237 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 2238 if (type != -1 && cnt != type) 2239 continue; 2240 if (!sb_has_quota_loaded(sb, cnt)) 2241 continue; 2242 2243 if (flags & DQUOT_SUSPENDED) { 2244 spin_lock(&dq_state_lock); 2245 dqopt->flags |= 2246 dquot_state_flag(DQUOT_SUSPENDED, cnt); 2247 spin_unlock(&dq_state_lock); 2248 } else { 2249 spin_lock(&dq_state_lock); 2250 dqopt->flags &= ~dquot_state_flag(flags, cnt); 2251 /* Turning off suspended quotas? */ 2252 if (!sb_has_quota_loaded(sb, cnt) && 2253 sb_has_quota_suspended(sb, cnt)) { 2254 dqopt->flags &= ~dquot_state_flag( 2255 DQUOT_SUSPENDED, cnt); 2256 spin_unlock(&dq_state_lock); 2257 vfs_cleanup_quota_inode(sb, cnt); 2258 continue; 2259 } 2260 spin_unlock(&dq_state_lock); 2261 } 2262 2263 /* We still have to keep quota loaded? */ 2264 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED)) 2265 continue; 2266 2267 /* Note: these are blocking operations */ 2268 drop_dquot_ref(sb, cnt); 2269 invalidate_dquots(sb, cnt); 2270 /* 2271 * Now all dquots should be invalidated, all writes done so we 2272 * should be only users of the info. No locks needed. 2273 */ 2274 if (info_dirty(&dqopt->info[cnt])) 2275 sb->dq_op->write_info(sb, cnt); 2276 if (dqopt->ops[cnt]->free_file_info) 2277 dqopt->ops[cnt]->free_file_info(sb, cnt); 2278 put_quota_format(dqopt->info[cnt].dqi_format); 2279 dqopt->info[cnt].dqi_flags = 0; 2280 dqopt->info[cnt].dqi_igrace = 0; 2281 dqopt->info[cnt].dqi_bgrace = 0; 2282 dqopt->ops[cnt] = NULL; 2283 } 2284 2285 /* Skip syncing and setting flags if quota files are hidden */ 2286 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) 2287 goto put_inodes; 2288 2289 /* Sync the superblock so that buffers with quota data are written to 2290 * disk (and so userspace sees correct data afterwards). */ 2291 if (sb->s_op->sync_fs) 2292 sb->s_op->sync_fs(sb, 1); 2293 sync_blockdev(sb->s_bdev); 2294 /* Now the quota files are just ordinary files and we can set the 2295 * inode flags back. Moreover we discard the pagecache so that 2296 * userspace sees the writes we did bypassing the pagecache. We 2297 * must also discard the blockdev buffers so that we see the 2298 * changes done by userspace on the next quotaon() */ 2299 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 2300 if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) { 2301 inode_lock(dqopt->files[cnt]); 2302 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0); 2303 inode_unlock(dqopt->files[cnt]); 2304 } 2305 if (sb->s_bdev) 2306 invalidate_bdev(sb->s_bdev); 2307 put_inodes: 2308 /* We are done when suspending quotas */ 2309 if (flags & DQUOT_SUSPENDED) 2310 return 0; 2311 2312 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 2313 if (!sb_has_quota_loaded(sb, cnt)) 2314 vfs_cleanup_quota_inode(sb, cnt); 2315 return 0; 2316 } 2317 EXPORT_SYMBOL(dquot_disable); 2318 2319 int dquot_quota_off(struct super_block *sb, int type) 2320 { 2321 return dquot_disable(sb, type, 2322 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); 2323 } 2324 EXPORT_SYMBOL(dquot_quota_off); 2325 2326 /* 2327 * Turn quotas on on a device 2328 */ 2329 2330 static int vfs_setup_quota_inode(struct inode *inode, int type) 2331 { 2332 struct super_block *sb = inode->i_sb; 2333 struct quota_info *dqopt = sb_dqopt(sb); 2334 2335 if (is_bad_inode(inode)) 2336 return -EUCLEAN; 2337 if (!S_ISREG(inode->i_mode)) 2338 return -EACCES; 2339 if (IS_RDONLY(inode)) 2340 return -EROFS; 2341 if (sb_has_quota_loaded(sb, type)) 2342 return -EBUSY; 2343 2344 /* 2345 * Quota files should never be encrypted. They should be thought of as 2346 * filesystem metadata, not user data. New-style internal quota files 2347 * cannot be encrypted by users anyway, but old-style external quota 2348 * files could potentially be incorrectly created in an encrypted 2349 * directory, hence this explicit check. Some reasons why encrypted 2350 * quota files don't work include: (1) some filesystems that support 2351 * encryption don't handle it in their quota_read and quota_write, and 2352 * (2) cleaning up encrypted quota files at unmount would need special 2353 * consideration, as quota files are cleaned up later than user files. 2354 */ 2355 if (IS_ENCRYPTED(inode)) 2356 return -EINVAL; 2357 2358 dqopt->files[type] = igrab(inode); 2359 if (!dqopt->files[type]) 2360 return -EIO; 2361 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) { 2362 /* We don't want quota and atime on quota files (deadlocks 2363 * possible) Also nobody should write to the file - we use 2364 * special IO operations which ignore the immutable bit. */ 2365 inode_lock(inode); 2366 inode->i_flags |= S_NOQUOTA; 2367 inode_unlock(inode); 2368 /* 2369 * When S_NOQUOTA is set, remove dquot references as no more 2370 * references can be added 2371 */ 2372 __dquot_drop(inode); 2373 } 2374 return 0; 2375 } 2376 2377 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id, 2378 unsigned int flags) 2379 { 2380 struct quota_format_type *fmt; 2381 struct quota_info *dqopt = sb_dqopt(sb); 2382 int error; 2383 2384 lockdep_assert_held_write(&sb->s_umount); 2385 2386 /* Just unsuspend quotas? */ 2387 if (WARN_ON_ONCE(flags & DQUOT_SUSPENDED)) 2388 return -EINVAL; 2389 2390 fmt = find_quota_format(format_id); 2391 if (!fmt) 2392 return -ESRCH; 2393 if (!sb->dq_op || !sb->s_qcop || 2394 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) { 2395 error = -EINVAL; 2396 goto out_fmt; 2397 } 2398 /* Filesystems outside of init_user_ns not yet supported */ 2399 if (sb->s_user_ns != &init_user_ns) { 2400 error = -EINVAL; 2401 goto out_fmt; 2402 } 2403 /* Usage always has to be set... */ 2404 if (!(flags & DQUOT_USAGE_ENABLED)) { 2405 error = -EINVAL; 2406 goto out_fmt; 2407 } 2408 if (sb_has_quota_loaded(sb, type)) { 2409 error = -EBUSY; 2410 goto out_fmt; 2411 } 2412 2413 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) { 2414 /* As we bypass the pagecache we must now flush all the 2415 * dirty data and invalidate caches so that kernel sees 2416 * changes from userspace. It is not enough to just flush 2417 * the quota file since if blocksize < pagesize, invalidation 2418 * of the cache could fail because of other unrelated dirty 2419 * data */ 2420 sync_filesystem(sb); 2421 invalidate_bdev(sb->s_bdev); 2422 } 2423 2424 error = -EINVAL; 2425 if (!fmt->qf_ops->check_quota_file(sb, type)) 2426 goto out_fmt; 2427 2428 dqopt->ops[type] = fmt->qf_ops; 2429 dqopt->info[type].dqi_format = fmt; 2430 dqopt->info[type].dqi_fmt_id = format_id; 2431 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list); 2432 error = dqopt->ops[type]->read_file_info(sb, type); 2433 if (error < 0) 2434 goto out_fmt; 2435 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) { 2436 spin_lock(&dq_data_lock); 2437 dqopt->info[type].dqi_flags |= DQF_SYS_FILE; 2438 spin_unlock(&dq_data_lock); 2439 } 2440 spin_lock(&dq_state_lock); 2441 dqopt->flags |= dquot_state_flag(flags, type); 2442 spin_unlock(&dq_state_lock); 2443 2444 error = add_dquot_ref(sb, type); 2445 if (error) 2446 dquot_disable(sb, type, 2447 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); 2448 2449 return error; 2450 out_fmt: 2451 put_quota_format(fmt); 2452 2453 return error; 2454 } 2455 EXPORT_SYMBOL(dquot_load_quota_sb); 2456 2457 /* 2458 * More powerful function for turning on quotas on given quota inode allowing 2459 * setting of individual quota flags 2460 */ 2461 int dquot_load_quota_inode(struct inode *inode, int type, int format_id, 2462 unsigned int flags) 2463 { 2464 int err; 2465 2466 err = vfs_setup_quota_inode(inode, type); 2467 if (err < 0) 2468 return err; 2469 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags); 2470 if (err < 0) 2471 vfs_cleanup_quota_inode(inode->i_sb, type); 2472 return err; 2473 } 2474 EXPORT_SYMBOL(dquot_load_quota_inode); 2475 2476 /* Reenable quotas on remount RW */ 2477 int dquot_resume(struct super_block *sb, int type) 2478 { 2479 struct quota_info *dqopt = sb_dqopt(sb); 2480 int ret = 0, cnt; 2481 unsigned int flags; 2482 2483 rwsem_assert_held_write(&sb->s_umount); 2484 2485 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 2486 if (type != -1 && cnt != type) 2487 continue; 2488 if (!sb_has_quota_suspended(sb, cnt)) 2489 continue; 2490 2491 spin_lock(&dq_state_lock); 2492 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED | 2493 DQUOT_LIMITS_ENABLED, 2494 cnt); 2495 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt); 2496 spin_unlock(&dq_state_lock); 2497 2498 flags = dquot_generic_flag(flags, cnt); 2499 ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id, 2500 flags); 2501 if (ret < 0) 2502 vfs_cleanup_quota_inode(sb, cnt); 2503 } 2504 2505 return ret; 2506 } 2507 EXPORT_SYMBOL(dquot_resume); 2508 2509 int dquot_quota_on(struct super_block *sb, int type, int format_id, 2510 const struct path *path) 2511 { 2512 int error = security_quota_on(path->dentry); 2513 if (error) 2514 return error; 2515 /* Quota file not on the same filesystem? */ 2516 if (path->dentry->d_sb != sb) 2517 error = -EXDEV; 2518 else 2519 error = dquot_load_quota_inode(d_inode(path->dentry), type, 2520 format_id, DQUOT_USAGE_ENABLED | 2521 DQUOT_LIMITS_ENABLED); 2522 return error; 2523 } 2524 EXPORT_SYMBOL(dquot_quota_on); 2525 2526 /* 2527 * This function is used when filesystem needs to initialize quotas 2528 * during mount time. 2529 */ 2530 int dquot_quota_on_mount(struct super_block *sb, char *qf_name, 2531 int format_id, int type) 2532 { 2533 struct dentry *dentry; 2534 int error; 2535 2536 dentry = lookup_noperm_positive_unlocked(&QSTR(qf_name), sb->s_root); 2537 if (IS_ERR(dentry)) 2538 return PTR_ERR(dentry); 2539 2540 error = security_quota_on(dentry); 2541 if (!error) 2542 error = dquot_load_quota_inode(d_inode(dentry), type, format_id, 2543 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); 2544 2545 dput(dentry); 2546 return error; 2547 } 2548 EXPORT_SYMBOL(dquot_quota_on_mount); 2549 2550 static int dquot_quota_enable(struct super_block *sb, unsigned int flags) 2551 { 2552 int ret; 2553 int type; 2554 struct quota_info *dqopt = sb_dqopt(sb); 2555 2556 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) 2557 return -ENOSYS; 2558 /* Accounting cannot be turned on while fs is mounted */ 2559 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT); 2560 if (!flags) 2561 return -EINVAL; 2562 for (type = 0; type < MAXQUOTAS; type++) { 2563 if (!(flags & qtype_enforce_flag(type))) 2564 continue; 2565 /* Can't enforce without accounting */ 2566 if (!sb_has_quota_usage_enabled(sb, type)) { 2567 ret = -EINVAL; 2568 goto out_err; 2569 } 2570 if (sb_has_quota_limits_enabled(sb, type)) { 2571 /* compatible with XFS */ 2572 ret = -EEXIST; 2573 goto out_err; 2574 } 2575 spin_lock(&dq_state_lock); 2576 dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type); 2577 spin_unlock(&dq_state_lock); 2578 } 2579 return 0; 2580 out_err: 2581 /* Backout enforcement enablement we already did */ 2582 for (type--; type >= 0; type--) { 2583 if (flags & qtype_enforce_flag(type)) 2584 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED); 2585 } 2586 return ret; 2587 } 2588 2589 static int dquot_quota_disable(struct super_block *sb, unsigned int flags) 2590 { 2591 int ret; 2592 int type; 2593 struct quota_info *dqopt = sb_dqopt(sb); 2594 2595 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) 2596 return -ENOSYS; 2597 /* 2598 * We don't support turning off accounting via quotactl. In principle 2599 * quota infrastructure can do this but filesystems don't expect 2600 * userspace to be able to do it. 2601 */ 2602 if (flags & 2603 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT)) 2604 return -EOPNOTSUPP; 2605 2606 /* Filter out limits not enabled */ 2607 for (type = 0; type < MAXQUOTAS; type++) 2608 if (!sb_has_quota_limits_enabled(sb, type)) 2609 flags &= ~qtype_enforce_flag(type); 2610 /* Nothing left? */ 2611 if (!flags) 2612 return -EEXIST; 2613 for (type = 0; type < MAXQUOTAS; type++) { 2614 if (flags & qtype_enforce_flag(type)) { 2615 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED); 2616 if (ret < 0) 2617 goto out_err; 2618 } 2619 } 2620 return 0; 2621 out_err: 2622 /* Backout enforcement disabling we already did */ 2623 for (type--; type >= 0; type--) { 2624 if (flags & qtype_enforce_flag(type)) { 2625 spin_lock(&dq_state_lock); 2626 dqopt->flags |= 2627 dquot_state_flag(DQUOT_LIMITS_ENABLED, type); 2628 spin_unlock(&dq_state_lock); 2629 } 2630 } 2631 return ret; 2632 } 2633 2634 /* Generic routine for getting common part of quota structure */ 2635 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di) 2636 { 2637 struct mem_dqblk *dm = &dquot->dq_dqb; 2638 2639 memset(di, 0, sizeof(*di)); 2640 spin_lock(&dquot->dq_dqb_lock); 2641 di->d_spc_hardlimit = dm->dqb_bhardlimit; 2642 di->d_spc_softlimit = dm->dqb_bsoftlimit; 2643 di->d_ino_hardlimit = dm->dqb_ihardlimit; 2644 di->d_ino_softlimit = dm->dqb_isoftlimit; 2645 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace; 2646 di->d_ino_count = dm->dqb_curinodes; 2647 di->d_spc_timer = dm->dqb_btime; 2648 di->d_ino_timer = dm->dqb_itime; 2649 spin_unlock(&dquot->dq_dqb_lock); 2650 } 2651 2652 int dquot_get_dqblk(struct super_block *sb, struct kqid qid, 2653 struct qc_dqblk *di) 2654 { 2655 struct dquot *dquot; 2656 2657 dquot = dqget(sb, qid); 2658 if (IS_ERR(dquot)) 2659 return PTR_ERR(dquot); 2660 do_get_dqblk(dquot, di); 2661 dqput(dquot); 2662 2663 return 0; 2664 } 2665 EXPORT_SYMBOL(dquot_get_dqblk); 2666 2667 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid, 2668 struct qc_dqblk *di) 2669 { 2670 struct dquot *dquot; 2671 int err; 2672 2673 if (!sb->dq_op->get_next_id) 2674 return -ENOSYS; 2675 err = sb->dq_op->get_next_id(sb, qid); 2676 if (err < 0) 2677 return err; 2678 dquot = dqget(sb, *qid); 2679 if (IS_ERR(dquot)) 2680 return PTR_ERR(dquot); 2681 do_get_dqblk(dquot, di); 2682 dqput(dquot); 2683 2684 return 0; 2685 } 2686 EXPORT_SYMBOL(dquot_get_next_dqblk); 2687 2688 #define VFS_QC_MASK \ 2689 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \ 2690 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \ 2691 QC_SPC_TIMER | QC_INO_TIMER) 2692 2693 /* Generic routine for setting common part of quota structure */ 2694 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di) 2695 { 2696 struct mem_dqblk *dm = &dquot->dq_dqb; 2697 int check_blim = 0, check_ilim = 0; 2698 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type]; 2699 int ret; 2700 2701 if (di->d_fieldmask & ~VFS_QC_MASK) 2702 return -EINVAL; 2703 2704 if (((di->d_fieldmask & QC_SPC_SOFT) && 2705 di->d_spc_softlimit > dqi->dqi_max_spc_limit) || 2706 ((di->d_fieldmask & QC_SPC_HARD) && 2707 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) || 2708 ((di->d_fieldmask & QC_INO_SOFT) && 2709 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) || 2710 ((di->d_fieldmask & QC_INO_HARD) && 2711 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit))) 2712 return -ERANGE; 2713 2714 spin_lock(&dquot->dq_dqb_lock); 2715 if (di->d_fieldmask & QC_SPACE) { 2716 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace; 2717 check_blim = 1; 2718 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags); 2719 } 2720 2721 if (di->d_fieldmask & QC_SPC_SOFT) 2722 dm->dqb_bsoftlimit = di->d_spc_softlimit; 2723 if (di->d_fieldmask & QC_SPC_HARD) 2724 dm->dqb_bhardlimit = di->d_spc_hardlimit; 2725 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) { 2726 check_blim = 1; 2727 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags); 2728 } 2729 2730 if (di->d_fieldmask & QC_INO_COUNT) { 2731 dm->dqb_curinodes = di->d_ino_count; 2732 check_ilim = 1; 2733 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags); 2734 } 2735 2736 if (di->d_fieldmask & QC_INO_SOFT) 2737 dm->dqb_isoftlimit = di->d_ino_softlimit; 2738 if (di->d_fieldmask & QC_INO_HARD) 2739 dm->dqb_ihardlimit = di->d_ino_hardlimit; 2740 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) { 2741 check_ilim = 1; 2742 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags); 2743 } 2744 2745 if (di->d_fieldmask & QC_SPC_TIMER) { 2746 dm->dqb_btime = di->d_spc_timer; 2747 check_blim = 1; 2748 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags); 2749 } 2750 2751 if (di->d_fieldmask & QC_INO_TIMER) { 2752 dm->dqb_itime = di->d_ino_timer; 2753 check_ilim = 1; 2754 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags); 2755 } 2756 2757 if (check_blim) { 2758 if (!dm->dqb_bsoftlimit || 2759 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) { 2760 dm->dqb_btime = 0; 2761 clear_bit(DQ_BLKS_B, &dquot->dq_flags); 2762 } else if (!(di->d_fieldmask & QC_SPC_TIMER)) 2763 /* Set grace only if user hasn't provided his own... */ 2764 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace; 2765 } 2766 if (check_ilim) { 2767 if (!dm->dqb_isoftlimit || 2768 dm->dqb_curinodes <= dm->dqb_isoftlimit) { 2769 dm->dqb_itime = 0; 2770 clear_bit(DQ_INODES_B, &dquot->dq_flags); 2771 } else if (!(di->d_fieldmask & QC_INO_TIMER)) 2772 /* Set grace only if user hasn't provided his own... */ 2773 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace; 2774 } 2775 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || 2776 dm->dqb_isoftlimit) 2777 clear_bit(DQ_FAKE_B, &dquot->dq_flags); 2778 else 2779 set_bit(DQ_FAKE_B, &dquot->dq_flags); 2780 spin_unlock(&dquot->dq_dqb_lock); 2781 ret = mark_dquot_dirty(dquot); 2782 if (ret < 0) 2783 return ret; 2784 return 0; 2785 } 2786 2787 int dquot_set_dqblk(struct super_block *sb, struct kqid qid, 2788 struct qc_dqblk *di) 2789 { 2790 struct dquot *dquot; 2791 int rc; 2792 2793 dquot = dqget(sb, qid); 2794 if (IS_ERR(dquot)) { 2795 rc = PTR_ERR(dquot); 2796 goto out; 2797 } 2798 rc = do_set_dqblk(dquot, di); 2799 dqput(dquot); 2800 out: 2801 return rc; 2802 } 2803 EXPORT_SYMBOL(dquot_set_dqblk); 2804 2805 /* Generic routine for getting common part of quota file information */ 2806 int dquot_get_state(struct super_block *sb, struct qc_state *state) 2807 { 2808 struct mem_dqinfo *mi; 2809 struct qc_type_state *tstate; 2810 struct quota_info *dqopt = sb_dqopt(sb); 2811 int type; 2812 2813 memset(state, 0, sizeof(*state)); 2814 for (type = 0; type < MAXQUOTAS; type++) { 2815 if (!sb_has_quota_active(sb, type)) 2816 continue; 2817 tstate = state->s_state + type; 2818 mi = sb_dqopt(sb)->info + type; 2819 tstate->flags = QCI_ACCT_ENABLED; 2820 spin_lock(&dq_data_lock); 2821 if (mi->dqi_flags & DQF_SYS_FILE) 2822 tstate->flags |= QCI_SYSFILE; 2823 if (mi->dqi_flags & DQF_ROOT_SQUASH) 2824 tstate->flags |= QCI_ROOT_SQUASH; 2825 if (sb_has_quota_limits_enabled(sb, type)) 2826 tstate->flags |= QCI_LIMITS_ENFORCED; 2827 tstate->spc_timelimit = mi->dqi_bgrace; 2828 tstate->ino_timelimit = mi->dqi_igrace; 2829 if (dqopt->files[type]) { 2830 tstate->ino = dqopt->files[type]->i_ino; 2831 tstate->blocks = dqopt->files[type]->i_blocks; 2832 } 2833 tstate->nextents = 1; /* We don't know... */ 2834 spin_unlock(&dq_data_lock); 2835 } 2836 return 0; 2837 } 2838 EXPORT_SYMBOL(dquot_get_state); 2839 2840 /* Generic routine for setting common part of quota file information */ 2841 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii) 2842 { 2843 struct mem_dqinfo *mi; 2844 2845 if ((ii->i_fieldmask & QC_WARNS_MASK) || 2846 (ii->i_fieldmask & QC_RT_SPC_TIMER)) 2847 return -EINVAL; 2848 if (!sb_has_quota_active(sb, type)) 2849 return -ESRCH; 2850 mi = sb_dqopt(sb)->info + type; 2851 if (ii->i_fieldmask & QC_FLAGS) { 2852 if ((ii->i_flags & QCI_ROOT_SQUASH && 2853 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD)) 2854 return -EINVAL; 2855 } 2856 spin_lock(&dq_data_lock); 2857 if (ii->i_fieldmask & QC_SPC_TIMER) 2858 mi->dqi_bgrace = ii->i_spc_timelimit; 2859 if (ii->i_fieldmask & QC_INO_TIMER) 2860 mi->dqi_igrace = ii->i_ino_timelimit; 2861 if (ii->i_fieldmask & QC_FLAGS) { 2862 if (ii->i_flags & QCI_ROOT_SQUASH) 2863 mi->dqi_flags |= DQF_ROOT_SQUASH; 2864 else 2865 mi->dqi_flags &= ~DQF_ROOT_SQUASH; 2866 } 2867 spin_unlock(&dq_data_lock); 2868 mark_info_dirty(sb, type); 2869 /* Force write to disk */ 2870 return sb->dq_op->write_info(sb, type); 2871 } 2872 EXPORT_SYMBOL(dquot_set_dqinfo); 2873 2874 const struct quotactl_ops dquot_quotactl_sysfile_ops = { 2875 .quota_enable = dquot_quota_enable, 2876 .quota_disable = dquot_quota_disable, 2877 .quota_sync = dquot_quota_sync, 2878 .get_state = dquot_get_state, 2879 .set_info = dquot_set_dqinfo, 2880 .get_dqblk = dquot_get_dqblk, 2881 .get_nextdqblk = dquot_get_next_dqblk, 2882 .set_dqblk = dquot_set_dqblk 2883 }; 2884 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops); 2885 2886 static int do_proc_dqstats(const struct ctl_table *table, int write, 2887 void *buffer, size_t *lenp, loff_t *ppos) 2888 { 2889 unsigned int type = (unsigned long *)table->data - dqstats.stat; 2890 s64 value = percpu_counter_sum(&dqstats.counter[type]); 2891 2892 /* Filter negative values for non-monotonic counters */ 2893 if (value < 0 && (type == DQST_ALLOC_DQUOTS || 2894 type == DQST_FREE_DQUOTS)) 2895 value = 0; 2896 2897 /* Update global table */ 2898 dqstats.stat[type] = value; 2899 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 2900 } 2901 2902 static const struct ctl_table fs_dqstats_table[] = { 2903 { 2904 .procname = "lookups", 2905 .data = &dqstats.stat[DQST_LOOKUPS], 2906 .maxlen = sizeof(unsigned long), 2907 .mode = 0444, 2908 .proc_handler = do_proc_dqstats, 2909 }, 2910 { 2911 .procname = "drops", 2912 .data = &dqstats.stat[DQST_DROPS], 2913 .maxlen = sizeof(unsigned long), 2914 .mode = 0444, 2915 .proc_handler = do_proc_dqstats, 2916 }, 2917 { 2918 .procname = "reads", 2919 .data = &dqstats.stat[DQST_READS], 2920 .maxlen = sizeof(unsigned long), 2921 .mode = 0444, 2922 .proc_handler = do_proc_dqstats, 2923 }, 2924 { 2925 .procname = "writes", 2926 .data = &dqstats.stat[DQST_WRITES], 2927 .maxlen = sizeof(unsigned long), 2928 .mode = 0444, 2929 .proc_handler = do_proc_dqstats, 2930 }, 2931 { 2932 .procname = "cache_hits", 2933 .data = &dqstats.stat[DQST_CACHE_HITS], 2934 .maxlen = sizeof(unsigned long), 2935 .mode = 0444, 2936 .proc_handler = do_proc_dqstats, 2937 }, 2938 { 2939 .procname = "allocated_dquots", 2940 .data = &dqstats.stat[DQST_ALLOC_DQUOTS], 2941 .maxlen = sizeof(unsigned long), 2942 .mode = 0444, 2943 .proc_handler = do_proc_dqstats, 2944 }, 2945 { 2946 .procname = "free_dquots", 2947 .data = &dqstats.stat[DQST_FREE_DQUOTS], 2948 .maxlen = sizeof(unsigned long), 2949 .mode = 0444, 2950 .proc_handler = do_proc_dqstats, 2951 }, 2952 { 2953 .procname = "syncs", 2954 .data = &dqstats.stat[DQST_SYNCS], 2955 .maxlen = sizeof(unsigned long), 2956 .mode = 0444, 2957 .proc_handler = do_proc_dqstats, 2958 }, 2959 }; 2960 2961 static int __init dquot_init(void) 2962 { 2963 int i, ret; 2964 unsigned long nr_hash; 2965 struct shrinker *dqcache_shrinker; 2966 2967 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__); 2968 2969 register_sysctl_init("fs/quota", fs_dqstats_table); 2970 2971 dquot_cachep = kmem_cache_create("dquot", 2972 sizeof(struct dquot), sizeof(unsigned long) * 4, 2973 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 2974 SLAB_PANIC), 2975 NULL); 2976 2977 dquot_hash = kmalloc(PAGE_SIZE, GFP_KERNEL); 2978 if (!dquot_hash) 2979 panic("Cannot create dquot hash table"); 2980 2981 ret = percpu_counter_init_many(dqstats.counter, 0, GFP_KERNEL, 2982 _DQST_DQSTAT_LAST); 2983 if (ret) 2984 panic("Cannot create dquot stat counters"); 2985 2986 /* Find power-of-two hlist_heads which can fit into allocation */ 2987 nr_hash = PAGE_SIZE / sizeof(struct hlist_head); 2988 dq_hash_bits = ilog2(nr_hash); 2989 2990 nr_hash = 1UL << dq_hash_bits; 2991 dq_hash_mask = nr_hash - 1; 2992 for (i = 0; i < nr_hash; i++) 2993 INIT_HLIST_HEAD(dquot_hash + i); 2994 2995 pr_info("VFS: Dquot-cache hash table entries: %ld (%ld bytes)\n", 2996 nr_hash, PAGE_SIZE); 2997 2998 dqcache_shrinker = shrinker_alloc(0, "dquota-cache"); 2999 if (!dqcache_shrinker) 3000 panic("Cannot allocate dquot shrinker"); 3001 3002 dqcache_shrinker->count_objects = dqcache_shrink_count; 3003 dqcache_shrinker->scan_objects = dqcache_shrink_scan; 3004 3005 shrinker_register(dqcache_shrinker); 3006 3007 quota_unbound_wq = alloc_workqueue("quota_events_unbound", 3008 WQ_UNBOUND | WQ_MEM_RECLAIM, WQ_MAX_ACTIVE); 3009 if (!quota_unbound_wq) 3010 panic("Cannot create quota_unbound_wq\n"); 3011 3012 return 0; 3013 } 3014 fs_initcall(dquot_init); 3015