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