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