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