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