Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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