LCOV - code coverage report
Current view: top level - fs/quota - dquot.c (source / functions) Hit Total Coverage
Test: Real Lines: 80 1054 7.6 %
Date: 2020-10-17 15:46:16 Functions: 1 86 1.2 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14