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 | { |
Jan Kara | b48d380 | 2008-07-25 01:46:49 -0700 | [diff] [blame] | 565 | int ret; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!dquot) |
| 568 | return; |
| 569 | #ifdef __DQUOT_PARANOIA |
| 570 | if (!atomic_read(&dquot->dq_count)) { |
| 571 | printk("VFS: dqput: trying to free free dquot\n"); |
| 572 | printk("VFS: device %s, dquot of %s %d\n", |
| 573 | dquot->dq_sb->s_id, |
| 574 | quotatypes[dquot->dq_type], |
| 575 | dquot->dq_id); |
| 576 | BUG(); |
| 577 | } |
| 578 | #endif |
| 579 | |
| 580 | spin_lock(&dq_list_lock); |
| 581 | dqstats.drops++; |
| 582 | spin_unlock(&dq_list_lock); |
| 583 | we_slept: |
| 584 | spin_lock(&dq_list_lock); |
| 585 | if (atomic_read(&dquot->dq_count) > 1) { |
| 586 | /* We have more than one user... nothing to do */ |
| 587 | atomic_dec(&dquot->dq_count); |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 588 | /* Releasing dquot during quotaoff phase? */ |
| 589 | if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) && |
| 590 | atomic_read(&dquot->dq_count) == 1) |
| 591 | wake_up(&dquot->dq_wait_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | spin_unlock(&dq_list_lock); |
| 593 | return; |
| 594 | } |
| 595 | /* Need to release dquot? */ |
| 596 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) { |
| 597 | spin_unlock(&dq_list_lock); |
| 598 | /* Commit dquot before releasing */ |
Jan Kara | b48d380 | 2008-07-25 01:46:49 -0700 | [diff] [blame] | 599 | ret = dquot->dq_sb->dq_op->write_dquot(dquot); |
| 600 | if (ret < 0) { |
| 601 | printk(KERN_ERR "VFS: cannot write quota structure on " |
| 602 | "device %s (error %d). Quota may get out of " |
| 603 | "sync!\n", dquot->dq_sb->s_id, ret); |
| 604 | /* |
| 605 | * We clear dirty bit anyway, so that we avoid |
| 606 | * infinite loop here |
| 607 | */ |
| 608 | spin_lock(&dq_list_lock); |
| 609 | clear_dquot_dirty(dquot); |
| 610 | spin_unlock(&dq_list_lock); |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | goto we_slept; |
| 613 | } |
| 614 | /* Clear flag in case dquot was inactive (something bad happened) */ |
| 615 | clear_dquot_dirty(dquot); |
| 616 | if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) { |
| 617 | spin_unlock(&dq_list_lock); |
| 618 | dquot->dq_sb->dq_op->release_dquot(dquot); |
| 619 | goto we_slept; |
| 620 | } |
| 621 | atomic_dec(&dquot->dq_count); |
| 622 | #ifdef __DQUOT_PARANOIA |
| 623 | /* sanity check */ |
Eric Sesterhenn | 8abf6a4 | 2006-04-02 13:36:13 +0200 | [diff] [blame] | 624 | BUG_ON(!list_empty(&dquot->dq_free)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | #endif |
| 626 | put_dquot_last(dquot); |
| 627 | spin_unlock(&dq_list_lock); |
| 628 | } |
| 629 | |
| 630 | static struct dquot *get_empty_dquot(struct super_block *sb, int type) |
| 631 | { |
| 632 | struct dquot *dquot; |
| 633 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 634 | dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if(!dquot) |
| 636 | return NODQUOT; |
| 637 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 638 | mutex_init(&dquot->dq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | INIT_LIST_HEAD(&dquot->dq_free); |
| 640 | INIT_LIST_HEAD(&dquot->dq_inuse); |
| 641 | INIT_HLIST_NODE(&dquot->dq_hash); |
| 642 | INIT_LIST_HEAD(&dquot->dq_dirty); |
Jan Kara | 6362e4d | 2006-03-23 03:00:17 -0800 | [diff] [blame] | 643 | init_waitqueue_head(&dquot->dq_wait_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | dquot->dq_sb = sb; |
| 645 | dquot->dq_type = type; |
| 646 | atomic_set(&dquot->dq_count, 1); |
| 647 | |
| 648 | return dquot; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Get reference to dquot |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 653 | * MUST be called with either dqptr_sem or dqonoff_mutex held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | */ |
| 655 | static struct dquot *dqget(struct super_block *sb, unsigned int id, int type) |
| 656 | { |
| 657 | unsigned int hashent = hashfn(sb, id, type); |
| 658 | struct dquot *dquot, *empty = NODQUOT; |
| 659 | |
| 660 | if (!sb_has_quota_enabled(sb, type)) |
| 661 | return NODQUOT; |
| 662 | we_slept: |
| 663 | spin_lock(&dq_list_lock); |
| 664 | if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) { |
| 665 | if (empty == NODQUOT) { |
| 666 | spin_unlock(&dq_list_lock); |
| 667 | if ((empty = get_empty_dquot(sb, type)) == NODQUOT) |
| 668 | schedule(); /* Try to wait for a moment... */ |
| 669 | goto we_slept; |
| 670 | } |
| 671 | dquot = empty; |
| 672 | dquot->dq_id = id; |
| 673 | /* all dquots go on the inuse_list */ |
| 674 | put_inuse(dquot); |
| 675 | /* hash it first so it can be found */ |
| 676 | insert_dquot_hash(dquot); |
| 677 | dqstats.lookups++; |
| 678 | spin_unlock(&dq_list_lock); |
| 679 | } else { |
| 680 | if (!atomic_read(&dquot->dq_count)) |
| 681 | remove_free_dquot(dquot); |
| 682 | atomic_inc(&dquot->dq_count); |
| 683 | dqstats.cache_hits++; |
| 684 | dqstats.lookups++; |
| 685 | spin_unlock(&dq_list_lock); |
| 686 | if (empty) |
| 687 | kmem_cache_free(dquot_cachep, empty); |
| 688 | } |
| 689 | /* Wait for dq_lock - after this we know that either dquot_release() is already |
| 690 | * finished or it will be canceled due to dq_count > 1 test */ |
| 691 | wait_on_dquot(dquot); |
| 692 | /* Read the dquot and instantiate it (everything done only if needed) */ |
| 693 | if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) { |
| 694 | dqput(dquot); |
| 695 | return NODQUOT; |
| 696 | } |
| 697 | #ifdef __DQUOT_PARANOIA |
Eric Sesterhenn | 8abf6a4 | 2006-04-02 13:36:13 +0200 | [diff] [blame] | 698 | BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | #endif |
| 700 | |
| 701 | return dquot; |
| 702 | } |
| 703 | |
| 704 | static int dqinit_needed(struct inode *inode, int type) |
| 705 | { |
| 706 | int cnt; |
| 707 | |
| 708 | if (IS_NOQUOTA(inode)) |
| 709 | return 0; |
| 710 | if (type != -1) |
| 711 | return inode->i_dquot[type] == NODQUOT; |
| 712 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 713 | if (inode->i_dquot[cnt] == NODQUOT) |
| 714 | return 1; |
| 715 | return 0; |
| 716 | } |
| 717 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 718 | /* This routine is guarded by dqonoff_mutex mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | static void add_dquot_ref(struct super_block *sb, int type) |
| 720 | { |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 721 | struct inode *inode, *old_inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 723 | spin_lock(&inode_lock); |
| 724 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
| 725 | if (!atomic_read(&inode->i_writecount)) |
| 726 | continue; |
| 727 | if (!dqinit_needed(inode, type)) |
| 728 | continue; |
| 729 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) |
| 730 | continue; |
| 731 | |
| 732 | __iget(inode); |
| 733 | spin_unlock(&inode_lock); |
| 734 | |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 735 | iput(old_inode); |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 736 | sb->dq_op->initialize(inode, type); |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 737 | /* We hold a reference to 'inode' so it couldn't have been |
| 738 | * removed from s_inodes list while we dropped the inode_lock. |
| 739 | * We cannot iput the inode now as we can be holding the last |
| 740 | * reference and we cannot iput it under inode_lock. So we |
| 741 | * keep the reference and iput it later. */ |
| 742 | old_inode = inode; |
| 743 | spin_lock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
Christoph Hellwig | d003fb7 | 2007-02-12 00:51:58 -0800 | [diff] [blame] | 745 | spin_unlock(&inode_lock); |
Jan Kara | 941d238 | 2008-02-06 01:37:36 -0800 | [diff] [blame] | 746 | iput(old_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */ |
| 750 | static inline int dqput_blocks(struct dquot *dquot) |
| 751 | { |
| 752 | if (atomic_read(&dquot->dq_count) <= 1) |
| 753 | return 1; |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /* Remove references to dquots from inode - add dquot to list for freeing if needed */ |
| 758 | /* 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] | 759 | static int remove_inode_dquot_ref(struct inode *inode, int type, |
| 760 | struct list_head *tofree_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
| 762 | struct dquot *dquot = inode->i_dquot[type]; |
| 763 | |
| 764 | inode->i_dquot[type] = NODQUOT; |
| 765 | if (dquot != NODQUOT) { |
| 766 | if (dqput_blocks(dquot)) { |
| 767 | #ifdef __DQUOT_PARANOIA |
| 768 | if (atomic_read(&dquot->dq_count) != 1) |
| 769 | printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count)); |
| 770 | #endif |
| 771 | spin_lock(&dq_list_lock); |
| 772 | list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */ |
| 773 | spin_unlock(&dq_list_lock); |
| 774 | return 1; |
| 775 | } |
| 776 | else |
| 777 | dqput(dquot); /* We have guaranteed we won't block */ |
| 778 | } |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | /* Free list of dquots - called from inode.c */ |
| 783 | /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */ |
| 784 | static void put_dquot_list(struct list_head *tofree_head) |
| 785 | { |
| 786 | struct list_head *act_head; |
| 787 | struct dquot *dquot; |
| 788 | |
| 789 | act_head = tofree_head->next; |
| 790 | /* So now we have dquots on the list... Just free them */ |
| 791 | while (act_head != tofree_head) { |
| 792 | dquot = list_entry(act_head, struct dquot, dq_free); |
| 793 | act_head = act_head->next; |
| 794 | list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */ |
| 795 | dqput(dquot); |
| 796 | } |
| 797 | } |
| 798 | |
Christoph Hellwig | fb58b73 | 2007-02-12 00:51:57 -0800 | [diff] [blame] | 799 | static void remove_dquot_ref(struct super_block *sb, int type, |
| 800 | struct list_head *tofree_head) |
| 801 | { |
| 802 | struct inode *inode; |
| 803 | |
| 804 | spin_lock(&inode_lock); |
| 805 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
| 806 | if (!IS_NOQUOTA(inode)) |
| 807 | remove_inode_dquot_ref(inode, type, tofree_head); |
| 808 | } |
| 809 | spin_unlock(&inode_lock); |
| 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* Gather all references from inodes and drop them */ |
| 813 | static void drop_dquot_ref(struct super_block *sb, int type) |
| 814 | { |
| 815 | LIST_HEAD(tofree_head); |
| 816 | |
Christoph Hellwig | fb58b73 | 2007-02-12 00:51:57 -0800 | [diff] [blame] | 817 | if (sb->dq_op) { |
| 818 | down_write(&sb_dqopt(sb)->dqptr_sem); |
| 819 | remove_dquot_ref(sb, type, &tofree_head); |
| 820 | up_write(&sb_dqopt(sb)->dqptr_sem); |
| 821 | put_dquot_list(&tofree_head); |
| 822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number) |
| 826 | { |
| 827 | dquot->dq_dqb.dqb_curinodes += number; |
| 828 | } |
| 829 | |
| 830 | static inline void dquot_incr_space(struct dquot *dquot, qsize_t number) |
| 831 | { |
| 832 | dquot->dq_dqb.dqb_curspace += number; |
| 833 | } |
| 834 | |
| 835 | static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number) |
| 836 | { |
| 837 | if (dquot->dq_dqb.dqb_curinodes > number) |
| 838 | dquot->dq_dqb.dqb_curinodes -= number; |
| 839 | else |
| 840 | dquot->dq_dqb.dqb_curinodes = 0; |
| 841 | if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit) |
| 842 | dquot->dq_dqb.dqb_itime = (time_t) 0; |
| 843 | clear_bit(DQ_INODES_B, &dquot->dq_flags); |
| 844 | } |
| 845 | |
| 846 | static inline void dquot_decr_space(struct dquot *dquot, qsize_t number) |
| 847 | { |
| 848 | if (dquot->dq_dqb.dqb_curspace > number) |
| 849 | dquot->dq_dqb.dqb_curspace -= number; |
| 850 | else |
| 851 | dquot->dq_dqb.dqb_curspace = 0; |
| 852 | if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit) |
| 853 | dquot->dq_dqb.dqb_btime = (time_t) 0; |
| 854 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); |
| 855 | } |
| 856 | |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 857 | static int warning_issued(struct dquot *dquot, const int warntype) |
| 858 | { |
| 859 | int flag = (warntype == QUOTA_NL_BHARDWARN || |
| 860 | warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B : |
| 861 | ((warntype == QUOTA_NL_IHARDWARN || |
| 862 | warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0); |
| 863 | |
| 864 | if (!flag) |
| 865 | return 0; |
| 866 | return test_and_set_bit(flag, &dquot->dq_flags); |
| 867 | } |
| 868 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 869 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | static int flag_print_warnings = 1; |
| 871 | |
| 872 | static inline int need_print_warning(struct dquot *dquot) |
| 873 | { |
| 874 | if (!flag_print_warnings) |
| 875 | return 0; |
| 876 | |
| 877 | switch (dquot->dq_type) { |
| 878 | case USRQUOTA: |
| 879 | return current->fsuid == dquot->dq_id; |
| 880 | case GRPQUOTA: |
| 881 | return in_group_p(dquot->dq_id); |
| 882 | } |
| 883 | return 0; |
| 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* Print warning to user which exceeded quota */ |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 887 | static void print_warning(struct dquot *dquot, const int warntype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
| 889 | char *msg = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 890 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 892 | if (!need_print_warning(dquot)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return; |
| 894 | |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 895 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 896 | tty = get_current_tty(); |
| 897 | if (!tty) |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 898 | goto out_lock; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 899 | tty_write_message(tty, dquot->dq_sb->s_id); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 900 | if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 901 | tty_write_message(tty, ": warning, "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | else |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 903 | tty_write_message(tty, ": write failed, "); |
| 904 | tty_write_message(tty, quotatypes[dquot->dq_type]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | switch (warntype) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 906 | case QUOTA_NL_IHARDWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | msg = " file limit reached.\r\n"; |
| 908 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 909 | case QUOTA_NL_ISOFTLONGWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | msg = " file quota exceeded too long.\r\n"; |
| 911 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 912 | case QUOTA_NL_ISOFTWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | msg = " file quota exceeded.\r\n"; |
| 914 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 915 | case QUOTA_NL_BHARDWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | msg = " block limit reached.\r\n"; |
| 917 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 918 | case QUOTA_NL_BSOFTLONGWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | msg = " block quota exceeded too long.\r\n"; |
| 920 | break; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 921 | case QUOTA_NL_BSOFTWARN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | msg = " block quota exceeded.\r\n"; |
| 923 | break; |
| 924 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 925 | tty_write_message(tty, msg); |
Jan Kara | b525a7e | 2006-09-29 02:00:26 -0700 | [diff] [blame] | 926 | out_lock: |
| 927 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 929 | #endif |
| 930 | |
| 931 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 932 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 933 | /* Netlink family structure for quota */ |
| 934 | static struct genl_family quota_genl_family = { |
| 935 | .id = GENL_ID_GENERATE, |
| 936 | .hdrsize = 0, |
| 937 | .name = "VFS_DQUOT", |
| 938 | .version = 1, |
| 939 | .maxattr = QUOTA_NL_A_MAX, |
| 940 | }; |
| 941 | |
| 942 | /* Send warning to userspace about user which exceeded quota */ |
| 943 | static void send_warning(const struct dquot *dquot, const char warntype) |
| 944 | { |
| 945 | static atomic_t seq; |
| 946 | struct sk_buff *skb; |
| 947 | void *msg_head; |
| 948 | int ret; |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 949 | int msg_size = 4 * nla_total_size(sizeof(u32)) + |
| 950 | 2 * nla_total_size(sizeof(u64)); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 951 | |
| 952 | /* We have to allocate using GFP_NOFS as we are called from a |
| 953 | * filesystem performing write and thus further recursion into |
| 954 | * the fs to free some data could cause deadlocks. */ |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 955 | skb = genlmsg_new(msg_size, GFP_NOFS); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 956 | if (!skb) { |
| 957 | printk(KERN_ERR |
| 958 | "VFS: Not enough memory to send quota warning.\n"); |
| 959 | return; |
| 960 | } |
| 961 | msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq), |
| 962 | "a_genl_family, 0, QUOTA_NL_C_WARNING); |
| 963 | if (!msg_head) { |
| 964 | printk(KERN_ERR |
| 965 | "VFS: Cannot store netlink header in quota warning.\n"); |
| 966 | goto err_out; |
| 967 | } |
| 968 | ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type); |
| 969 | if (ret) |
| 970 | goto attr_err_out; |
| 971 | ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id); |
| 972 | if (ret) |
| 973 | goto attr_err_out; |
| 974 | ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype); |
| 975 | if (ret) |
| 976 | goto attr_err_out; |
| 977 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, |
| 978 | MAJOR(dquot->dq_sb->s_dev)); |
| 979 | if (ret) |
| 980 | goto attr_err_out; |
| 981 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, |
| 982 | MINOR(dquot->dq_sb->s_dev)); |
| 983 | if (ret) |
| 984 | goto attr_err_out; |
| 985 | ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid); |
| 986 | if (ret) |
| 987 | goto attr_err_out; |
| 988 | genlmsg_end(skb, msg_head); |
| 989 | |
| 990 | ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS); |
| 991 | if (ret < 0 && ret != -ESRCH) |
| 992 | printk(KERN_ERR |
| 993 | "VFS: Failed to send notification message: %d\n", ret); |
| 994 | return; |
| 995 | attr_err_out: |
Jan Kara | 22dd483 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 996 | printk(KERN_ERR "VFS: Not enough space to compose quota message!\n"); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 997 | err_out: |
| 998 | kfree_skb(skb); |
| 999 | } |
| 1000 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Jan Kara | 087ee8d | 2007-12-17 16:20:26 -0800 | [diff] [blame] | 1002 | static inline void flush_warnings(struct dquot * const *dquots, char *warntype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
| 1004 | int i; |
| 1005 | |
| 1006 | for (i = 0; i < MAXQUOTAS; i++) |
Jan Kara | c525460 | 2007-12-22 14:03:25 -0800 | [diff] [blame] | 1007 | if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN && |
| 1008 | !warning_issued(dquots[i], warntype[i])) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1009 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | print_warning(dquots[i], warntype[i]); |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1011 | #endif |
| 1012 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 1013 | send_warning(dquots[i], warntype[i]); |
| 1014 | #endif |
| 1015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static inline char ignore_hardlimit(struct dquot *dquot) |
| 1019 | { |
| 1020 | struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; |
| 1021 | |
| 1022 | return capable(CAP_SYS_RESOURCE) && |
| 1023 | (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH)); |
| 1024 | } |
| 1025 | |
| 1026 | /* needs dq_data_lock */ |
| 1027 | static int check_idq(struct dquot *dquot, ulong inodes, char *warntype) |
| 1028 | { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1029 | *warntype = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) |
| 1031 | return QUOTA_OK; |
| 1032 | |
| 1033 | if (dquot->dq_dqb.dqb_ihardlimit && |
| 1034 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit && |
| 1035 | !ignore_hardlimit(dquot)) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1036 | *warntype = QUOTA_NL_IHARDWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | return NO_QUOTA; |
| 1038 | } |
| 1039 | |
| 1040 | if (dquot->dq_dqb.dqb_isoftlimit && |
| 1041 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && |
| 1042 | dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime && |
| 1043 | !ignore_hardlimit(dquot)) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1044 | *warntype = QUOTA_NL_ISOFTLONGWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | return NO_QUOTA; |
| 1046 | } |
| 1047 | |
| 1048 | if (dquot->dq_dqb.dqb_isoftlimit && |
| 1049 | (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit && |
| 1050 | dquot->dq_dqb.dqb_itime == 0) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1051 | *warntype = QUOTA_NL_ISOFTWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace; |
| 1053 | } |
| 1054 | |
| 1055 | return QUOTA_OK; |
| 1056 | } |
| 1057 | |
| 1058 | /* needs dq_data_lock */ |
| 1059 | static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype) |
| 1060 | { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1061 | *warntype = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags)) |
| 1063 | return QUOTA_OK; |
| 1064 | |
| 1065 | if (dquot->dq_dqb.dqb_bhardlimit && |
| 1066 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit && |
| 1067 | !ignore_hardlimit(dquot)) { |
| 1068 | if (!prealloc) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1069 | *warntype = QUOTA_NL_BHARDWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | return NO_QUOTA; |
| 1071 | } |
| 1072 | |
| 1073 | if (dquot->dq_dqb.dqb_bsoftlimit && |
| 1074 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && |
| 1075 | dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime && |
| 1076 | !ignore_hardlimit(dquot)) { |
| 1077 | if (!prealloc) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1078 | *warntype = QUOTA_NL_BSOFTLONGWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | return NO_QUOTA; |
| 1080 | } |
| 1081 | |
| 1082 | if (dquot->dq_dqb.dqb_bsoftlimit && |
| 1083 | toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit && |
| 1084 | dquot->dq_dqb.dqb_btime == 0) { |
| 1085 | if (!prealloc) { |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1086 | *warntype = QUOTA_NL_BSOFTWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace; |
| 1088 | } |
| 1089 | else |
| 1090 | /* |
| 1091 | * We don't allow preallocation to exceed softlimit so exceeding will |
| 1092 | * be always printed |
| 1093 | */ |
| 1094 | return NO_QUOTA; |
| 1095 | } |
| 1096 | |
| 1097 | return QUOTA_OK; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Initialize quota pointers in inode |
| 1102 | * Transaction must be started at entry |
| 1103 | */ |
| 1104 | int dquot_initialize(struct inode *inode, int type) |
| 1105 | { |
| 1106 | unsigned int id = 0; |
| 1107 | int cnt, ret = 0; |
| 1108 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1109 | /* First test before acquiring mutex - solves deadlocks when we |
| 1110 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | if (IS_NOQUOTA(inode)) |
| 1112 | return 0; |
| 1113 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1114 | /* Having dqptr_sem we know NOQUOTA flags can't be altered... */ |
| 1115 | if (IS_NOQUOTA(inode)) |
| 1116 | goto out_err; |
| 1117 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1118 | if (type != -1 && cnt != type) |
| 1119 | continue; |
| 1120 | if (inode->i_dquot[cnt] == NODQUOT) { |
| 1121 | switch (cnt) { |
| 1122 | case USRQUOTA: |
| 1123 | id = inode->i_uid; |
| 1124 | break; |
| 1125 | case GRPQUOTA: |
| 1126 | id = inode->i_gid; |
| 1127 | break; |
| 1128 | } |
| 1129 | inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt); |
| 1130 | } |
| 1131 | } |
| 1132 | out_err: |
| 1133 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Release all quotas referenced by inode |
| 1139 | * Transaction must be started at an entry |
| 1140 | */ |
| 1141 | int dquot_drop(struct inode *inode) |
| 1142 | { |
| 1143 | int cnt; |
| 1144 | |
| 1145 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1146 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1147 | if (inode->i_dquot[cnt] != NODQUOT) { |
| 1148 | dqput(inode->i_dquot[cnt]); |
| 1149 | inode->i_dquot[cnt] = NODQUOT; |
| 1150 | } |
| 1151 | } |
| 1152 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Jan Kara | b85f4b8 | 2008-07-25 01:46:50 -0700 | [diff] [blame^] | 1156 | /* Wrapper to remove references to quota structures from inode */ |
| 1157 | void vfs_dq_drop(struct inode *inode) |
| 1158 | { |
| 1159 | /* Here we can get arbitrary inode from clear_inode() so we have |
| 1160 | * to be careful. OTOH we don't need locking as quota operations |
| 1161 | * are allowed to change only at mount time */ |
| 1162 | if (!IS_NOQUOTA(inode) && inode->i_sb && inode->i_sb->dq_op |
| 1163 | && inode->i_sb->dq_op->drop) { |
| 1164 | int cnt; |
| 1165 | /* Test before calling to rule out calls from proc and such |
| 1166 | * where we are not allowed to block. Note that this is |
| 1167 | * actually reliable test even without the lock - the caller |
| 1168 | * must assure that nobody can come after the DQUOT_DROP and |
| 1169 | * add quota pointers back anyway */ |
| 1170 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1171 | if (inode->i_dquot[cnt] != NODQUOT) |
| 1172 | break; |
| 1173 | if (cnt < MAXQUOTAS) |
| 1174 | inode->i_sb->dq_op->drop(inode); |
| 1175 | } |
| 1176 | } |
| 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | /* |
| 1179 | * Following four functions update i_blocks+i_bytes fields and |
| 1180 | * quota information (together with appropriate checks) |
| 1181 | * NOTE: We absolutely rely on the fact that caller dirties |
| 1182 | * the inode (usually macros in quotaops.h care about this) and |
| 1183 | * holds a handle for the current transaction so that dquot write and |
| 1184 | * inode write go into the same transaction. |
| 1185 | */ |
| 1186 | |
| 1187 | /* |
| 1188 | * This operation can block, but only after everything is updated |
| 1189 | */ |
| 1190 | int dquot_alloc_space(struct inode *inode, qsize_t number, int warn) |
| 1191 | { |
| 1192 | int cnt, ret = NO_QUOTA; |
| 1193 | char warntype[MAXQUOTAS]; |
| 1194 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1195 | /* First test before acquiring mutex - solves deadlocks when we |
| 1196 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | if (IS_NOQUOTA(inode)) { |
| 1198 | out_add: |
| 1199 | inode_add_bytes(inode, number); |
| 1200 | return QUOTA_OK; |
| 1201 | } |
| 1202 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1203 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
| 1205 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1206 | if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */ |
| 1207 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1208 | goto out_add; |
| 1209 | } |
| 1210 | spin_lock(&dq_data_lock); |
| 1211 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1212 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1213 | continue; |
| 1214 | if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA) |
| 1215 | goto warn_put_all; |
| 1216 | } |
| 1217 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1218 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1219 | continue; |
| 1220 | dquot_incr_space(inode->i_dquot[cnt], number); |
| 1221 | } |
| 1222 | inode_add_bytes(inode, number); |
| 1223 | ret = QUOTA_OK; |
| 1224 | warn_put_all: |
| 1225 | spin_unlock(&dq_data_lock); |
| 1226 | if (ret == QUOTA_OK) |
| 1227 | /* Dirtify all the dquots - this can block when journalling */ |
| 1228 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1229 | if (inode->i_dquot[cnt]) |
| 1230 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1231 | flush_warnings(inode->i_dquot, warntype); |
| 1232 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * This operation can block, but only after everything is updated |
| 1238 | */ |
| 1239 | int dquot_alloc_inode(const struct inode *inode, unsigned long number) |
| 1240 | { |
| 1241 | int cnt, ret = NO_QUOTA; |
| 1242 | char warntype[MAXQUOTAS]; |
| 1243 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1244 | /* First test before acquiring mutex - solves deadlocks when we |
| 1245 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | if (IS_NOQUOTA(inode)) |
| 1247 | return QUOTA_OK; |
| 1248 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1249 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1251 | if (IS_NOQUOTA(inode)) { |
| 1252 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1253 | return QUOTA_OK; |
| 1254 | } |
| 1255 | spin_lock(&dq_data_lock); |
| 1256 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1257 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1258 | continue; |
| 1259 | if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA) |
| 1260 | goto warn_put_all; |
| 1261 | } |
| 1262 | |
| 1263 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1264 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1265 | continue; |
| 1266 | dquot_incr_inodes(inode->i_dquot[cnt], number); |
| 1267 | } |
| 1268 | ret = QUOTA_OK; |
| 1269 | warn_put_all: |
| 1270 | spin_unlock(&dq_data_lock); |
| 1271 | if (ret == QUOTA_OK) |
| 1272 | /* Dirtify all the dquots - this can block when journalling */ |
| 1273 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1274 | if (inode->i_dquot[cnt]) |
| 1275 | mark_dquot_dirty(inode->i_dquot[cnt]); |
Jan Kara | 087ee8d | 2007-12-17 16:20:26 -0800 | [diff] [blame] | 1276 | flush_warnings(inode->i_dquot, warntype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * This operation can block, but only after everything is updated |
| 1283 | */ |
| 1284 | int dquot_free_space(struct inode *inode, qsize_t 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 | out_sub: |
| 1292 | inode_sub_bytes(inode, number); |
| 1293 | return QUOTA_OK; |
| 1294 | } |
| 1295 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1296 | /* Now recheck reliably when holding dqptr_sem */ |
| 1297 | if (IS_NOQUOTA(inode)) { |
| 1298 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1299 | goto out_sub; |
| 1300 | } |
| 1301 | spin_lock(&dq_data_lock); |
| 1302 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1303 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1304 | continue; |
| 1305 | dquot_decr_space(inode->i_dquot[cnt], number); |
| 1306 | } |
| 1307 | inode_sub_bytes(inode, number); |
| 1308 | spin_unlock(&dq_data_lock); |
| 1309 | /* Dirtify all the dquots - this can block when journalling */ |
| 1310 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1311 | if (inode->i_dquot[cnt]) |
| 1312 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1313 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1314 | return QUOTA_OK; |
| 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * This operation can block, but only after everything is updated |
| 1319 | */ |
| 1320 | int dquot_free_inode(const struct inode *inode, unsigned long number) |
| 1321 | { |
| 1322 | unsigned int cnt; |
| 1323 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1324 | /* First test before acquiring mutex - solves deadlocks when we |
| 1325 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | if (IS_NOQUOTA(inode)) |
| 1327 | return QUOTA_OK; |
| 1328 | down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1329 | /* Now recheck reliably when holding dqptr_sem */ |
| 1330 | if (IS_NOQUOTA(inode)) { |
| 1331 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1332 | return QUOTA_OK; |
| 1333 | } |
| 1334 | spin_lock(&dq_data_lock); |
| 1335 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1336 | if (inode->i_dquot[cnt] == NODQUOT) |
| 1337 | continue; |
| 1338 | dquot_decr_inodes(inode->i_dquot[cnt], number); |
| 1339 | } |
| 1340 | spin_unlock(&dq_data_lock); |
| 1341 | /* Dirtify all the dquots - this can block when journalling */ |
| 1342 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1343 | if (inode->i_dquot[cnt]) |
| 1344 | mark_dquot_dirty(inode->i_dquot[cnt]); |
| 1345 | up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1346 | return QUOTA_OK; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * Transfer the number of inode and blocks from one diskquota to an other. |
| 1351 | * |
| 1352 | * This operation can block, but only after everything is updated |
| 1353 | * A transaction must be started when entering this function. |
| 1354 | */ |
| 1355 | int dquot_transfer(struct inode *inode, struct iattr *iattr) |
| 1356 | { |
| 1357 | qsize_t space; |
| 1358 | struct dquot *transfer_from[MAXQUOTAS]; |
| 1359 | struct dquot *transfer_to[MAXQUOTAS]; |
| 1360 | int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid, |
| 1361 | chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid; |
| 1362 | char warntype[MAXQUOTAS]; |
| 1363 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1364 | /* First test before acquiring mutex - solves deadlocks when we |
| 1365 | * re-enter the quota code and are already holding the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | if (IS_NOQUOTA(inode)) |
| 1367 | return QUOTA_OK; |
| 1368 | /* Clear the arrays */ |
| 1369 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1370 | transfer_to[cnt] = transfer_from[cnt] = NODQUOT; |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 1371 | warntype[cnt] = QUOTA_NL_NOWARN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | } |
| 1373 | down_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1374 | /* Now recheck reliably when holding dqptr_sem */ |
| 1375 | if (IS_NOQUOTA(inode)) { /* File without quota accounting? */ |
| 1376 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1377 | return QUOTA_OK; |
| 1378 | } |
| 1379 | /* First build the transfer_to list - here we can block on |
| 1380 | * reading/instantiating of dquots. We know that the transaction for |
| 1381 | * us was already started so we don't violate lock ranking here */ |
| 1382 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1383 | switch (cnt) { |
| 1384 | case USRQUOTA: |
| 1385 | if (!chuid) |
| 1386 | continue; |
| 1387 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt); |
| 1388 | break; |
| 1389 | case GRPQUOTA: |
| 1390 | if (!chgid) |
| 1391 | continue; |
| 1392 | transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt); |
| 1393 | break; |
| 1394 | } |
| 1395 | } |
| 1396 | spin_lock(&dq_data_lock); |
| 1397 | space = inode_get_bytes(inode); |
| 1398 | /* Build the transfer_from list and check the limits */ |
| 1399 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1400 | if (transfer_to[cnt] == NODQUOT) |
| 1401 | continue; |
| 1402 | transfer_from[cnt] = inode->i_dquot[cnt]; |
| 1403 | if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA || |
| 1404 | check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA) |
| 1405 | goto warn_put_all; |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * Finally perform the needed transfer from transfer_from to transfer_to |
| 1410 | */ |
| 1411 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1412 | /* |
| 1413 | * Skip changes for same uid or gid or for turned off quota-type. |
| 1414 | */ |
| 1415 | if (transfer_to[cnt] == NODQUOT) |
| 1416 | continue; |
| 1417 | |
| 1418 | /* Due to IO error we might not have transfer_from[] structure */ |
| 1419 | if (transfer_from[cnt]) { |
| 1420 | dquot_decr_inodes(transfer_from[cnt], 1); |
| 1421 | dquot_decr_space(transfer_from[cnt], space); |
| 1422 | } |
| 1423 | |
| 1424 | dquot_incr_inodes(transfer_to[cnt], 1); |
| 1425 | dquot_incr_space(transfer_to[cnt], space); |
| 1426 | |
| 1427 | inode->i_dquot[cnt] = transfer_to[cnt]; |
| 1428 | } |
| 1429 | ret = QUOTA_OK; |
| 1430 | warn_put_all: |
| 1431 | spin_unlock(&dq_data_lock); |
| 1432 | /* Dirtify all the dquots - this can block when journalling */ |
| 1433 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1434 | if (transfer_from[cnt]) |
| 1435 | mark_dquot_dirty(transfer_from[cnt]); |
| 1436 | if (transfer_to[cnt]) |
| 1437 | mark_dquot_dirty(transfer_to[cnt]); |
| 1438 | } |
| 1439 | flush_warnings(transfer_to, warntype); |
| 1440 | |
| 1441 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1442 | if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT) |
| 1443 | dqput(transfer_from[cnt]); |
| 1444 | if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT) |
| 1445 | dqput(transfer_to[cnt]); |
| 1446 | } |
| 1447 | up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
Jan Kara | b85f4b8 | 2008-07-25 01:46:50 -0700 | [diff] [blame^] | 1451 | /* Wrapper for transferring ownership of an inode */ |
| 1452 | int vfs_dq_transfer(struct inode *inode, struct iattr *iattr) |
| 1453 | { |
| 1454 | if (sb_any_quota_enabled(inode->i_sb) && !IS_NOQUOTA(inode)) { |
| 1455 | vfs_dq_init(inode); |
| 1456 | if (inode->i_sb->dq_op->transfer(inode, iattr) == NO_QUOTA) |
| 1457 | return 1; |
| 1458 | } |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | /* |
| 1464 | * Write info of quota file to disk |
| 1465 | */ |
| 1466 | int dquot_commit_info(struct super_block *sb, int type) |
| 1467 | { |
| 1468 | int ret; |
| 1469 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1470 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1471 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | ret = dqopt->ops[type]->write_file_info(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1473 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | return ret; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * Definitions of diskquota operations. |
| 1479 | */ |
| 1480 | struct dquot_operations dquot_operations = { |
| 1481 | .initialize = dquot_initialize, |
| 1482 | .drop = dquot_drop, |
| 1483 | .alloc_space = dquot_alloc_space, |
| 1484 | .alloc_inode = dquot_alloc_inode, |
| 1485 | .free_space = dquot_free_space, |
| 1486 | .free_inode = dquot_free_inode, |
| 1487 | .transfer = dquot_transfer, |
| 1488 | .write_dquot = dquot_commit, |
| 1489 | .acquire_dquot = dquot_acquire, |
| 1490 | .release_dquot = dquot_release, |
| 1491 | .mark_dirty = dquot_mark_dquot_dirty, |
| 1492 | .write_info = dquot_commit_info |
| 1493 | }; |
| 1494 | |
| 1495 | static inline void set_enable_flags(struct quota_info *dqopt, int type) |
| 1496 | { |
| 1497 | switch (type) { |
| 1498 | case USRQUOTA: |
| 1499 | dqopt->flags |= DQUOT_USR_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1500 | dqopt->flags &= ~DQUOT_USR_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | break; |
| 1502 | case GRPQUOTA: |
| 1503 | dqopt->flags |= DQUOT_GRP_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1504 | dqopt->flags &= ~DQUOT_GRP_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | } |
| 1508 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1509 | static inline void reset_enable_flags(struct quota_info *dqopt, int type, |
| 1510 | int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | { |
| 1512 | switch (type) { |
| 1513 | case USRQUOTA: |
| 1514 | dqopt->flags &= ~DQUOT_USR_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1515 | if (remount) |
| 1516 | dqopt->flags |= DQUOT_USR_SUSPENDED; |
| 1517 | else |
| 1518 | dqopt->flags &= ~DQUOT_USR_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | break; |
| 1520 | case GRPQUOTA: |
| 1521 | dqopt->flags &= ~DQUOT_GRP_ENABLED; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1522 | if (remount) |
| 1523 | dqopt->flags |= DQUOT_GRP_SUSPENDED; |
| 1524 | else |
| 1525 | dqopt->flags &= ~DQUOT_GRP_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | break; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | /* |
| 1532 | * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount) |
| 1533 | */ |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1534 | int vfs_quota_off(struct super_block *sb, int type, int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | { |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1536 | int cnt, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1538 | struct inode *toputinode[MAXQUOTAS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
| 1540 | /* We need to serialize quota_off() for device */ |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1541 | mutex_lock(&dqopt->dqonoff_mutex); |
Jan Kara | 9377abd | 2008-05-12 14:02:08 -0700 | [diff] [blame] | 1542 | |
| 1543 | /* |
| 1544 | * Skip everything if there's nothing to do. We have to do this because |
| 1545 | * sometimes we are called when fill_super() failed and calling |
| 1546 | * sync_fs() in such cases does no good. |
| 1547 | */ |
| 1548 | if (!sb_any_quota_enabled(sb) && !sb_any_quota_suspended(sb)) { |
| 1549 | mutex_unlock(&dqopt->dqonoff_mutex); |
| 1550 | return 0; |
| 1551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1553 | toputinode[cnt] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | if (type != -1 && cnt != type) |
| 1555 | continue; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1556 | /* If we keep inodes of quota files after remount and quotaoff |
| 1557 | * is called, drop kept inodes. */ |
| 1558 | if (!remount && sb_has_quota_suspended(sb, cnt)) { |
| 1559 | iput(dqopt->files[cnt]); |
| 1560 | dqopt->files[cnt] = NULL; |
| 1561 | reset_enable_flags(dqopt, cnt, 0); |
| 1562 | continue; |
| 1563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | if (!sb_has_quota_enabled(sb, cnt)) |
| 1565 | continue; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1566 | reset_enable_flags(dqopt, cnt, remount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
| 1568 | /* Note: these are blocking operations */ |
| 1569 | drop_dquot_ref(sb, cnt); |
| 1570 | invalidate_dquots(sb, cnt); |
| 1571 | /* |
| 1572 | * Now all dquots should be invalidated, all writes done so we should be only |
| 1573 | * users of the info. No locks needed. |
| 1574 | */ |
| 1575 | if (info_dirty(&dqopt->info[cnt])) |
| 1576 | sb->dq_op->write_info(sb, cnt); |
| 1577 | if (dqopt->ops[cnt]->free_file_info) |
| 1578 | dqopt->ops[cnt]->free_file_info(sb, cnt); |
| 1579 | put_quota_format(dqopt->info[cnt].dqi_format); |
| 1580 | |
| 1581 | toputinode[cnt] = dqopt->files[cnt]; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1582 | if (!remount) |
| 1583 | dqopt->files[cnt] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | dqopt->info[cnt].dqi_flags = 0; |
| 1585 | dqopt->info[cnt].dqi_igrace = 0; |
| 1586 | dqopt->info[cnt].dqi_bgrace = 0; |
| 1587 | dqopt->ops[cnt] = NULL; |
| 1588 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1589 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | /* Sync the superblock so that buffers with quota data are written to |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 1591 | * disk (and so userspace sees correct data afterwards). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | if (sb->s_op->sync_fs) |
| 1593 | sb->s_op->sync_fs(sb, 1); |
| 1594 | sync_blockdev(sb->s_bdev); |
| 1595 | /* Now the quota files are just ordinary files and we can set the |
| 1596 | * inode flags back. Moreover we discard the pagecache so that |
| 1597 | * userspace sees the writes we did bypassing the pagecache. We |
| 1598 | * must also discard the blockdev buffers so that we see the |
| 1599 | * changes done by userspace on the next quotaon() */ |
| 1600 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) |
| 1601 | if (toputinode[cnt]) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1602 | mutex_lock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | /* If quota was reenabled in the meantime, we have |
| 1604 | * nothing to do */ |
| 1605 | if (!sb_has_quota_enabled(sb, cnt)) { |
Jan Kara | 7925409 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 1606 | mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | toputinode[cnt]->i_flags &= ~(S_IMMUTABLE | |
| 1608 | S_NOATIME | S_NOQUOTA); |
| 1609 | truncate_inode_pages(&toputinode[cnt]->i_data, 0); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1610 | mutex_unlock(&toputinode[cnt]->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | mark_inode_dirty(toputinode[cnt]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1613 | mutex_unlock(&dqopt->dqonoff_mutex); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1614 | /* On remount RO, we keep the inode pointer so that we |
| 1615 | * can reenable quota on the subsequent remount RW. |
| 1616 | * But we have better not keep inode pointer when there |
| 1617 | * is pending delete on the quota file... */ |
| 1618 | if (!remount) |
| 1619 | iput(toputinode[cnt]); |
| 1620 | else if (!toputinode[cnt]->i_nlink) |
| 1621 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | } |
| 1623 | if (sb->s_bdev) |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1624 | invalidate_bdev(sb->s_bdev); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1625 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * Turn quotas on on a device |
| 1630 | */ |
| 1631 | |
| 1632 | /* Helper function when we already have the inode */ |
| 1633 | static int vfs_quota_on_inode(struct inode *inode, int type, int format_id) |
| 1634 | { |
| 1635 | struct quota_format_type *fmt = find_quota_format(format_id); |
| 1636 | struct super_block *sb = inode->i_sb; |
| 1637 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1638 | int error; |
| 1639 | int oldflags = -1; |
| 1640 | |
| 1641 | if (!fmt) |
| 1642 | return -ESRCH; |
| 1643 | if (!S_ISREG(inode->i_mode)) { |
| 1644 | error = -EACCES; |
| 1645 | goto out_fmt; |
| 1646 | } |
| 1647 | if (IS_RDONLY(inode)) { |
| 1648 | error = -EROFS; |
| 1649 | goto out_fmt; |
| 1650 | } |
| 1651 | if (!sb->s_op->quota_write || !sb->s_op->quota_read) { |
| 1652 | error = -EINVAL; |
| 1653 | goto out_fmt; |
| 1654 | } |
| 1655 | |
| 1656 | /* As we bypass the pagecache we must now flush the inode so that |
| 1657 | * we see all the changes from userspace... */ |
| 1658 | write_inode_now(inode, 1); |
| 1659 | /* And now flush the block cache so that kernel sees the changes */ |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1660 | invalidate_bdev(sb->s_bdev); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1661 | mutex_lock(&inode->i_mutex); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1662 | mutex_lock(&dqopt->dqonoff_mutex); |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1663 | if (sb_has_quota_enabled(sb, type) || |
| 1664 | sb_has_quota_suspended(sb, type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | error = -EBUSY; |
| 1666 | goto out_lock; |
| 1667 | } |
| 1668 | /* We don't want quota and atime on quota files (deadlocks possible) |
| 1669 | * Also nobody should write to the file - we use special IO operations |
| 1670 | * which ignore the immutable bit. */ |
| 1671 | down_write(&dqopt->dqptr_sem); |
| 1672 | oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA); |
| 1673 | inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE; |
| 1674 | up_write(&dqopt->dqptr_sem); |
Jan Kara | 31e7ad6 | 2005-04-16 15:25:46 -0700 | [diff] [blame] | 1675 | sb->dq_op->drop(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | |
| 1677 | error = -EIO; |
| 1678 | dqopt->files[type] = igrab(inode); |
| 1679 | if (!dqopt->files[type]) |
| 1680 | goto out_lock; |
| 1681 | error = -EINVAL; |
| 1682 | if (!fmt->qf_ops->check_quota_file(sb, type)) |
| 1683 | goto out_file_init; |
| 1684 | |
| 1685 | dqopt->ops[type] = fmt->qf_ops; |
| 1686 | dqopt->info[type].dqi_format = fmt; |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1687 | dqopt->info[type].dqi_fmt_id = format_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1689 | mutex_lock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1691 | mutex_unlock(&dqopt->dqio_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto out_file_init; |
| 1693 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1694 | mutex_unlock(&dqopt->dqio_mutex); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1695 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | set_enable_flags(dqopt, type); |
| 1697 | |
| 1698 | add_dquot_ref(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1699 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | return 0; |
| 1702 | |
| 1703 | out_file_init: |
| 1704 | dqopt->files[type] = NULL; |
| 1705 | iput(inode); |
| 1706 | out_lock: |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1707 | mutex_unlock(&dqopt->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | if (oldflags != -1) { |
| 1709 | down_write(&dqopt->dqptr_sem); |
| 1710 | /* Set the flags back (in the case of accidental quotaon() |
| 1711 | * on a wrong file we don't want to mess up the flags) */ |
| 1712 | inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE); |
| 1713 | inode->i_flags |= oldflags; |
| 1714 | up_write(&dqopt->dqptr_sem); |
| 1715 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1716 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | out_fmt: |
| 1718 | put_quota_format(fmt); |
| 1719 | |
| 1720 | return error; |
| 1721 | } |
| 1722 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1723 | /* Reenable quotas on remount RW */ |
| 1724 | static int vfs_quota_on_remount(struct super_block *sb, int type) |
| 1725 | { |
| 1726 | struct quota_info *dqopt = sb_dqopt(sb); |
| 1727 | struct inode *inode; |
| 1728 | int ret; |
| 1729 | |
| 1730 | mutex_lock(&dqopt->dqonoff_mutex); |
| 1731 | if (!sb_has_quota_suspended(sb, type)) { |
| 1732 | mutex_unlock(&dqopt->dqonoff_mutex); |
| 1733 | return 0; |
| 1734 | } |
| 1735 | BUG_ON(sb_has_quota_enabled(sb, type)); |
| 1736 | |
| 1737 | inode = dqopt->files[type]; |
| 1738 | dqopt->files[type] = NULL; |
| 1739 | reset_enable_flags(dqopt, type, 0); |
| 1740 | mutex_unlock(&dqopt->dqonoff_mutex); |
| 1741 | |
| 1742 | ret = vfs_quota_on_inode(inode, type, dqopt->info[type].dqi_fmt_id); |
| 1743 | iput(inode); |
| 1744 | |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | /* Actual function called from quotactl() */ |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1749 | int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path, |
| 1750 | int remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | { |
| 1752 | struct nameidata nd; |
| 1753 | int error; |
| 1754 | |
Jan Kara | 0ff5af8 | 2008-04-28 02:14:33 -0700 | [diff] [blame] | 1755 | if (remount) |
| 1756 | return vfs_quota_on_remount(sb, type); |
| 1757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 1759 | if (error < 0) |
| 1760 | return error; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1761 | error = security_quota_on(nd.path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | if (error) |
| 1763 | goto out_path; |
| 1764 | /* Quota file not on the same filesystem? */ |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1765 | if (nd.path.mnt->mnt_sb != sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | error = -EXDEV; |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 1767 | else |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1768 | error = vfs_quota_on_inode(nd.path.dentry->d_inode, type, |
| 1769 | format_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | out_path: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 1771 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | return error; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * This function is used when filesystem needs to initialize quotas |
| 1777 | * during mount time. |
| 1778 | */ |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1779 | int vfs_quota_on_mount(struct super_block *sb, char *qf_name, |
| 1780 | int format_id, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | { |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1782 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | int error; |
| 1784 | |
Christoph Hellwig | 2fa389c | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1785 | dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name)); |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1786 | if (IS_ERR(dentry)) |
| 1787 | return PTR_ERR(dentry); |
| 1788 | |
Jan Kara | 154f484 | 2005-11-28 13:44:14 -0800 | [diff] [blame] | 1789 | if (!dentry->d_inode) { |
| 1790 | error = -ENOENT; |
| 1791 | goto out; |
| 1792 | } |
| 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | error = security_quota_on(dentry); |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1795 | if (!error) |
| 1796 | error = vfs_quota_on_inode(dentry->d_inode, type, format_id); |
| 1797 | |
Jan Kara | 154f484 | 2005-11-28 13:44:14 -0800 | [diff] [blame] | 1798 | out: |
Christoph Hellwig | 84de856 | 2005-06-23 00:09:16 -0700 | [diff] [blame] | 1799 | dput(dentry); |
| 1800 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Jan Kara | b85f4b8 | 2008-07-25 01:46:50 -0700 | [diff] [blame^] | 1803 | /* Wrapper to turn on quotas when remounting rw */ |
| 1804 | int vfs_dq_quota_on_remount(struct super_block *sb) |
| 1805 | { |
| 1806 | int cnt; |
| 1807 | int ret = 0, err; |
| 1808 | |
| 1809 | if (!sb->s_qcop || !sb->s_qcop->quota_on) |
| 1810 | return -ENOSYS; |
| 1811 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 1812 | err = sb->s_qcop->quota_on(sb, cnt, 0, NULL, 1); |
| 1813 | if (err < 0 && !ret) |
| 1814 | ret = err; |
| 1815 | } |
| 1816 | return ret; |
| 1817 | } |
| 1818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | /* Generic routine for getting common part of quota structure */ |
| 1820 | static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di) |
| 1821 | { |
| 1822 | struct mem_dqblk *dm = &dquot->dq_dqb; |
| 1823 | |
| 1824 | spin_lock(&dq_data_lock); |
| 1825 | di->dqb_bhardlimit = dm->dqb_bhardlimit; |
| 1826 | di->dqb_bsoftlimit = dm->dqb_bsoftlimit; |
| 1827 | di->dqb_curspace = dm->dqb_curspace; |
| 1828 | di->dqb_ihardlimit = dm->dqb_ihardlimit; |
| 1829 | di->dqb_isoftlimit = dm->dqb_isoftlimit; |
| 1830 | di->dqb_curinodes = dm->dqb_curinodes; |
| 1831 | di->dqb_btime = dm->dqb_btime; |
| 1832 | di->dqb_itime = dm->dqb_itime; |
| 1833 | di->dqb_valid = QIF_ALL; |
| 1834 | spin_unlock(&dq_data_lock); |
| 1835 | } |
| 1836 | |
| 1837 | int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) |
| 1838 | { |
| 1839 | struct dquot *dquot; |
| 1840 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1841 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | if (!(dquot = dqget(sb, id, type))) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1843 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | return -ESRCH; |
| 1845 | } |
| 1846 | do_get_dqblk(dquot, di); |
| 1847 | dqput(dquot); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1848 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | /* Generic routine for setting common part of quota structure */ |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1853 | static int do_set_dqblk(struct dquot *dquot, struct if_dqblk *di) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | { |
| 1855 | struct mem_dqblk *dm = &dquot->dq_dqb; |
| 1856 | int check_blim = 0, check_ilim = 0; |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1857 | struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type]; |
| 1858 | |
| 1859 | if ((di->dqb_valid & QIF_BLIMITS && |
| 1860 | (di->dqb_bhardlimit > dqi->dqi_maxblimit || |
| 1861 | di->dqb_bsoftlimit > dqi->dqi_maxblimit)) || |
| 1862 | (di->dqb_valid & QIF_ILIMITS && |
| 1863 | (di->dqb_ihardlimit > dqi->dqi_maxilimit || |
| 1864 | di->dqb_isoftlimit > dqi->dqi_maxilimit))) |
| 1865 | return -ERANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | |
| 1867 | spin_lock(&dq_data_lock); |
| 1868 | if (di->dqb_valid & QIF_SPACE) { |
| 1869 | dm->dqb_curspace = di->dqb_curspace; |
| 1870 | check_blim = 1; |
| 1871 | } |
| 1872 | if (di->dqb_valid & QIF_BLIMITS) { |
| 1873 | dm->dqb_bsoftlimit = di->dqb_bsoftlimit; |
| 1874 | dm->dqb_bhardlimit = di->dqb_bhardlimit; |
| 1875 | check_blim = 1; |
| 1876 | } |
| 1877 | if (di->dqb_valid & QIF_INODES) { |
| 1878 | dm->dqb_curinodes = di->dqb_curinodes; |
| 1879 | check_ilim = 1; |
| 1880 | } |
| 1881 | if (di->dqb_valid & QIF_ILIMITS) { |
| 1882 | dm->dqb_isoftlimit = di->dqb_isoftlimit; |
| 1883 | dm->dqb_ihardlimit = di->dqb_ihardlimit; |
| 1884 | check_ilim = 1; |
| 1885 | } |
| 1886 | if (di->dqb_valid & QIF_BTIME) |
| 1887 | dm->dqb_btime = di->dqb_btime; |
| 1888 | if (di->dqb_valid & QIF_ITIME) |
| 1889 | dm->dqb_itime = di->dqb_itime; |
| 1890 | |
| 1891 | if (check_blim) { |
| 1892 | if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) { |
| 1893 | dm->dqb_btime = 0; |
| 1894 | clear_bit(DQ_BLKS_B, &dquot->dq_flags); |
| 1895 | } |
| 1896 | 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] | 1897 | dm->dqb_btime = get_seconds() + dqi->dqi_bgrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
| 1899 | if (check_ilim) { |
| 1900 | if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) { |
| 1901 | dm->dqb_itime = 0; |
| 1902 | clear_bit(DQ_INODES_B, &dquot->dq_flags); |
| 1903 | } |
| 1904 | 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] | 1905 | dm->dqb_itime = get_seconds() + dqi->dqi_igrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit) |
| 1908 | clear_bit(DQ_FAKE_B, &dquot->dq_flags); |
| 1909 | else |
| 1910 | set_bit(DQ_FAKE_B, &dquot->dq_flags); |
| 1911 | spin_unlock(&dq_data_lock); |
| 1912 | mark_dquot_dirty(dquot); |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1913 | |
| 1914 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di) |
| 1918 | { |
| 1919 | struct dquot *dquot; |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1920 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1922 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | if (!(dquot = dqget(sb, id, type))) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1924 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | return -ESRCH; |
| 1926 | } |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1927 | rc = do_set_dqblk(dquot, di); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | dqput(dquot); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1929 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 1930 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | /* Generic routine for getting common part of quota file information */ |
| 1934 | int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
| 1935 | { |
| 1936 | struct mem_dqinfo *mi; |
| 1937 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1938 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | if (!sb_has_quota_enabled(sb, type)) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1940 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | return -ESRCH; |
| 1942 | } |
| 1943 | mi = sb_dqopt(sb)->info + type; |
| 1944 | spin_lock(&dq_data_lock); |
| 1945 | ii->dqi_bgrace = mi->dqi_bgrace; |
| 1946 | ii->dqi_igrace = mi->dqi_igrace; |
| 1947 | ii->dqi_flags = mi->dqi_flags & DQF_MASK; |
| 1948 | ii->dqi_valid = IIF_ALL; |
| 1949 | spin_unlock(&dq_data_lock); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1950 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | /* Generic routine for setting common part of quota file information */ |
| 1955 | int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii) |
| 1956 | { |
| 1957 | struct mem_dqinfo *mi; |
| 1958 | |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1959 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | if (!sb_has_quota_enabled(sb, type)) { |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1961 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | return -ESRCH; |
| 1963 | } |
| 1964 | mi = sb_dqopt(sb)->info + type; |
| 1965 | spin_lock(&dq_data_lock); |
| 1966 | if (ii->dqi_valid & IIF_BGRACE) |
| 1967 | mi->dqi_bgrace = ii->dqi_bgrace; |
| 1968 | if (ii->dqi_valid & IIF_IGRACE) |
| 1969 | mi->dqi_igrace = ii->dqi_igrace; |
| 1970 | if (ii->dqi_valid & IIF_FLAGS) |
| 1971 | mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK); |
| 1972 | spin_unlock(&dq_data_lock); |
| 1973 | mark_info_dirty(sb, type); |
| 1974 | /* Force write to disk */ |
| 1975 | sb->dq_op->write_info(sb, type); |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 1976 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | return 0; |
| 1978 | } |
| 1979 | |
| 1980 | struct quotactl_ops vfs_quotactl_ops = { |
| 1981 | .quota_on = vfs_quota_on, |
| 1982 | .quota_off = vfs_quota_off, |
| 1983 | .quota_sync = vfs_quota_sync, |
| 1984 | .get_info = vfs_get_dqinfo, |
| 1985 | .set_info = vfs_set_dqinfo, |
| 1986 | .get_dqblk = vfs_get_dqblk, |
| 1987 | .set_dqblk = vfs_set_dqblk |
| 1988 | }; |
| 1989 | |
| 1990 | static ctl_table fs_dqstats_table[] = { |
| 1991 | { |
| 1992 | .ctl_name = FS_DQ_LOOKUPS, |
| 1993 | .procname = "lookups", |
| 1994 | .data = &dqstats.lookups, |
| 1995 | .maxlen = sizeof(int), |
| 1996 | .mode = 0444, |
| 1997 | .proc_handler = &proc_dointvec, |
| 1998 | }, |
| 1999 | { |
| 2000 | .ctl_name = FS_DQ_DROPS, |
| 2001 | .procname = "drops", |
| 2002 | .data = &dqstats.drops, |
| 2003 | .maxlen = sizeof(int), |
| 2004 | .mode = 0444, |
| 2005 | .proc_handler = &proc_dointvec, |
| 2006 | }, |
| 2007 | { |
| 2008 | .ctl_name = FS_DQ_READS, |
| 2009 | .procname = "reads", |
| 2010 | .data = &dqstats.reads, |
| 2011 | .maxlen = sizeof(int), |
| 2012 | .mode = 0444, |
| 2013 | .proc_handler = &proc_dointvec, |
| 2014 | }, |
| 2015 | { |
| 2016 | .ctl_name = FS_DQ_WRITES, |
| 2017 | .procname = "writes", |
| 2018 | .data = &dqstats.writes, |
| 2019 | .maxlen = sizeof(int), |
| 2020 | .mode = 0444, |
| 2021 | .proc_handler = &proc_dointvec, |
| 2022 | }, |
| 2023 | { |
| 2024 | .ctl_name = FS_DQ_CACHE_HITS, |
| 2025 | .procname = "cache_hits", |
| 2026 | .data = &dqstats.cache_hits, |
| 2027 | .maxlen = sizeof(int), |
| 2028 | .mode = 0444, |
| 2029 | .proc_handler = &proc_dointvec, |
| 2030 | }, |
| 2031 | { |
| 2032 | .ctl_name = FS_DQ_ALLOCATED, |
| 2033 | .procname = "allocated_dquots", |
| 2034 | .data = &dqstats.allocated_dquots, |
| 2035 | .maxlen = sizeof(int), |
| 2036 | .mode = 0444, |
| 2037 | .proc_handler = &proc_dointvec, |
| 2038 | }, |
| 2039 | { |
| 2040 | .ctl_name = FS_DQ_FREE, |
| 2041 | .procname = "free_dquots", |
| 2042 | .data = &dqstats.free_dquots, |
| 2043 | .maxlen = sizeof(int), |
| 2044 | .mode = 0444, |
| 2045 | .proc_handler = &proc_dointvec, |
| 2046 | }, |
| 2047 | { |
| 2048 | .ctl_name = FS_DQ_SYNCS, |
| 2049 | .procname = "syncs", |
| 2050 | .data = &dqstats.syncs, |
| 2051 | .maxlen = sizeof(int), |
| 2052 | .mode = 0444, |
| 2053 | .proc_handler = &proc_dointvec, |
| 2054 | }, |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 2055 | #ifdef CONFIG_PRINT_QUOTA_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | { |
| 2057 | .ctl_name = FS_DQ_WARNINGS, |
| 2058 | .procname = "warnings", |
| 2059 | .data = &flag_print_warnings, |
| 2060 | .maxlen = sizeof(int), |
| 2061 | .mode = 0644, |
| 2062 | .proc_handler = &proc_dointvec, |
| 2063 | }, |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 2064 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | { .ctl_name = 0 }, |
| 2066 | }; |
| 2067 | |
| 2068 | static ctl_table fs_table[] = { |
| 2069 | { |
| 2070 | .ctl_name = FS_DQSTATS, |
| 2071 | .procname = "quota", |
| 2072 | .mode = 0555, |
| 2073 | .child = fs_dqstats_table, |
| 2074 | }, |
| 2075 | { .ctl_name = 0 }, |
| 2076 | }; |
| 2077 | |
| 2078 | static ctl_table sys_table[] = { |
| 2079 | { |
| 2080 | .ctl_name = CTL_FS, |
| 2081 | .procname = "fs", |
| 2082 | .mode = 0555, |
| 2083 | .child = fs_table, |
| 2084 | }, |
| 2085 | { .ctl_name = 0 }, |
| 2086 | }; |
| 2087 | |
| 2088 | static int __init dquot_init(void) |
| 2089 | { |
| 2090 | int i; |
| 2091 | unsigned long nr_hash, order; |
| 2092 | |
| 2093 | printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__); |
| 2094 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 2095 | register_sysctl_table(sys_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2097 | dquot_cachep = kmem_cache_create("dquot", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | sizeof(struct dquot), sizeof(unsigned long) * 4, |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 2099 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 2100 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2101 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
| 2103 | order = 0; |
| 2104 | dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order); |
| 2105 | if (!dquot_hash) |
| 2106 | panic("Cannot create dquot hash table"); |
| 2107 | |
| 2108 | /* Find power-of-two hlist_heads which can fit into allocation */ |
| 2109 | nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head); |
| 2110 | dq_hash_bits = 0; |
| 2111 | do { |
| 2112 | dq_hash_bits++; |
| 2113 | } while (nr_hash >> dq_hash_bits); |
| 2114 | dq_hash_bits--; |
| 2115 | |
| 2116 | nr_hash = 1UL << dq_hash_bits; |
| 2117 | dq_hash_mask = nr_hash - 1; |
| 2118 | for (i = 0; i < nr_hash; i++) |
| 2119 | INIT_HLIST_HEAD(dquot_hash + i); |
| 2120 | |
| 2121 | printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n", |
| 2122 | nr_hash, order, (PAGE_SIZE << order)); |
| 2123 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 2124 | register_shrinker(&dqcache_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
Jan Kara | 8e89346 | 2007-10-16 23:29:31 -0700 | [diff] [blame] | 2126 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 2127 | if (genl_register_family("a_genl_family) != 0) |
| 2128 | printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n"); |
| 2129 | #endif |
| 2130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | return 0; |
| 2132 | } |
| 2133 | module_init(dquot_init); |
| 2134 | |
| 2135 | EXPORT_SYMBOL(register_quota_format); |
| 2136 | EXPORT_SYMBOL(unregister_quota_format); |
| 2137 | EXPORT_SYMBOL(dqstats); |
| 2138 | EXPORT_SYMBOL(dq_data_lock); |
| 2139 | EXPORT_SYMBOL(vfs_quota_on); |
| 2140 | EXPORT_SYMBOL(vfs_quota_on_mount); |
| 2141 | EXPORT_SYMBOL(vfs_quota_off); |
| 2142 | EXPORT_SYMBOL(vfs_quota_sync); |
| 2143 | EXPORT_SYMBOL(vfs_get_dqinfo); |
| 2144 | EXPORT_SYMBOL(vfs_set_dqinfo); |
| 2145 | EXPORT_SYMBOL(vfs_get_dqblk); |
| 2146 | EXPORT_SYMBOL(vfs_set_dqblk); |
| 2147 | EXPORT_SYMBOL(dquot_commit); |
| 2148 | EXPORT_SYMBOL(dquot_commit_info); |
| 2149 | EXPORT_SYMBOL(dquot_acquire); |
| 2150 | EXPORT_SYMBOL(dquot_release); |
| 2151 | EXPORT_SYMBOL(dquot_mark_dquot_dirty); |
| 2152 | EXPORT_SYMBOL(dquot_initialize); |
| 2153 | EXPORT_SYMBOL(dquot_drop); |
Jan Kara | b85f4b8 | 2008-07-25 01:46:50 -0700 | [diff] [blame^] | 2154 | EXPORT_SYMBOL(vfs_dq_drop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | EXPORT_SYMBOL(dquot_alloc_space); |
| 2156 | EXPORT_SYMBOL(dquot_alloc_inode); |
| 2157 | EXPORT_SYMBOL(dquot_free_space); |
| 2158 | EXPORT_SYMBOL(dquot_free_inode); |
| 2159 | EXPORT_SYMBOL(dquot_transfer); |
Jan Kara | b85f4b8 | 2008-07-25 01:46:50 -0700 | [diff] [blame^] | 2160 | EXPORT_SYMBOL(vfs_dq_transfer); |
| 2161 | EXPORT_SYMBOL(vfs_dq_quota_on_remount); |