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 | * |
| 12 | * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $ |
| 13 | * |
| 14 | * Author: Marco van Wieringen <mvw@planets.elm.net> |
| 15 | * |
| 16 | * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96 |
| 17 | * |
| 18 | * Revised list management to avoid races |
| 19 | * -- Bill Hawes, <whawes@star.net>, 9/98 |
| 20 | * |
| 21 | * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...(). |
| 22 | * As the consequence the locking was moved from dquot_decr_...(), |
| 23 | * dquot_incr_...() to calling functions. |
| 24 | * invalidate_dquots() now writes modified dquots. |
| 25 | * Serialized quota_off() and quota_on() for mount point. |
| 26 | * Fixed a few bugs in grow_dquots(). |
| 27 | * Fixed deadlock in write_dquot() - we no longer account quotas on |
| 28 | * quota files |
| 29 | * remove_dquot_ref() moved to inode.c - it now traverses through inodes |
| 30 | * add_dquot_ref() restarts after blocking |
| 31 | * Added check for bogus uid and fixed check for group in quotactl. |
| 32 | * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99 |
| 33 | * |
| 34 | * Used struct list_head instead of own list struct |
| 35 | * Invalidation of referenced dquots is no longer possible |
| 36 | * Improved free_dquots list management |
| 37 | * Quota and i_blocks are now updated in one place to avoid races |
| 38 | * Warnings are now delayed so we won't block in critical section |
| 39 | * Write updated not to require dquot lock |
| 40 | * Jan Kara, <jack@suse.cz>, 9/2000 |
| 41 | * |
| 42 | * Added dynamic quota structure allocation |
| 43 | * Jan Kara <jack@suse.cz> 12/2000 |
| 44 | * |
| 45 | * Rewritten quota interface. Implemented new quota format and |
| 46 | * formats registering. |
| 47 | * Jan Kara, <jack@suse.cz>, 2001,2002 |
| 48 | * |
| 49 | * New SMP locking. |
| 50 | * Jan Kara, <jack@suse.cz>, 10/2002 |
| 51 | * |
| 52 | * Added journalled quota support, fix lock inversion problems |
| 53 | * Jan Kara, <jack@suse.cz>, 2003,2004 |
| 54 | * |
| 55 | * (C) Copyright 1994 - 1997 Marco van Wieringen |
| 56 | */ |
| 57 | |
| 58 | #include <linux/errno.h> |
| 59 | #include <linux/kernel.h> |
| 60 | #include <linux/fs.h> |
| 61 | #include <linux/mount.h> |
| 62 | #include <linux/mm.h> |
| 63 | #include <linux/time.h> |
| 64 | #include <linux/types.h> |
| 65 | #include <linux/string.h> |
| 66 | #include <linux/fcntl.h> |
| 67 | #include <linux/stat.h> |
| 68 | #include <linux/tty.h> |
| 69 | #include <linux/file.h> |
| 70 | #include <linux/slab.h> |
| 71 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/init.h> |
| 73 | #include <linux/module.h> |
| 74 | #include <linux/proc_fs.h> |
| 75 | #include <linux/security.h> |
| 76 | #include <linux/kmod.h> |
| 77 | #include <linux/namei.h> |
| 78 | #include <linux/buffer_head.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 79 | #include <linux/capability.h> |
Adrian Bunk | be586ba | 2005-11-07 00:59:35 -0800 | [diff] [blame] | 80 | #include <linux/quotaops.h> |
Christoph Hellwig | fb58b73 | 2007-02-12 00:51:57 -0800 | [diff] [blame] | 81 | #include <linux/writeback.h> /* for inode_lock, oddly enough.. */ |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 83 | #include <net/netlink.h> |
| 84 | #include <net/genetlink.h> |
| 85 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | #include <asm/uaccess.h> |
| 88 | |
| 89 | #define __DQUOT_PARANOIA |
| 90 | |
| 91 | /* |
| 92 | * There are two quota SMP locks. dq_list_lock protects all lists with quotas |
| 93 | * and quota formats and also dqstats structure containing statistics about the |
| 94 | * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures |
| 95 | * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes. |
| 96 | * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly |
| 97 | * in inode_add_bytes() and inode_sub_bytes(). |
| 98 | * |
| 99 | * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock |
| 100 | * |
| 101 | * Note that some things (eg. sb pointer, type, id) doesn't change during |
| 102 | * the life of the dquot structure and so needn't to be protected by a lock |
| 103 | * |
| 104 | * Any operation working on dquots via inode pointers must hold dqptr_sem. If |
| 105 | * operation is just reading pointers from inode (or not using them at all) the |
| 106 | * read lock is enough. If pointers are altered function must hold write lock |
| 107 | * (these locking rules also apply for S_NOQUOTA flag in the inode - note that |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 108 | * for altering the flag i_mutex is also needed). If operation is holding |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * reference to dquot in other way (e.g. quotactl ops) it must be guarded by |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 110 | * dqonoff_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * This locking assures that: |
| 112 | * a) update/access to dquot pointers in inode is serialized |
| 113 | * b) everyone is guarded against invalidate_dquots() |
| 114 | * |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 115 | * 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] | 116 | * from inodes (dquot_alloc_space() and such don't check the dq_lock). |
| 117 | * Currently dquot is locked only when it is being read to memory (or space for |
| 118 | * it is being allocated) on the first dqget() and when it is being released on |
| 119 | * the last dqput(). The allocation and release oparations are serialized by |
| 120 | * the dq_lock and by checking the use count in dquot_release(). Write |
| 121 | * operations on dquots don't hold dq_lock as they copy data under dq_data_lock |
| 122 | * spinlock to internal buffers before writing. |
| 123 | * |
| 124 | * Lock ordering (including related VFS locks) is the following: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 125 | * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock > |
| 126 | * dqio_mutex |
| 127 | * i_mutex on quota files is special (it's below dqio_mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
| 129 | |
| 130 | static DEFINE_SPINLOCK(dq_list_lock); |
| 131 | DEFINE_SPINLOCK(dq_data_lock); |
| 132 | |
| 133 | static char *quotatypes[] = INITQFNAMES; |
| 134 | static struct quota_format_type *quota_formats; /* List of registered formats */ |
| 135 | static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES; |
| 136 | |
| 137 | /* SLAB cache for dquot structures */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 138 | static struct kmem_cache *dquot_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | int register_quota_format(struct quota_format_type *fmt) |
| 141 | { |
| 142 | spin_lock(&dq_list_lock); |
| 143 | fmt->qf_next = quota_formats; |
| 144 | quota_formats = fmt; |
| 145 | spin_unlock(&dq_list_lock); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | void unregister_quota_format(struct quota_format_type *fmt) |
| 150 | { |
| 151 | struct quota_format_type **actqf; |
| 152 | |
| 153 | spin_lock(&dq_list_lock); |
| 154 | for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next); |
| 155 | if (*actqf) |
| 156 | *actqf = (*actqf)->qf_next; |
| 157 | spin_unlock(&dq_list_lock); |
| 158 | } |
| 159 | |
| 160 | static struct quota_format_type *find_quota_format(int id) |
| 161 | { |
| 162 | struct quota_format_type *actqf; |
| 163 | |
| 164 | spin_lock(&dq_list_lock); |
| 165 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next); |
| 166 | if (!actqf || !try_module_get(actqf->qf_owner)) { |
| 167 | int qm; |
| 168 | |
| 169 | spin_unlock(&dq_list_lock); |
| 170 | |
| 171 | for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++); |
| 172 | if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name)) |
| 173 | return NULL; |
| 174 | |
| 175 | spin_lock(&dq_list_lock); |
| 176 | for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next); |
| 177 | if (actqf && !try_module_get(actqf->qf_owner)) |
| 178 | actqf = NULL; |
| 179 | } |
| 180 | spin_unlock(&dq_list_lock); |
| 181 | return actqf; |
| 182 | } |
| 183 | |
| 184 | static void put_quota_format(struct quota_format_type *fmt) |
| 185 | { |
| 186 | module_put(fmt->qf_owner); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Dquot List Management: |
| 191 | * The quota code uses three lists for dquot management: the inuse_list, |
| 192 | * free_dquots, and dquot_hash[] array. A single dquot structure may be |
| 193 | * on all three lists, depending on its current state. |
| 194 | * |
| 195 | * All dquots are placed to the end of inuse_list when first created, and this |
| 196 | * list is used for invalidate operation, which must look at every dquot. |
| 197 | * |
| 198 | * Unused dquots (dq_count == 0) are added to the free_dquots list when freed, |
| 199 | * and this list is searched whenever we need an available dquot. Dquots are |
| 200 | * removed from the list as soon as they are used again, and |
| 201 | * dqstats.free_dquots gives the number of dquots on the list. When |
| 202 | * dquot is invalidated it's completely released from memory. |
| 203 | * |
| 204 | * Dquots with a specific identity (device, type and id) are placed on |
| 205 | * one of the dquot_hash[] hash chains. The provides an efficient search |
| 206 | * mechanism to locate a specific dquot. |
| 207 | */ |
| 208 | |
| 209 | static LIST_HEAD(inuse_list); |
| 210 | static LIST_HEAD(free_dquots); |
| 211 | static unsigned int dq_hash_bits, dq_hash_mask; |
| 212 | static struct hlist_head *dquot_hash; |
| 213 | |
| 214 | struct dqstats dqstats; |
| 215 | |
| 216 | static void dqput(struct dquot *dquot); |
| 217 | |
| 218 | static inline unsigned int |
| 219 | hashfn(const struct super_block *sb, unsigned int id, int type) |
| 220 | { |
| 221 | unsigned long tmp; |
| 222 | |
| 223 | tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type); |
| 224 | return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Following list functions expect dq_list_lock to be held |
| 229 | */ |
| 230 | static inline void insert_dquot_hash(struct dquot *dquot) |
| 231 | { |
| 232 | struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type); |
| 233 | hlist_add_head(&dquot->dq_hash, head); |
| 234 | } |
| 235 | |
| 236 | static inline void remove_dquot_hash(struct dquot *dquot) |
| 237 | { |
| 238 | hlist_del_init(&dquot->dq_hash); |
| 239 | } |
| 240 | |
| 241 | static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type) |
| 242 | { |
| 243 | struct hlist_node *node; |
| 244 | struct dquot *dquot; |
| 245 | |
| 246 | hlist_for_each (node, dquot_hash+hashent) { |
| 247 | dquot = hlist_entry(node, struct dquot, dq_hash); |
| 248 | if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type) |
| 249 | return dquot; |
| 250 | } |
| 251 | return NODQUOT; |
| 252 | } |
| 253 | |
| 254 | /* Add a dquot to the tail of the free list */ |
| 255 | static inline void put_dquot_last(struct dquot *dquot) |
| 256 | { |
Akinobu Mita | 8e13059 | 2006-06-26 00:24:37 -0700 | [diff] [blame] | 257 | list_add_tail(&dquot->dq_free, &free_dquots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | dqstats.free_dquots++; |
| 259 | } |
| 260 | |
| 261 | static inline void remove_free_dquot(struct dquot *dquot) |
| 262 | { |
| 263 | if (list_empty(&dquot->dq_free)) |
| 264 | return; |
| 265 | list_del_init(&dquot->dq_free); |
| 266 | dqstats.free_dquots--; |
| 267 | } |
| 268 | |
| 269 | static inline void put_inuse(struct dquot *dquot) |
| 270 | { |
| 271 | /* We add to the back of inuse list so we don't have to restart |
| 272 | * when traversing this list and we block */ |
Akinobu Mita | 8e13059 | 2006-06-26 00:24:37 -0700 | [diff] [blame] | 273 | list_add_tail(&dquot->dq_inuse, &inuse_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | dqstats.allocated_dquots++; |
| 275 | } |
| 276 | |
| 277 | static inline void remove_inuse(struct dquot *dquot) |
| 278 | { |
| 279 | dqstats.allocated_dquots--; |
| 280 | list_del(&dquot->dq_inuse); |
| 281 | } |
| 282 | /* |
| 283 | * End of list functions needing dq_list_lock |
| 284 | */ |
| 285 | |
| 286 | static void wait_on_dquot(struct dquot *dquot) |
| 287 | { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 288 | mutex_lock(&dquot->dq_lock); |
| 289 | mutex_unlock(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jan Kara | 03f6e92 | 2008-04-28 02:14:32 -0700 | [diff] [blame] | 292 | static inline int dquot_dirty(struct dquot *dquot) |
| 293 | { |
| 294 | return test_bit(DQ_MOD_B, &dquot->dq_flags); |
| 295 | } |
| 296 | |
| 297 | static inline int mark_dquot_dirty(struct dquot *dquot) |
| 298 | { |
| 299 | return dquot->dq_sb->dq_op->mark_dirty(dquot); |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | int dquot_mark_dquot_dirty(struct dquot *dquot) |
| 303 | { |
| 304 | spin_lock(&dq_list_lock); |
| 305 | if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) |
| 306 | list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)-> |
| 307 | info[dquot->dq_type].dqi_dirty_list); |
| 308 | spin_unlock(&dq_list_lock); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* This function needs dq_list_lock */ |
| 313 | static inline int clear_dquot_dirty(struct dquot *dquot) |
| 314 | { |
| 315 | if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) |
| 316 | return 0; |
| 317 | list_del_init(&dquot->dq_dirty); |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | void mark_info_dirty(struct super_block *sb, int type) |
| 322 | { |
| 323 | set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags); |
| 324 | } |
| 325 | EXPORT_SYMBOL(mark_info_dirty); |
| 326 | |
| 327 | /* |
| 328 | * Read dquot from disk and alloc space for it |
| 329 | */ |
| 330 | |
| 331 | int dquot_acquire(struct dquot *dquot) |
| 332 | { |
| 333 | int ret = 0, ret2 = 0; |
| 334 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 335 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 336 | mutex_lock(&dquot->dq_lock); |
| 337 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (!test_bit(DQ_READ_B, &dquot->dq_flags)) |
| 339 | ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot); |
| 340 | if (ret < 0) |
| 341 | goto out_iolock; |
| 342 | set_bit(DQ_READ_B, &dquot->dq_flags); |
| 343 | /* Instantiate dquot if needed */ |
| 344 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) { |
| 345 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); |
| 346 | /* Write the info if needed */ |
| 347 | if (info_dirty(&dqopt->info[dquot->dq_type])) |
| 348 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); |
| 349 | if (ret < 0) |
| 350 | goto out_iolock; |
| 351 | if (ret2 < 0) { |
| 352 | ret = ret2; |
| 353 | goto out_iolock; |
| 354 | } |
| 355 | } |
| 356 | set_bit(DQ_ACTIVE_B, &dquot->dq_flags); |
| 357 | out_iolock: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 358 | mutex_unlock(&dqopt->dqio_mutex); |
| 359 | mutex_unlock(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Write dquot to disk |
| 365 | */ |
| 366 | int dquot_commit(struct dquot *dquot) |
| 367 | { |
| 368 | int ret = 0, ret2 = 0; |
| 369 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 370 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 371 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | spin_lock(&dq_list_lock); |
| 373 | if (!clear_dquot_dirty(dquot)) { |
| 374 | spin_unlock(&dq_list_lock); |
| 375 | goto out_sem; |
| 376 | } |
| 377 | spin_unlock(&dq_list_lock); |
| 378 | /* Inactive dquot can be only if there was error during read/init |
| 379 | * => we have better not writing it */ |
| 380 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { |
| 381 | ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot); |
| 382 | if (info_dirty(&dqopt->info[dquot->dq_type])) |
| 383 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); |
| 384 | if (ret >= 0) |
| 385 | ret = ret2; |
| 386 | } |
| 387 | out_sem: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 388 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Release dquot |
| 394 | */ |
| 395 | int dquot_release(struct dquot *dquot) |
| 396 | { |
| 397 | int ret = 0, ret2 = 0; |
| 398 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 399 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 400 | mutex_lock(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* Check whether we are not racing with some other dqget() */ |
| 402 | if (atomic_read(&dquot->dq_count) > 1) |
| 403 | goto out_dqlock; |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 404 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (dqopt->ops[dquot->dq_type]->release_dqblk) { |
| 406 | ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot); |
| 407 | /* Write the info */ |
| 408 | if (info_dirty(&dqopt->info[dquot->dq_type])) |
| 409 | ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type); |
| 410 | if (ret >= 0) |
| 411 | ret = ret2; |
| 412 | } |
| 413 | clear_bit(DQ_ACTIVE_B, &dquot->dq_flags); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 414 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | out_dqlock: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 416 | mutex_unlock(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | /* Invalidate all dquots on the list. Note that this function is called after |
| 421 | * 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] | 422 | * quota users. There can still be some users of quotas due to inodes being |
| 423 | * just deleted or pruned by prune_icache() (those are not attached to any |
| 424 | * list). We have to wait for such users. |
| 425 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | static void invalidate_dquots(struct super_block *sb, int type) |
| 427 | { |
Domen Puncer | c33ed27 | 2005-06-25 14:59:36 -0700 | [diff] [blame] | 428 | struct dquot *dquot, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 430 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | spin_lock(&dq_list_lock); |
Domen Puncer | c33ed27 | 2005-06-25 14:59:36 -0700 | [diff] [blame] | 432 | list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (dquot->dq_sb != sb) |
| 434 | continue; |
| 435 | if (dquot->dq_type != type) |
| 436 | continue; |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 437 | /* Wait for dquot users */ |
| 438 | if (atomic_read(&dquot->dq_count)) { |
| 439 | DEFINE_WAIT(wait); |
| 440 | |
| 441 | atomic_inc(&dquot->dq_count); |
| 442 | prepare_to_wait(&dquot->dq_wait_unused, &wait, |
| 443 | TASK_UNINTERRUPTIBLE); |
| 444 | spin_unlock(&dq_list_lock); |
| 445 | /* Once dqput() wakes us up, we know it's time to free |
| 446 | * the dquot. |
| 447 | * IMPORTANT: we rely on the fact that there is always |
| 448 | * at most one process waiting for dquot to free. |
| 449 | * Otherwise dq_count would be > 1 and we would never |
| 450 | * wake up. |
| 451 | */ |
| 452 | if (atomic_read(&dquot->dq_count) > 1) |
| 453 | schedule(); |
| 454 | finish_wait(&dquot->dq_wait_unused, &wait); |
| 455 | dqput(dquot); |
| 456 | /* At this moment dquot() need not exist (it could be |
| 457 | * reclaimed by prune_dqcache(). Hence we must |
| 458 | * restart. */ |
| 459 | goto restart; |
| 460 | } |
| 461 | /* |
| 462 | * Quota now has no users and it has been written on last |
| 463 | * dqput() |
| 464 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | remove_dquot_hash(dquot); |
| 466 | remove_free_dquot(dquot); |
| 467 | remove_inuse(dquot); |
| 468 | kmem_cache_free(dquot_cachep, dquot); |
| 469 | } |
| 470 | spin_unlock(&dq_list_lock); |
| 471 | } |
| 472 | |
| 473 | int vfs_quota_sync(struct super_block *sb, int type) |
| 474 | { |
| 475 | struct list_head *dirty; |
| 476 | struct dquot *dquot; |
| 477 | struct quota_info *dqopt = sb_dqopt(sb); |
| 478 | int cnt; |
| 479 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 480 | mutex_lock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 482 | if (type != -1 && cnt != type) |
| 483 | continue; |
| 484 | if (!sb_has_quota_enabled(sb, cnt)) |
| 485 | continue; |
| 486 | spin_lock(&dq_list_lock); |
| 487 | dirty = &dqopt->info[cnt].dqi_dirty_list; |
| 488 | while (!list_empty(dirty)) { |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 489 | dquot = list_first_entry(dirty, struct dquot, dq_dirty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* Dirty and inactive can be only bad dquot... */ |
| 491 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { |
| 492 | clear_dquot_dirty(dquot); |
| 493 | continue; |
| 494 | } |
| 495 | /* Now we have active dquot from which someone is |
| 496 | * holding reference so we can safely just increase |
| 497 | * use count */ |
| 498 | atomic_inc(&dquot->dq_count); |
| 499 | dqstats.lookups++; |
| 500 | spin_unlock(&dq_list_lock); |
| 501 | sb->dq_op->write_dquot(dquot); |
| 502 | dqput(dquot); |
| 503 | spin_lock(&dq_list_lock); |
| 504 | } |
| 505 | spin_unlock(&dq_list_lock); |
| 506 | } |
| 507 | |
| 508 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 509 | if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt) |
| 510 | && info_dirty(&dqopt->info[cnt])) |
| 511 | sb->dq_op->write_info(sb, cnt); |
| 512 | spin_lock(&dq_list_lock); |
| 513 | dqstats.syncs++; |
| 514 | spin_unlock(&dq_list_lock); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 515 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* Free unused dquots from cache */ |
| 521 | static void prune_dqcache(int count) |
| 522 | { |
| 523 | struct list_head *head; |
| 524 | struct dquot *dquot; |
| 525 | |
| 526 | head = free_dquots.prev; |
| 527 | while (head != &free_dquots && count) { |
| 528 | dquot = list_entry(head, struct dquot, dq_free); |
| 529 | remove_dquot_hash(dquot); |
| 530 | remove_free_dquot(dquot); |
| 531 | remove_inuse(dquot); |
| 532 | kmem_cache_free(dquot_cachep, dquot); |
| 533 | count--; |
| 534 | head = free_dquots.prev; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * This is called from kswapd when we think we need some |
| 540 | * more memory |
| 541 | */ |
| 542 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 543 | static int shrink_dqcache_memory(int nr, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | if (nr) { |
| 546 | spin_lock(&dq_list_lock); |
| 547 | prune_dqcache(nr); |
| 548 | spin_unlock(&dq_list_lock); |
| 549 | } |
| 550 | return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure; |
| 551 | } |
| 552 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 553 | static struct shrinker dqcache_shrinker = { |
| 554 | .shrink = shrink_dqcache_memory, |
| 555 | .seeks = DEFAULT_SEEKS, |
| 556 | }; |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Put reference to dquot |
| 560 | * NOTE: If you change this function please check whether dqput_blocks() works right... |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 561 | * MUST be called with either dqptr_sem or dqonoff_mutex held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | */ |
| 563 | static void dqput(struct dquot *dquot) |
| 564 | { |
| 565 | if (!dquot) |
| 566 | return; |
| 567 | #ifdef __DQUOT_PARANOIA |
| 568 | if (!atomic_read(&dquot->dq_count)) { |
| 569 | printk("VFS: dqput: trying to free free dquot\n"); |
| 570 | printk("VFS: device %s, dquot of %s %d\n", |
| 571 | dquot->dq_sb->s_id, |
| 572 | quotatypes[dquot->dq_type], |
| 573 | dquot->dq_id); |
| 574 | BUG(); |
| 575 | } |
| 576 | #endif |
| 577 | |
| 578 | spin_lock(&dq_list_lock); |
| 579 | dqstats.drops++; |
| 580 | spin_unlock(&dq_list_lock); |
| 581 | we_slept: |
| 582 | spin_lock(&dq_list_lock); |
| 583 | if (atomic_read(&dquot->dq_count) > 1) { |
| 584 | /* We have more than one user... nothing to do */ |
| 585 | atomic_dec(&dquot->dq_count); |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 586 | /* Releasing dquot during quotaoff phase? */ |
| 587 | if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) && |
| 588 | atomic_read(&dquot->dq_count) == 1) |
| 589 | wake_up(&dquot->dq_wait_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | spin_unlock(&dq_list_lock); |
| 591 | return; |
| 592 | } |
| 593 | /* Need to release dquot? */ |
| 594 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) { |
| 595 | spin_unlock(&dq_list_lock); |
| 596 | /* Commit dquot before releasing */ |
| 597 | dquot->dq_sb->dq_op->write_dquot(dquot); |
| 598 | goto we_slept; |
| 599 | } |
| 600 | /* Clear flag in case dquot was inactive (something bad happened) */ |
| 601 | clear_dquot_dirty(dquot); |
| 602 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { |
| 603 | spin_unlock(&dq_list_lock); |
| 604 | dquot->dq_sb->dq_op->release_dquot(dquot); |
| 605 | goto we_slept; |
| 606 | } |
| 607 | atomic_dec(&dquot->dq_count); |
| 608 | #ifdef __DQUOT_PARANOIA |
| 609 | /* sanity check */ |
Eric Sesterhenn | 8abf6a4 | 2006-04-02 13:36:13 +0200 | [diff] [blame] | 610 | BUG_ON(!list_empty(&dquot->dq_free)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | #endif |
| 612 | put_dquot_last(dquot); |
| 613 | spin_unlock(&dq_list_lock); |
| 614 | } |
| 615 | |
| 616 | static struct dquot *get_empty_dquot(struct super_block *sb, int type) |
| 617 | { |
| 618 | struct dquot *dquot; |
| 619 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 620 | dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if(!dquot) |
| 622 | return NODQUOT; |
| 623 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 624 | mutex_init(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | INIT_LIST_HEAD(&dquot->dq_free); |
| 626 | INIT_LIST_HEAD(&dquot->dq_inuse); |
| 627 | INIT_HLIST_NODE(&dquot->dq_hash); |
| 628 | INIT_LIST_HEAD(&dquot->dq_dirty); |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 629 | init_waitqueue_head(&dquot->dq_wait_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | dquot->dq_sb = sb; |
| 631 | dquot->dq_type = type; |
| 632 | atomic_set(&dquot->dq_count, 1); |
| 633 | |
| 634 | return dquot; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Get reference to dquot |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 639 | * MUST be called with either dqptr_sem or dqonoff_mutex held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
| 641 | static struct dquot *dqget(struct super_block *sb, unsigned int id, int type) |
| 642 | { |
| 643 | unsigned int hashent = hashfn(sb, id, type); |
| 644 | struct dquot *dquot, *empty = NODQUOT; |
| 645 | |
| 646 | if (!sb_has_quota_enabled(sb, type)) |
| 647 | return NODQUOT; |
| 648 | we_slept: |
| 649 | spin_lock(&dq_list_lock); |
| 650 | if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) { |
| 651 | if (empty == NODQUOT) { |
| 652 | spin_unlock(&dq_list_lock); |
| 653 | if ((empty = get_empty_dquot(sb, type)) == NODQUOT) |
| 654 | schedule(); /* Try to wait for a moment... */ |
| 655 | goto we_slept; |
| 656 | } |
| 657 | dquot = empty; |
| 658 | dquot->dq_id = id; |
| 659 | /* all dquots go on the inuse_list */ |
| 660 | put_inuse(dquot); |
| 661 | /* hash it first so it can be found */ |
| 662 | insert_dquot_hash(dquot); |
| 663 | dqstats.lookups++; |
| 664 | spin_unlock(&dq_list_lock); |
| 665 | } else { |
| 666 | if (!atomic_read(&dquot->dq_count)) |
| 667 | remove_free_dquot(dquot); |
| 668 | atomic_inc(&dquot->dq_count); |
| 669 | dqstats.cache_hits++; |
| 670 | dqstats.lookups++; |
| 671 | spin_unlock(&dq_list_lock); |
| 672 | if (empty) |
| 673 | kmem_cache_free(dquot_cachep, empty); |
| 674 | } |
| 675 | /* Wait for dq_lock - after this we know that either dquot_release() is already |
| 676 | * finished or it will be canceled due to dq_count > 1 test */ |
| 677 | wait_on_dquot(dquot); |
| 678 | /* Read the dquot and instantiate it (everything done only if needed) */ |
| 679 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) { |
| 680 | dqput(dquot); |
| 681 | return NODQUOT; |
| 682 | } |
| 683 | #ifdef __DQUOT_PARANOIA |
Eric Sesterhenn | 8abf6a4 | 2006-04-02 13:36:13 +0200 | [diff] [blame] | 684 | BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | #endif |
| 686 | |
| 687 | return dquot; |
| 688 | } |
| 689 | |
| 690 | static int dqinit_needed(struct inode *inode, int type) |
| 691 | { |
| 692 | int cnt; |
| 693 | |
| 694 | if (IS_NOQUOTA(inode)) |
| 695 | return 0; |
| 696 | if (type != -1) |
| 697 | return inode->i_dquot[type] == NODQUOT; |
| 698 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 699 | if (inode->i_dquot[cnt] == NODQUOT) |
| 700 | return 1; |
| 701 | return 0; |
| 702 | } |
| 703 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 704 | /* This routine is guarded by dqonoff_mutex mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | static void add_dquot_ref(struct super_block *sb, int type) |
| 706 | { |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 707 | struct inode *inode, *old_inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 709 | spin_lock(&inode_lock); |
| 710 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
| 711 | if (!atomic_read(&inode->i_writecount)) |
| 712 | continue; |
| 713 | if (!dqinit_needed(inode, type)) |
| 714 | continue; |
| 715 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) |
| 716 | continue; |
| 717 | |
| 718 | __iget(inode); |
| 719 | spin_unlock(&inode_lock); |
| 720 | |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 721 | iput(old_inode); |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 722 | sb->dq_op->initialize(inode, type); |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 723 | /* We hold a reference to 'inode' so it couldn't have been |
| 724 | * removed from s_inodes list while we dropped the inode_lock. |
| 725 | * We cannot iput the inode now as we can be holding the last |
| 726 | * reference and we cannot iput it under inode_lock. So we |
| 727 | * keep the reference and iput it later. */ |
| 728 | old_inode = inode; |
| 729 | spin_lock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 731 | spin_unlock(&inode_lock); |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 732 | iput(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */ |
| 736 | static inline int dqput_blocks(struct dquot *dquot) |
| 737 | { |
| 738 | if (atomic_read(&dquot->dq_count) <= 1) |
| 739 | return 1; |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | /* Remove references to dquots from inode - add dquot to list for freeing if needed */ |
| 744 | /* 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] | 745 | static int remove_inode_dquot_ref(struct inode *inode, int type, |
| 746 | struct list_head *tofree_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
| 748 | struct dquot *dquot = inode->i_dquot[type]; |
| 749 | |
| 750 | inode->i_dquot[type] = NODQUOT; |
| 751 | if (dquot != NODQUOT) { |
| 752 | if (dqput_blocks(dquot)) { |
| 753 | #ifdef __DQUOT_PARANOIA |
| 754 | if (atomic_read(&dquot->dq_count) != 1) |
| 755 | printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count)); |
| 756 | #endif |
| 757 | spin_lock(&dq_list_lock); |
| 758 | list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */ |
| 759 | spin_unlock(&dq_list_lock); |
| 760 | return 1; |
| 761 | } |
| 762 | else |
| 763 | dqput(dquot); /* We have guaranteed we won't block */ |
| 764 | } |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | /* Free list of dquots - called from inode.c */ |
| 769 | /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */ |
| 770 | static void put_dquot_list(struct list_head *tofree_head) |
| 771 | { |
| 772 | struct list_head *act_head; |
| 773 | struct dquot *dquot; |
| 774 | |
| 775 | act_head = tofree_head->next; |
| 776 | /* So now we have dquots on the list... Just free them */ |
| 777 | while (act_head != tofree_head) { |
| 778 | dquot = list_entry(act_head, struct dquot, dq_free); |
| 779 | act_head = act_head->next; |
| 780 | list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */ |
| 781 | dqput(dquot); |
| 782 | } |
| 783 | } |
| 784 | |
Christoph Hellwig | fb58b73 | 2007-02-12 00:51:57 -0800 | [diff] [blame] | 785 | static void remove_dquot_ref(struct super_block *sb, int type, |
| 786 | struct list_head *tofree_head) |
| 787 | { |
| 788 | struct inode *inode; |
| 789 | |
| 790 | spin_lock(&inode_lock); |
| 791 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
| 792 | if (!IS_NOQUOTA(inode)) |
| 793 | remove_inode_dquot_ref(inode, type, tofree_head); |
| 794 | } |
| 795 | spin_unlock(&inode_lock); |
| 796 | } |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | /* Gather all references from inodes and drop them */ |
| 799 | static void drop_dquot_ref(struct super_block *sb, int type) |
| 800 | { |
| 801 | LIST_HEAD(tofree_head); |
| 802 | |
Christoph Hellwig | fb58b73 | 2007-02-12 00:51:57 -0800 | [diff] [blame] | 803 | if (sb->dq_op) { |
| 804 | down_write(&sb_dqopt(sb)->dqptr_sem); |
| 805 | remove_dquot_ref(sb, type, &tofree_head); |
| 806 | up_write(&sb_dqopt(sb)->dqptr_sem); |
| 807 | put_dquot_list(&tofree_head); |
| 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number) |
| 812 | { |
| 813 | dquot->dq_dqb.dqb_curinodes += number; |
| 814 | } |
| 815 | |
| 816 | static inline void dquot_incr_space(struct dquot *dquot, qsize_t number) |
| 817 | { |
| 818 | dquot->dq_dqb.dqb_curspace += number; |
| 819 | } |
| 820 | |
| 821 | static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number) |
| 822 | { |
| 823 | if (dquot->dq_dqb.dqb_curinodes > number) |
| 824 | dquot->dq_dqb.dqb_curinodes -= number; |
| 825 | else |
| 826 | dquot->dq_dqb.dqb_curinodes = 0; |
| 827 | if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit) |
| 828 | dquot->dq_dqb.dqb_itime = (time_t) 0; |
| 829 | clear_bit(DQ_INODES_B, &dquot->dq_flags); |
| 830 | } |
| 831 | |
| 832 | static inline void dquot_decr_space(struct dquot *dquot, qsize_t number) |
| 833 | { |
| 834 | if (dquot->dq_dqb.dqb_curspace > number) |
| 835 | dquot->dq_dqb.dqb_curspace -= number; |
| 836 | else |
| 837 | dquot->dq_dqb.dqb_curspace = 0; |
| 838 | if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit) |
| 839 | dquot->dq_dqb.dqb_btime = (time_t) 0; |
| 840 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); |
| 841 | } |
| 842 | |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 843 | static int warning_issued(struct dquot *dquot, const int warntype) |
| 844 | { |
| 845 | int flag = (warntype == QUOTA_NL_BHARDWARN || |
| 846 | warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B : |
| 847 | ((warntype == QUOTA_NL_IHARDWARN || |
| 848 | warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0); |
| 849 | |
| 850 | if (!flag) |
| 851 | return 0; |
| 852 | return test_and_set_bit(flag, &dquot->dq_flags); |
| 853 | } |
| 854 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 855 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | static int flag_print_warnings = 1; |
| 857 | |
| 858 | static inline int need_print_warning(struct dquot *dquot) |
| 859 | { |
| 860 | if (!flag_print_warnings) |
| 861 | return 0; |
| 862 | |
| 863 | switch (dquot->dq_type) { |
| 864 | case USRQUOTA: |
| 865 | return current->fsuid == dquot->dq_id; |
| 866 | case GRPQUOTA: |
| 867 | return in_group_p(dquot->dq_id); |
| 868 | } |
| 869 | return 0; |
| 870 | } |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | /* Print warning to user which exceeded quota */ |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 873 | static void print_warning(struct dquot *dquot, const int warntype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
| 875 | char *msg = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 876 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 878 | if (!need_print_warning(dquot)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return; |
| 880 | |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 881 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 882 | tty = get_current_tty(); |
| 883 | if (!tty) |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 884 | goto out_lock; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 885 | tty_write_message(tty, dquot->dq_sb->s_id); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 886 | if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 887 | tty_write_message(tty, ": warning, "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | else |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 889 | tty_write_message(tty, ": write failed, "); |
| 890 | tty_write_message(tty, quotatypes[dquot->dq_type]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | switch (warntype) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 892 | case QUOTA_NL_IHARDWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | msg = " file limit reached.\r\n"; |
| 894 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 895 | case QUOTA_NL_ISOFTLONGWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | msg = " file quota exceeded too long.\r\n"; |
| 897 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 898 | case QUOTA_NL_ISOFTWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | msg = " file quota exceeded.\r\n"; |
| 900 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 901 | case QUOTA_NL_BHARDWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | msg = " block limit reached.\r\n"; |
| 903 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 904 | case QUOTA_NL_BSOFTLONGWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | msg = " block quota exceeded too long.\r\n"; |
| 906 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 907 | case QUOTA_NL_BSOFTWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | msg = " block quota exceeded.\r\n"; |
| 909 | break; |
| 910 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 911 | tty_write_message(tty, msg); |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 912 | out_lock: |
| 913 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 915 | #endif |
| 916 | |
| 917 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 918 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 919 | /* Netlink family structure for quota */ |
| 920 | static struct genl_family quota_genl_family = { |
| 921 | .id = GENL_ID_GENERATE, |
| 922 | .hdrsize = 0, |
| 923 | .name = "VFS_DQUOT", |
| 924 | .version = 1, |
| 925 | .maxattr = QUOTA_NL_A_MAX, |
| 926 | }; |
| 927 | |
| 928 | /* Send warning to userspace about user which exceeded quota */ |
| 929 | static void send_warning(const struct dquot *dquot, const char warntype) |
| 930 | { |
| 931 | static atomic_t seq; |
| 932 | struct sk_buff *skb; |
| 933 | void *msg_head; |
| 934 | int ret; |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 935 | int msg_size = 4 * nla_total_size(sizeof(u32)) + |
| 936 | 2 * nla_total_size(sizeof(u64)); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 937 | |
| 938 | /* We have to allocate using GFP_NOFS as we are called from a |
| 939 | * filesystem performing write and thus further recursion into |
| 940 | * the fs to free some data could cause deadlocks. */ |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 941 | skb = genlmsg_new(msg_size, GFP_NOFS); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 942 | if (!skb) { |
| 943 | printk(KERN_ERR |
| 944 | "VFS: Not enough memory to send quota warning.\n"); |
| 945 | return; |
| 946 | } |
| 947 | msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq), |
| 948 | "a_genl_family, 0, QUOTA_NL_C_WARNING); |
| 949 | if (!msg_head) { |
| 950 | printk(KERN_ERR |
| 951 | "VFS: Cannot store netlink header in quota warning.\n"); |
| 952 | goto err_out; |
| 953 | } |
| 954 | ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type); |
| 955 | if (ret) |
| 956 | goto attr_err_out; |
| 957 | ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id); |
| 958 | if (ret) |
| 959 | goto attr_err_out; |
| 960 | ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype); |
| 961 | if (ret) |
| 962 | goto attr_err_out; |
| 963 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, |
| 964 | MAJOR(dquot->dq_sb->s_dev)); |
| 965 | if (ret) |
| 966 | goto attr_err_out; |
| 967 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, |
| 968 | MINOR(dquot->dq_sb->s_dev)); |
| 969 | if (ret) |
| 970 | goto attr_err_out; |
| 971 | ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid); |
| 972 | if (ret) |
| 973 | goto attr_err_out; |
| 974 | genlmsg_end(skb, msg_head); |
| 975 | |
| 976 | ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS); |
| 977 | if (ret < 0 && ret != -ESRCH) |
| 978 | printk(KERN_ERR |
| 979 | "VFS: Failed to send notification message: %d\n", ret); |
| 980 | return; |
| 981 | attr_err_out: |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 982 | printk(KERN_ERR "VFS: Not enough space to compose quota message!\n"); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 983 | err_out: |
| 984 | kfree_skb(skb); |
| 985 | } |
| 986 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
Jan Kara | 087ee8d | 2007-12-17 16:20:26 -0800 | [diff] [blame] | 988 | static inline void flush_warnings(struct dquot * const *dquots, char *warntype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | { |
| 990 | int i; |
| 991 | |
| 992 | for (i = 0; i < MAXQUOTAS; i++) |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 993 | if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN && |
| 994 | !warning_issued(dquots[i], warntype[i])) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 995 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | print_warning(dquots[i], warntype[i]); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 997 | #endif |
| 998 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 999 | send_warning(dquots[i], warntype[i]); |
| 1000 | #endif |
| 1001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static inline char ignore_hardlimit(struct dquot *dquot) |
| 1005 | { |
| 1006 | struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; |
| 1007 | |
| 1008 | return capable(CAP_SYS_RESOURCE) && |
| 1009 | (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH)); |
| 1010 | } |
| 1011 | |
| 1012 | /* needs dq_data_lock */ |
| 1013 | static int check_idq(struct dquot *dquot, ulong inodes, char *warntype) |
| 1014 | { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1015 | *warntype = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) |
| 1017 | return QUOTA_OK; |
| 1018 | |
| 1019 | if (dquot->dq_dqb.dqb_ihardlimit && |
| 1020 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit && |
| 1021 | !ignore_hardlimit(dquot)) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1022 | *warntype = QUOTA_NL_IHARDWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | return NO_QUOTA; |
| 1024 | } |
| 1025 | |
| 1026 | if (dquot->dq_dqb.dqb_isoftlimit && |
| 1027 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && |
| 1028 | dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime && |
| 1029 | !ignore_hardlimit(dquot)) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1030 | *warntype = QUOTA_NL_ISOFTLONGWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | return NO_QUOTA; |
| 1032 | } |
| 1033 | |
| 1034 | if (dquot->dq_dqb.dqb_isoftlimit && |
| 1035 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && |
| 1036 | dquot->dq_dqb.dqb_itime == 0) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1037 | *warntype = QUOTA_NL_ISOFTWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace; |
| 1039 | } |
| 1040 | |
| 1041 | return QUOTA_OK; |
| 1042 | } |
| 1043 | |
| 1044 | /* needs dq_data_lock */ |
| 1045 | static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype) |
| 1046 | { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1047 | *warntype = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) |
| 1049 | return QUOTA_OK; |
| 1050 | |
| 1051 | if (dquot->dq_dqb.dqb_bhardlimit && |
| 1052 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit && |
| 1053 | !ignore_hardlimit(dquot)) { |
| 1054 | if (!prealloc) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1055 | *warntype = QUOTA_NL_BHARDWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return NO_QUOTA; |
| 1057 | } |
| 1058 | |
| 1059 | if (dquot->dq_dqb.dqb_bsoftlimit && |
| 1060 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && |
| 1061 | dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime && |
| 1062 | !ignore_hardlimit(dquot)) { |
| 1063 | if (!prealloc) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1064 | *warntype = QUOTA_NL_BSOFTLONGWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | return NO_QUOTA; |
| 1066 | } |
| 1067 | |
| 1068 | if (dquot->dq_dqb.dqb_bsoftlimit && |
| 1069 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && |
| 1070 | dquot->dq_dqb.dqb_btime == 0) { |
| 1071 | if (!prealloc) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1072 | *warntype = QUOTA_NL_BSOFTWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace; |
| 1074 | } |
| 1075 | else |
| 1076 | /* |
| 1077 | * We don't allow preallocation to exceed softlimit so exceeding will |
| 1078 | * be always printed |
| 1079 | */ |
| 1080 | return NO_QUOTA; |
| 1081 | } |
| 1082 | |
| 1083 | return QUOTA_OK; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Initialize quota pointers in inode |
| 1088 | * Transaction must be started at entry |
| 1089 | */ |
| 1090 | int dquot_initialize(struct inode *inode, int type) |
| 1091 | { |
| 1092 | unsigned int id = 0; |
| 1093 | int cnt, ret = 0; |
| 1094 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1095 | /* First test before acquiring mutex - solves deadlocks when we |
| 1096 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | if (IS_NOQUOTA(inode)) |
| 1098 | return 0; |
| 1099 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1100 | /* Having dqptr_sem we know NOQUOTA flags can't be altered... */ |
| 1101 | if (IS_NOQUOTA(inode)) |
| 1102 | goto out_err; |
| 1103 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1104 | if (type != -1 && cnt != type) |
| 1105 | continue; |
| 1106 | if (inode->i_dquot[cnt] == NODQUOT) { |
| 1107 | switch (cnt) { |
| 1108 | case USRQUOTA: |
| 1109 | id = inode->i_uid; |
| 1110 | break; |
| 1111 | case GRPQUOTA: |
| 1112 | id = inode->i_gid; |
| 1113 | break; |
| 1114 | } |
| 1115 | inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt); |
| 1116 | } |
| 1117 | } |
| 1118 | out_err: |
| 1119 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Release all quotas referenced by inode |
| 1125 | * Transaction must be started at an entry |
| 1126 | */ |
| 1127 | int dquot_drop(struct inode *inode) |
| 1128 | { |
| 1129 | int cnt; |
| 1130 | |
| 1131 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1132 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1133 | if (inode->i_dquot[cnt] != NODQUOT) { |
| 1134 | dqput(inode->i_dquot[cnt]); |
| 1135 | inode->i_dquot[cnt] = NODQUOT; |
| 1136 | } |
| 1137 | } |
| 1138 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Following four functions update i_blocks+i_bytes fields and |
| 1144 | * quota information (together with appropriate checks) |
| 1145 | * NOTE: We absolutely rely on the fact that caller dirties |
| 1146 | * the inode (usually macros in quotaops.h care about this) and |
| 1147 | * holds a handle for the current transaction so that dquot write and |
| 1148 | * inode write go into the same transaction. |
| 1149 | */ |
| 1150 | |
| 1151 | /* |
| 1152 | * This operation can block, but only after everything is updated |
| 1153 | */ |
| 1154 | int dquot_alloc_space(struct inode *inode, qsize_t number, int warn) |
| 1155 | { |
| 1156 | int cnt, ret = NO_QUOTA; |
| 1157 | char warntype[MAXQUOTAS]; |
| 1158 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1159 | /* First test before acquiring mutex - solves deadlocks when we |
| 1160 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | if (IS_NOQUOTA(inode)) { |
| 1162 | out_add: |
| 1163 | inode_add_bytes(inode, number); |
| 1164 | return QUOTA_OK; |
| 1165 | } |
| 1166 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1167 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
| 1169 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1170 | if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */ |
| 1171 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1172 | goto out_add; |
| 1173 | } |
| 1174 | spin_lock(&dq_data_lock); |
| 1175 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1176 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1177 | continue; |
| 1178 | if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA) |
| 1179 | goto warn_put_all; |
| 1180 | } |
| 1181 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1182 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1183 | continue; |
| 1184 | dquot_incr_space(inode->i_dquot[cnt], number); |
| 1185 | } |
| 1186 | inode_add_bytes(inode, number); |
| 1187 | ret = QUOTA_OK; |
| 1188 | warn_put_all: |
| 1189 | spin_unlock(&dq_data_lock); |
| 1190 | if (ret == QUOTA_OK) |
| 1191 | /* Dirtify all the dquots - this can block when journalling */ |
| 1192 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1193 | if (inode->i_dquot[cnt]) |
| 1194 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1195 | flush_warnings(inode->i_dquot, warntype); |
| 1196 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1197 | return ret; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * This operation can block, but only after everything is updated |
| 1202 | */ |
| 1203 | int dquot_alloc_inode(const struct inode *inode, unsigned long number) |
| 1204 | { |
| 1205 | int cnt, ret = NO_QUOTA; |
| 1206 | char warntype[MAXQUOTAS]; |
| 1207 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1208 | /* First test before acquiring mutex - solves deadlocks when we |
| 1209 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | if (IS_NOQUOTA(inode)) |
| 1211 | return QUOTA_OK; |
| 1212 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1213 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1215 | if (IS_NOQUOTA(inode)) { |
| 1216 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1217 | return QUOTA_OK; |
| 1218 | } |
| 1219 | spin_lock(&dq_data_lock); |
| 1220 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1221 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1222 | continue; |
| 1223 | if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA) |
| 1224 | goto warn_put_all; |
| 1225 | } |
| 1226 | |
| 1227 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1228 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1229 | continue; |
| 1230 | dquot_incr_inodes(inode->i_dquot[cnt], number); |
| 1231 | } |
| 1232 | ret = QUOTA_OK; |
| 1233 | warn_put_all: |
| 1234 | spin_unlock(&dq_data_lock); |
| 1235 | if (ret == QUOTA_OK) |
| 1236 | /* Dirtify all the dquots - this can block when journalling */ |
| 1237 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1238 | if (inode->i_dquot[cnt]) |
| 1239 | mark_dquot_dirty(inode->i_dquot[cnt]); |
Jan Kara | 087ee8d | 2007-12-17 16:20:26 -0800 | [diff] [blame] | 1240 | flush_warnings(inode->i_dquot, warntype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * This operation can block, but only after everything is updated |
| 1247 | */ |
| 1248 | int dquot_free_space(struct inode *inode, qsize_t number) |
| 1249 | { |
| 1250 | unsigned int cnt; |
| 1251 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1252 | /* First test before acquiring mutex - solves deadlocks when we |
| 1253 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (IS_NOQUOTA(inode)) { |
| 1255 | out_sub: |
| 1256 | inode_sub_bytes(inode, number); |
| 1257 | return QUOTA_OK; |
| 1258 | } |
| 1259 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1260 | /* Now recheck reliably when holding dqptr_sem */ |
| 1261 | if (IS_NOQUOTA(inode)) { |
| 1262 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1263 | goto out_sub; |
| 1264 | } |
| 1265 | spin_lock(&dq_data_lock); |
| 1266 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1267 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1268 | continue; |
| 1269 | dquot_decr_space(inode->i_dquot[cnt], number); |
| 1270 | } |
| 1271 | inode_sub_bytes(inode, number); |
| 1272 | spin_unlock(&dq_data_lock); |
| 1273 | /* Dirtify all the dquots - this can block when journalling */ |
| 1274 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1275 | if (inode->i_dquot[cnt]) |
| 1276 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1277 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1278 | return QUOTA_OK; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * This operation can block, but only after everything is updated |
| 1283 | */ |
| 1284 | int dquot_free_inode(const struct inode *inode, unsigned long number) |
| 1285 | { |
| 1286 | unsigned int cnt; |
| 1287 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1288 | /* First test before acquiring mutex - solves deadlocks when we |
| 1289 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | if (IS_NOQUOTA(inode)) |
| 1291 | return QUOTA_OK; |
| 1292 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1293 | /* Now recheck reliably when holding dqptr_sem */ |
| 1294 | if (IS_NOQUOTA(inode)) { |
| 1295 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1296 | return QUOTA_OK; |
| 1297 | } |
| 1298 | spin_lock(&dq_data_lock); |
| 1299 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1300 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1301 | continue; |
| 1302 | dquot_decr_inodes(inode->i_dquot[cnt], number); |
| 1303 | } |
| 1304 | spin_unlock(&dq_data_lock); |
| 1305 | /* Dirtify all the dquots - this can block when journalling */ |
| 1306 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1307 | if (inode->i_dquot[cnt]) |
| 1308 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1309 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1310 | return QUOTA_OK; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * Transfer the number of inode and blocks from one diskquota to an other. |
| 1315 | * |
| 1316 | * This operation can block, but only after everything is updated |
| 1317 | * A transaction must be started when entering this function. |
| 1318 | */ |
| 1319 | int dquot_transfer(struct inode *inode, struct iattr *iattr) |
| 1320 | { |
| 1321 | qsize_t space; |
| 1322 | struct dquot *transfer_from[MAXQUOTAS]; |
| 1323 | struct dquot *transfer_to[MAXQUOTAS]; |
| 1324 | int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid, |
| 1325 | chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid; |
| 1326 | char warntype[MAXQUOTAS]; |
| 1327 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1328 | /* First test before acquiring mutex - solves deadlocks when we |
| 1329 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | if (IS_NOQUOTA(inode)) |
| 1331 | return QUOTA_OK; |
| 1332 | /* Clear the arrays */ |
| 1333 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1334 | transfer_to[cnt] = transfer_from[cnt] = NODQUOT; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1335 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1338 | /* Now recheck reliably when holding dqptr_sem */ |
| 1339 | if (IS_NOQUOTA(inode)) { /* File without quota accounting? */ |
| 1340 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1341 | return QUOTA_OK; |
| 1342 | } |
| 1343 | /* First build the transfer_to list - here we can block on |
| 1344 | * reading/instantiating of dquots. We know that the transaction for |
| 1345 | * us was already started so we don't violate lock ranking here */ |
| 1346 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1347 | switch (cnt) { |
| 1348 | case USRQUOTA: |
| 1349 | if (!chuid) |
| 1350 | continue; |
| 1351 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt); |
| 1352 | break; |
| 1353 | case GRPQUOTA: |
| 1354 | if (!chgid) |
| 1355 | continue; |
| 1356 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt); |
| 1357 | break; |
| 1358 | } |
| 1359 | } |
| 1360 | spin_lock(&dq_data_lock); |
| 1361 | space = inode_get_bytes(inode); |
| 1362 | /* Build the transfer_from list and check the limits */ |
| 1363 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1364 | if (transfer_to[cnt] == NODQUOT) |
| 1365 | continue; |
| 1366 | transfer_from[cnt] = inode->i_dquot[cnt]; |
| 1367 | if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA || |
| 1368 | check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA) |
| 1369 | goto warn_put_all; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * Finally perform the needed transfer from transfer_from to transfer_to |
| 1374 | */ |
| 1375 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1376 | /* |
| 1377 | * Skip changes for same uid or gid or for turned off quota-type. |
| 1378 | */ |
| 1379 | if (transfer_to[cnt] == NODQUOT) |
| 1380 | continue; |
| 1381 | |
| 1382 | /* Due to IO error we might not have transfer_from[] structure */ |
| 1383 | if (transfer_from[cnt]) { |
| 1384 | dquot_decr_inodes(transfer_from[cnt], 1); |
| 1385 | dquot_decr_space(transfer_from[cnt], space); |
| 1386 | } |
| 1387 | |
| 1388 | dquot_incr_inodes(transfer_to[cnt], 1); |
| 1389 | dquot_incr_space(transfer_to[cnt], space); |
| 1390 | |
| 1391 | inode->i_dquot[cnt] = transfer_to[cnt]; |
| 1392 | } |
| 1393 | ret = QUOTA_OK; |
| 1394 | warn_put_all: |
| 1395 | spin_unlock(&dq_data_lock); |
| 1396 | /* Dirtify all the dquots - this can block when journalling */ |
| 1397 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1398 | if (transfer_from[cnt]) |
| 1399 | mark_dquot_dirty(transfer_from[cnt]); |
| 1400 | if (transfer_to[cnt]) |
| 1401 | mark_dquot_dirty(transfer_to[cnt]); |
| 1402 | } |
| 1403 | flush_warnings(transfer_to, warntype); |
| 1404 | |
| 1405 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1406 | if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT) |
| 1407 | dqput(transfer_from[cnt]); |
| 1408 | if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT) |
| 1409 | dqput(transfer_to[cnt]); |
| 1410 | } |
| 1411 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1412 | return ret; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * Write info of quota file to disk |
| 1417 | */ |
| 1418 | int dquot_commit_info(struct super_block *sb, int type) |
| 1419 | { |
| 1420 | int ret; |
| 1421 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1422 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1423 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | ret = dqopt->ops[type]->write_file_info(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1425 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Definitions of diskquota operations. |
| 1431 | */ |
| 1432 | struct dquot_operations dquot_operations = { |
| 1433 | .initialize = dquot_initialize, |
| 1434 | .drop = dquot_drop, |
| 1435 | .alloc_space = dquot_alloc_space, |
| 1436 | .alloc_inode = dquot_alloc_inode, |
| 1437 | .free_space = dquot_free_space, |
| 1438 | .free_inode = dquot_free_inode, |
| 1439 | .transfer = dquot_transfer, |
| 1440 | .write_dquot = dquot_commit, |
| 1441 | .acquire_dquot = dquot_acquire, |
| 1442 | .release_dquot = dquot_release, |
| 1443 | .mark_dirty = dquot_mark_dquot_dirty, |
| 1444 | .write_info = dquot_commit_info |
| 1445 | }; |
| 1446 | |
| 1447 | static inline void set_enable_flags(struct quota_info *dqopt, int type) |
| 1448 | { |
| 1449 | switch (type) { |
| 1450 | case USRQUOTA: |
| 1451 | dqopt->flags |= DQUOT_USR_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1452 | dqopt->flags &= ~DQUOT_USR_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | break; |
| 1454 | case GRPQUOTA: |
| 1455 | dqopt->flags |= DQUOT_GRP_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1456 | dqopt->flags &= ~DQUOT_GRP_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | } |
| 1460 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1461 | static inline void reset_enable_flags(struct quota_info *dqopt, int type, |
| 1462 | int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { |
| 1464 | switch (type) { |
| 1465 | case USRQUOTA: |
| 1466 | dqopt->flags &= ~DQUOT_USR_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1467 | if (remount) |
| 1468 | dqopt->flags |= DQUOT_USR_SUSPENDED; |
| 1469 | else |
| 1470 | dqopt->flags &= ~DQUOT_USR_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | break; |
| 1472 | case GRPQUOTA: |
| 1473 | dqopt->flags &= ~DQUOT_GRP_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1474 | if (remount) |
| 1475 | dqopt->flags |= DQUOT_GRP_SUSPENDED; |
| 1476 | else |
| 1477 | dqopt->flags &= ~DQUOT_GRP_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount) |
| 1485 | */ |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1486 | int vfs_quota_off(struct super_block *sb, int type, int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | { |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1488 | int cnt, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1490 | struct inode *toputinode[MAXQUOTAS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | /* We need to serialize quota_off() for device */ |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1493 | mutex_lock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1495 | toputinode[cnt] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | if (type != -1 && cnt != type) |
| 1497 | continue; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1498 | /* If we keep inodes of quota files after remount and quotaoff |
| 1499 | * is called, drop kept inodes. */ |
| 1500 | if (!remount && sb_has_quota_suspended(sb, cnt)) { |
| 1501 | iput(dqopt->files[cnt]); |
| 1502 | dqopt->files[cnt] = NULL; |
| 1503 | reset_enable_flags(dqopt, cnt, 0); |
| 1504 | continue; |
| 1505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | if (!sb_has_quota_enabled(sb, cnt)) |
| 1507 | continue; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1508 | reset_enable_flags(dqopt, cnt, remount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
| 1510 | /* Note: these are blocking operations */ |
| 1511 | drop_dquot_ref(sb, cnt); |
| 1512 | invalidate_dquots(sb, cnt); |
| 1513 | /* |
| 1514 | * Now all dquots should be invalidated, all writes done so we should be only |
| 1515 | * users of the info. No locks needed. |
| 1516 | */ |
| 1517 | if (info_dirty(&dqopt->info[cnt])) |
| 1518 | sb->dq_op->write_info(sb, cnt); |
| 1519 | if (dqopt->ops[cnt]->free_file_info) |
| 1520 | dqopt->ops[cnt]->free_file_info(sb, cnt); |
| 1521 | put_quota_format(dqopt->info[cnt].dqi_format); |
| 1522 | |
| 1523 | toputinode[cnt] = dqopt->files[cnt]; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1524 | if (!remount) |
| 1525 | dqopt->files[cnt] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | dqopt->info[cnt].dqi_flags = 0; |
| 1527 | dqopt->info[cnt].dqi_igrace = 0; |
| 1528 | dqopt->info[cnt].dqi_bgrace = 0; |
| 1529 | dqopt->ops[cnt] = NULL; |
| 1530 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1531 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | /* Sync the superblock so that buffers with quota data are written to |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 1533 | * disk (and so userspace sees correct data afterwards). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | if (sb->s_op->sync_fs) |
| 1535 | sb->s_op->sync_fs(sb, 1); |
| 1536 | sync_blockdev(sb->s_bdev); |
| 1537 | /* Now the quota files are just ordinary files and we can set the |
| 1538 | * inode flags back. Moreover we discard the pagecache so that |
| 1539 | * userspace sees the writes we did bypassing the pagecache. We |
| 1540 | * must also discard the blockdev buffers so that we see the |
| 1541 | * changes done by userspace on the next quotaon() */ |
| 1542 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1543 | if (toputinode[cnt]) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1544 | mutex_lock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | /* If quota was reenabled in the meantime, we have |
| 1546 | * nothing to do */ |
| 1547 | if (!sb_has_quota_enabled(sb, cnt)) { |
Jan Kara | 7925409 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 1548 | mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | toputinode[cnt]->i_flags &= ~(S_IMMUTABLE | |
| 1550 | S_NOATIME | S_NOQUOTA); |
| 1551 | truncate_inode_pages(&toputinode[cnt]->i_data, 0); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1552 | mutex_unlock(&toputinode[cnt]->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | mark_inode_dirty(toputinode[cnt]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1555 | mutex_unlock(&dqopt->dqonoff_mutex); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1556 | /* On remount RO, we keep the inode pointer so that we |
| 1557 | * can reenable quota on the subsequent remount RW. |
| 1558 | * But we have better not keep inode pointer when there |
| 1559 | * is pending delete on the quota file... */ |
| 1560 | if (!remount) |
| 1561 | iput(toputinode[cnt]); |
| 1562 | else if (!toputinode[cnt]->i_nlink) |
| 1563 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | if (sb->s_bdev) |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1566 | invalidate_bdev(sb->s_bdev); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1567 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * Turn quotas on on a device |
| 1572 | */ |
| 1573 | |
| 1574 | /* Helper function when we already have the inode */ |
| 1575 | static int vfs_quota_on_inode(struct inode *inode, int type, int format_id) |
| 1576 | { |
| 1577 | struct quota_format_type *fmt = find_quota_format(format_id); |
| 1578 | struct super_block *sb = inode->i_sb; |
| 1579 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1580 | int error; |
| 1581 | int oldflags = -1; |
| 1582 | |
| 1583 | if (!fmt) |
| 1584 | return -ESRCH; |
| 1585 | if (!S_ISREG(inode->i_mode)) { |
| 1586 | error = -EACCES; |
| 1587 | goto out_fmt; |
| 1588 | } |
| 1589 | if (IS_RDONLY(inode)) { |
| 1590 | error = -EROFS; |
| 1591 | goto out_fmt; |
| 1592 | } |
| 1593 | if (!sb->s_op->quota_write || !sb->s_op->quota_read) { |
| 1594 | error = -EINVAL; |
| 1595 | goto out_fmt; |
| 1596 | } |
| 1597 | |
| 1598 | /* As we bypass the pagecache we must now flush the inode so that |
| 1599 | * we see all the changes from userspace... */ |
| 1600 | write_inode_now(inode, 1); |
| 1601 | /* And now flush the block cache so that kernel sees the changes */ |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1602 | invalidate_bdev(sb->s_bdev); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1603 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1604 | mutex_lock(&dqopt->dqonoff_mutex); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1605 | if (sb_has_quota_enabled(sb, type) || |
| 1606 | sb_has_quota_suspended(sb, type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | error = -EBUSY; |
| 1608 | goto out_lock; |
| 1609 | } |
| 1610 | /* We don't want quota and atime on quota files (deadlocks possible) |
| 1611 | * Also nobody should write to the file - we use special IO operations |
| 1612 | * which ignore the immutable bit. */ |
| 1613 | down_write(&dqopt->dqptr_sem); |
| 1614 | oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA); |
| 1615 | inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE; |
| 1616 | up_write(&dqopt->dqptr_sem); |
Jan Kara | 31e7ad6 | 2005-04-16 15:25:46 -0700 | [diff] [blame] | 1617 | sb->dq_op->drop(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | error = -EIO; |
| 1620 | dqopt->files[type] = igrab(inode); |
| 1621 | if (!dqopt->files[type]) |
| 1622 | goto out_lock; |
| 1623 | error = -EINVAL; |
| 1624 | if (!fmt->qf_ops->check_quota_file(sb, type)) |
| 1625 | goto out_file_init; |
| 1626 | |
| 1627 | dqopt->ops[type] = fmt->qf_ops; |
| 1628 | dqopt->info[type].dqi_format = fmt; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1629 | dqopt->info[type].dqi_fmt_id = format_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1631 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1633 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | goto out_file_init; |
| 1635 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1636 | mutex_unlock(&dqopt->dqio_mutex); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1637 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | set_enable_flags(dqopt, type); |
| 1639 | |
| 1640 | add_dquot_ref(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1641 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
| 1643 | return 0; |
| 1644 | |
| 1645 | out_file_init: |
| 1646 | dqopt->files[type] = NULL; |
| 1647 | iput(inode); |
| 1648 | out_lock: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1649 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | if (oldflags != -1) { |
| 1651 | down_write(&dqopt->dqptr_sem); |
| 1652 | /* Set the flags back (in the case of accidental quotaon() |
| 1653 | * on a wrong file we don't want to mess up the flags) */ |
| 1654 | inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE); |
| 1655 | inode->i_flags |= oldflags; |
| 1656 | up_write(&dqopt->dqptr_sem); |
| 1657 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1658 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | out_fmt: |
| 1660 | put_quota_format(fmt); |
| 1661 | |
| 1662 | return error; |
| 1663 | } |
| 1664 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1665 | /* Reenable quotas on remount RW */ |
| 1666 | static int vfs_quota_on_remount(struct super_block *sb, int type) |
| 1667 | { |
| 1668 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1669 | struct inode *inode; |
| 1670 | int ret; |
| 1671 | |
| 1672 | mutex_lock(&dqopt->dqonoff_mutex); |
| 1673 | if (!sb_has_quota_suspended(sb, type)) { |
| 1674 | mutex_unlock(&dqopt->dqonoff_mutex); |
| 1675 | return 0; |
| 1676 | } |
| 1677 | BUG_ON(sb_has_quota_enabled(sb, type)); |
| 1678 | |
| 1679 | inode = dqopt->files[type]; |
| 1680 | dqopt->files[type] = NULL; |
| 1681 | reset_enable_flags(dqopt, type, 0); |
| 1682 | mutex_unlock(&dqopt->dqonoff_mutex); |
| 1683 | |
| 1684 | ret = vfs_quota_on_inode(inode, type, dqopt->info[type].dqi_fmt_id); |
| 1685 | iput(inode); |
| 1686 | |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | /* Actual function called from quotactl() */ |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1691 | int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path, |
| 1692 | int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | { |
| 1694 | struct nameidata nd; |
| 1695 | int error; |
| 1696 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1697 | if (remount) |
| 1698 | return vfs_quota_on_remount(sb, type); |
| 1699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 1701 | if (error < 0) |
| 1702 | return error; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1703 | error = security_quota_on(nd.path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | if (error) |
| 1705 | goto out_path; |
| 1706 | /* Quota file not on the same filesystem? */ |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1707 | if (nd.path.mnt->mnt_sb != sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | error = -EXDEV; |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 1709 | else |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1710 | error = vfs_quota_on_inode(nd.path.dentry->d_inode, type, |
| 1711 | format_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | out_path: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 1713 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | return error; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * This function is used when filesystem needs to initialize quotas |
| 1719 | * during mount time. |
| 1720 | */ |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1721 | int vfs_quota_on_mount(struct super_block *sb, char *qf_name, |
| 1722 | int format_id, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | { |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1724 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | int error; |
| 1726 | |
Christoph Hellwig | 2fa389c | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1727 | dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name)); |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1728 | if (IS_ERR(dentry)) |
| 1729 | return PTR_ERR(dentry); |
| 1730 | |
Jan Kara | 154f484 | 2005-11-28 13:44:14 -0800 | [diff] [blame] | 1731 | if (!dentry->d_inode) { |
| 1732 | error = -ENOENT; |
| 1733 | goto out; |
| 1734 | } |
| 1735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | error = security_quota_on(dentry); |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1737 | if (!error) |
| 1738 | error = vfs_quota_on_inode(dentry->d_inode, type, format_id); |
| 1739 | |
Jan Kara | 154f484 | 2005-11-28 13:44:14 -0800 | [diff] [blame] | 1740 | out: |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1741 | dput(dentry); |
| 1742 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /* Generic routine for getting common part of quota structure */ |
| 1746 | static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di) |
| 1747 | { |
| 1748 | struct mem_dqblk *dm = &dquot->dq_dqb; |
| 1749 | |
| 1750 | spin_lock(&dq_data_lock); |
| 1751 | di->dqb_bhardlimit = dm->dqb_bhardlimit; |
| 1752 | di->dqb_bsoftlimit = dm->dqb_bsoftlimit; |
| 1753 | di->dqb_curspace = dm->dqb_curspace; |
| 1754 | di->dqb_ihardlimit = dm->dqb_ihardlimit; |
| 1755 | di->dqb_isoftlimit = dm->dqb_isoftlimit; |
| 1756 | di->dqb_curinodes = dm->dqb_curinodes; |
| 1757 | di->dqb_btime = dm->dqb_btime; |
| 1758 | di->dqb_itime = dm->dqb_itime; |
| 1759 | di->dqb_valid = QIF_ALL; |
| 1760 | spin_unlock(&dq_data_lock); |
| 1761 | } |
| 1762 | |
| 1763 | int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) |
| 1764 | { |
| 1765 | struct dquot *dquot; |
| 1766 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1767 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | if (!(dquot = dqget(sb, id, type))) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1769 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | return -ESRCH; |
| 1771 | } |
| 1772 | do_get_dqblk(dquot, di); |
| 1773 | dqput(dquot); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1774 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | return 0; |
| 1776 | } |
| 1777 | |
| 1778 | /* Generic routine for setting common part of quota structure */ |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1779 | static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | { |
| 1781 | struct mem_dqblk *dm = &dquot->dq_dqb; |
| 1782 | int check_blim = 0, check_ilim = 0; |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1783 | struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; |
| 1784 | |
| 1785 | if ((di->dqb_valid & QIF_BLIMITS && |
| 1786 | (di->dqb_bhardlimit > dqi->dqi_maxblimit || |
| 1787 | di->dqb_bsoftlimit > dqi->dqi_maxblimit)) || |
| 1788 | (di->dqb_valid & QIF_ILIMITS && |
| 1789 | (di->dqb_ihardlimit > dqi->dqi_maxilimit || |
| 1790 | di->dqb_isoftlimit > dqi->dqi_maxilimit))) |
| 1791 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
| 1793 | spin_lock(&dq_data_lock); |
| 1794 | if (di->dqb_valid & QIF_SPACE) { |
| 1795 | dm->dqb_curspace = di->dqb_curspace; |
| 1796 | check_blim = 1; |
| 1797 | } |
| 1798 | if (di->dqb_valid & QIF_BLIMITS) { |
| 1799 | dm->dqb_bsoftlimit = di->dqb_bsoftlimit; |
| 1800 | dm->dqb_bhardlimit = di->dqb_bhardlimit; |
| 1801 | check_blim = 1; |
| 1802 | } |
| 1803 | if (di->dqb_valid & QIF_INODES) { |
| 1804 | dm->dqb_curinodes = di->dqb_curinodes; |
| 1805 | check_ilim = 1; |
| 1806 | } |
| 1807 | if (di->dqb_valid & QIF_ILIMITS) { |
| 1808 | dm->dqb_isoftlimit = di->dqb_isoftlimit; |
| 1809 | dm->dqb_ihardlimit = di->dqb_ihardlimit; |
| 1810 | check_ilim = 1; |
| 1811 | } |
| 1812 | if (di->dqb_valid & QIF_BTIME) |
| 1813 | dm->dqb_btime = di->dqb_btime; |
| 1814 | if (di->dqb_valid & QIF_ITIME) |
| 1815 | dm->dqb_itime = di->dqb_itime; |
| 1816 | |
| 1817 | if (check_blim) { |
| 1818 | if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) { |
| 1819 | dm->dqb_btime = 0; |
| 1820 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); |
| 1821 | } |
| 1822 | 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] | 1823 | dm->dqb_btime = get_seconds() + dqi->dqi_bgrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | } |
| 1825 | if (check_ilim) { |
| 1826 | if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) { |
| 1827 | dm->dqb_itime = 0; |
| 1828 | clear_bit(DQ_INODES_B, &dquot->dq_flags); |
| 1829 | } |
| 1830 | 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] | 1831 | dm->dqb_itime = get_seconds() + dqi->dqi_igrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit) |
| 1834 | clear_bit(DQ_FAKE_B, &dquot->dq_flags); |
| 1835 | else |
| 1836 | set_bit(DQ_FAKE_B, &dquot->dq_flags); |
| 1837 | spin_unlock(&dq_data_lock); |
| 1838 | mark_dquot_dirty(dquot); |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1839 | |
| 1840 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) |
| 1844 | { |
| 1845 | struct dquot *dquot; |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1846 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1848 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | if (!(dquot = dqget(sb, id, type))) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1850 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | return -ESRCH; |
| 1852 | } |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1853 | rc = do_set_dqblk(dquot, di); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | dqput(dquot); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1855 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1856 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /* Generic routine for getting common part of quota file information */ |
| 1860 | int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
| 1861 | { |
| 1862 | struct mem_dqinfo *mi; |
| 1863 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1864 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | if (!sb_has_quota_enabled(sb, type)) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1866 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | return -ESRCH; |
| 1868 | } |
| 1869 | mi = sb_dqopt(sb)->info + type; |
| 1870 | spin_lock(&dq_data_lock); |
| 1871 | ii->dqi_bgrace = mi->dqi_bgrace; |
| 1872 | ii->dqi_igrace = mi->dqi_igrace; |
| 1873 | ii->dqi_flags = mi->dqi_flags & DQF_MASK; |
| 1874 | ii->dqi_valid = IIF_ALL; |
| 1875 | spin_unlock(&dq_data_lock); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1876 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | /* Generic routine for setting common part of quota file information */ |
| 1881 | int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
| 1882 | { |
| 1883 | struct mem_dqinfo *mi; |
| 1884 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1885 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | if (!sb_has_quota_enabled(sb, type)) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1887 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | return -ESRCH; |
| 1889 | } |
| 1890 | mi = sb_dqopt(sb)->info + type; |
| 1891 | spin_lock(&dq_data_lock); |
| 1892 | if (ii->dqi_valid & IIF_BGRACE) |
| 1893 | mi->dqi_bgrace = ii->dqi_bgrace; |
| 1894 | if (ii->dqi_valid & IIF_IGRACE) |
| 1895 | mi->dqi_igrace = ii->dqi_igrace; |
| 1896 | if (ii->dqi_valid & IIF_FLAGS) |
| 1897 | mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK); |
| 1898 | spin_unlock(&dq_data_lock); |
| 1899 | mark_info_dirty(sb, type); |
| 1900 | /* Force write to disk */ |
| 1901 | sb->dq_op->write_info(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1902 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | struct quotactl_ops vfs_quotactl_ops = { |
| 1907 | .quota_on = vfs_quota_on, |
| 1908 | .quota_off = vfs_quota_off, |
| 1909 | .quota_sync = vfs_quota_sync, |
| 1910 | .get_info = vfs_get_dqinfo, |
| 1911 | .set_info = vfs_set_dqinfo, |
| 1912 | .get_dqblk = vfs_get_dqblk, |
| 1913 | .set_dqblk = vfs_set_dqblk |
| 1914 | }; |
| 1915 | |
| 1916 | static ctl_table fs_dqstats_table[] = { |
| 1917 | { |
| 1918 | .ctl_name = FS_DQ_LOOKUPS, |
| 1919 | .procname = "lookups", |
| 1920 | .data = &dqstats.lookups, |
| 1921 | .maxlen = sizeof(int), |
| 1922 | .mode = 0444, |
| 1923 | .proc_handler = &proc_dointvec, |
| 1924 | }, |
| 1925 | { |
| 1926 | .ctl_name = FS_DQ_DROPS, |
| 1927 | .procname = "drops", |
| 1928 | .data = &dqstats.drops, |
| 1929 | .maxlen = sizeof(int), |
| 1930 | .mode = 0444, |
| 1931 | .proc_handler = &proc_dointvec, |
| 1932 | }, |
| 1933 | { |
| 1934 | .ctl_name = FS_DQ_READS, |
| 1935 | .procname = "reads", |
| 1936 | .data = &dqstats.reads, |
| 1937 | .maxlen = sizeof(int), |
| 1938 | .mode = 0444, |
| 1939 | .proc_handler = &proc_dointvec, |
| 1940 | }, |
| 1941 | { |
| 1942 | .ctl_name = FS_DQ_WRITES, |
| 1943 | .procname = "writes", |
| 1944 | .data = &dqstats.writes, |
| 1945 | .maxlen = sizeof(int), |
| 1946 | .mode = 0444, |
| 1947 | .proc_handler = &proc_dointvec, |
| 1948 | }, |
| 1949 | { |
| 1950 | .ctl_name = FS_DQ_CACHE_HITS, |
| 1951 | .procname = "cache_hits", |
| 1952 | .data = &dqstats.cache_hits, |
| 1953 | .maxlen = sizeof(int), |
| 1954 | .mode = 0444, |
| 1955 | .proc_handler = &proc_dointvec, |
| 1956 | }, |
| 1957 | { |
| 1958 | .ctl_name = FS_DQ_ALLOCATED, |
| 1959 | .procname = "allocated_dquots", |
| 1960 | .data = &dqstats.allocated_dquots, |
| 1961 | .maxlen = sizeof(int), |
| 1962 | .mode = 0444, |
| 1963 | .proc_handler = &proc_dointvec, |
| 1964 | }, |
| 1965 | { |
| 1966 | .ctl_name = FS_DQ_FREE, |
| 1967 | .procname = "free_dquots", |
| 1968 | .data = &dqstats.free_dquots, |
| 1969 | .maxlen = sizeof(int), |
| 1970 | .mode = 0444, |
| 1971 | .proc_handler = &proc_dointvec, |
| 1972 | }, |
| 1973 | { |
| 1974 | .ctl_name = FS_DQ_SYNCS, |
| 1975 | .procname = "syncs", |
| 1976 | .data = &dqstats.syncs, |
| 1977 | .maxlen = sizeof(int), |
| 1978 | .mode = 0444, |
| 1979 | .proc_handler = &proc_dointvec, |
| 1980 | }, |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1981 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | { |
| 1983 | .ctl_name = FS_DQ_WARNINGS, |
| 1984 | .procname = "warnings", |
| 1985 | .data = &flag_print_warnings, |
| 1986 | .maxlen = sizeof(int), |
| 1987 | .mode = 0644, |
| 1988 | .proc_handler = &proc_dointvec, |
| 1989 | }, |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1990 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | { .ctl_name = 0 }, |
| 1992 | }; |
| 1993 | |
| 1994 | static ctl_table fs_table[] = { |
| 1995 | { |
| 1996 | .ctl_name = FS_DQSTATS, |
| 1997 | .procname = "quota", |
| 1998 | .mode = 0555, |
| 1999 | .child = fs_dqstats_table, |
| 2000 | }, |
| 2001 | { .ctl_name = 0 }, |
| 2002 | }; |
| 2003 | |
| 2004 | static ctl_table sys_table[] = { |
| 2005 | { |
| 2006 | .ctl_name = CTL_FS, |
| 2007 | .procname = "fs", |
| 2008 | .mode = 0555, |
| 2009 | .child = fs_table, |
| 2010 | }, |
| 2011 | { .ctl_name = 0 }, |
| 2012 | }; |
| 2013 | |
| 2014 | static int __init dquot_init(void) |
| 2015 | { |
| 2016 | int i; |
| 2017 | unsigned long nr_hash, order; |
| 2018 | |
| 2019 | printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__); |
| 2020 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 2021 | register_sysctl_table(sys_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2023 | dquot_cachep = kmem_cache_create("dquot", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | sizeof(struct dquot), sizeof(unsigned long) * 4, |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 2025 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 2026 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2027 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
| 2029 | order = 0; |
| 2030 | dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order); |
| 2031 | if (!dquot_hash) |
| 2032 | panic("Cannot create dquot hash table"); |
| 2033 | |
| 2034 | /* Find power-of-two hlist_heads which can fit into allocation */ |
| 2035 | nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head); |
| 2036 | dq_hash_bits = 0; |
| 2037 | do { |
| 2038 | dq_hash_bits++; |
| 2039 | } while (nr_hash >> dq_hash_bits); |
| 2040 | dq_hash_bits--; |
| 2041 | |
| 2042 | nr_hash = 1UL << dq_hash_bits; |
| 2043 | dq_hash_mask = nr_hash - 1; |
| 2044 | for (i = 0; i < nr_hash; i++) |
| 2045 | INIT_HLIST_HEAD(dquot_hash + i); |
| 2046 | |
| 2047 | printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n", |
| 2048 | nr_hash, order, (PAGE_SIZE << order)); |
| 2049 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 2050 | register_shrinker(&dqcache_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 2052 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 2053 | if (genl_register_family("a_genl_family) != 0) |
| 2054 | printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n"); |
| 2055 | #endif |
| 2056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | return 0; |
| 2058 | } |
| 2059 | module_init(dquot_init); |
| 2060 | |
| 2061 | EXPORT_SYMBOL(register_quota_format); |
| 2062 | EXPORT_SYMBOL(unregister_quota_format); |
| 2063 | EXPORT_SYMBOL(dqstats); |
| 2064 | EXPORT_SYMBOL(dq_data_lock); |
| 2065 | EXPORT_SYMBOL(vfs_quota_on); |
| 2066 | EXPORT_SYMBOL(vfs_quota_on_mount); |
| 2067 | EXPORT_SYMBOL(vfs_quota_off); |
| 2068 | EXPORT_SYMBOL(vfs_quota_sync); |
| 2069 | EXPORT_SYMBOL(vfs_get_dqinfo); |
| 2070 | EXPORT_SYMBOL(vfs_set_dqinfo); |
| 2071 | EXPORT_SYMBOL(vfs_get_dqblk); |
| 2072 | EXPORT_SYMBOL(vfs_set_dqblk); |
| 2073 | EXPORT_SYMBOL(dquot_commit); |
| 2074 | EXPORT_SYMBOL(dquot_commit_info); |
| 2075 | EXPORT_SYMBOL(dquot_acquire); |
| 2076 | EXPORT_SYMBOL(dquot_release); |
| 2077 | EXPORT_SYMBOL(dquot_mark_dquot_dirty); |
| 2078 | EXPORT_SYMBOL(dquot_initialize); |
| 2079 | EXPORT_SYMBOL(dquot_drop); |
| 2080 | EXPORT_SYMBOL(dquot_alloc_space); |
| 2081 | EXPORT_SYMBOL(dquot_alloc_inode); |
| 2082 | EXPORT_SYMBOL(dquot_free_space); |
| 2083 | EXPORT_SYMBOL(dquot_free_inode); |
| 2084 | EXPORT_SYMBOL(dquot_transfer); |