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