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