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