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