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