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