linux/fs/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/kmod.h>
  75#include <linux/namei.h>
  76#include <linux/buffer_head.h>
  77#include <linux/capability.h>
  78#include <linux/quotaops.h>
  79#include <linux/writeback.h> /* for inode_lock, oddly enough.. */
  80#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  81#include <net/netlink.h>
  82#include <net/genetlink.h>
  83#endif
  84
  85#include <asm/uaccess.h>
  86
  87#define __DQUOT_PARANOIA
  88
  89/*
  90 * There are three quota SMP locks. dq_list_lock protects all lists with quotas
  91 * and quota formats, dqstats structure containing statistics about the lists
  92 * dq_data_lock protects data from dq_dqb and also mem_dqinfo structures and
  93 * also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  94 * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  95 * in inode_add_bytes() and inode_sub_bytes(). dq_state_lock protects
  96 * modifications of quota state (on quotaon and quotaoff) and readers who care
  97 * about latest values take it as well.
  98 *
  99 * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock,
 100 *   dq_list_lock > dq_state_lock
 101 *
 102 * Note that some things (eg. sb pointer, type, id) doesn't change during
 103 * the life of the dquot structure and so needn't to be protected by a lock
 104 *
 105 * Any operation working on dquots via inode pointers must hold dqptr_sem.  If
 106 * operation is just reading pointers from inode (or not using them at all) the
 107 * read lock is enough. If pointers are altered function must hold write lock
 108 * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
 109 * for altering the flag i_mutex is also needed).
 110 *
 111 * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
 112 * from inodes (dquot_alloc_space() and such don't check the dq_lock).
 113 * Currently dquot is locked only when it is being read to memory (or space for
 114 * it is being allocated) on the first dqget() and when it is being released on
 115 * the last dqput(). The allocation and release oparations are serialized by
 116 * the dq_lock and by checking the use count in dquot_release().  Write
 117 * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
 118 * spinlock to internal buffers before writing.
 119 *
 120 * Lock ordering (including related VFS locks) is the following:
 121 *   i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
 122 *   dqio_mutex
 123 * The lock ordering of dqptr_sem imposed by quota code is only dqonoff_sem >
 124 * dqptr_sem. But filesystem has to count with the fact that functions such as
 125 * dquot_alloc_space() acquire dqptr_sem and they usually have to be called
 126 * from inside a transaction to keep filesystem consistency after a crash. Also
 127 * filesystems usually want to do some IO on dquot from ->mark_dirty which is
 128 * called with dqptr_sem held.
 129 * i_mutex on quota files is special (it's below dqio_mutex)
 130 */
 131
 132static DEFINE_SPINLOCK(dq_list_lock);
 133static DEFINE_SPINLOCK(dq_state_lock);
 134DEFINE_SPINLOCK(dq_data_lock);
 135
 136static char *quotatypes[] = INITQFNAMES;
 137static struct quota_format_type *quota_formats; /* List of registered formats */
 138static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
 139
 140/* SLAB cache for dquot structures */
 141static struct kmem_cache *dquot_cachep;
 142
 143int register_quota_format(struct quota_format_type *fmt)
 144{
 145        spin_lock(&dq_list_lock);
 146        fmt->qf_next = quota_formats;
 147        quota_formats = fmt;
 148        spin_unlock(&dq_list_lock);
 149        return 0;
 150}
 151
 152void unregister_quota_format(struct quota_format_type *fmt)
 153{
 154        struct quota_format_type **actqf;
 155
 156        spin_lock(&dq_list_lock);
 157        for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
 158        if (*actqf)
 159                *actqf = (*actqf)->qf_next;
 160        spin_unlock(&dq_list_lock);
 161}
 162
 163static struct quota_format_type *find_quota_format(int id)
 164{
 165        struct quota_format_type *actqf;
 166
 167        spin_lock(&dq_list_lock);
 168        for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
 169        if (!actqf || !try_module_get(actqf->qf_owner)) {
 170                int qm;
 171
 172                spin_unlock(&dq_list_lock);
 173                
 174                for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
 175                if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
 176                        return NULL;
 177
 178                spin_lock(&dq_list_lock);
 179                for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
 180                if (actqf && !try_module_get(actqf->qf_owner))
 181                        actqf = NULL;
 182        }
 183        spin_unlock(&dq_list_lock);
 184        return actqf;
 185}
 186
 187static void put_quota_format(struct quota_format_type *fmt)
 188{
 189        module_put(fmt->qf_owner);
 190}
 191
 192/*
 193 * Dquot List Management:
 194 * The quota code uses three lists for dquot management: the inuse_list,
 195 * free_dquots, and dquot_hash[] array. A single dquot structure may be
 196 * on all three lists, depending on its current state.
 197 *
 198 * All dquots are placed to the end of inuse_list when first created, and this
 199 * list is used for invalidate operation, which must look at every dquot.
 200 *
 201 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
 202 * and this list is searched whenever we need an available dquot.  Dquots are
 203 * removed from the list as soon as they are used again, and
 204 * dqstats.free_dquots gives the number of dquots on the list. When
 205 * dquot is invalidated it's completely released from memory.
 206 *
 207 * Dquots with a specific identity (device, type and id) are placed on
 208 * one of the dquot_hash[] hash chains. The provides an efficient search
 209 * mechanism to locate a specific dquot.
 210 */
 211
 212static LIST_HEAD(inuse_list);
 213static LIST_HEAD(free_dquots);
 214static unsigned int dq_hash_bits, dq_hash_mask;
 215static struct hlist_head *dquot_hash;
 216
 217struct dqstats dqstats;
 218
 219static inline unsigned int
 220hashfn(const struct super_block *sb, unsigned int id, int type)
 221{
 222        unsigned long tmp;
 223
 224        tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
 225        return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
 226}
 227
 228/*
 229 * Following list functions expect dq_list_lock to be held
 230 */
 231static inline void insert_dquot_hash(struct dquot *dquot)
 232{
 233        struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
 234        hlist_add_head(&dquot->dq_hash, head);
 235}
 236
 237static inline void remove_dquot_hash(struct dquot *dquot)
 238{
 239        hlist_del_init(&dquot->dq_hash);
 240}
 241
 242static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
 243{
 244        struct hlist_node *node;
 245        struct dquot *dquot;
 246
 247        hlist_for_each (node, dquot_hash+hashent) {
 248                dquot = hlist_entry(node, struct dquot, dq_hash);
 249                if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
 250                        return dquot;
 251        }
 252        return NODQUOT;
 253}
 254
 255/* Add a dquot to the tail of the free list */
 256static inline void put_dquot_last(struct dquot *dquot)
 257{
 258        list_add_tail(&dquot->dq_free, &free_dquots);
 259        dqstats.free_dquots++;
 260}
 261
 262static inline void remove_free_dquot(struct dquot *dquot)
 263{
 264        if (list_empty(&dquot->dq_free))
 265                return;
 266        list_del_init(&dquot->dq_free);
 267        dqstats.free_dquots--;
 268}
 269
 270static inline void put_inuse(struct dquot *dquot)
 271{
 272        /* We add to the back of inuse list so we don't have to restart
 273         * when traversing this list and we block */
 274        list_add_tail(&dquot->dq_inuse, &inuse_list);
 275        dqstats.allocated_dquots++;
 276}
 277
 278static inline void remove_inuse(struct dquot *dquot)
 279{
 280        dqstats.allocated_dquots--;
 281        list_del(&dquot->dq_inuse);
 282}
 283/*
 284 * End of list functions needing dq_list_lock
 285 */
 286
 287static void wait_on_dquot(struct dquot *dquot)
 288{
 289        mutex_lock(&dquot->dq_lock);
 290        mutex_unlock(&dquot->dq_lock);
 291}
 292
 293static inline int dquot_dirty(struct dquot *dquot)
 294{
 295        return test_bit(DQ_MOD_B, &dquot->dq_flags);
 296}
 297
 298static inline int mark_dquot_dirty(struct dquot *dquot)
 299{
 300        return dquot->dq_sb->dq_op->mark_dirty(dquot);
 301}
 302
 303int dquot_mark_dquot_dirty(struct dquot *dquot)
 304{
 305        spin_lock(&dq_list_lock);
 306        if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
 307                list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
 308                                info[dquot->dq_type].dqi_dirty_list);
 309        spin_unlock(&dq_list_lock);
 310        return 0;
 311}
 312
 313/* This function needs dq_list_lock */
 314static inline int clear_dquot_dirty(struct dquot *dquot)
 315{
 316        if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
 317                return 0;
 318        list_del_init(&dquot->dq_dirty);
 319        return 1;
 320}
 321
 322void mark_info_dirty(struct super_block *sb, int type)
 323{
 324        set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
 325}
 326EXPORT_SYMBOL(mark_info_dirty);
 327
 328/*
 329 *      Read dquot from disk and alloc space for it
 330 */
 331
 332int dquot_acquire(struct dquot *dquot)
 333{
 334        int ret = 0, ret2 = 0;
 335        struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
 336
 337        mutex_lock(&dquot->dq_lock);
 338        mutex_lock(&dqopt->dqio_mutex);
 339        if (!test_bit(DQ_READ_B, &dquot->dq_flags))
 340                ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
 341        if (ret < 0)
 342                goto out_iolock;
 343        set_bit(DQ_READ_B, &dquot->dq_flags);
 344        /* Instantiate dquot if needed */
 345        if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
 346                ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
 347                /* Write the info if needed */
 348                if (info_dirty(&dqopt->info[dquot->dq_type]))
 349                        ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
 350                if (ret < 0)
 351                        goto out_iolock;
 352                if (ret2 < 0) {
 353                        ret = ret2;
 354                        goto out_iolock;
 355                }
 356        }
 357        set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
 358out_iolock:
 359        mutex_unlock(&dqopt->dqio_mutex);
 360        mutex_unlock(&dquot->dq_lock);
 361        return ret;
 362}
 363
 364/*
 365 *      Write dquot to disk
 366 */
 367int dquot_commit(struct dquot *dquot)
 368{
 369        int ret = 0, ret2 = 0;
 370        struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
 371
 372        mutex_lock(&dqopt->dqio_mutex);
 373        spin_lock(&dq_list_lock);
 374        if (!clear_dquot_dirty(dquot)) {
 375                spin_unlock(&dq_list_lock);
 376                goto out_sem;
 377        }
 378        spin_unlock(&dq_list_lock);
 379        /* Inactive dquot can be only if there was error during read/init
 380         * => we have better not writing it */
 381        if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
 382                ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
 383                if (info_dirty(&dqopt->info[dquot->dq_type]))
 384                        ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
 385                if (ret >= 0)
 386                        ret = ret2;
 387        }
 388out_sem:
 389        mutex_unlock(&dqopt->dqio_mutex);
 390        return ret;
 391}
 392
 393/*
 394 *      Release dquot
 395 */
 396int dquot_release(struct dquot *dquot)
 397{
 398        int ret = 0, ret2 = 0;
 399        struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
 400
 401        mutex_lock(&dquot->dq_lock);
 402        /* Check whether we are not racing with some other dqget() */
 403        if (atomic_read(&dquot->dq_count) > 1)
 404                goto out_dqlock;
 405        mutex_lock(&dqopt->dqio_mutex);
 406        if (dqopt->ops[dquot->dq_type]->release_dqblk) {
 407                ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
 408                /* Write the info */
 409                if (info_dirty(&dqopt->info[dquot->dq_type]))
 410                        ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
 411                if (ret >= 0)
 412                        ret = ret2;
 413        }
 414        clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
 415        mutex_unlock(&dqopt->dqio_mutex);
 416out_dqlock:
 417        mutex_unlock(&dquot->dq_lock);
 418        return ret;
 419}
 420
 421void dquot_destroy(struct dquot *dquot)
 422{
 423        kmem_cache_free(dquot_cachep, dquot);
 424}
 425EXPORT_SYMBOL(dquot_destroy);
 426
 427static inline void do_destroy_dquot(struct dquot *dquot)
 428{
 429        dquot->dq_sb->dq_op->destroy_dquot(dquot);
 430}
 431
 432/* Invalidate all dquots on the list. Note that this function is called after
 433 * quota is disabled and pointers from inodes removed so there cannot be new
 434 * quota users. There can still be some users of quotas due to inodes being
 435 * just deleted or pruned by prune_icache() (those are not attached to any
 436 * list) or parallel quotactl call. We have to wait for such users.
 437 */
 438static void invalidate_dquots(struct super_block *sb, int type)
 439{
 440        struct dquot *dquot, *tmp;
 441
 442restart:
 443        spin_lock(&dq_list_lock);
 444        list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
 445                if (dquot->dq_sb != sb)
 446                        continue;
 447                if (dquot->dq_type != type)
 448                        continue;
 449                /* Wait for dquot users */
 450                if (atomic_read(&dquot->dq_count)) {
 451                        DEFINE_WAIT(wait);
 452
 453                        atomic_inc(&dquot->dq_count);
 454                        prepare_to_wait(&dquot->dq_wait_unused, &wait,
 455                                        TASK_UNINTERRUPTIBLE);
 456                        spin_unlock(&dq_list_lock);
 457                        /* Once dqput() wakes us up, we know it's time to free
 458                         * the dquot.
 459                         * IMPORTANT: we rely on the fact that there is always
 460                         * at most one process waiting for dquot to free.
 461                         * Otherwise dq_count would be > 1 and we would never
 462                         * wake up.
 463                         */
 464                        if (atomic_read(&dquot->dq_count) > 1)
 465                                schedule();
 466                        finish_wait(&dquot->dq_wait_unused, &wait);
 467                        dqput(dquot);
 468                        /* At this moment dquot() need not exist (it could be
 469                         * reclaimed by prune_dqcache(). Hence we must
 470                         * restart. */
 471                        goto restart;
 472                }
 473                /*
 474                 * Quota now has no users and it has been written on last
 475                 * dqput()
 476                 */
 477                remove_dquot_hash(dquot);
 478                remove_free_dquot(dquot);
 479                remove_inuse(dquot);
 480                do_destroy_dquot(dquot);
 481        }
 482        spin_unlock(&dq_list_lock);
 483}
 484
 485/* Call callback for every active dquot on given filesystem */
 486int dquot_scan_active(struct super_block *sb,
 487                      int (*fn)(struct dquot *dquot, unsigned long priv),
 488                      unsigned long priv)
 489{
 490        struct dquot *dquot, *old_dquot = NULL;
 491        int ret = 0;
 492
 493        mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
 494        spin_lock(&dq_list_lock);
 495        list_for_each_entry(dquot, &inuse_list, dq_inuse) {
 496                if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
 497                        continue;
 498                if (dquot->dq_sb != sb)
 499                        continue;
 500                /* Now we have active dquot so we can just increase use count */
 501                atomic_inc(&dquot->dq_count);
 502                dqstats.lookups++;
 503                spin_unlock(&dq_list_lock);
 504                dqput(old_dquot);
 505                old_dquot = dquot;
 506                ret = fn(dquot, priv);
 507                if (ret < 0)
 508                        goto out;
 509                spin_lock(&dq_list_lock);
 510                /* We are safe to continue now because our dquot could not
 511                 * be moved out of the inuse list while we hold the reference */
 512        }
 513        spin_unlock(&dq_list_lock);
 514out:
 515        dqput(old_dquot);
 516        mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
 517        return ret;
 518}
 519
 520int vfs_quota_sync(struct super_block *sb, int type)
 521{
 522        struct list_head *dirty;
 523        struct dquot *dquot;
 524        struct quota_info *dqopt = sb_dqopt(sb);
 525        int cnt;
 526
 527        mutex_lock(&dqopt->dqonoff_mutex);
 528        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
 529                if (type != -1 && cnt != type)
 530                        continue;
 531                if (!sb_has_quota_active(sb, cnt))
 532                        continue;
 533                spin_lock(&dq_list_lock);
 534                dirty = &dqopt->info[cnt].dqi_dirty_list;
 535                while (!list_empty(dirty)) {
 536                        dquot = list_first_entry(dirty, struct dquot, dq_dirty);
 537                        /* Dirty and inactive can be only bad dquot... */
 538                        if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
 539                                clear_dquot_dirty(dquot);
 540                                continue;
 541                        }
 542                        /* Now we have active dquot from which someone is
 543                         * holding reference so we can safely just increase
 544                         * use count */
 545                        atomic_inc(&dquot->dq_count);
 546                        dqstats.lookups++;
 547                        spin_unlock(&dq_list_lock);
 548                        sb->dq_op->write_dquot(dquot);
 549                        dqput(dquot);
 550                        spin_lock(&dq_list_lock);
 551                }
 552                spin_unlock(&dq_list_lock);
 553        }
 554
 555        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
 556                if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
 557                    && info_dirty(&dqopt->info[cnt]))
 558                        sb->dq_op->write_info(sb, cnt);
 559        spin_lock(&dq_list_lock);
 560        dqstats.syncs++;
 561        spin_unlock(&dq_list_lock);
 562        mutex_unlock(&dqopt->dqonoff_mutex);
 563
 564        return 0;
 565}
 566
 567/* Free unused dquots from cache */
 568static void prune_dqcache(int count)
 569{
 570        struct list_head *head;
 571        struct dquot *dquot;
 572
 573        head = free_dquots.prev;
 574        while (head != &free_dquots && count) {
 575                dquot = list_entry(head, struct dquot, dq_free);
 576                remove_dquot_hash(dquot);
 577                remove_free_dquot(dquot);
 578                remove_inuse(dquot);
 579                do_destroy_dquot(dquot);
 580                count--;
 581                head = free_dquots.prev;
 582        }
 583}
 584
 585/*
 586 * This is called from kswapd when we think we need some
 587 * more memory
 588 */
 589
 590static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
 591{
 592        if (nr) {
 593                spin_lock(&dq_list_lock);
 594                prune_dqcache(nr);
 595                spin_unlock(&dq_list_lock);
 596        }
 597        return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
 598}
 599
 600static struct shrinker dqcache_shrinker = {
 601        .shrink = shrink_dqcache_memory,
 602        .seeks = DEFAULT_SEEKS,
 603};
 604
 605/*
 606 * Put reference to dquot
 607 * NOTE: If you change this function please check whether dqput_blocks() works right...
 608 */
 609void dqput(struct dquot *dquot)
 610{
 611        int ret;
 612
 613        if (!dquot)
 614                return;
 615#ifdef __DQUOT_PARANOIA
 616        if (!atomic_read(&dquot->dq_count)) {
 617                printk("VFS: dqput: trying to free free dquot\n");
 618                printk("VFS: device %s, dquot of %s %d\n",
 619                        dquot->dq_sb->s_id,
 620                        quotatypes[dquot->dq_type],
 621                        dquot->dq_id);
 622                BUG();
 623        }
 624#endif
 625        
 626        spin_lock(&dq_list_lock);
 627        dqstats.drops++;
 628        spin_unlock(&dq_list_lock);
 629we_slept:
 630        spin_lock(&dq_list_lock);
 631        if (atomic_read(&dquot->dq_count) > 1) {
 632                /* We have more than one user... nothing to do */
 633                atomic_dec(&dquot->dq_count);
 634                /* Releasing dquot during quotaoff phase? */
 635                if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_type) &&
 636                    atomic_read(&dquot->dq_count) == 1)
 637                        wake_up(&dquot->dq_wait_unused);
 638                spin_unlock(&dq_list_lock);
 639                return;
 640        }
 641        /* Need to release dquot? */
 642        if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
 643                spin_unlock(&dq_list_lock);
 644                /* Commit dquot before releasing */
 645                ret = dquot->dq_sb->dq_op->write_dquot(dquot);
 646                if (ret < 0) {
 647                        printk(KERN_ERR "VFS: cannot write quota structure on "
 648                                "device %s (error %d). Quota may get out of "
 649                                "sync!\n", dquot->dq_sb->s_id, ret);
 650                        /*
 651                         * We clear dirty bit anyway, so that we avoid
 652                         * infinite loop here
 653                         */
 654                        spin_lock(&dq_list_lock);
 655                        clear_dquot_dirty(dquot);
 656                        spin_unlock(&dq_list_lock);
 657                }
 658                goto we_slept;
 659        }
 660        /* Clear flag in case dquot was inactive (something bad happened) */
 661        clear_dquot_dirty(dquot);
 662        if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
 663                spin_unlock(&dq_list_lock);
 664                dquot->dq_sb->dq_op->release_dquot(dquot);
 665                goto we_slept;
 666        }
 667        atomic_dec(&dquot->dq_count);
 668#ifdef __DQUOT_PARANOIA
 669        /* sanity check */
 670        BUG_ON(!list_empty(&dquot->dq_free));
 671#endif
 672        put_dquot_last(dquot);
 673        spin_unlock(&dq_list_lock);
 674}
 675
 676struct dquot *dquot_alloc(struct super_block *sb, int type)
 677{
 678        return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
 679}
 680EXPORT_SYMBOL(dquot_alloc);
 681
 682static struct dquot *get_empty_dquot(struct super_block *sb, int type)
 683{
 684        struct dquot *dquot;
 685
 686        dquot = sb->dq_op->alloc_dquot(sb, type);
 687        if(!dquot)
 688                return NODQUOT;
 689
 690        mutex_init(&dquot->dq_lock);
 691        INIT_LIST_HEAD(&dquot->dq_free);
 692        INIT_LIST_HEAD(&dquot->dq_inuse);
 693        INIT_HLIST_NODE(&dquot->dq_hash);
 694        INIT_LIST_HEAD(&dquot->dq_dirty);
 695        init_waitqueue_head(&dquot->dq_wait_unused);
 696        dquot->dq_sb = sb;
 697        dquot->dq_type = type;
 698        atomic_set(&dquot->dq_count, 1);
 699
 700        return dquot;
 701}
 702
 703/*
 704 * Get reference to dquot
 705 *
 706 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
 707 * destroying our dquot by:
 708 *   a) checking for quota flags under dq_list_lock and
 709 *   b) getting a reference to dquot before we release dq_list_lock
 710 */
 711struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
 712{
 713        unsigned int hashent = hashfn(sb, id, type);
 714        struct dquot *dquot = NODQUOT, *empty = NODQUOT;
 715
 716        if (!sb_has_quota_active(sb, type))
 717                return NODQUOT;
 718we_slept:
 719        spin_lock(&dq_list_lock);
 720        spin_lock(&dq_state_lock);
 721        if (!sb_has_quota_active(sb, type)) {
 722                spin_unlock(&dq_state_lock);
 723                spin_unlock(&dq_list_lock);
 724                goto out;
 725        }
 726        spin_unlock(&dq_state_lock);
 727
 728        if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
 729                if (empty == NODQUOT) {
 730                        spin_unlock(&dq_list_lock);
 731                        if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
 732                                schedule();     /* Try to wait for a moment... */
 733                        goto we_slept;
 734                }
 735                dquot = empty;
 736                empty = NODQUOT;
 737                dquot->dq_id = id;
 738                /* all dquots go on the inuse_list */
 739                put_inuse(dquot);
 740                /* hash it first so it can be found */
 741                insert_dquot_hash(dquot);
 742                dqstats.lookups++;
 743                spin_unlock(&dq_list_lock);
 744        } else {
 745                if (!atomic_read(&dquot->dq_count))
 746                        remove_free_dquot(dquot);
 747                atomic_inc(&dquot->dq_count);
 748                dqstats.cache_hits++;
 749                dqstats.lookups++;
 750                spin_unlock(&dq_list_lock);
 751        }
 752        /* Wait for dq_lock - after this we know that either dquot_release() is already
 753         * finished or it will be canceled due to dq_count > 1 test */
 754        wait_on_dquot(dquot);
 755        /* Read the dquot and instantiate it (everything done only if needed) */
 756        if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
 757                dqput(dquot);
 758                dquot = NODQUOT;
 759                goto out;
 760        }
 761#ifdef __DQUOT_PARANOIA
 762        BUG_ON(!dquot->dq_sb);  /* Has somebody invalidated entry under us? */
 763#endif
 764out:
 765        if (empty)
 766                do_destroy_dquot(empty);
 767
 768        return dquot;
 769}
 770
 771static int dqinit_needed(struct inode *inode, int type)
 772{
 773        int cnt;
 774
 775        if (IS_NOQUOTA(inode))
 776                return 0;
 777        if (type != -1)
 778                return inode->i_dquot[type] == NODQUOT;
 779        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
 780                if (inode->i_dquot[cnt] == NODQUOT)
 781                        return 1;
 782        return 0;
 783}
 784
 785/* This routine is guarded by dqonoff_mutex mutex */
 786static void add_dquot_ref(struct super_block *sb, int type)
 787{
 788        struct inode *inode, *old_inode = NULL;
 789
 790        spin_lock(&inode_lock);
 791        list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
 792                if (!atomic_read(&inode->i_writecount))
 793                        continue;
 794                if (!dqinit_needed(inode, type))
 795                        continue;
 796                if (inode->i_state & (I_FREEING|I_WILL_FREE))
 797                        continue;
 798
 799                __iget(inode);
 800                spin_unlock(&inode_lock);
 801
 802                iput(old_inode);
 803                sb->dq_op->initialize(inode, type);
 804                /* We hold a reference to 'inode' so it couldn't have been
 805                 * removed from s_inodes list while we dropped the inode_lock.
 806                 * We cannot iput the inode now as we can be holding the last
 807                 * reference and we cannot iput it under inode_lock. So we
 808                 * keep the reference and iput it later. */
 809                old_inode = inode;
 810                spin_lock(&inode_lock);
 811        }
 812        spin_unlock(&inode_lock);
 813        iput(old_inode);
 814}
 815
 816/* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
 817static inline int dqput_blocks(struct dquot *dquot)
 818{
 819        if (atomic_read(&dquot->dq_count) <= 1)
 820                return 1;
 821        return 0;
 822}
 823
 824/* Remove references to dquots from inode - add dquot to list for freeing if needed */
 825/* We can't race with anybody because we hold dqptr_sem for writing... */
 826static int remove_inode_dquot_ref(struct inode *inode, int type,
 827                                  struct list_head *tofree_head)
 828{
 829        struct dquot *dquot = inode->i_dquot[type];
 830
 831        inode->i_dquot[type] = NODQUOT;
 832        if (dquot != NODQUOT) {
 833                if (dqput_blocks(dquot)) {
 834#ifdef __DQUOT_PARANOIA
 835                        if (atomic_read(&dquot->dq_count) != 1)
 836                                printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
 837#endif
 838                        spin_lock(&dq_list_lock);
 839                        list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
 840                        spin_unlock(&dq_list_lock);
 841                        return 1;
 842                }
 843                else
 844                        dqput(dquot);   /* We have guaranteed we won't block */
 845        }
 846        return 0;
 847}
 848
 849/* Free list of dquots - called from inode.c */
 850/* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
 851static void put_dquot_list(struct list_head *tofree_head)
 852{
 853        struct list_head *act_head;
 854        struct dquot *dquot;
 855
 856        act_head = tofree_head->next;
 857        /* So now we have dquots on the list... Just free them */
 858        while (act_head != tofree_head) {
 859                dquot = list_entry(act_head, struct dquot, dq_free);
 860                act_head = act_head->next;
 861                list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
 862                dqput(dquot);
 863        }
 864}
 865
 866static void remove_dquot_ref(struct super_block *sb, int type,
 867                struct list_head *tofree_head)
 868{
 869        struct inode *inode;
 870
 871        spin_lock(&inode_lock);
 872        list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
 873                if (!IS_NOQUOTA(inode))
 874                        remove_inode_dquot_ref(inode, type, tofree_head);
 875        }
 876        spin_unlock(&inode_lock);
 877}
 878
 879/* Gather all references from inodes and drop them */
 880static void drop_dquot_ref(struct super_block *sb, int type)
 881{
 882        LIST_HEAD(tofree_head);
 883
 884        if (sb->dq_op) {
 885                down_write(&sb_dqopt(sb)->dqptr_sem);
 886                remove_dquot_ref(sb, type, &tofree_head);
 887                up_write(&sb_dqopt(sb)->dqptr_sem);
 888                put_dquot_list(&tofree_head);
 889        }
 890}
 891
 892static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
 893{
 894        dquot->dq_dqb.dqb_curinodes += number;
 895}
 896
 897static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
 898{
 899        dquot->dq_dqb.dqb_curspace += number;
 900}
 901
 902static inline void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
 903{
 904        if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
 905            dquot->dq_dqb.dqb_curinodes >= number)
 906                dquot->dq_dqb.dqb_curinodes -= number;
 907        else
 908                dquot->dq_dqb.dqb_curinodes = 0;
 909        if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
 910                dquot->dq_dqb.dqb_itime = (time_t) 0;
 911        clear_bit(DQ_INODES_B, &dquot->dq_flags);
 912}
 913
 914static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
 915{
 916        if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
 917            dquot->dq_dqb.dqb_curspace >= number)
 918                dquot->dq_dqb.dqb_curspace -= number;
 919        else
 920                dquot->dq_dqb.dqb_curspace = 0;
 921        if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
 922                dquot->dq_dqb.dqb_btime = (time_t) 0;
 923        clear_bit(DQ_BLKS_B, &dquot->dq_flags);
 924}
 925
 926static int warning_issued(struct dquot *dquot, const int warntype)
 927{
 928        int flag = (warntype == QUOTA_NL_BHARDWARN ||
 929                warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
 930                ((warntype == QUOTA_NL_IHARDWARN ||
 931                warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
 932
 933        if (!flag)
 934                return 0;
 935        return test_and_set_bit(flag, &dquot->dq_flags);
 936}
 937
 938#ifdef CONFIG_PRINT_QUOTA_WARNING
 939static int flag_print_warnings = 1;
 940
 941static inline int need_print_warning(struct dquot *dquot)
 942{
 943        if (!flag_print_warnings)
 944                return 0;
 945
 946        switch (dquot->dq_type) {
 947                case USRQUOTA:
 948                        return current_fsuid() == dquot->dq_id;
 949                case GRPQUOTA:
 950                        return in_group_p(dquot->dq_id);
 951        }
 952        return 0;
 953}
 954
 955/* Print warning to user which exceeded quota */
 956static void print_warning(struct dquot *dquot, const int warntype)
 957{
 958        char *msg = NULL;
 959        struct tty_struct *tty;
 960
 961        if (warntype == QUOTA_NL_IHARDBELOW ||
 962            warntype == QUOTA_NL_ISOFTBELOW ||
 963            warntype == QUOTA_NL_BHARDBELOW ||
 964            warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
 965                return;
 966
 967        tty = get_current_tty();
 968        if (!tty)
 969                return;
 970        tty_write_message(tty, dquot->dq_sb->s_id);
 971        if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
 972                tty_write_message(tty, ": warning, ");
 973        else
 974                tty_write_message(tty, ": write failed, ");
 975        tty_write_message(tty, quotatypes[dquot->dq_type]);
 976        switch (warntype) {
 977                case QUOTA_NL_IHARDWARN:
 978                        msg = " file limit reached.\r\n";
 979                        break;
 980                case QUOTA_NL_ISOFTLONGWARN:
 981                        msg = " file quota exceeded too long.\r\n";
 982                        break;
 983                case QUOTA_NL_ISOFTWARN:
 984                        msg = " file quota exceeded.\r\n";
 985                        break;
 986                case QUOTA_NL_BHARDWARN:
 987                        msg = " block limit reached.\r\n";
 988                        break;
 989                case QUOTA_NL_BSOFTLONGWARN:
 990                        msg = " block quota exceeded too long.\r\n";
 991                        break;
 992                case QUOTA_NL_BSOFTWARN:
 993                        msg = " block quota exceeded.\r\n";
 994                        break;
 995        }
 996        tty_write_message(tty, msg);
 997        tty_kref_put(tty);
 998}
 999#endif
1000
1001#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1002
1003/* Netlink family structure for quota */
1004static struct genl_family quota_genl_family = {
1005        .id = GENL_ID_GENERATE,
1006        .hdrsize = 0,
1007        .name = "VFS_DQUOT",
1008        .version = 1,
1009        .maxattr = QUOTA_NL_A_MAX,
1010};
1011
1012/* Send warning to userspace about user which exceeded quota */
1013static void send_warning(const struct dquot *dquot, const char warntype)
1014{
1015        static atomic_t seq;
1016        struct sk_buff *skb;
1017        void *msg_head;
1018        int ret;
1019        int msg_size = 4 * nla_total_size(sizeof(u32)) +
1020                       2 * nla_total_size(sizeof(u64));
1021
1022        /* We have to allocate using GFP_NOFS as we are called from a
1023         * filesystem performing write and thus further recursion into
1024         * the fs to free some data could cause deadlocks. */
1025        skb = genlmsg_new(msg_size, GFP_NOFS);
1026        if (!skb) {
1027                printk(KERN_ERR
1028                  "VFS: Not enough memory to send quota warning.\n");
1029                return;
1030        }
1031        msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
1032                        &quota_genl_family, 0, QUOTA_NL_C_WARNING);
1033        if (!msg_head) {
1034                printk(KERN_ERR
1035                  "VFS: Cannot store netlink header in quota warning.\n");
1036                goto err_out;
1037        }
1038        ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
1039        if (ret)
1040                goto attr_err_out;
1041        ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
1042        if (ret)
1043                goto attr_err_out;
1044        ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
1045        if (ret)
1046                goto attr_err_out;
1047        ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
1048                MAJOR(dquot->dq_sb->s_dev));
1049        if (ret)
1050                goto attr_err_out;
1051        ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
1052                MINOR(dquot->dq_sb->s_dev));
1053        if (ret)
1054                goto attr_err_out;
1055        ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
1056        if (ret)
1057                goto attr_err_out;
1058        genlmsg_end(skb, msg_head);
1059
1060        ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
1061        if (ret < 0 && ret != -ESRCH)
1062                printk(KERN_ERR
1063                        "VFS: Failed to send notification message: %d\n", ret);
1064        return;
1065attr_err_out:
1066        printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
1067err_out:
1068        kfree_skb(skb);
1069}
1070#endif
1071
1072static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
1073{
1074        int i;
1075
1076        for (i = 0; i < MAXQUOTAS; i++)
1077                if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
1078                    !warning_issued(dquots[i], warntype[i])) {
1079#ifdef CONFIG_PRINT_QUOTA_WARNING
1080                        print_warning(dquots[i], warntype[i]);
1081#endif
1082#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1083                        send_warning(dquots[i], warntype[i]);
1084#endif
1085                }
1086}
1087
1088static inline char ignore_hardlimit(struct dquot *dquot)
1089{
1090        struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
1091
1092        return capable(CAP_SYS_RESOURCE) &&
1093            (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
1094}
1095
1096/* needs dq_data_lock */
1097static int check_idq(struct dquot *dquot, qsize_t inodes, char *warntype)
1098{
1099        *warntype = QUOTA_NL_NOWARN;
1100        if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1101            test_bit(DQ_FAKE_B, &dquot->dq_flags))
1102                return QUOTA_OK;
1103
1104        if (dquot->dq_dqb.dqb_ihardlimit &&
1105           (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1106            !ignore_hardlimit(dquot)) {
1107                *warntype = QUOTA_NL_IHARDWARN;
1108                return NO_QUOTA;
1109        }
1110
1111        if (dquot->dq_dqb.dqb_isoftlimit &&
1112           (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1113            dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1114            !ignore_hardlimit(dquot)) {
1115                *warntype = QUOTA_NL_ISOFTLONGWARN;
1116                return NO_QUOTA;
1117        }
1118
1119        if (dquot->dq_dqb.dqb_isoftlimit &&
1120           (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1121            dquot->dq_dqb.dqb_itime == 0) {
1122                *warntype = QUOTA_NL_ISOFTWARN;
1123                dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1124        }
1125
1126        return QUOTA_OK;
1127}
1128
1129/* needs dq_data_lock */
1130static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1131{
1132        *warntype = QUOTA_NL_NOWARN;
1133        if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type) ||
1134            test_bit(DQ_FAKE_B, &dquot->dq_flags))
1135                return QUOTA_OK;
1136
1137        if (dquot->dq_dqb.dqb_bhardlimit &&
1138            dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bhardlimit &&
1139            !ignore_hardlimit(dquot)) {
1140                if (!prealloc)
1141                        *warntype = QUOTA_NL_BHARDWARN;
1142                return NO_QUOTA;
1143        }
1144
1145        if (dquot->dq_dqb.dqb_bsoftlimit &&
1146            dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1147            dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1148            !ignore_hardlimit(dquot)) {
1149                if (!prealloc)
1150                        *warntype = QUOTA_NL_BSOFTLONGWARN;
1151                return NO_QUOTA;
1152        }
1153
1154        if (dquot->dq_dqb.dqb_bsoftlimit &&
1155            dquot->dq_dqb.dqb_curspace + space > dquot->dq_dqb.dqb_bsoftlimit &&
1156            dquot->dq_dqb.dqb_btime == 0) {
1157                if (!prealloc) {
1158                        *warntype = QUOTA_NL_BSOFTWARN;
1159                        dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1160                }
1161                else
1162                        /*
1163                         * We don't allow preallocation to exceed softlimit so exceeding will
1164                         * be always printed
1165                         */
1166                        return NO_QUOTA;
1167        }
1168
1169        return QUOTA_OK;
1170}
1171
1172static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1173{
1174        if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1175            dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1176            !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_type))
1177                return QUOTA_NL_NOWARN;
1178
1179        if (dquot->dq_dqb.dqb_curinodes - inodes <= dquot->dq_dqb.dqb_isoftlimit)
1180                return QUOTA_NL_ISOFTBELOW;
1181        if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1182            dquot->dq_dqb.dqb_curinodes - inodes < dquot->dq_dqb.dqb_ihardlimit)
1183                return QUOTA_NL_IHARDBELOW;
1184        return QUOTA_NL_NOWARN;
1185}
1186
1187static int info_bdq_free(struct dquot *dquot, qsize_t space)
1188{
1189        if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1190            dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1191                return QUOTA_NL_NOWARN;
1192
1193        if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1194                return QUOTA_NL_BSOFTBELOW;
1195        if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1196            dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1197                return QUOTA_NL_BHARDBELOW;
1198        return QUOTA_NL_NOWARN;
1199}
1200/*
1201 *      Initialize quota pointers in inode
1202 *      We do things in a bit complicated way but by that we avoid calling
1203 *      dqget() and thus filesystem callbacks under dqptr_sem.
1204 */
1205int dquot_initialize(struct inode *inode, int type)
1206{
1207        unsigned int id = 0;
1208        int cnt, ret = 0;
1209        struct dquot *got[MAXQUOTAS] = { NODQUOT, NODQUOT };
1210        struct super_block *sb = inode->i_sb;
1211
1212        /* First test before acquiring mutex - solves deadlocks when we
1213         * re-enter the quota code and are already holding the mutex */
1214        if (IS_NOQUOTA(inode))
1215                return 0;
1216
1217        /* First get references to structures we might need. */
1218        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1219                if (type != -1 && cnt != type)
1220                        continue;
1221                switch (cnt) {
1222                case USRQUOTA:
1223                        id = inode->i_uid;
1224                        break;
1225                case GRPQUOTA:
1226                        id = inode->i_gid;
1227                        break;
1228                }
1229                got[cnt] = dqget(sb, id, cnt);
1230        }
1231
1232        down_write(&sb_dqopt(sb)->dqptr_sem);
1233        /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
1234        if (IS_NOQUOTA(inode))
1235                goto out_err;
1236        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1237                if (type != -1 && cnt != type)
1238                        continue;
1239                /* Avoid races with quotaoff() */
1240                if (!sb_has_quota_active(sb, cnt))
1241                        continue;
1242                if (inode->i_dquot[cnt] == NODQUOT) {
1243                        inode->i_dquot[cnt] = got[cnt];
1244                        got[cnt] = NODQUOT;
1245                }
1246        }
1247out_err:
1248        up_write(&sb_dqopt(sb)->dqptr_sem);
1249        /* Drop unused references */
1250        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1251                dqput(got[cnt]);
1252        return ret;
1253}
1254
1255/*
1256 *      Release all quotas referenced by inode
1257 */
1258int dquot_drop(struct inode *inode)
1259{
1260        int cnt;
1261        struct dquot *put[MAXQUOTAS];
1262
1263        down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1264        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1265                put[cnt] = inode->i_dquot[cnt];
1266                inode->i_dquot[cnt] = NODQUOT;
1267        }
1268        up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1269
1270        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1271                dqput(put[cnt]);
1272        return 0;
1273}
1274
1275/* Wrapper to remove references to quota structures from inode */
1276void vfs_dq_drop(struct inode *inode)
1277{
1278        /* Here we can get arbitrary inode from clear_inode() so we have
1279         * to be careful. OTOH we don't need locking as quota operations
1280         * are allowed to change only at mount time */
1281        if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op
1282            && inode->i_sb->dq_op->drop) {
1283                int cnt;
1284                /* Test before calling to rule out calls from proc and such
1285                 * where we are not allowed to block. Note that this is
1286                 * actually reliable test even without the lock - the caller
1287                 * must assure that nobody can come after the DQUOT_DROP and
1288                 * add quota pointers back anyway */
1289                for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1290                        if (inode->i_dquot[cnt] != NODQUOT)
1291                                break;
1292                if (cnt < MAXQUOTAS)
1293                        inode->i_sb->dq_op->drop(inode);
1294        }
1295}
1296
1297/*
1298 * Following four functions update i_blocks+i_bytes fields and
1299 * quota information (together with appropriate checks)
1300 * NOTE: We absolutely rely on the fact that caller dirties
1301 * the inode (usually macros in quotaops.h care about this) and
1302 * holds a handle for the current transaction so that dquot write and
1303 * inode write go into the same transaction.
1304 */
1305
1306/*
1307 * This operation can block, but only after everything is updated
1308 */
1309int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1310{
1311        int cnt, ret = NO_QUOTA;
1312        char warntype[MAXQUOTAS];
1313
1314        /* First test before acquiring mutex - solves deadlocks when we
1315         * re-enter the quota code and are already holding the mutex */
1316        if (IS_NOQUOTA(inode)) {
1317out_add:
1318                inode_add_bytes(inode, number);
1319                return QUOTA_OK;
1320        }
1321        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1322                warntype[cnt] = QUOTA_NL_NOWARN;
1323
1324        down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1325        if (IS_NOQUOTA(inode)) {        /* Now we can do reliable test... */
1326                up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1327                goto out_add;
1328        }
1329        spin_lock(&dq_data_lock);
1330        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1331                if (inode->i_dquot[cnt] == NODQUOT)
1332                        continue;
1333                if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1334                        goto warn_put_all;
1335        }
1336        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1337                if (inode->i_dquot[cnt] == NODQUOT)
1338                        continue;
1339                dquot_incr_space(inode->i_dquot[cnt], number);
1340        }
1341        inode_add_bytes(inode, number);
1342        ret = QUOTA_OK;
1343warn_put_all:
1344        spin_unlock(&dq_data_lock);
1345        if (ret == QUOTA_OK)
1346                /* Dirtify all the dquots - this can block when journalling */
1347                for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1348                        if (inode->i_dquot[cnt])
1349                                mark_dquot_dirty(inode->i_dquot[cnt]);
1350        flush_warnings(inode->i_dquot, warntype);
1351        up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1352        return ret;
1353}
1354
1355/*
1356 * This operation can block, but only after everything is updated
1357 */
1358int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1359{
1360        int cnt, ret = NO_QUOTA;
1361        char warntype[MAXQUOTAS];
1362
1363        /* First test before acquiring mutex - solves deadlocks when we
1364         * re-enter the quota code and are already holding the mutex */
1365        if (IS_NOQUOTA(inode))
1366                return QUOTA_OK;
1367        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1368                warntype[cnt] = QUOTA_NL_NOWARN;
1369        down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1370        if (IS_NOQUOTA(inode)) {
1371                up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1372                return QUOTA_OK;
1373        }
1374        spin_lock(&dq_data_lock);
1375        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1376                if (inode->i_dquot[cnt] == NODQUOT)
1377                        continue;
1378                if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1379                        goto warn_put_all;
1380        }
1381
1382        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1383                if (inode->i_dquot[cnt] == NODQUOT)
1384                        continue;
1385                dquot_incr_inodes(inode->i_dquot[cnt], number);
1386        }
1387        ret = QUOTA_OK;
1388warn_put_all:
1389        spin_unlock(&dq_data_lock);
1390        if (ret == QUOTA_OK)
1391                /* Dirtify all the dquots - this can block when journalling */
1392                for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1393                        if (inode->i_dquot[cnt])
1394                                mark_dquot_dirty(inode->i_dquot[cnt]);
1395        flush_warnings(inode->i_dquot, warntype);
1396        up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1397        return ret;
1398}
1399
1400/*
1401 * This operation can block, but only after everything is updated
1402 */
1403int dquot_free_space(struct inode *inode, qsize_t number)
1404{
1405        unsigned int cnt;
1406        char warntype[MAXQUOTAS];
1407
1408        /* First test before acquiring mutex - solves deadlocks when we
1409         * re-enter the quota code and are already holding the mutex */
1410        if (IS_NOQUOTA(inode)) {
1411out_sub:
1412                inode_sub_bytes(inode, number);
1413                return QUOTA_OK;
1414        }
1415
1416        down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1417        /* Now recheck reliably when holding dqptr_sem */
1418        if (IS_NOQUOTA(inode)) {
1419                up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1420                goto out_sub;
1421        }
1422        spin_lock(&dq_data_lock);
1423        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1424                if (inode->i_dquot[cnt] == NODQUOT)
1425                        continue;
1426                warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1427                dquot_decr_space(inode->i_dquot[cnt], number);
1428        }
1429        inode_sub_bytes(inode, number);
1430        spin_unlock(&dq_data_lock);
1431        /* Dirtify all the dquots - this can block when journalling */
1432        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1433                if (inode->i_dquot[cnt])
1434                        mark_dquot_dirty(inode->i_dquot[cnt]);
1435        flush_warnings(inode->i_dquot, warntype);
1436        up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1437        return QUOTA_OK;
1438}
1439
1440/*
1441 * This operation can block, but only after everything is updated
1442 */
1443int dquot_free_inode(const struct inode *inode, qsize_t number)
1444{
1445        unsigned int cnt;
1446        char warntype[MAXQUOTAS];
1447
1448        /* First test before acquiring mutex - solves deadlocks when we
1449         * re-enter the quota code and are already holding the mutex */
1450        if (IS_NOQUOTA(inode))
1451                return QUOTA_OK;
1452
1453        down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1454        /* Now recheck reliably when holding dqptr_sem */
1455        if (IS_NOQUOTA(inode)) {
1456                up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1457                return QUOTA_OK;
1458        }
1459        spin_lock(&dq_data_lock);
1460        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1461                if (inode->i_dquot[cnt] == NODQUOT)
1462                        continue;
1463                warntype[cnt] = info_idq_free(inode->i_dquot[cnt], number);
1464                dquot_decr_inodes(inode->i_dquot[cnt], number);
1465        }
1466        spin_unlock(&dq_data_lock);
1467        /* Dirtify all the dquots - this can block when journalling */
1468        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1469                if (inode->i_dquot[cnt])
1470                        mark_dquot_dirty(inode->i_dquot[cnt]);
1471        flush_warnings(inode->i_dquot, warntype);
1472        up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1473        return QUOTA_OK;
1474}
1475
1476/*
1477 * Transfer the number of inode and blocks from one diskquota to an other.
1478 *
1479 * This operation can block, but only after everything is updated
1480 * A transaction must be started when entering this function.
1481 */
1482int dquot_transfer(struct inode *inode, struct iattr *iattr)
1483{
1484        qsize_t space;
1485        struct dquot *transfer_from[MAXQUOTAS];
1486        struct dquot *transfer_to[MAXQUOTAS];
1487        int cnt, ret = QUOTA_OK;
1488        int chuid = iattr->ia_valid & ATTR_UID && inode->i_uid != iattr->ia_uid,
1489            chgid = iattr->ia_valid & ATTR_GID && inode->i_gid != iattr->ia_gid;
1490        char warntype_to[MAXQUOTAS];
1491        char warntype_from_inodes[MAXQUOTAS], warntype_from_space[MAXQUOTAS];
1492
1493        /* First test before acquiring mutex - solves deadlocks when we
1494         * re-enter the quota code and are already holding the mutex */
1495        if (IS_NOQUOTA(inode))
1496                return QUOTA_OK;
1497        /* Initialize the arrays */
1498        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1499                transfer_from[cnt] = NODQUOT;
1500                transfer_to[cnt] = NODQUOT;
1501                warntype_to[cnt] = QUOTA_NL_NOWARN;
1502                switch (cnt) {
1503                        case USRQUOTA:
1504                                if (!chuid)
1505                                        continue;
1506                                transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1507                                break;
1508                        case GRPQUOTA:
1509                                if (!chgid)
1510                                        continue;
1511                                transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1512                                break;
1513                }
1514        }
1515
1516        down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1517        /* Now recheck reliably when holding dqptr_sem */
1518        if (IS_NOQUOTA(inode)) {        /* File without quota accounting? */
1519                up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1520                goto put_all;
1521        }
1522        spin_lock(&dq_data_lock);
1523        space = inode_get_bytes(inode);
1524        /* Build the transfer_from list and check the limits */
1525        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1526                if (transfer_to[cnt] == NODQUOT)
1527                        continue;
1528                transfer_from[cnt] = inode->i_dquot[cnt];
1529                if (check_idq(transfer_to[cnt], 1, warntype_to + cnt) ==
1530                    NO_QUOTA || check_bdq(transfer_to[cnt], space, 0,
1531                    warntype_to + cnt) == NO_QUOTA)
1532                        goto over_quota;
1533        }
1534
1535        /*
1536         * Finally perform the needed transfer from transfer_from to transfer_to
1537         */
1538        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1539                /*
1540                 * Skip changes for same uid or gid or for turned off quota-type.
1541                 */
1542                if (transfer_to[cnt] == NODQUOT)
1543                        continue;
1544
1545                /* Due to IO error we might not have transfer_from[] structure */
1546                if (transfer_from[cnt]) {
1547                        warntype_from_inodes[cnt] =
1548                                info_idq_free(transfer_from[cnt], 1);
1549                        warntype_from_space[cnt] =
1550                                info_bdq_free(transfer_from[cnt], space);
1551                        dquot_decr_inodes(transfer_from[cnt], 1);
1552                        dquot_decr_space(transfer_from[cnt], space);
1553                }
1554
1555                dquot_incr_inodes(transfer_to[cnt], 1);
1556                dquot_incr_space(transfer_to[cnt], space);
1557
1558                inode->i_dquot[cnt] = transfer_to[cnt];
1559        }
1560        spin_unlock(&dq_data_lock);
1561        up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1562
1563        /* Dirtify all the dquots - this can block when journalling */
1564        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1565                if (transfer_from[cnt])
1566                        mark_dquot_dirty(transfer_from[cnt]);
1567                if (transfer_to[cnt]) {
1568                        mark_dquot_dirty(transfer_to[cnt]);
1569                        /* The reference we got is transferred to the inode */
1570                        transfer_to[cnt] = NODQUOT;
1571                }
1572        }
1573warn_put_all:
1574        flush_warnings(transfer_to, warntype_to);
1575        flush_warnings(transfer_from, warntype_from_inodes);
1576        flush_warnings(transfer_from, warntype_from_space);
1577put_all:
1578        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1579                dqput(transfer_from[cnt]);
1580                dqput(transfer_to[cnt]);
1581        }
1582        return ret;
1583over_quota:
1584        spin_unlock(&dq_data_lock);
1585        up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1586        /* Clear dquot pointers we don't want to dqput() */
1587        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1588                transfer_from[cnt] = NODQUOT;
1589        ret = NO_QUOTA;
1590        goto warn_put_all;
1591}
1592
1593/* Wrapper for transferring ownership of an inode */
1594int vfs_dq_transfer(struct inode *inode, struct iattr *iattr)
1595{
1596        if (sb_any_quota_active(inode->i_sb) && !IS_NOQUOTA(inode)) {
1597                vfs_dq_init(inode);
1598                if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA)
1599                        return 1;
1600        }
1601        return 0;
1602}
1603
1604
1605/*
1606 * Write info of quota file to disk
1607 */
1608int dquot_commit_info(struct super_block *sb, int type)
1609{
1610        int ret;
1611        struct quota_info *dqopt = sb_dqopt(sb);
1612
1613        mutex_lock(&dqopt->dqio_mutex);
1614        ret = dqopt->ops[type]->write_file_info(sb, type);
1615        mutex_unlock(&dqopt->dqio_mutex);
1616        return ret;
1617}
1618
1619/*
1620 * Definitions of diskquota operations.
1621 */
1622struct dquot_operations dquot_operations = {
1623        .initialize     = dquot_initialize,
1624        .drop           = dquot_drop,
1625        .alloc_space    = dquot_alloc_space,
1626        .alloc_inode    = dquot_alloc_inode,
1627        .free_space     = dquot_free_space,
1628        .free_inode     = dquot_free_inode,
1629        .transfer       = dquot_transfer,
1630        .write_dquot    = dquot_commit,
1631        .acquire_dquot  = dquot_acquire,
1632        .release_dquot  = dquot_release,
1633        .mark_dirty     = dquot_mark_dquot_dirty,
1634        .write_info     = dquot_commit_info,
1635        .alloc_dquot    = dquot_alloc,
1636        .destroy_dquot  = dquot_destroy,
1637};
1638
1639/*
1640 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1641 */
1642int vfs_quota_disable(struct super_block *sb, int type, unsigned int flags)
1643{
1644        int cnt, ret = 0;
1645        struct quota_info *dqopt = sb_dqopt(sb);
1646        struct inode *toputinode[MAXQUOTAS];
1647
1648        /* Cannot turn off usage accounting without turning off limits, or
1649         * suspend quotas and simultaneously turn quotas off. */
1650        if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
1651            || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
1652            DQUOT_USAGE_ENABLED)))
1653                return -EINVAL;
1654
1655        /* We need to serialize quota_off() for device */
1656        mutex_lock(&dqopt->dqonoff_mutex);
1657
1658        /*
1659         * Skip everything if there's nothing to do. We have to do this because
1660         * sometimes we are called when fill_super() failed and calling
1661         * sync_fs() in such cases does no good.
1662         */
1663        if (!sb_any_quota_loaded(sb)) {
1664                mutex_unlock(&dqopt->dqonoff_mutex);
1665                return 0;
1666        }
1667        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1668                toputinode[cnt] = NULL;
1669                if (type != -1 && cnt != type)
1670                        continue;
1671                if (!sb_has_quota_loaded(sb, cnt))
1672                        continue;
1673
1674                if (flags & DQUOT_SUSPENDED) {
1675                        spin_lock(&dq_state_lock);
1676                        dqopt->flags |=
1677                                dquot_state_flag(DQUOT_SUSPENDED, cnt);
1678                        spin_unlock(&dq_state_lock);
1679                } else {
1680                        spin_lock(&dq_state_lock);
1681                        dqopt->flags &= ~dquot_state_flag(flags, cnt);
1682                        /* Turning off suspended quotas? */
1683                        if (!sb_has_quota_loaded(sb, cnt) &&
1684                            sb_has_quota_suspended(sb, cnt)) {
1685                                dqopt->flags &= ~dquot_state_flag(
1686                                                        DQUOT_SUSPENDED, cnt);
1687                                spin_unlock(&dq_state_lock);
1688                                iput(dqopt->files[cnt]);
1689                                dqopt->files[cnt] = NULL;
1690                                continue;
1691                        }
1692                        spin_unlock(&dq_state_lock);
1693                }
1694
1695                /* We still have to keep quota loaded? */
1696                if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
1697                        continue;
1698
1699                /* Note: these are blocking operations */
1700                drop_dquot_ref(sb, cnt);
1701                invalidate_dquots(sb, cnt);
1702                /*
1703                 * Now all dquots should be invalidated, all writes done so we should be only
1704                 * users of the info. No locks needed.
1705                 */
1706                if (info_dirty(&dqopt->info[cnt]))
1707                        sb->dq_op->write_info(sb, cnt);
1708                if (dqopt->ops[cnt]->free_file_info)
1709                        dqopt->ops[cnt]->free_file_info(sb, cnt);
1710                put_quota_format(dqopt->info[cnt].dqi_format);
1711
1712                toputinode[cnt] = dqopt->files[cnt];
1713                if (!sb_has_quota_loaded(sb, cnt))
1714                        dqopt->files[cnt] = NULL;
1715                dqopt->info[cnt].dqi_flags = 0;
1716                dqopt->info[cnt].dqi_igrace = 0;
1717                dqopt->info[cnt].dqi_bgrace = 0;
1718                dqopt->ops[cnt] = NULL;
1719        }
1720        mutex_unlock(&dqopt->dqonoff_mutex);
1721
1722        /* Skip syncing and setting flags if quota files are hidden */
1723        if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
1724                goto put_inodes;
1725
1726        /* Sync the superblock so that buffers with quota data are written to
1727         * disk (and so userspace sees correct data afterwards). */
1728        if (sb->s_op->sync_fs)
1729                sb->s_op->sync_fs(sb, 1);
1730        sync_blockdev(sb->s_bdev);
1731        /* Now the quota files are just ordinary files and we can set the
1732         * inode flags back. Moreover we discard the pagecache so that
1733         * userspace sees the writes we did bypassing the pagecache. We
1734         * must also discard the blockdev buffers so that we see the
1735         * changes done by userspace on the next quotaon() */
1736        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1737                if (toputinode[cnt]) {
1738                        mutex_lock(&dqopt->dqonoff_mutex);
1739                        /* If quota was reenabled in the meantime, we have
1740                         * nothing to do */
1741                        if (!sb_has_quota_loaded(sb, cnt)) {
1742                                mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1743                                toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1744                                  S_NOATIME | S_NOQUOTA);
1745                                truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1746                                mutex_unlock(&toputinode[cnt]->i_mutex);
1747                                mark_inode_dirty(toputinode[cnt]);
1748                        }
1749                        mutex_unlock(&dqopt->dqonoff_mutex);
1750                }
1751        if (sb->s_bdev)
1752                invalidate_bdev(sb->s_bdev);
1753put_inodes:
1754        for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1755                if (toputinode[cnt]) {
1756                        /* On remount RO, we keep the inode pointer so that we
1757                         * can reenable quota on the subsequent remount RW. We
1758                         * have to check 'flags' variable and not use sb_has_
1759                         * function because another quotaon / quotaoff could
1760                         * change global state before we got here. We refuse
1761                         * to suspend quotas when there is pending delete on
1762                         * the quota file... */
1763                        if (!(flags & DQUOT_SUSPENDED))
1764                                iput(toputinode[cnt]);
1765                        else if (!toputinode[cnt]->i_nlink)
1766                                ret = -EBUSY;
1767                }
1768        return ret;
1769}
1770
1771int vfs_quota_off(struct super_block *sb, int type, int remount)
1772{
1773        return vfs_quota_disable(sb, type, remount ? DQUOT_SUSPENDED :
1774                                 (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED));
1775}
1776
1777/*
1778 *      Turn quotas on on a device
1779 */
1780
1781/*
1782 * Helper function to turn quotas on when we already have the inode of
1783 * quota file and no quota information is loaded.
1784 */
1785static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
1786        unsigned int flags)
1787{
1788        struct quota_format_type *fmt = find_quota_format(format_id);
1789        struct super_block *sb = inode->i_sb;
1790        struct quota_info *dqopt = sb_dqopt(sb);
1791        int error;
1792        int oldflags = -1;
1793
1794        if (!fmt)
1795                return -ESRCH;
1796        if (!S_ISREG(inode->i_mode)) {
1797                error = -EACCES;
1798                goto out_fmt;
1799        }
1800        if (IS_RDONLY(inode)) {
1801                error = -EROFS;
1802                goto out_fmt;
1803        }
1804        if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1805                error = -EINVAL;
1806                goto out_fmt;
1807        }
1808        /* Usage always has to be set... */
1809        if (!(flags & DQUOT_USAGE_ENABLED)) {
1810                error = -EINVAL;
1811                goto out_fmt;
1812        }
1813
1814        if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1815                /* As we bypass the pagecache we must now flush the inode so
1816                 * that we see all the changes from userspace... */
1817                write_inode_now(inode, 1);
1818                /* And now flush the block cache so that kernel sees the
1819                 * changes */
1820                invalidate_bdev(sb->s_bdev);
1821        }
1822        mutex_lock(&inode->i_mutex);
1823        mutex_lock(&dqopt->dqonoff_mutex);
1824        if (sb_has_quota_loaded(sb, type)) {
1825                error = -EBUSY;
1826                goto out_lock;
1827        }
1828
1829        if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
1830                /* We don't want quota and atime on quota files (deadlocks
1831                 * possible) Also nobody should write to the file - we use
1832                 * special IO operations which ignore the immutable bit. */
1833                down_write(&dqopt->dqptr_sem);
1834                oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1835                inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1836                up_write(&dqopt->dqptr_sem);
1837                sb->dq_op->drop(inode);
1838        }
1839
1840        error = -EIO;
1841        dqopt->files[type] = igrab(inode);
1842        if (!dqopt->files[type])
1843                goto out_lock;
1844        error = -EINVAL;
1845        if (!fmt->qf_ops->check_quota_file(sb, type))
1846                goto out_file_init;
1847
1848        dqopt->ops[type] = fmt->qf_ops;
1849        dqopt->info[type].dqi_format = fmt;
1850        dqopt->info[type].dqi_fmt_id = format_id;
1851        INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1852        mutex_lock(&dqopt->dqio_mutex);
1853        if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1854                mutex_unlock(&dqopt->dqio_mutex);
1855                goto out_file_init;
1856        }
1857        mutex_unlock(&dqopt->dqio_mutex);
1858        mutex_unlock(&inode->i_mutex);
1859        spin_lock(&dq_state_lock);
1860        dqopt->flags |= dquot_state_flag(flags, type);
1861        spin_unlock(&dq_state_lock);
1862
1863        add_dquot_ref(sb, type);
1864        mutex_unlock(&dqopt->dqonoff_mutex);
1865
1866        return 0;
1867
1868out_file_init:
1869        dqopt->files[type] = NULL;
1870        iput(inode);
1871out_lock:
1872        mutex_unlock(&dqopt->dqonoff_mutex);
1873        if (oldflags != -1) {
1874                down_write(&dqopt->dqptr_sem);
1875                /* Set the flags back (in the case of accidental quotaon()
1876                 * on a wrong file we don't want to mess up the flags) */
1877                inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1878                inode->i_flags |= oldflags;
1879                up_write(&dqopt->dqptr_sem);
1880        }
1881        mutex_unlock(&inode->i_mutex);
1882out_fmt:
1883        put_quota_format(fmt);
1884
1885        return error; 
1886}
1887
1888/* Reenable quotas on remount RW */
1889static int vfs_quota_on_remount(struct super_block *sb, int type)
1890{
1891        struct quota_info *dqopt = sb_dqopt(sb);
1892        struct inode *inode;
1893        int ret;
1894        unsigned int flags;
1895
1896        mutex_lock(&dqopt->dqonoff_mutex);
1897        if (!sb_has_quota_suspended(sb, type)) {
1898                mutex_unlock(&dqopt->dqonoff_mutex);
1899                return 0;
1900        }
1901        inode = dqopt->files[type];
1902        dqopt->files[type] = NULL;
1903        spin_lock(&dq_state_lock);
1904        flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
1905                                                DQUOT_LIMITS_ENABLED, type);
1906        dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, type);
1907        spin_unlock(&dq_state_lock);
1908        mutex_unlock(&dqopt->dqonoff_mutex);
1909
1910        flags = dquot_generic_flag(flags, type);
1911        ret = vfs_load_quota_inode(inode, type, dqopt->info[type].dqi_fmt_id,
1912                                   flags);
1913        iput(inode);
1914
1915        return ret;
1916}
1917
1918int vfs_quota_on_path(struct super_block *sb, int type, int format_id,
1919                      struct path *path)
1920{
1921        int error = security_quota_on(path->dentry);
1922        if (error)
1923                return error;
1924        /* Quota file not on the same filesystem? */
1925        if (path->mnt->mnt_sb != sb)
1926                error = -EXDEV;
1927        else
1928                error = vfs_load_quota_inode(path->dentry->d_inode, type,
1929                                             format_id, DQUOT_USAGE_ENABLED |
1930                                             DQUOT_LIMITS_ENABLED);
1931        return error;
1932}
1933
1934int vfs_quota_on(struct super_block *sb, int type, int format_id, char *name,
1935                 int remount)
1936{
1937        struct path path;
1938        int error;
1939
1940        if (remount)
1941                return vfs_quota_on_remount(sb, type);
1942
1943        error = kern_path(name, LOOKUP_FOLLOW, &path);
1944        if (!error) {
1945                error = vfs_quota_on_path(sb, type, format_id, &path);
1946                path_put(&path);
1947        }
1948        return error;
1949}
1950
1951/*
1952 * More powerful function for turning on quotas allowing setting
1953 * of individual quota flags
1954 */
1955int vfs_quota_enable(struct inode *inode, int type, int format_id,
1956                unsigned int flags)
1957{
1958        int ret = 0;
1959        struct super_block *sb = inode->i_sb;
1960        struct quota_info *dqopt = sb_dqopt(sb);
1961
1962        /* Just unsuspend quotas? */
1963        if (flags & DQUOT_SUSPENDED)
1964                return vfs_quota_on_remount(sb, type);
1965        if (!flags)
1966                return 0;
1967        /* Just updating flags needed? */
1968        if (sb_has_quota_loaded(sb, type)) {
1969                mutex_lock(&dqopt->dqonoff_mutex);
1970                /* Now do a reliable test... */
1971                if (!sb_has_quota_loaded(sb, type)) {
1972                        mutex_unlock(&dqopt->dqonoff_mutex);
1973                        goto load_quota;
1974                }
1975                if (flags & DQUOT_USAGE_ENABLED &&
1976                    sb_has_quota_usage_enabled(sb, type)) {
1977                        ret = -EBUSY;
1978                        goto out_lock;
1979                }
1980                if (flags & DQUOT_LIMITS_ENABLED &&
1981                    sb_has_quota_limits_enabled(sb, type)) {
1982                        ret = -EBUSY;
1983                        goto out_lock;
1984                }
1985                spin_lock(&dq_state_lock);
1986                sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
1987                spin_unlock(&dq_state_lock);
1988out_lock:
1989                mutex_unlock(&dqopt->dqonoff_mutex);
1990                return ret;
1991        }
1992
1993load_quota:
1994        return vfs_load_quota_inode(inode, type, format_id, flags);
1995}
1996
1997/*
1998 * This function is used when filesystem needs to initialize quotas
1999 * during mount time.
2000 */
2001int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
2002                int format_id, int type)
2003{
2004        struct dentry *dentry;
2005        int error;
2006
2007        dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2008        if (IS_ERR(dentry))
2009                return PTR_ERR(dentry);
2010
2011        if (!dentry->d_inode) {
2012                error = -ENOENT;
2013                goto out;
2014        }
2015
2016        error = security_quota_on(dentry);
2017        if (!error)
2018                error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2019                                DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2020
2021out:
2022        dput(dentry);
2023        return error;
2024}
2025
2026/* Wrapper to turn on quotas when remounting rw */
2027int vfs_dq_quota_on_remount(struct super_block *sb)
2028{
2029        int cnt;
2030        int ret = 0, err;
2031
2032        if (!sb->s_qcop || !sb->s_qcop->quota_on)
2033                return -ENOSYS;
2034        for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2035                err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1);
2036                if (err < 0 && !ret)
2037                        ret = err;
2038        }
2039        return ret;
2040}
2041
2042static inline qsize_t qbtos(qsize_t blocks)
2043{
2044        return blocks << QIF_DQBLKSIZE_BITS;
2045}
2046
2047static inline qsize_t stoqb(qsize_t space)
2048{
2049        return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2050}
2051
2052/* Generic routine for getting common part of quota structure */
2053static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
2054{
2055        struct mem_dqblk *dm = &dquot->dq_dqb;
2056
2057        spin_lock(&dq_data_lock);
2058        di->dqb_bhardlimit = stoqb(dm->dqb_bhardlimit);
2059        di->dqb_bsoftlimit = stoqb(dm->dqb_bsoftlimit);
2060        di->dqb_curspace = dm->dqb_curspace;
2061        di->dqb_ihardlimit = dm->dqb_ihardlimit;
2062        di->dqb_isoftlimit = dm->dqb_isoftlimit;
2063        di->dqb_curinodes = dm->dqb_curinodes;
2064        di->dqb_btime = dm->dqb_btime;
2065        di->dqb_itime = dm->dqb_itime;
2066        di->dqb_valid = QIF_ALL;
2067        spin_unlock(&dq_data_lock);
2068}
2069
2070int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2071{
2072        struct dquot *dquot;
2073
2074        dquot = dqget(sb, id, type);
2075        if (dquot == NODQUOT)
2076                return -ESRCH;
2077        do_get_dqblk(dquot, di);
2078        dqput(dquot);
2079
2080        return 0;
2081}
2082
2083/* Generic routine for setting common part of quota structure */
2084static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
2085{
2086        struct mem_dqblk *dm = &dquot->dq_dqb;
2087        int check_blim = 0, check_ilim = 0;
2088        struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
2089
2090        if ((di->dqb_valid & QIF_BLIMITS &&
2091             (di->dqb_bhardlimit > dqi->dqi_maxblimit ||
2092              di->dqb_bsoftlimit > dqi->dqi_maxblimit)) ||
2093            (di->dqb_valid & QIF_ILIMITS &&
2094             (di->dqb_ihardlimit > dqi->dqi_maxilimit ||
2095              di->dqb_isoftlimit > dqi->dqi_maxilimit)))
2096                return -ERANGE;
2097
2098        spin_lock(&dq_data_lock);
2099        if (di->dqb_valid & QIF_SPACE) {
2100                dm->dqb_curspace = di->dqb_curspace;
2101                check_blim = 1;
2102                __set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2103        }
2104        if (di->dqb_valid & QIF_BLIMITS) {
2105                dm->dqb_bsoftlimit = qbtos(di->dqb_bsoftlimit);
2106                dm->dqb_bhardlimit = qbtos(di->dqb_bhardlimit);
2107                check_blim = 1;
2108                __set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2109        }
2110        if (di->dqb_valid & QIF_INODES) {
2111                dm->dqb_curinodes = di->dqb_curinodes;
2112                check_ilim = 1;
2113                __set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2114        }
2115        if (di->dqb_valid & QIF_ILIMITS) {
2116                dm->dqb_isoftlimit = di->dqb_isoftlimit;
2117                dm->dqb_ihardlimit = di->dqb_ihardlimit;
2118                check_ilim = 1;
2119                __set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2120        }
2121        if (di->dqb_valid & QIF_BTIME) {
2122                dm->dqb_btime = di->dqb_btime;
2123                check_blim = 1;
2124                __set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2125        }
2126        if (di->dqb_valid & QIF_ITIME) {
2127                dm->dqb_itime = di->dqb_itime;
2128                check_ilim = 1;
2129                __set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2130        }
2131
2132        if (check_blim) {
2133                if (!dm->dqb_bsoftlimit || dm->dqb_curspace < dm->dqb_bsoftlimit) {
2134                        dm->dqb_btime = 0;
2135                        clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2136                }
2137                else if (!(di->dqb_valid & QIF_BTIME))  /* Set grace only if user hasn't provided his own... */
2138                        dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2139        }
2140        if (check_ilim) {
2141                if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
2142                        dm->dqb_itime = 0;
2143                        clear_bit(DQ_INODES_B, &dquot->dq_flags);
2144                }
2145                else if (!(di->dqb_valid & QIF_ITIME))  /* Set grace only if user hasn't provided his own... */
2146                        dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2147        }
2148        if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
2149                clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2150        else
2151                set_bit(DQ_FAKE_B, &dquot->dq_flags);
2152        spin_unlock(&dq_data_lock);
2153        mark_dquot_dirty(dquot);
2154
2155        return 0;
2156}
2157
2158int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
2159{
2160        struct dquot *dquot;
2161        int rc;
2162
2163        dquot = dqget(sb, id, type);
2164        if (!dquot) {
2165                rc = -ESRCH;
2166                goto out;
2167        }
2168        rc = do_set_dqblk(dquot, di);
2169        dqput(dquot);
2170out:
2171        return rc;
2172}
2173
2174/* Generic routine for getting common part of quota file information */
2175int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2176{
2177        struct mem_dqinfo *mi;
2178  
2179        mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2180        if (!sb_has_quota_active(sb, type)) {
2181                mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2182                return -ESRCH;
2183        }
2184        mi = sb_dqopt(sb)->info + type;
2185        spin_lock(&dq_data_lock);
2186        ii->dqi_bgrace = mi->dqi_bgrace;
2187        ii->dqi_igrace = mi->dqi_igrace;
2188        ii->dqi_flags = mi->dqi_flags & DQF_MASK;
2189        ii->dqi_valid = IIF_ALL;
2190        spin_unlock(&dq_data_lock);
2191        mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2192        return 0;
2193}
2194
2195/* Generic routine for setting common part of quota file information */
2196int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2197{
2198        struct mem_dqinfo *mi;
2199        int err = 0;
2200
2201        mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2202        if (!sb_has_quota_active(sb, type)) {
2203                err = -ESRCH;
2204                goto out;
2205        }
2206        mi = sb_dqopt(sb)->info + type;
2207        spin_lock(&dq_data_lock);
2208        if (ii->dqi_valid & IIF_BGRACE)
2209                mi->dqi_bgrace = ii->dqi_bgrace;
2210        if (ii->dqi_valid & IIF_IGRACE)
2211                mi->dqi_igrace = ii->dqi_igrace;
2212        if (ii->dqi_valid & IIF_FLAGS)
2213                mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
2214        spin_unlock(&dq_data_lock);
2215        mark_info_dirty(sb, type);
2216        /* Force write to disk */
2217        sb->dq_op->write_info(sb, type);
2218out:
2219        mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2220        return err;
2221}
2222
2223struct quotactl_ops vfs_quotactl_ops = {
2224        .quota_on       = vfs_quota_on,
2225        .quota_off      = vfs_quota_off,
2226        .quota_sync     = vfs_quota_sync,
2227        .get_info       = vfs_get_dqinfo,
2228        .set_info       = vfs_set_dqinfo,
2229        .get_dqblk      = vfs_get_dqblk,
2230        .set_dqblk      = vfs_set_dqblk
2231};
2232
2233static ctl_table fs_dqstats_table[] = {
2234        {
2235                .ctl_name       = FS_DQ_LOOKUPS,
2236                .procname       = "lookups",
2237                .data           = &dqstats.lookups,
2238                .maxlen         = sizeof(int),
2239                .mode           = 0444,
2240                .proc_handler   = &proc_dointvec,
2241        },
2242        {
2243                .ctl_name       = FS_DQ_DROPS,
2244                .procname       = "drops",
2245                .data           = &dqstats.drops,
2246                .maxlen         = sizeof(int),
2247                .mode           = 0444,
2248                .proc_handler   = &proc_dointvec,
2249        },
2250        {
2251                .ctl_name       = FS_DQ_READS,
2252                .procname       = "reads",
2253                .data           = &dqstats.reads,
2254                .maxlen         = sizeof(int),
2255                .mode           = 0444,
2256                .proc_handler   = &proc_dointvec,
2257        },
2258        {
2259                .ctl_name       = FS_DQ_WRITES,
2260                .procname       = "writes",
2261                .data           = &dqstats.writes,
2262                .maxlen         = sizeof(int),
2263                .mode           = 0444,
2264                .proc_handler   = &proc_dointvec,
2265        },
2266        {
2267                .ctl_name       = FS_DQ_CACHE_HITS,
2268                .procname       = "cache_hits",
2269                .data           = &dqstats.cache_hits,
2270                .maxlen         = sizeof(int),
2271                .mode           = 0444,
2272                .proc_handler   = &proc_dointvec,
2273        },
2274        {
2275                .ctl_name       = FS_DQ_ALLOCATED,
2276                .procname       = "allocated_dquots",
2277                .data           = &dqstats.allocated_dquots,
2278                .maxlen         = sizeof(int),
2279                .mode           = 0444,
2280                .proc_handler   = &proc_dointvec,
2281        },
2282        {
2283                .ctl_name       = FS_DQ_FREE,
2284                .procname       = "free_dquots",
2285                .data           = &dqstats.free_dquots,
2286                .maxlen         = sizeof(int),
2287                .mode           = 0444,
2288                .proc_handler   = &proc_dointvec,
2289        },
2290        {
2291                .ctl_name       = FS_DQ_SYNCS,
2292                .procname       = "syncs",
2293                .data           = &dqstats.syncs,
2294                .maxlen         = sizeof(int),
2295                .mode           = 0444,
2296                .proc_handler   = &proc_dointvec,
2297        },
2298#ifdef CONFIG_PRINT_QUOTA_WARNING
2299        {
2300                .ctl_name       = FS_DQ_WARNINGS,
2301                .procname       = "warnings",
2302                .data           = &flag_print_warnings,
2303                .maxlen         = sizeof(int),
2304                .mode           = 0644,
2305                .proc_handler   = &proc_dointvec,
2306        },
2307#endif
2308        { .ctl_name = 0 },
2309};
2310
2311static ctl_table fs_table[] = {
2312        {
2313                .ctl_name       = FS_DQSTATS,
2314                .procname       = "quota",
2315                .mode           = 0555,
2316                .child          = fs_dqstats_table,
2317        },
2318        { .ctl_name = 0 },
2319};
2320
2321static ctl_table sys_table[] = {
2322        {
2323                .ctl_name       = CTL_FS,
2324                .procname       = "fs",
2325                .mode           = 0555,
2326                .child          = fs_table,
2327        },
2328        { .ctl_name = 0 },
2329};
2330
2331static int __init dquot_init(void)
2332{
2333        int i;
2334        unsigned long nr_hash, order;
2335
2336        printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2337
2338        register_sysctl_table(sys_table);
2339
2340        dquot_cachep = kmem_cache_create("dquot",
2341                        sizeof(struct dquot), sizeof(unsigned long) * 4,
2342                        (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2343                                SLAB_MEM_SPREAD|SLAB_PANIC),
2344                        NULL);
2345
2346        order = 0;
2347        dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2348        if (!dquot_hash)
2349                panic("Cannot create dquot hash table");
2350
2351        /* Find power-of-two hlist_heads which can fit into allocation */
2352        nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2353        dq_hash_bits = 0;
2354        do {
2355                dq_hash_bits++;
2356        } while (nr_hash >> dq_hash_bits);
2357        dq_hash_bits--;
2358
2359        nr_hash = 1UL << dq_hash_bits;
2360        dq_hash_mask = nr_hash - 1;
2361        for (i = 0; i < nr_hash; i++)
2362                INIT_HLIST_HEAD(dquot_hash + i);
2363
2364        printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2365                        nr_hash, order, (PAGE_SIZE << order));
2366
2367        register_shrinker(&dqcache_shrinker);
2368
2369#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
2370        if (genl_register_family(&quota_genl_family) != 0)
2371                printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
2372#endif
2373
2374        return 0;
2375}
2376module_init(dquot_init);
2377
2378EXPORT_SYMBOL(register_quota_format);
2379EXPORT_SYMBOL(unregister_quota_format);
2380EXPORT_SYMBOL(dqstats);
2381EXPORT_SYMBOL(dq_data_lock);
2382EXPORT_SYMBOL(vfs_quota_enable);
2383EXPORT_SYMBOL(vfs_quota_on);
2384EXPORT_SYMBOL(vfs_quota_on_path);
2385EXPORT_SYMBOL(vfs_quota_on_mount);
2386EXPORT_SYMBOL(vfs_quota_disable);
2387EXPORT_SYMBOL(vfs_quota_off);
2388EXPORT_SYMBOL(dquot_scan_active);
2389EXPORT_SYMBOL(vfs_quota_sync);
2390EXPORT_SYMBOL(vfs_get_dqinfo);
2391EXPORT_SYMBOL(vfs_set_dqinfo);
2392EXPORT_SYMBOL(vfs_get_dqblk);
2393EXPORT_SYMBOL(vfs_set_dqblk);
2394EXPORT_SYMBOL(dquot_commit);
2395EXPORT_SYMBOL(dquot_commit_info);
2396EXPORT_SYMBOL(dquot_acquire);
2397EXPORT_SYMBOL(dquot_release);
2398EXPORT_SYMBOL(dquot_mark_dquot_dirty);
2399EXPORT_SYMBOL(dquot_initialize);
2400EXPORT_SYMBOL(dquot_drop);
2401EXPORT_SYMBOL(vfs_dq_drop);
2402EXPORT_SYMBOL(dqget);
2403EXPORT_SYMBOL(dqput);
2404EXPORT_SYMBOL(dquot_alloc_space);
2405EXPORT_SYMBOL(dquot_alloc_inode);
2406EXPORT_SYMBOL(dquot_free_space);
2407EXPORT_SYMBOL(dquot_free_inode);
2408EXPORT_SYMBOL(dquot_transfer);
2409EXPORT_SYMBOL(vfs_dq_transfer);
2410EXPORT_SYMBOL(vfs_dq_quota_on_remount);
2411
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.