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