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