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