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