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