1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the diskquota system for the LINUX operating system. QUOTA
4 * is implemented using the BSD system call interface as the means of
5 * communication with the user level. This file contains the generic routines
6 * called by the different filesystems on allocation of an inode or block.
7 * These routines take care of the administration needed to have a consistent
8 * diskquota tracking system. The ideas of both user and group quotas are based
9 * on the Melbourne quota system as used on BSD derived systems. The internal
10 * implementation is based on one of the several variants of the LINUX
11 * inode-subsystem with added complexity of the diskquota system.
13 * Author: Marco van Wieringen <mvw@planets.elm.net>
15 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
17 * Revised list management to avoid races
18 * -- Bill Hawes, <whawes@star.net>, 9/98
20 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
21 * As the consequence the locking was moved from dquot_decr_...(),
22 * dquot_incr_...() to calling functions.
23 * invalidate_dquots() now writes modified dquots.
24 * Serialized quota_off() and quota_on() for mount point.
25 * Fixed a few bugs in grow_dquots().
26 * Fixed deadlock in write_dquot() - we no longer account quotas on
28 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
29 * add_dquot_ref() restarts after blocking
30 * Added check for bogus uid and fixed check for group in quotactl.
31 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
33 * Used struct list_head instead of own list struct
34 * Invalidation of referenced dquots is no longer possible
35 * Improved free_dquots list management
36 * Quota and i_blocks are now updated in one place to avoid races
37 * Warnings are now delayed so we won't block in critical section
38 * Write updated not to require dquot lock
39 * Jan Kara, <jack@suse.cz>, 9/2000
41 * Added dynamic quota structure allocation
42 * Jan Kara <jack@suse.cz> 12/2000
44 * Rewritten quota interface. Implemented new quota format and
45 * formats registering.
46 * Jan Kara, <jack@suse.cz>, 2001,2002
49 * Jan Kara, <jack@suse.cz>, 10/2002
51 * Added journalled quota support, fix lock inversion problems
52 * Jan Kara, <jack@suse.cz>, 2003,2004
54 * (C) Copyright 1994 - 1997 Marco van Wieringen
57 #include <linux/errno.h>
58 #include <linux/kernel.h>
60 #include <linux/mount.h>
62 #include <linux/time.h>
63 #include <linux/types.h>
64 #include <linux/string.h>
65 #include <linux/fcntl.h>
66 #include <linux/stat.h>
67 #include <linux/tty.h>
68 #include <linux/file.h>
69 #include <linux/slab.h>
70 #include <linux/sysctl.h>
71 #include <linux/init.h>
72 #include <linux/module.h>
73 #include <linux/proc_fs.h>
74 #include <linux/security.h>
75 #include <linux/sched.h>
76 #include <linux/cred.h>
77 #include <linux/kmod.h>
78 #include <linux/namei.h>
79 #include <linux/capability.h>
80 #include <linux/quotaops.h>
81 #include <linux/blkdev.h>
82 #include <linux/sched/mm.h>
83 #include "../internal.h" /* ugh */
85 #include <linux/uaccess.h>
88 * There are five quota SMP locks:
89 * * dq_list_lock protects all lists with quotas and quota formats.
90 * * dquot->dq_dqb_lock protects data from dq_dqb
91 * * inode->i_lock protects inode->i_blocks, i_bytes and also guards
92 * consistency of dquot->dq_dqb with inode->i_blocks, i_bytes so that
93 * dquot_transfer() can stabilize amount it transfers
94 * * dq_data_lock protects mem_dqinfo structures and modifications of dquot
95 * pointers in the inode
96 * * dq_state_lock protects modifications of quota state (on quotaon and
97 * quotaoff) and readers who care about latest values take it as well.
99 * The spinlock ordering is hence:
100 * dq_data_lock > dq_list_lock > i_lock > dquot->dq_dqb_lock,
101 * dq_list_lock > dq_state_lock
103 * Note that some things (eg. sb pointer, type, id) doesn't change during
104 * the life of the dquot structure and so needn't to be protected by a lock
106 * Operation accessing dquots via inode pointers are protected by dquot_srcu.
107 * Operation of reading pointer needs srcu_read_lock(&dquot_srcu), and
108 * synchronize_srcu(&dquot_srcu) is called after clearing pointers from
109 * inode and before dropping dquot references to avoid use of dquots after
110 * they are freed. dq_data_lock is used to serialize the pointer setting and
111 * clearing operations.
112 * Special care needs to be taken about S_NOQUOTA inode flag (marking that
113 * inode is a quota file). Functions adding pointers from inode to dquots have
114 * to check this flag under dq_data_lock and then (if S_NOQUOTA is not set) they
115 * have to do all pointer modifications before dropping dq_data_lock. This makes
116 * sure they cannot race with quotaon which first sets S_NOQUOTA flag and
117 * then drops all pointers to dquots from an inode.
119 * Each dquot has its dq_lock mutex. Dquot is locked when it is being read to
120 * memory (or space for it is being allocated) on the first dqget(), when it is
121 * being written out, and when it is being released on the last dqput(). The
122 * allocation and release operations are serialized by the dq_lock and by
123 * checking the use count in dquot_release().
125 * Lock ordering (including related VFS locks) is the following:
126 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
129 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_list_lock
);
130 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_state_lock
);
131 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dq_data_lock
);
132 EXPORT_SYMBOL(dq_data_lock
);
133 DEFINE_STATIC_SRCU(dquot_srcu
);
135 static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq
);
137 void __quota_error(struct super_block
*sb
, const char *func
,
138 const char *fmt
, ...)
140 if (printk_ratelimit()) {
142 struct va_format vaf
;
149 printk(KERN_ERR
"Quota error (device %s): %s: %pV\n",
150 sb
->s_id
, func
, &vaf
);
155 EXPORT_SYMBOL(__quota_error
);
157 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
158 static char *quotatypes
[] = INITQFNAMES
;
160 static struct quota_format_type
*quota_formats
; /* List of registered formats */
161 static struct quota_module_name module_names
[] = INIT_QUOTA_MODULE_NAMES
;
163 /* SLAB cache for dquot structures */
164 static struct kmem_cache
*dquot_cachep
;
166 int register_quota_format(struct quota_format_type
*fmt
)
168 spin_lock(&dq_list_lock
);
169 fmt
->qf_next
= quota_formats
;
171 spin_unlock(&dq_list_lock
);
174 EXPORT_SYMBOL(register_quota_format
);
176 void unregister_quota_format(struct quota_format_type
*fmt
)
178 struct quota_format_type
**actqf
;
180 spin_lock(&dq_list_lock
);
181 for (actqf
= "a_formats
; *actqf
&& *actqf
!= fmt
;
182 actqf
= &(*actqf
)->qf_next
)
185 *actqf
= (*actqf
)->qf_next
;
186 spin_unlock(&dq_list_lock
);
188 EXPORT_SYMBOL(unregister_quota_format
);
190 static struct quota_format_type
*find_quota_format(int id
)
192 struct quota_format_type
*actqf
;
194 spin_lock(&dq_list_lock
);
195 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
196 actqf
= actqf
->qf_next
)
198 if (!actqf
|| !try_module_get(actqf
->qf_owner
)) {
201 spin_unlock(&dq_list_lock
);
203 for (qm
= 0; module_names
[qm
].qm_fmt_id
&&
204 module_names
[qm
].qm_fmt_id
!= id
; qm
++)
206 if (!module_names
[qm
].qm_fmt_id
||
207 request_module(module_names
[qm
].qm_mod_name
))
210 spin_lock(&dq_list_lock
);
211 for (actqf
= quota_formats
; actqf
&& actqf
->qf_fmt_id
!= id
;
212 actqf
= actqf
->qf_next
)
214 if (actqf
&& !try_module_get(actqf
->qf_owner
))
217 spin_unlock(&dq_list_lock
);
221 static void put_quota_format(struct quota_format_type
*fmt
)
223 module_put(fmt
->qf_owner
);
227 * Dquot List Management:
228 * The quota code uses four lists for dquot management: the inuse_list,
229 * free_dquots, dqi_dirty_list, and dquot_hash[] array. A single dquot
230 * structure may be on some of those lists, depending on its current state.
232 * All dquots are placed to the end of inuse_list when first created, and this
233 * list is used for invalidate operation, which must look at every dquot.
235 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
236 * and this list is searched whenever we need an available dquot. Dquots are
237 * removed from the list as soon as they are used again, and
238 * dqstats.free_dquots gives the number of dquots on the list. When
239 * dquot is invalidated it's completely released from memory.
241 * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
242 * dirtied, and this list is searched when writing dirty dquots back to
243 * quota file. Note that some filesystems do dirty dquot tracking on their
244 * own (e.g. in a journal) and thus don't use dqi_dirty_list.
246 * Dquots with a specific identity (device, type and id) are placed on
247 * one of the dquot_hash[] hash chains. The provides an efficient search
248 * mechanism to locate a specific dquot.
251 static LIST_HEAD(inuse_list
);
252 static LIST_HEAD(free_dquots
);
253 static unsigned int dq_hash_bits
, dq_hash_mask
;
254 static struct hlist_head
*dquot_hash
;
256 struct dqstats dqstats
;
257 EXPORT_SYMBOL(dqstats
);
259 static qsize_t
inode_get_rsv_space(struct inode
*inode
);
260 static qsize_t
__inode_get_rsv_space(struct inode
*inode
);
261 static int __dquot_initialize(struct inode
*inode
, int type
);
263 static inline unsigned int
264 hashfn(const struct super_block
*sb
, struct kqid qid
)
266 unsigned int id
= from_kqid(&init_user_ns
, qid
);
270 tmp
= (((unsigned long)sb
>>L1_CACHE_SHIFT
) ^ id
) * (MAXQUOTAS
- type
);
271 return (tmp
+ (tmp
>> dq_hash_bits
)) & dq_hash_mask
;
275 * Following list functions expect dq_list_lock to be held
277 static inline void insert_dquot_hash(struct dquot
*dquot
)
279 struct hlist_head
*head
;
280 head
= dquot_hash
+ hashfn(dquot
->dq_sb
, dquot
->dq_id
);
281 hlist_add_head(&dquot
->dq_hash
, head
);
284 static inline void remove_dquot_hash(struct dquot
*dquot
)
286 hlist_del_init(&dquot
->dq_hash
);
289 static struct dquot
*find_dquot(unsigned int hashent
, struct super_block
*sb
,
294 hlist_for_each_entry(dquot
, dquot_hash
+hashent
, dq_hash
)
295 if (dquot
->dq_sb
== sb
&& qid_eq(dquot
->dq_id
, qid
))
301 /* Add a dquot to the tail of the free list */
302 static inline void put_dquot_last(struct dquot
*dquot
)
304 list_add_tail(&dquot
->dq_free
, &free_dquots
);
305 dqstats_inc(DQST_FREE_DQUOTS
);
308 static inline void remove_free_dquot(struct dquot
*dquot
)
310 if (list_empty(&dquot
->dq_free
))
312 list_del_init(&dquot
->dq_free
);
313 dqstats_dec(DQST_FREE_DQUOTS
);
316 static inline void put_inuse(struct dquot
*dquot
)
318 /* We add to the back of inuse list so we don't have to restart
319 * when traversing this list and we block */
320 list_add_tail(&dquot
->dq_inuse
, &inuse_list
);
321 dqstats_inc(DQST_ALLOC_DQUOTS
);
324 static inline void remove_inuse(struct dquot
*dquot
)
326 dqstats_dec(DQST_ALLOC_DQUOTS
);
327 list_del(&dquot
->dq_inuse
);
330 * End of list functions needing dq_list_lock
333 static void wait_on_dquot(struct dquot
*dquot
)
335 mutex_lock(&dquot
->dq_lock
);
336 mutex_unlock(&dquot
->dq_lock
);
339 static inline int dquot_dirty(struct dquot
*dquot
)
341 return test_bit(DQ_MOD_B
, &dquot
->dq_flags
);
344 static inline int mark_dquot_dirty(struct dquot
*dquot
)
346 return dquot
->dq_sb
->dq_op
->mark_dirty(dquot
);
349 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
350 int dquot_mark_dquot_dirty(struct dquot
*dquot
)
354 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
357 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NOLIST_DIRTY
)
358 return test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
);
360 /* If quota is dirty already, we don't have to acquire dq_list_lock */
361 if (test_bit(DQ_MOD_B
, &dquot
->dq_flags
))
364 spin_lock(&dq_list_lock
);
365 if (!test_and_set_bit(DQ_MOD_B
, &dquot
->dq_flags
)) {
366 list_add(&dquot
->dq_dirty
, &sb_dqopt(dquot
->dq_sb
)->
367 info
[dquot
->dq_id
.type
].dqi_dirty_list
);
370 spin_unlock(&dq_list_lock
);
373 EXPORT_SYMBOL(dquot_mark_dquot_dirty
);
375 /* Dirtify all the dquots - this can block when journalling */
376 static inline int mark_all_dquot_dirty(struct dquot
* const *dquot
)
381 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
383 /* Even in case of error we have to continue */
384 ret
= mark_dquot_dirty(dquot
[cnt
]);
391 static inline void dqput_all(struct dquot
**dquot
)
395 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
399 static inline int clear_dquot_dirty(struct dquot
*dquot
)
401 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NOLIST_DIRTY
)
402 return test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
);
404 spin_lock(&dq_list_lock
);
405 if (!test_and_clear_bit(DQ_MOD_B
, &dquot
->dq_flags
)) {
406 spin_unlock(&dq_list_lock
);
409 list_del_init(&dquot
->dq_dirty
);
410 spin_unlock(&dq_list_lock
);
414 void mark_info_dirty(struct super_block
*sb
, int type
)
416 spin_lock(&dq_data_lock
);
417 sb_dqopt(sb
)->info
[type
].dqi_flags
|= DQF_INFO_DIRTY
;
418 spin_unlock(&dq_data_lock
);
420 EXPORT_SYMBOL(mark_info_dirty
);
423 * Read dquot from disk and alloc space for it
426 int dquot_acquire(struct dquot
*dquot
)
428 int ret
= 0, ret2
= 0;
429 unsigned int memalloc
;
430 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
432 mutex_lock(&dquot
->dq_lock
);
433 memalloc
= memalloc_nofs_save();
434 if (!test_bit(DQ_READ_B
, &dquot
->dq_flags
)) {
435 ret
= dqopt
->ops
[dquot
->dq_id
.type
]->read_dqblk(dquot
);
439 /* Make sure flags update is visible after dquot has been filled */
440 smp_mb__before_atomic();
441 set_bit(DQ_READ_B
, &dquot
->dq_flags
);
442 /* Instantiate dquot if needed */
443 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
) && !dquot
->dq_off
) {
444 ret
= dqopt
->ops
[dquot
->dq_id
.type
]->commit_dqblk(dquot
);
445 /* Write the info if needed */
446 if (info_dirty(&dqopt
->info
[dquot
->dq_id
.type
])) {
447 ret2
= dqopt
->ops
[dquot
->dq_id
.type
]->write_file_info(
448 dquot
->dq_sb
, dquot
->dq_id
.type
);
458 * Make sure flags update is visible after on-disk struct has been
459 * allocated. Paired with smp_rmb() in dqget().
461 smp_mb__before_atomic();
462 set_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
464 memalloc_nofs_restore(memalloc
);
465 mutex_unlock(&dquot
->dq_lock
);
468 EXPORT_SYMBOL(dquot_acquire
);
471 * Write dquot to disk
473 int dquot_commit(struct dquot
*dquot
)
476 unsigned int memalloc
;
477 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
479 mutex_lock(&dquot
->dq_lock
);
480 memalloc
= memalloc_nofs_save();
481 if (!clear_dquot_dirty(dquot
))
483 /* Inactive dquot can be only if there was error during read/init
484 * => we have better not writing it */
485 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
486 ret
= dqopt
->ops
[dquot
->dq_id
.type
]->commit_dqblk(dquot
);
490 memalloc_nofs_restore(memalloc
);
491 mutex_unlock(&dquot
->dq_lock
);
494 EXPORT_SYMBOL(dquot_commit
);
499 int dquot_release(struct dquot
*dquot
)
501 int ret
= 0, ret2
= 0;
502 unsigned int memalloc
;
503 struct quota_info
*dqopt
= sb_dqopt(dquot
->dq_sb
);
505 mutex_lock(&dquot
->dq_lock
);
506 memalloc
= memalloc_nofs_save();
507 /* Check whether we are not racing with some other dqget() */
508 if (dquot_is_busy(dquot
))
510 if (dqopt
->ops
[dquot
->dq_id
.type
]->release_dqblk
) {
511 ret
= dqopt
->ops
[dquot
->dq_id
.type
]->release_dqblk(dquot
);
513 if (info_dirty(&dqopt
->info
[dquot
->dq_id
.type
])) {
514 ret2
= dqopt
->ops
[dquot
->dq_id
.type
]->write_file_info(
515 dquot
->dq_sb
, dquot
->dq_id
.type
);
520 clear_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
);
522 memalloc_nofs_restore(memalloc
);
523 mutex_unlock(&dquot
->dq_lock
);
526 EXPORT_SYMBOL(dquot_release
);
528 void dquot_destroy(struct dquot
*dquot
)
530 kmem_cache_free(dquot_cachep
, dquot
);
532 EXPORT_SYMBOL(dquot_destroy
);
534 static inline void do_destroy_dquot(struct dquot
*dquot
)
536 dquot
->dq_sb
->dq_op
->destroy_dquot(dquot
);
539 /* Invalidate all dquots on the list. Note that this function is called after
540 * quota is disabled and pointers from inodes removed so there cannot be new
541 * quota users. There can still be some users of quotas due to inodes being
542 * just deleted or pruned by prune_icache() (those are not attached to any
543 * list) or parallel quotactl call. We have to wait for such users.
545 static void invalidate_dquots(struct super_block
*sb
, int type
)
547 struct dquot
*dquot
, *tmp
;
550 spin_lock(&dq_list_lock
);
551 list_for_each_entry_safe(dquot
, tmp
, &inuse_list
, dq_inuse
) {
552 if (dquot
->dq_sb
!= sb
)
554 if (dquot
->dq_id
.type
!= type
)
556 /* Wait for dquot users */
557 if (atomic_read(&dquot
->dq_count
)) {
559 spin_unlock(&dq_list_lock
);
561 * Once dqput() wakes us up, we know it's time to free
563 * IMPORTANT: we rely on the fact that there is always
564 * at most one process waiting for dquot to free.
565 * Otherwise dq_count would be > 1 and we would never
568 wait_event(dquot_ref_wq
,
569 atomic_read(&dquot
->dq_count
) == 1);
571 /* At this moment dquot() need not exist (it could be
572 * reclaimed by prune_dqcache(). Hence we must
577 * Quota now has no users and it has been written on last
580 remove_dquot_hash(dquot
);
581 remove_free_dquot(dquot
);
583 do_destroy_dquot(dquot
);
585 spin_unlock(&dq_list_lock
);
588 /* Call callback for every active dquot on given filesystem */
589 int dquot_scan_active(struct super_block
*sb
,
590 int (*fn
)(struct dquot
*dquot
, unsigned long priv
),
593 struct dquot
*dquot
, *old_dquot
= NULL
;
596 WARN_ON_ONCE(!rwsem_is_locked(&sb
->s_umount
));
598 spin_lock(&dq_list_lock
);
599 list_for_each_entry(dquot
, &inuse_list
, dq_inuse
) {
600 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
))
602 if (dquot
->dq_sb
!= sb
)
604 /* Now we have active dquot so we can just increase use count */
605 atomic_inc(&dquot
->dq_count
);
606 spin_unlock(&dq_list_lock
);
610 * ->release_dquot() can be racing with us. Our reference
611 * protects us from new calls to it so just wait for any
612 * outstanding call and recheck the DQ_ACTIVE_B after that.
614 wait_on_dquot(dquot
);
615 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
616 ret
= fn(dquot
, priv
);
620 spin_lock(&dq_list_lock
);
621 /* We are safe to continue now because our dquot could not
622 * be moved out of the inuse list while we hold the reference */
624 spin_unlock(&dq_list_lock
);
629 EXPORT_SYMBOL(dquot_scan_active
);
631 /* Write all dquot structures to quota files */
632 int dquot_writeback_dquots(struct super_block
*sb
, int type
)
634 struct list_head dirty
;
636 struct quota_info
*dqopt
= sb_dqopt(sb
);
640 WARN_ON_ONCE(!rwsem_is_locked(&sb
->s_umount
));
642 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
643 if (type
!= -1 && cnt
!= type
)
645 if (!sb_has_quota_active(sb
, cnt
))
647 spin_lock(&dq_list_lock
);
648 /* Move list away to avoid livelock. */
649 list_replace_init(&dqopt
->info
[cnt
].dqi_dirty_list
, &dirty
);
650 while (!list_empty(&dirty
)) {
651 dquot
= list_first_entry(&dirty
, struct dquot
,
654 WARN_ON(!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
));
656 /* Now we have active dquot from which someone is
657 * holding reference so we can safely just increase
660 spin_unlock(&dq_list_lock
);
661 err
= sb
->dq_op
->write_dquot(dquot
);
664 * Clear dirty bit anyway to avoid infinite
667 clear_dquot_dirty(dquot
);
672 spin_lock(&dq_list_lock
);
674 spin_unlock(&dq_list_lock
);
677 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
678 if ((cnt
== type
|| type
== -1) && sb_has_quota_active(sb
, cnt
)
679 && info_dirty(&dqopt
->info
[cnt
]))
680 sb
->dq_op
->write_info(sb
, cnt
);
681 dqstats_inc(DQST_SYNCS
);
685 EXPORT_SYMBOL(dquot_writeback_dquots
);
687 /* Write all dquot structures to disk and make them visible from userspace */
688 int dquot_quota_sync(struct super_block
*sb
, int type
)
690 struct quota_info
*dqopt
= sb_dqopt(sb
);
694 ret
= dquot_writeback_dquots(sb
, type
);
697 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
700 /* This is not very clever (and fast) but currently I don't know about
701 * any other simple way of getting quota data to disk and we must get
702 * them there for userspace to be visible... */
703 if (sb
->s_op
->sync_fs
) {
704 ret
= sb
->s_op
->sync_fs(sb
, 1);
708 ret
= sync_blockdev(sb
->s_bdev
);
713 * Now when everything is written we can discard the pagecache so
714 * that userspace sees the changes.
716 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
717 if (type
!= -1 && cnt
!= type
)
719 if (!sb_has_quota_active(sb
, cnt
))
721 inode_lock(dqopt
->files
[cnt
]);
722 truncate_inode_pages(&dqopt
->files
[cnt
]->i_data
, 0);
723 inode_unlock(dqopt
->files
[cnt
]);
728 EXPORT_SYMBOL(dquot_quota_sync
);
731 dqcache_shrink_scan(struct shrinker
*shrink
, struct shrink_control
*sc
)
734 unsigned long freed
= 0;
736 spin_lock(&dq_list_lock
);
737 while (!list_empty(&free_dquots
) && sc
->nr_to_scan
) {
738 dquot
= list_first_entry(&free_dquots
, struct dquot
, dq_free
);
739 remove_dquot_hash(dquot
);
740 remove_free_dquot(dquot
);
742 do_destroy_dquot(dquot
);
746 spin_unlock(&dq_list_lock
);
751 dqcache_shrink_count(struct shrinker
*shrink
, struct shrink_control
*sc
)
753 return vfs_pressure_ratio(
754 percpu_counter_read_positive(&dqstats
.counter
[DQST_FREE_DQUOTS
]));
757 static struct shrinker dqcache_shrinker
= {
758 .count_objects
= dqcache_shrink_count
,
759 .scan_objects
= dqcache_shrink_scan
,
760 .seeks
= DEFAULT_SEEKS
,
764 * Put reference to dquot
766 void dqput(struct dquot
*dquot
)
772 #ifdef CONFIG_QUOTA_DEBUG
773 if (!atomic_read(&dquot
->dq_count
)) {
774 quota_error(dquot
->dq_sb
, "trying to free free dquot of %s %d",
775 quotatypes
[dquot
->dq_id
.type
],
776 from_kqid(&init_user_ns
, dquot
->dq_id
));
780 dqstats_inc(DQST_DROPS
);
782 spin_lock(&dq_list_lock
);
783 if (atomic_read(&dquot
->dq_count
) > 1) {
784 /* We have more than one user... nothing to do */
785 atomic_dec(&dquot
->dq_count
);
786 /* Releasing dquot during quotaoff phase? */
787 if (!sb_has_quota_active(dquot
->dq_sb
, dquot
->dq_id
.type
) &&
788 atomic_read(&dquot
->dq_count
) == 1)
789 wake_up(&dquot_ref_wq
);
790 spin_unlock(&dq_list_lock
);
793 /* Need to release dquot? */
794 if (dquot_dirty(dquot
)) {
795 spin_unlock(&dq_list_lock
);
796 /* Commit dquot before releasing */
797 ret
= dquot
->dq_sb
->dq_op
->write_dquot(dquot
);
799 quota_error(dquot
->dq_sb
, "Can't write quota structure"
800 " (error %d). Quota may get out of sync!",
803 * We clear dirty bit anyway, so that we avoid
806 clear_dquot_dirty(dquot
);
810 if (test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
811 spin_unlock(&dq_list_lock
);
812 dquot
->dq_sb
->dq_op
->release_dquot(dquot
);
815 atomic_dec(&dquot
->dq_count
);
816 #ifdef CONFIG_QUOTA_DEBUG
818 BUG_ON(!list_empty(&dquot
->dq_free
));
820 put_dquot_last(dquot
);
821 spin_unlock(&dq_list_lock
);
823 EXPORT_SYMBOL(dqput
);
825 struct dquot
*dquot_alloc(struct super_block
*sb
, int type
)
827 return kmem_cache_zalloc(dquot_cachep
, GFP_NOFS
);
829 EXPORT_SYMBOL(dquot_alloc
);
831 static struct dquot
*get_empty_dquot(struct super_block
*sb
, int type
)
835 dquot
= sb
->dq_op
->alloc_dquot(sb
, type
);
839 mutex_init(&dquot
->dq_lock
);
840 INIT_LIST_HEAD(&dquot
->dq_free
);
841 INIT_LIST_HEAD(&dquot
->dq_inuse
);
842 INIT_HLIST_NODE(&dquot
->dq_hash
);
843 INIT_LIST_HEAD(&dquot
->dq_dirty
);
845 dquot
->dq_id
= make_kqid_invalid(type
);
846 atomic_set(&dquot
->dq_count
, 1);
847 spin_lock_init(&dquot
->dq_dqb_lock
);
853 * Get reference to dquot
855 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
856 * destroying our dquot by:
857 * a) checking for quota flags under dq_list_lock and
858 * b) getting a reference to dquot before we release dq_list_lock
860 struct dquot
*dqget(struct super_block
*sb
, struct kqid qid
)
862 unsigned int hashent
= hashfn(sb
, qid
);
863 struct dquot
*dquot
, *empty
= NULL
;
865 if (!qid_has_mapping(sb
->s_user_ns
, qid
))
866 return ERR_PTR(-EINVAL
);
868 if (!sb_has_quota_active(sb
, qid
.type
))
869 return ERR_PTR(-ESRCH
);
871 spin_lock(&dq_list_lock
);
872 spin_lock(&dq_state_lock
);
873 if (!sb_has_quota_active(sb
, qid
.type
)) {
874 spin_unlock(&dq_state_lock
);
875 spin_unlock(&dq_list_lock
);
876 dquot
= ERR_PTR(-ESRCH
);
879 spin_unlock(&dq_state_lock
);
881 dquot
= find_dquot(hashent
, sb
, qid
);
884 spin_unlock(&dq_list_lock
);
885 empty
= get_empty_dquot(sb
, qid
.type
);
887 schedule(); /* Try to wait for a moment... */
893 /* all dquots go on the inuse_list */
895 /* hash it first so it can be found */
896 insert_dquot_hash(dquot
);
897 spin_unlock(&dq_list_lock
);
898 dqstats_inc(DQST_LOOKUPS
);
900 if (!atomic_read(&dquot
->dq_count
))
901 remove_free_dquot(dquot
);
902 atomic_inc(&dquot
->dq_count
);
903 spin_unlock(&dq_list_lock
);
904 dqstats_inc(DQST_CACHE_HITS
);
905 dqstats_inc(DQST_LOOKUPS
);
907 /* Wait for dq_lock - after this we know that either dquot_release() is
908 * already finished or it will be canceled due to dq_count > 1 test */
909 wait_on_dquot(dquot
);
910 /* Read the dquot / allocate space in quota file */
911 if (!test_bit(DQ_ACTIVE_B
, &dquot
->dq_flags
)) {
914 err
= sb
->dq_op
->acquire_dquot(dquot
);
917 dquot
= ERR_PTR(err
);
922 * Make sure following reads see filled structure - paired with
923 * smp_mb__before_atomic() in dquot_acquire().
926 #ifdef CONFIG_QUOTA_DEBUG
927 BUG_ON(!dquot
->dq_sb
); /* Has somebody invalidated entry under us? */
931 do_destroy_dquot(empty
);
935 EXPORT_SYMBOL(dqget
);
937 static inline struct dquot
**i_dquot(struct inode
*inode
)
939 return inode
->i_sb
->s_op
->get_dquots(inode
);
942 static int dqinit_needed(struct inode
*inode
, int type
)
944 struct dquot
* const *dquots
;
947 if (IS_NOQUOTA(inode
))
950 dquots
= i_dquot(inode
);
952 return !dquots
[type
];
953 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
959 /* This routine is guarded by s_umount semaphore */
960 static int add_dquot_ref(struct super_block
*sb
, int type
)
962 struct inode
*inode
, *old_inode
= NULL
;
963 #ifdef CONFIG_QUOTA_DEBUG
968 spin_lock(&sb
->s_inode_list_lock
);
969 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
970 spin_lock(&inode
->i_lock
);
971 if ((inode
->i_state
& (I_FREEING
|I_WILL_FREE
|I_NEW
)) ||
972 !atomic_read(&inode
->i_writecount
) ||
973 !dqinit_needed(inode
, type
)) {
974 spin_unlock(&inode
->i_lock
);
978 spin_unlock(&inode
->i_lock
);
979 spin_unlock(&sb
->s_inode_list_lock
);
981 #ifdef CONFIG_QUOTA_DEBUG
982 if (unlikely(inode_get_rsv_space(inode
) > 0))
986 err
= __dquot_initialize(inode
, type
);
993 * We hold a reference to 'inode' so it couldn't have been
994 * removed from s_inodes list while we dropped the
995 * s_inode_list_lock. We cannot iput the inode now as we can be
996 * holding the last reference and we cannot iput it under
997 * s_inode_list_lock. So we keep the reference and iput it
1002 spin_lock(&sb
->s_inode_list_lock
);
1004 spin_unlock(&sb
->s_inode_list_lock
);
1007 #ifdef CONFIG_QUOTA_DEBUG
1009 quota_error(sb
, "Writes happened before quota was turned on "
1010 "thus quota information is probably inconsistent. "
1011 "Please run quotacheck(8)");
1018 * Remove references to dquots from inode and add dquot to list for freeing
1019 * if we have the last reference to dquot
1021 static void remove_inode_dquot_ref(struct inode
*inode
, int type
,
1022 struct list_head
*tofree_head
)
1024 struct dquot
**dquots
= i_dquot(inode
);
1025 struct dquot
*dquot
= dquots
[type
];
1030 dquots
[type
] = NULL
;
1031 if (list_empty(&dquot
->dq_free
)) {
1033 * The inode still has reference to dquot so it can't be in the
1036 spin_lock(&dq_list_lock
);
1037 list_add(&dquot
->dq_free
, tofree_head
);
1038 spin_unlock(&dq_list_lock
);
1041 * Dquot is already in a list to put so we won't drop the last
1049 * Free list of dquots
1050 * Dquots are removed from inodes and no new references can be got so we are
1051 * the only ones holding reference
1053 static void put_dquot_list(struct list_head
*tofree_head
)
1055 struct list_head
*act_head
;
1056 struct dquot
*dquot
;
1058 act_head
= tofree_head
->next
;
1059 while (act_head
!= tofree_head
) {
1060 dquot
= list_entry(act_head
, struct dquot
, dq_free
);
1061 act_head
= act_head
->next
;
1062 /* Remove dquot from the list so we won't have problems... */
1063 list_del_init(&dquot
->dq_free
);
1068 static void remove_dquot_ref(struct super_block
*sb
, int type
,
1069 struct list_head
*tofree_head
)
1071 struct inode
*inode
;
1072 #ifdef CONFIG_QUOTA_DEBUG
1076 spin_lock(&sb
->s_inode_list_lock
);
1077 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
1079 * We have to scan also I_NEW inodes because they can already
1080 * have quota pointer initialized. Luckily, we need to touch
1081 * only quota pointers and these have separate locking
1084 spin_lock(&dq_data_lock
);
1085 if (!IS_NOQUOTA(inode
)) {
1086 #ifdef CONFIG_QUOTA_DEBUG
1087 if (unlikely(inode_get_rsv_space(inode
) > 0))
1090 remove_inode_dquot_ref(inode
, type
, tofree_head
);
1092 spin_unlock(&dq_data_lock
);
1094 spin_unlock(&sb
->s_inode_list_lock
);
1095 #ifdef CONFIG_QUOTA_DEBUG
1097 printk(KERN_WARNING
"VFS (%s): Writes happened after quota"
1098 " was disabled thus quota information is probably "
1099 "inconsistent. Please run quotacheck(8).\n", sb
->s_id
);
1104 /* Gather all references from inodes and drop them */
1105 static void drop_dquot_ref(struct super_block
*sb
, int type
)
1107 LIST_HEAD(tofree_head
);
1110 remove_dquot_ref(sb
, type
, &tofree_head
);
1111 synchronize_srcu(&dquot_srcu
);
1112 put_dquot_list(&tofree_head
);
1117 void dquot_free_reserved_space(struct dquot
*dquot
, qsize_t number
)
1119 if (dquot
->dq_dqb
.dqb_rsvspace
>= number
)
1120 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1123 dquot
->dq_dqb
.dqb_rsvspace
= 0;
1125 if (dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
<=
1126 dquot
->dq_dqb
.dqb_bsoftlimit
)
1127 dquot
->dq_dqb
.dqb_btime
= (time64_t
) 0;
1128 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1131 static void dquot_decr_inodes(struct dquot
*dquot
, qsize_t number
)
1133 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1134 dquot
->dq_dqb
.dqb_curinodes
>= number
)
1135 dquot
->dq_dqb
.dqb_curinodes
-= number
;
1137 dquot
->dq_dqb
.dqb_curinodes
= 0;
1138 if (dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1139 dquot
->dq_dqb
.dqb_itime
= (time64_t
) 0;
1140 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
1143 static void dquot_decr_space(struct dquot
*dquot
, qsize_t number
)
1145 if (sb_dqopt(dquot
->dq_sb
)->flags
& DQUOT_NEGATIVE_USAGE
||
1146 dquot
->dq_dqb
.dqb_curspace
>= number
)
1147 dquot
->dq_dqb
.dqb_curspace
-= number
;
1149 dquot
->dq_dqb
.dqb_curspace
= 0;
1150 if (dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
<=
1151 dquot
->dq_dqb
.dqb_bsoftlimit
)
1152 dquot
->dq_dqb
.dqb_btime
= (time64_t
) 0;
1153 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
1157 struct super_block
*w_sb
;
1158 struct kqid w_dq_id
;
1162 static int warning_issued(struct dquot
*dquot
, const int warntype
)
1164 int flag
= (warntype
== QUOTA_NL_BHARDWARN
||
1165 warntype
== QUOTA_NL_BSOFTLONGWARN
) ? DQ_BLKS_B
:
1166 ((warntype
== QUOTA_NL_IHARDWARN
||
1167 warntype
== QUOTA_NL_ISOFTLONGWARN
) ? DQ_INODES_B
: 0);
1171 return test_and_set_bit(flag
, &dquot
->dq_flags
);
1174 #ifdef CONFIG_PRINT_QUOTA_WARNING
1175 static int flag_print_warnings
= 1;
1177 static int need_print_warning(struct dquot_warn
*warn
)
1179 if (!flag_print_warnings
)
1182 switch (warn
->w_dq_id
.type
) {
1184 return uid_eq(current_fsuid(), warn
->w_dq_id
.uid
);
1186 return in_group_p(warn
->w_dq_id
.gid
);
1193 /* Print warning to user which exceeded quota */
1194 static void print_warning(struct dquot_warn
*warn
)
1197 struct tty_struct
*tty
;
1198 int warntype
= warn
->w_type
;
1200 if (warntype
== QUOTA_NL_IHARDBELOW
||
1201 warntype
== QUOTA_NL_ISOFTBELOW
||
1202 warntype
== QUOTA_NL_BHARDBELOW
||
1203 warntype
== QUOTA_NL_BSOFTBELOW
|| !need_print_warning(warn
))
1206 tty
= get_current_tty();
1209 tty_write_message(tty
, warn
->w_sb
->s_id
);
1210 if (warntype
== QUOTA_NL_ISOFTWARN
|| warntype
== QUOTA_NL_BSOFTWARN
)
1211 tty_write_message(tty
, ": warning, ");
1213 tty_write_message(tty
, ": write failed, ");
1214 tty_write_message(tty
, quotatypes
[warn
->w_dq_id
.type
]);
1216 case QUOTA_NL_IHARDWARN
:
1217 msg
= " file limit reached.\r\n";
1219 case QUOTA_NL_ISOFTLONGWARN
:
1220 msg
= " file quota exceeded too long.\r\n";
1222 case QUOTA_NL_ISOFTWARN
:
1223 msg
= " file quota exceeded.\r\n";
1225 case QUOTA_NL_BHARDWARN
:
1226 msg
= " block limit reached.\r\n";
1228 case QUOTA_NL_BSOFTLONGWARN
:
1229 msg
= " block quota exceeded too long.\r\n";
1231 case QUOTA_NL_BSOFTWARN
:
1232 msg
= " block quota exceeded.\r\n";
1235 tty_write_message(tty
, msg
);
1240 static void prepare_warning(struct dquot_warn
*warn
, struct dquot
*dquot
,
1243 if (warning_issued(dquot
, warntype
))
1245 warn
->w_type
= warntype
;
1246 warn
->w_sb
= dquot
->dq_sb
;
1247 warn
->w_dq_id
= dquot
->dq_id
;
1251 * Write warnings to the console and send warning messages over netlink.
1253 * Note that this function can call into tty and networking code.
1255 static void flush_warnings(struct dquot_warn
*warn
)
1259 for (i
= 0; i
< MAXQUOTAS
; i
++) {
1260 if (warn
[i
].w_type
== QUOTA_NL_NOWARN
)
1262 #ifdef CONFIG_PRINT_QUOTA_WARNING
1263 print_warning(&warn
[i
]);
1265 quota_send_warning(warn
[i
].w_dq_id
,
1266 warn
[i
].w_sb
->s_dev
, warn
[i
].w_type
);
1270 static int ignore_hardlimit(struct dquot
*dquot
)
1272 struct mem_dqinfo
*info
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_id
.type
];
1274 return capable(CAP_SYS_RESOURCE
) &&
1275 (info
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
||
1276 !(info
->dqi_flags
& DQF_ROOT_SQUASH
));
1279 static int dquot_add_inodes(struct dquot
*dquot
, qsize_t inodes
,
1280 struct dquot_warn
*warn
)
1285 spin_lock(&dquot
->dq_dqb_lock
);
1286 newinodes
= dquot
->dq_dqb
.dqb_curinodes
+ inodes
;
1287 if (!sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_id
.type
) ||
1288 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1291 if (dquot
->dq_dqb
.dqb_ihardlimit
&&
1292 newinodes
> dquot
->dq_dqb
.dqb_ihardlimit
&&
1293 !ignore_hardlimit(dquot
)) {
1294 prepare_warning(warn
, dquot
, QUOTA_NL_IHARDWARN
);
1299 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1300 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1301 dquot
->dq_dqb
.dqb_itime
&&
1302 ktime_get_real_seconds() >= dquot
->dq_dqb
.dqb_itime
&&
1303 !ignore_hardlimit(dquot
)) {
1304 prepare_warning(warn
, dquot
, QUOTA_NL_ISOFTLONGWARN
);
1309 if (dquot
->dq_dqb
.dqb_isoftlimit
&&
1310 newinodes
> dquot
->dq_dqb
.dqb_isoftlimit
&&
1311 dquot
->dq_dqb
.dqb_itime
== 0) {
1312 prepare_warning(warn
, dquot
, QUOTA_NL_ISOFTWARN
);
1313 dquot
->dq_dqb
.dqb_itime
= ktime_get_real_seconds() +
1314 sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_id
.type
].dqi_igrace
;
1317 dquot
->dq_dqb
.dqb_curinodes
= newinodes
;
1320 spin_unlock(&dquot
->dq_dqb_lock
);
1324 static int dquot_add_space(struct dquot
*dquot
, qsize_t space
,
1325 qsize_t rsv_space
, unsigned int flags
,
1326 struct dquot_warn
*warn
)
1329 struct super_block
*sb
= dquot
->dq_sb
;
1332 spin_lock(&dquot
->dq_dqb_lock
);
1333 if (!sb_has_quota_limits_enabled(sb
, dquot
->dq_id
.type
) ||
1334 test_bit(DQ_FAKE_B
, &dquot
->dq_flags
))
1337 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
1338 + space
+ rsv_space
;
1340 if (dquot
->dq_dqb
.dqb_bhardlimit
&&
1341 tspace
> dquot
->dq_dqb
.dqb_bhardlimit
&&
1342 !ignore_hardlimit(dquot
)) {
1343 if (flags
& DQUOT_SPACE_WARN
)
1344 prepare_warning(warn
, dquot
, QUOTA_NL_BHARDWARN
);
1349 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1350 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1351 dquot
->dq_dqb
.dqb_btime
&&
1352 ktime_get_real_seconds() >= dquot
->dq_dqb
.dqb_btime
&&
1353 !ignore_hardlimit(dquot
)) {
1354 if (flags
& DQUOT_SPACE_WARN
)
1355 prepare_warning(warn
, dquot
, QUOTA_NL_BSOFTLONGWARN
);
1360 if (dquot
->dq_dqb
.dqb_bsoftlimit
&&
1361 tspace
> dquot
->dq_dqb
.dqb_bsoftlimit
&&
1362 dquot
->dq_dqb
.dqb_btime
== 0) {
1363 if (flags
& DQUOT_SPACE_WARN
) {
1364 prepare_warning(warn
, dquot
, QUOTA_NL_BSOFTWARN
);
1365 dquot
->dq_dqb
.dqb_btime
= ktime_get_real_seconds() +
1366 sb_dqopt(sb
)->info
[dquot
->dq_id
.type
].dqi_bgrace
;
1369 * We don't allow preallocation to exceed softlimit so exceeding will
1378 * We have to be careful and go through warning generation & grace time
1379 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1382 if (flags
& DQUOT_SPACE_NOFAIL
)
1385 dquot
->dq_dqb
.dqb_rsvspace
+= rsv_space
;
1386 dquot
->dq_dqb
.dqb_curspace
+= space
;
1388 spin_unlock(&dquot
->dq_dqb_lock
);
1392 static int info_idq_free(struct dquot
*dquot
, qsize_t inodes
)
1396 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1397 dquot
->dq_dqb
.dqb_curinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
||
1398 !sb_has_quota_limits_enabled(dquot
->dq_sb
, dquot
->dq_id
.type
))
1399 return QUOTA_NL_NOWARN
;
1401 newinodes
= dquot
->dq_dqb
.dqb_curinodes
- inodes
;
1402 if (newinodes
<= dquot
->dq_dqb
.dqb_isoftlimit
)
1403 return QUOTA_NL_ISOFTBELOW
;
1404 if (dquot
->dq_dqb
.dqb_curinodes
>= dquot
->dq_dqb
.dqb_ihardlimit
&&
1405 newinodes
< dquot
->dq_dqb
.dqb_ihardlimit
)
1406 return QUOTA_NL_IHARDBELOW
;
1407 return QUOTA_NL_NOWARN
;
1410 static int info_bdq_free(struct dquot
*dquot
, qsize_t space
)
1414 tspace
= dquot
->dq_dqb
.dqb_curspace
+ dquot
->dq_dqb
.dqb_rsvspace
;
1416 if (test_bit(DQ_FAKE_B
, &dquot
->dq_flags
) ||
1417 tspace
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1418 return QUOTA_NL_NOWARN
;
1420 if (tspace
- space
<= dquot
->dq_dqb
.dqb_bsoftlimit
)
1421 return QUOTA_NL_BSOFTBELOW
;
1422 if (tspace
>= dquot
->dq_dqb
.dqb_bhardlimit
&&
1423 tspace
- space
< dquot
->dq_dqb
.dqb_bhardlimit
)
1424 return QUOTA_NL_BHARDBELOW
;
1425 return QUOTA_NL_NOWARN
;
1428 static int dquot_active(const struct inode
*inode
)
1430 struct super_block
*sb
= inode
->i_sb
;
1432 if (IS_NOQUOTA(inode
))
1434 return sb_any_quota_loaded(sb
) & ~sb_any_quota_suspended(sb
);
1438 * Initialize quota pointers in inode
1440 * It is better to call this function outside of any transaction as it
1441 * might need a lot of space in journal for dquot structure allocation.
1443 static int __dquot_initialize(struct inode
*inode
, int type
)
1445 int cnt
, init_needed
= 0;
1446 struct dquot
**dquots
, *got
[MAXQUOTAS
] = {};
1447 struct super_block
*sb
= inode
->i_sb
;
1451 if (!dquot_active(inode
))
1454 dquots
= i_dquot(inode
);
1456 /* First get references to structures we might need. */
1457 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1461 struct dquot
*dquot
;
1463 if (type
!= -1 && cnt
!= type
)
1466 * The i_dquot should have been initialized in most cases,
1467 * we check it without locking here to avoid unnecessary
1468 * dqget()/dqput() calls.
1473 if (!sb_has_quota_active(sb
, cnt
))
1480 qid
= make_kqid_uid(inode
->i_uid
);
1483 qid
= make_kqid_gid(inode
->i_gid
);
1486 rc
= inode
->i_sb
->dq_op
->get_projid(inode
, &projid
);
1489 qid
= make_kqid_projid(projid
);
1492 dquot
= dqget(sb
, qid
);
1493 if (IS_ERR(dquot
)) {
1494 /* We raced with somebody turning quotas off... */
1495 if (PTR_ERR(dquot
) != -ESRCH
) {
1496 ret
= PTR_ERR(dquot
);
1504 /* All required i_dquot has been initialized */
1508 spin_lock(&dq_data_lock
);
1509 if (IS_NOQUOTA(inode
))
1511 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1512 if (type
!= -1 && cnt
!= type
)
1514 /* Avoid races with quotaoff() */
1515 if (!sb_has_quota_active(sb
, cnt
))
1517 /* We could race with quotaon or dqget() could have failed */
1521 dquots
[cnt
] = got
[cnt
];
1524 * Make quota reservation system happy if someone
1525 * did a write before quota was turned on
1527 rsv
= inode_get_rsv_space(inode
);
1528 if (unlikely(rsv
)) {
1529 spin_lock(&inode
->i_lock
);
1530 /* Get reservation again under proper lock */
1531 rsv
= __inode_get_rsv_space(inode
);
1532 spin_lock(&dquots
[cnt
]->dq_dqb_lock
);
1533 dquots
[cnt
]->dq_dqb
.dqb_rsvspace
+= rsv
;
1534 spin_unlock(&dquots
[cnt
]->dq_dqb_lock
);
1535 spin_unlock(&inode
->i_lock
);
1540 spin_unlock(&dq_data_lock
);
1542 /* Drop unused references */
1548 int dquot_initialize(struct inode
*inode
)
1550 return __dquot_initialize(inode
, -1);
1552 EXPORT_SYMBOL(dquot_initialize
);
1554 bool dquot_initialize_needed(struct inode
*inode
)
1556 struct dquot
**dquots
;
1559 if (!dquot_active(inode
))
1562 dquots
= i_dquot(inode
);
1563 for (i
= 0; i
< MAXQUOTAS
; i
++)
1564 if (!dquots
[i
] && sb_has_quota_active(inode
->i_sb
, i
))
1568 EXPORT_SYMBOL(dquot_initialize_needed
);
1571 * Release all quotas referenced by inode.
1573 * This function only be called on inode free or converting
1574 * a file to quota file, no other users for the i_dquot in
1575 * both cases, so we needn't call synchronize_srcu() after
1578 static void __dquot_drop(struct inode
*inode
)
1581 struct dquot
**dquots
= i_dquot(inode
);
1582 struct dquot
*put
[MAXQUOTAS
];
1584 spin_lock(&dq_data_lock
);
1585 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1586 put
[cnt
] = dquots
[cnt
];
1589 spin_unlock(&dq_data_lock
);
1593 void dquot_drop(struct inode
*inode
)
1595 struct dquot
* const *dquots
;
1598 if (IS_NOQUOTA(inode
))
1602 * Test before calling to rule out calls from proc and such
1603 * where we are not allowed to block. Note that this is
1604 * actually reliable test even without the lock - the caller
1605 * must assure that nobody can come after the DQUOT_DROP and
1606 * add quota pointers back anyway.
1608 dquots
= i_dquot(inode
);
1609 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1614 if (cnt
< MAXQUOTAS
)
1615 __dquot_drop(inode
);
1617 EXPORT_SYMBOL(dquot_drop
);
1620 * inode_reserved_space is managed internally by quota, and protected by
1621 * i_lock similar to i_blocks+i_bytes.
1623 static qsize_t
*inode_reserved_space(struct inode
* inode
)
1625 /* Filesystem must explicitly define it's own method in order to use
1626 * quota reservation interface */
1627 BUG_ON(!inode
->i_sb
->dq_op
->get_reserved_space
);
1628 return inode
->i_sb
->dq_op
->get_reserved_space(inode
);
1631 static qsize_t
__inode_get_rsv_space(struct inode
*inode
)
1633 if (!inode
->i_sb
->dq_op
->get_reserved_space
)
1635 return *inode_reserved_space(inode
);
1638 static qsize_t
inode_get_rsv_space(struct inode
*inode
)
1642 if (!inode
->i_sb
->dq_op
->get_reserved_space
)
1644 spin_lock(&inode
->i_lock
);
1645 ret
= __inode_get_rsv_space(inode
);
1646 spin_unlock(&inode
->i_lock
);
1651 * This functions updates i_blocks+i_bytes fields and quota information
1652 * (together with appropriate checks).
1654 * NOTE: We absolutely rely on the fact that caller dirties the inode
1655 * (usually helpers in quotaops.h care about this) and holds a handle for
1656 * the current transaction so that dquot write and inode write go into the
1661 * This operation can block, but only after everything is updated
1663 int __dquot_alloc_space(struct inode
*inode
, qsize_t number
, int flags
)
1665 int cnt
, ret
= 0, index
;
1666 struct dquot_warn warn
[MAXQUOTAS
];
1667 int reserve
= flags
& DQUOT_SPACE_RESERVE
;
1668 struct dquot
**dquots
;
1670 if (!dquot_active(inode
)) {
1672 spin_lock(&inode
->i_lock
);
1673 *inode_reserved_space(inode
) += number
;
1674 spin_unlock(&inode
->i_lock
);
1676 inode_add_bytes(inode
, number
);
1681 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1682 warn
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1684 dquots
= i_dquot(inode
);
1685 index
= srcu_read_lock(&dquot_srcu
);
1686 spin_lock(&inode
->i_lock
);
1687 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1691 ret
= dquot_add_space(dquots
[cnt
], 0, number
, flags
,
1694 ret
= dquot_add_space(dquots
[cnt
], number
, 0, flags
,
1698 /* Back out changes we already did */
1699 for (cnt
--; cnt
>= 0; cnt
--) {
1702 spin_lock(&dquots
[cnt
]->dq_dqb_lock
);
1704 dquot_free_reserved_space(dquots
[cnt
],
1707 dquot_decr_space(dquots
[cnt
], number
);
1708 spin_unlock(&dquots
[cnt
]->dq_dqb_lock
);
1710 spin_unlock(&inode
->i_lock
);
1711 goto out_flush_warn
;
1715 *inode_reserved_space(inode
) += number
;
1717 __inode_add_bytes(inode
, number
);
1718 spin_unlock(&inode
->i_lock
);
1721 goto out_flush_warn
;
1722 mark_all_dquot_dirty(dquots
);
1724 srcu_read_unlock(&dquot_srcu
, index
);
1725 flush_warnings(warn
);
1729 EXPORT_SYMBOL(__dquot_alloc_space
);
1732 * This operation can block, but only after everything is updated
1734 int dquot_alloc_inode(struct inode
*inode
)
1736 int cnt
, ret
= 0, index
;
1737 struct dquot_warn warn
[MAXQUOTAS
];
1738 struct dquot
* const *dquots
;
1740 if (!dquot_active(inode
))
1742 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
1743 warn
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1745 dquots
= i_dquot(inode
);
1746 index
= srcu_read_lock(&dquot_srcu
);
1747 spin_lock(&inode
->i_lock
);
1748 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1751 ret
= dquot_add_inodes(dquots
[cnt
], 1, &warn
[cnt
]);
1753 for (cnt
--; cnt
>= 0; cnt
--) {
1756 /* Back out changes we already did */
1757 spin_lock(&dquots
[cnt
]->dq_dqb_lock
);
1758 dquot_decr_inodes(dquots
[cnt
], 1);
1759 spin_unlock(&dquots
[cnt
]->dq_dqb_lock
);
1766 spin_unlock(&inode
->i_lock
);
1768 mark_all_dquot_dirty(dquots
);
1769 srcu_read_unlock(&dquot_srcu
, index
);
1770 flush_warnings(warn
);
1773 EXPORT_SYMBOL(dquot_alloc_inode
);
1776 * Convert in-memory reserved quotas to real consumed quotas
1778 int dquot_claim_space_nodirty(struct inode
*inode
, qsize_t number
)
1780 struct dquot
**dquots
;
1783 if (!dquot_active(inode
)) {
1784 spin_lock(&inode
->i_lock
);
1785 *inode_reserved_space(inode
) -= number
;
1786 __inode_add_bytes(inode
, number
);
1787 spin_unlock(&inode
->i_lock
);
1791 dquots
= i_dquot(inode
);
1792 index
= srcu_read_lock(&dquot_srcu
);
1793 spin_lock(&inode
->i_lock
);
1794 /* Claim reserved quotas to allocated quotas */
1795 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1797 struct dquot
*dquot
= dquots
[cnt
];
1799 spin_lock(&dquot
->dq_dqb_lock
);
1800 if (WARN_ON_ONCE(dquot
->dq_dqb
.dqb_rsvspace
< number
))
1801 number
= dquot
->dq_dqb
.dqb_rsvspace
;
1802 dquot
->dq_dqb
.dqb_curspace
+= number
;
1803 dquot
->dq_dqb
.dqb_rsvspace
-= number
;
1804 spin_unlock(&dquot
->dq_dqb_lock
);
1807 /* Update inode bytes */
1808 *inode_reserved_space(inode
) -= number
;
1809 __inode_add_bytes(inode
, number
);
1810 spin_unlock(&inode
->i_lock
);
1811 mark_all_dquot_dirty(dquots
);
1812 srcu_read_unlock(&dquot_srcu
, index
);
1815 EXPORT_SYMBOL(dquot_claim_space_nodirty
);
1818 * Convert allocated space back to in-memory reserved quotas
1820 void dquot_reclaim_space_nodirty(struct inode
*inode
, qsize_t number
)
1822 struct dquot
**dquots
;
1825 if (!dquot_active(inode
)) {
1826 spin_lock(&inode
->i_lock
);
1827 *inode_reserved_space(inode
) += number
;
1828 __inode_sub_bytes(inode
, number
);
1829 spin_unlock(&inode
->i_lock
);
1833 dquots
= i_dquot(inode
);
1834 index
= srcu_read_lock(&dquot_srcu
);
1835 spin_lock(&inode
->i_lock
);
1836 /* Claim reserved quotas to allocated quotas */
1837 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1839 struct dquot
*dquot
= dquots
[cnt
];
1841 spin_lock(&dquot
->dq_dqb_lock
);
1842 if (WARN_ON_ONCE(dquot
->dq_dqb
.dqb_curspace
< number
))
1843 number
= dquot
->dq_dqb
.dqb_curspace
;
1844 dquot
->dq_dqb
.dqb_rsvspace
+= number
;
1845 dquot
->dq_dqb
.dqb_curspace
-= number
;
1846 spin_unlock(&dquot
->dq_dqb_lock
);
1849 /* Update inode bytes */
1850 *inode_reserved_space(inode
) += number
;
1851 __inode_sub_bytes(inode
, number
);
1852 spin_unlock(&inode
->i_lock
);
1853 mark_all_dquot_dirty(dquots
);
1854 srcu_read_unlock(&dquot_srcu
, index
);
1857 EXPORT_SYMBOL(dquot_reclaim_space_nodirty
);
1860 * This operation can block, but only after everything is updated
1862 void __dquot_free_space(struct inode
*inode
, qsize_t number
, int flags
)
1865 struct dquot_warn warn
[MAXQUOTAS
];
1866 struct dquot
**dquots
;
1867 int reserve
= flags
& DQUOT_SPACE_RESERVE
, index
;
1869 if (!dquot_active(inode
)) {
1871 spin_lock(&inode
->i_lock
);
1872 *inode_reserved_space(inode
) -= number
;
1873 spin_unlock(&inode
->i_lock
);
1875 inode_sub_bytes(inode
, number
);
1880 dquots
= i_dquot(inode
);
1881 index
= srcu_read_lock(&dquot_srcu
);
1882 spin_lock(&inode
->i_lock
);
1883 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1886 warn
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1889 spin_lock(&dquots
[cnt
]->dq_dqb_lock
);
1890 wtype
= info_bdq_free(dquots
[cnt
], number
);
1891 if (wtype
!= QUOTA_NL_NOWARN
)
1892 prepare_warning(&warn
[cnt
], dquots
[cnt
], wtype
);
1894 dquot_free_reserved_space(dquots
[cnt
], number
);
1896 dquot_decr_space(dquots
[cnt
], number
);
1897 spin_unlock(&dquots
[cnt
]->dq_dqb_lock
);
1900 *inode_reserved_space(inode
) -= number
;
1902 __inode_sub_bytes(inode
, number
);
1903 spin_unlock(&inode
->i_lock
);
1907 mark_all_dquot_dirty(dquots
);
1909 srcu_read_unlock(&dquot_srcu
, index
);
1910 flush_warnings(warn
);
1912 EXPORT_SYMBOL(__dquot_free_space
);
1915 * This operation can block, but only after everything is updated
1917 void dquot_free_inode(struct inode
*inode
)
1920 struct dquot_warn warn
[MAXQUOTAS
];
1921 struct dquot
* const *dquots
;
1924 if (!dquot_active(inode
))
1927 dquots
= i_dquot(inode
);
1928 index
= srcu_read_lock(&dquot_srcu
);
1929 spin_lock(&inode
->i_lock
);
1930 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1933 warn
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1936 spin_lock(&dquots
[cnt
]->dq_dqb_lock
);
1937 wtype
= info_idq_free(dquots
[cnt
], 1);
1938 if (wtype
!= QUOTA_NL_NOWARN
)
1939 prepare_warning(&warn
[cnt
], dquots
[cnt
], wtype
);
1940 dquot_decr_inodes(dquots
[cnt
], 1);
1941 spin_unlock(&dquots
[cnt
]->dq_dqb_lock
);
1943 spin_unlock(&inode
->i_lock
);
1944 mark_all_dquot_dirty(dquots
);
1945 srcu_read_unlock(&dquot_srcu
, index
);
1946 flush_warnings(warn
);
1948 EXPORT_SYMBOL(dquot_free_inode
);
1951 * Transfer the number of inode and blocks from one diskquota to an other.
1952 * On success, dquot references in transfer_to are consumed and references
1953 * to original dquots that need to be released are placed there. On failure,
1954 * references are kept untouched.
1956 * This operation can block, but only after everything is updated
1957 * A transaction must be started when entering this function.
1959 * We are holding reference on transfer_from & transfer_to, no need to
1960 * protect them by srcu_read_lock().
1962 int __dquot_transfer(struct inode
*inode
, struct dquot
**transfer_to
)
1965 qsize_t rsv_space
= 0;
1966 qsize_t inode_usage
= 1;
1967 struct dquot
*transfer_from
[MAXQUOTAS
] = {};
1969 char is_valid
[MAXQUOTAS
] = {};
1970 struct dquot_warn warn_to
[MAXQUOTAS
];
1971 struct dquot_warn warn_from_inodes
[MAXQUOTAS
];
1972 struct dquot_warn warn_from_space
[MAXQUOTAS
];
1974 if (IS_NOQUOTA(inode
))
1977 if (inode
->i_sb
->dq_op
->get_inode_usage
) {
1978 ret
= inode
->i_sb
->dq_op
->get_inode_usage(inode
, &inode_usage
);
1983 /* Initialize the arrays */
1984 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
1985 warn_to
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1986 warn_from_inodes
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1987 warn_from_space
[cnt
].w_type
= QUOTA_NL_NOWARN
;
1990 spin_lock(&dq_data_lock
);
1991 spin_lock(&inode
->i_lock
);
1992 if (IS_NOQUOTA(inode
)) { /* File without quota accounting? */
1993 spin_unlock(&inode
->i_lock
);
1994 spin_unlock(&dq_data_lock
);
1997 cur_space
= __inode_get_bytes(inode
);
1998 rsv_space
= __inode_get_rsv_space(inode
);
2000 * Build the transfer_from list, check limits, and update usage in
2001 * the target structures.
2003 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2005 * Skip changes for same uid or gid or for turned off quota-type.
2007 if (!transfer_to
[cnt
])
2009 /* Avoid races with quotaoff() */
2010 if (!sb_has_quota_active(inode
->i_sb
, cnt
))
2013 transfer_from
[cnt
] = i_dquot(inode
)[cnt
];
2014 ret
= dquot_add_inodes(transfer_to
[cnt
], inode_usage
,
2018 ret
= dquot_add_space(transfer_to
[cnt
], cur_space
, rsv_space
,
2019 DQUOT_SPACE_WARN
, &warn_to
[cnt
]);
2021 spin_lock(&transfer_to
[cnt
]->dq_dqb_lock
);
2022 dquot_decr_inodes(transfer_to
[cnt
], inode_usage
);
2023 spin_unlock(&transfer_to
[cnt
]->dq_dqb_lock
);
2028 /* Decrease usage for source structures and update quota pointers */
2029 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2032 /* Due to IO error we might not have transfer_from[] structure */
2033 if (transfer_from
[cnt
]) {
2036 spin_lock(&transfer_from
[cnt
]->dq_dqb_lock
);
2037 wtype
= info_idq_free(transfer_from
[cnt
], inode_usage
);
2038 if (wtype
!= QUOTA_NL_NOWARN
)
2039 prepare_warning(&warn_from_inodes
[cnt
],
2040 transfer_from
[cnt
], wtype
);
2041 wtype
= info_bdq_free(transfer_from
[cnt
],
2042 cur_space
+ rsv_space
);
2043 if (wtype
!= QUOTA_NL_NOWARN
)
2044 prepare_warning(&warn_from_space
[cnt
],
2045 transfer_from
[cnt
], wtype
);
2046 dquot_decr_inodes(transfer_from
[cnt
], inode_usage
);
2047 dquot_decr_space(transfer_from
[cnt
], cur_space
);
2048 dquot_free_reserved_space(transfer_from
[cnt
],
2050 spin_unlock(&transfer_from
[cnt
]->dq_dqb_lock
);
2052 i_dquot(inode
)[cnt
] = transfer_to
[cnt
];
2054 spin_unlock(&inode
->i_lock
);
2055 spin_unlock(&dq_data_lock
);
2057 mark_all_dquot_dirty(transfer_from
);
2058 mark_all_dquot_dirty(transfer_to
);
2059 flush_warnings(warn_to
);
2060 flush_warnings(warn_from_inodes
);
2061 flush_warnings(warn_from_space
);
2062 /* Pass back references to put */
2063 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
2065 transfer_to
[cnt
] = transfer_from
[cnt
];
2068 /* Back out changes we already did */
2069 for (cnt
--; cnt
>= 0; cnt
--) {
2072 spin_lock(&transfer_to
[cnt
]->dq_dqb_lock
);
2073 dquot_decr_inodes(transfer_to
[cnt
], inode_usage
);
2074 dquot_decr_space(transfer_to
[cnt
], cur_space
);
2075 dquot_free_reserved_space(transfer_to
[cnt
], rsv_space
);
2076 spin_unlock(&transfer_to
[cnt
]->dq_dqb_lock
);
2078 spin_unlock(&inode
->i_lock
);
2079 spin_unlock(&dq_data_lock
);
2080 flush_warnings(warn_to
);
2083 EXPORT_SYMBOL(__dquot_transfer
);
2085 /* Wrapper for transferring ownership of an inode for uid/gid only
2086 * Called from FSXXX_setattr()
2088 int dquot_transfer(struct mnt_idmap
*idmap
, struct inode
*inode
,
2089 struct iattr
*iattr
)
2091 struct dquot
*transfer_to
[MAXQUOTAS
] = {};
2092 struct dquot
*dquot
;
2093 struct super_block
*sb
= inode
->i_sb
;
2096 if (!dquot_active(inode
))
2099 if (i_uid_needs_update(idmap
, iattr
, inode
)) {
2100 kuid_t kuid
= from_vfsuid(idmap
, i_user_ns(inode
),
2103 dquot
= dqget(sb
, make_kqid_uid(kuid
));
2104 if (IS_ERR(dquot
)) {
2105 if (PTR_ERR(dquot
) != -ESRCH
) {
2106 ret
= PTR_ERR(dquot
);
2111 transfer_to
[USRQUOTA
] = dquot
;
2113 if (i_gid_needs_update(idmap
, iattr
, inode
)) {
2114 kgid_t kgid
= from_vfsgid(idmap
, i_user_ns(inode
),
2117 dquot
= dqget(sb
, make_kqid_gid(kgid
));
2118 if (IS_ERR(dquot
)) {
2119 if (PTR_ERR(dquot
) != -ESRCH
) {
2120 ret
= PTR_ERR(dquot
);
2125 transfer_to
[GRPQUOTA
] = dquot
;
2127 ret
= __dquot_transfer(inode
, transfer_to
);
2129 dqput_all(transfer_to
);
2132 EXPORT_SYMBOL(dquot_transfer
);
2135 * Write info of quota file to disk
2137 int dquot_commit_info(struct super_block
*sb
, int type
)
2139 struct quota_info
*dqopt
= sb_dqopt(sb
);
2141 return dqopt
->ops
[type
]->write_file_info(sb
, type
);
2143 EXPORT_SYMBOL(dquot_commit_info
);
2145 int dquot_get_next_id(struct super_block
*sb
, struct kqid
*qid
)
2147 struct quota_info
*dqopt
= sb_dqopt(sb
);
2149 if (!sb_has_quota_active(sb
, qid
->type
))
2151 if (!dqopt
->ops
[qid
->type
]->get_next_id
)
2153 return dqopt
->ops
[qid
->type
]->get_next_id(sb
, qid
);
2155 EXPORT_SYMBOL(dquot_get_next_id
);
2158 * Definitions of diskquota operations.
2160 const struct dquot_operations dquot_operations
= {
2161 .write_dquot
= dquot_commit
,
2162 .acquire_dquot
= dquot_acquire
,
2163 .release_dquot
= dquot_release
,
2164 .mark_dirty
= dquot_mark_dquot_dirty
,
2165 .write_info
= dquot_commit_info
,
2166 .alloc_dquot
= dquot_alloc
,
2167 .destroy_dquot
= dquot_destroy
,
2168 .get_next_id
= dquot_get_next_id
,
2170 EXPORT_SYMBOL(dquot_operations
);
2173 * Generic helper for ->open on filesystems supporting disk quotas.
2175 int dquot_file_open(struct inode
*inode
, struct file
*file
)
2179 error
= generic_file_open(inode
, file
);
2180 if (!error
&& (file
->f_mode
& FMODE_WRITE
))
2181 error
= dquot_initialize(inode
);
2184 EXPORT_SYMBOL(dquot_file_open
);
2186 static void vfs_cleanup_quota_inode(struct super_block
*sb
, int type
)
2188 struct quota_info
*dqopt
= sb_dqopt(sb
);
2189 struct inode
*inode
= dqopt
->files
[type
];
2193 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2195 inode
->i_flags
&= ~S_NOQUOTA
;
2196 inode_unlock(inode
);
2198 dqopt
->files
[type
] = NULL
;
2203 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2205 int dquot_disable(struct super_block
*sb
, int type
, unsigned int flags
)
2208 struct quota_info
*dqopt
= sb_dqopt(sb
);
2210 /* s_umount should be held in exclusive mode */
2211 if (WARN_ON_ONCE(down_read_trylock(&sb
->s_umount
)))
2212 up_read(&sb
->s_umount
);
2214 /* Cannot turn off usage accounting without turning off limits, or
2215 * suspend quotas and simultaneously turn quotas off. */
2216 if ((flags
& DQUOT_USAGE_ENABLED
&& !(flags
& DQUOT_LIMITS_ENABLED
))
2217 || (flags
& DQUOT_SUSPENDED
&& flags
& (DQUOT_LIMITS_ENABLED
|
2218 DQUOT_USAGE_ENABLED
)))
2222 * Skip everything if there's nothing to do. We have to do this because
2223 * sometimes we are called when fill_super() failed and calling
2224 * sync_fs() in such cases does no good.
2226 if (!sb_any_quota_loaded(sb
))
2229 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2230 if (type
!= -1 && cnt
!= type
)
2232 if (!sb_has_quota_loaded(sb
, cnt
))
2235 if (flags
& DQUOT_SUSPENDED
) {
2236 spin_lock(&dq_state_lock
);
2238 dquot_state_flag(DQUOT_SUSPENDED
, cnt
);
2239 spin_unlock(&dq_state_lock
);
2241 spin_lock(&dq_state_lock
);
2242 dqopt
->flags
&= ~dquot_state_flag(flags
, cnt
);
2243 /* Turning off suspended quotas? */
2244 if (!sb_has_quota_loaded(sb
, cnt
) &&
2245 sb_has_quota_suspended(sb
, cnt
)) {
2246 dqopt
->flags
&= ~dquot_state_flag(
2247 DQUOT_SUSPENDED
, cnt
);
2248 spin_unlock(&dq_state_lock
);
2249 vfs_cleanup_quota_inode(sb
, cnt
);
2252 spin_unlock(&dq_state_lock
);
2255 /* We still have to keep quota loaded? */
2256 if (sb_has_quota_loaded(sb
, cnt
) && !(flags
& DQUOT_SUSPENDED
))
2259 /* Note: these are blocking operations */
2260 drop_dquot_ref(sb
, cnt
);
2261 invalidate_dquots(sb
, cnt
);
2263 * Now all dquots should be invalidated, all writes done so we
2264 * should be only users of the info. No locks needed.
2266 if (info_dirty(&dqopt
->info
[cnt
]))
2267 sb
->dq_op
->write_info(sb
, cnt
);
2268 if (dqopt
->ops
[cnt
]->free_file_info
)
2269 dqopt
->ops
[cnt
]->free_file_info(sb
, cnt
);
2270 put_quota_format(dqopt
->info
[cnt
].dqi_format
);
2271 dqopt
->info
[cnt
].dqi_flags
= 0;
2272 dqopt
->info
[cnt
].dqi_igrace
= 0;
2273 dqopt
->info
[cnt
].dqi_bgrace
= 0;
2274 dqopt
->ops
[cnt
] = NULL
;
2277 /* Skip syncing and setting flags if quota files are hidden */
2278 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)
2281 /* Sync the superblock so that buffers with quota data are written to
2282 * disk (and so userspace sees correct data afterwards). */
2283 if (sb
->s_op
->sync_fs
)
2284 sb
->s_op
->sync_fs(sb
, 1);
2285 sync_blockdev(sb
->s_bdev
);
2286 /* Now the quota files are just ordinary files and we can set the
2287 * inode flags back. Moreover we discard the pagecache so that
2288 * userspace sees the writes we did bypassing the pagecache. We
2289 * must also discard the blockdev buffers so that we see the
2290 * changes done by userspace on the next quotaon() */
2291 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
2292 if (!sb_has_quota_loaded(sb
, cnt
) && dqopt
->files
[cnt
]) {
2293 inode_lock(dqopt
->files
[cnt
]);
2294 truncate_inode_pages(&dqopt
->files
[cnt
]->i_data
, 0);
2295 inode_unlock(dqopt
->files
[cnt
]);
2298 invalidate_bdev(sb
->s_bdev
);
2300 /* We are done when suspending quotas */
2301 if (flags
& DQUOT_SUSPENDED
)
2304 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++)
2305 if (!sb_has_quota_loaded(sb
, cnt
))
2306 vfs_cleanup_quota_inode(sb
, cnt
);
2309 EXPORT_SYMBOL(dquot_disable
);
2311 int dquot_quota_off(struct super_block
*sb
, int type
)
2313 return dquot_disable(sb
, type
,
2314 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2316 EXPORT_SYMBOL(dquot_quota_off
);
2319 * Turn quotas on on a device
2322 static int vfs_setup_quota_inode(struct inode
*inode
, int type
)
2324 struct super_block
*sb
= inode
->i_sb
;
2325 struct quota_info
*dqopt
= sb_dqopt(sb
);
2327 if (is_bad_inode(inode
))
2329 if (!S_ISREG(inode
->i_mode
))
2331 if (IS_RDONLY(inode
))
2333 if (sb_has_quota_loaded(sb
, type
))
2336 dqopt
->files
[type
] = igrab(inode
);
2337 if (!dqopt
->files
[type
])
2339 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2340 /* We don't want quota and atime on quota files (deadlocks
2341 * possible) Also nobody should write to the file - we use
2342 * special IO operations which ignore the immutable bit. */
2344 inode
->i_flags
|= S_NOQUOTA
;
2345 inode_unlock(inode
);
2347 * When S_NOQUOTA is set, remove dquot references as no more
2348 * references can be added
2350 __dquot_drop(inode
);
2355 int dquot_load_quota_sb(struct super_block
*sb
, int type
, int format_id
,
2358 struct quota_format_type
*fmt
= find_quota_format(format_id
);
2359 struct quota_info
*dqopt
= sb_dqopt(sb
);
2362 /* Just unsuspend quotas? */
2363 BUG_ON(flags
& DQUOT_SUSPENDED
);
2364 /* s_umount should be held in exclusive mode */
2365 if (WARN_ON_ONCE(down_read_trylock(&sb
->s_umount
)))
2366 up_read(&sb
->s_umount
);
2370 if (!sb
->s_op
->quota_write
|| !sb
->s_op
->quota_read
||
2371 (type
== PRJQUOTA
&& sb
->dq_op
->get_projid
== NULL
)) {
2375 /* Filesystems outside of init_user_ns not yet supported */
2376 if (sb
->s_user_ns
!= &init_user_ns
) {
2380 /* Usage always has to be set... */
2381 if (!(flags
& DQUOT_USAGE_ENABLED
)) {
2385 if (sb_has_quota_loaded(sb
, type
)) {
2390 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
)) {
2391 /* As we bypass the pagecache we must now flush all the
2392 * dirty data and invalidate caches so that kernel sees
2393 * changes from userspace. It is not enough to just flush
2394 * the quota file since if blocksize < pagesize, invalidation
2395 * of the cache could fail because of other unrelated dirty
2397 sync_filesystem(sb
);
2398 invalidate_bdev(sb
->s_bdev
);
2402 if (!fmt
->qf_ops
->check_quota_file(sb
, type
))
2405 dqopt
->ops
[type
] = fmt
->qf_ops
;
2406 dqopt
->info
[type
].dqi_format
= fmt
;
2407 dqopt
->info
[type
].dqi_fmt_id
= format_id
;
2408 INIT_LIST_HEAD(&dqopt
->info
[type
].dqi_dirty_list
);
2409 error
= dqopt
->ops
[type
]->read_file_info(sb
, type
);
2412 if (dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
) {
2413 spin_lock(&dq_data_lock
);
2414 dqopt
->info
[type
].dqi_flags
|= DQF_SYS_FILE
;
2415 spin_unlock(&dq_data_lock
);
2417 spin_lock(&dq_state_lock
);
2418 dqopt
->flags
|= dquot_state_flag(flags
, type
);
2419 spin_unlock(&dq_state_lock
);
2421 error
= add_dquot_ref(sb
, type
);
2423 dquot_disable(sb
, type
, flags
);
2427 put_quota_format(fmt
);
2431 EXPORT_SYMBOL(dquot_load_quota_sb
);
2434 * More powerful function for turning on quotas on given quota inode allowing
2435 * setting of individual quota flags
2437 int dquot_load_quota_inode(struct inode
*inode
, int type
, int format_id
,
2442 err
= vfs_setup_quota_inode(inode
, type
);
2445 err
= dquot_load_quota_sb(inode
->i_sb
, type
, format_id
, flags
);
2447 vfs_cleanup_quota_inode(inode
->i_sb
, type
);
2450 EXPORT_SYMBOL(dquot_load_quota_inode
);
2452 /* Reenable quotas on remount RW */
2453 int dquot_resume(struct super_block
*sb
, int type
)
2455 struct quota_info
*dqopt
= sb_dqopt(sb
);
2459 /* s_umount should be held in exclusive mode */
2460 if (WARN_ON_ONCE(down_read_trylock(&sb
->s_umount
)))
2461 up_read(&sb
->s_umount
);
2463 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
2464 if (type
!= -1 && cnt
!= type
)
2466 if (!sb_has_quota_suspended(sb
, cnt
))
2469 spin_lock(&dq_state_lock
);
2470 flags
= dqopt
->flags
& dquot_state_flag(DQUOT_USAGE_ENABLED
|
2471 DQUOT_LIMITS_ENABLED
,
2473 dqopt
->flags
&= ~dquot_state_flag(DQUOT_STATE_FLAGS
, cnt
);
2474 spin_unlock(&dq_state_lock
);
2476 flags
= dquot_generic_flag(flags
, cnt
);
2477 ret
= dquot_load_quota_sb(sb
, cnt
, dqopt
->info
[cnt
].dqi_fmt_id
,
2480 vfs_cleanup_quota_inode(sb
, cnt
);
2485 EXPORT_SYMBOL(dquot_resume
);
2487 int dquot_quota_on(struct super_block
*sb
, int type
, int format_id
,
2488 const struct path
*path
)
2490 int error
= security_quota_on(path
->dentry
);
2493 /* Quota file not on the same filesystem? */
2494 if (path
->dentry
->d_sb
!= sb
)
2497 error
= dquot_load_quota_inode(d_inode(path
->dentry
), type
,
2498 format_id
, DQUOT_USAGE_ENABLED
|
2499 DQUOT_LIMITS_ENABLED
);
2502 EXPORT_SYMBOL(dquot_quota_on
);
2505 * This function is used when filesystem needs to initialize quotas
2506 * during mount time.
2508 int dquot_quota_on_mount(struct super_block
*sb
, char *qf_name
,
2509 int format_id
, int type
)
2511 struct dentry
*dentry
;
2514 dentry
= lookup_positive_unlocked(qf_name
, sb
->s_root
, strlen(qf_name
));
2516 return PTR_ERR(dentry
);
2518 error
= security_quota_on(dentry
);
2520 error
= dquot_load_quota_inode(d_inode(dentry
), type
, format_id
,
2521 DQUOT_USAGE_ENABLED
| DQUOT_LIMITS_ENABLED
);
2526 EXPORT_SYMBOL(dquot_quota_on_mount
);
2528 static int dquot_quota_enable(struct super_block
*sb
, unsigned int flags
)
2532 struct quota_info
*dqopt
= sb_dqopt(sb
);
2534 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
))
2536 /* Accounting cannot be turned on while fs is mounted */
2537 flags
&= ~(FS_QUOTA_UDQ_ACCT
| FS_QUOTA_GDQ_ACCT
| FS_QUOTA_PDQ_ACCT
);
2540 for (type
= 0; type
< MAXQUOTAS
; type
++) {
2541 if (!(flags
& qtype_enforce_flag(type
)))
2543 /* Can't enforce without accounting */
2544 if (!sb_has_quota_usage_enabled(sb
, type
)) {
2548 if (sb_has_quota_limits_enabled(sb
, type
)) {
2552 spin_lock(&dq_state_lock
);
2553 dqopt
->flags
|= dquot_state_flag(DQUOT_LIMITS_ENABLED
, type
);
2554 spin_unlock(&dq_state_lock
);
2558 /* Backout enforcement enablement we already did */
2559 for (type
--; type
>= 0; type
--) {
2560 if (flags
& qtype_enforce_flag(type
))
2561 dquot_disable(sb
, type
, DQUOT_LIMITS_ENABLED
);
2563 /* Error code translation for better compatibility with XFS */
2569 static int dquot_quota_disable(struct super_block
*sb
, unsigned int flags
)
2573 struct quota_info
*dqopt
= sb_dqopt(sb
);
2575 if (!(dqopt
->flags
& DQUOT_QUOTA_SYS_FILE
))
2578 * We don't support turning off accounting via quotactl. In principle
2579 * quota infrastructure can do this but filesystems don't expect
2580 * userspace to be able to do it.
2583 (FS_QUOTA_UDQ_ACCT
| FS_QUOTA_GDQ_ACCT
| FS_QUOTA_PDQ_ACCT
))
2586 /* Filter out limits not enabled */
2587 for (type
= 0; type
< MAXQUOTAS
; type
++)
2588 if (!sb_has_quota_limits_enabled(sb
, type
))
2589 flags
&= ~qtype_enforce_flag(type
);
2593 for (type
= 0; type
< MAXQUOTAS
; type
++) {
2594 if (flags
& qtype_enforce_flag(type
)) {
2595 ret
= dquot_disable(sb
, type
, DQUOT_LIMITS_ENABLED
);
2602 /* Backout enforcement disabling we already did */
2603 for (type
--; type
>= 0; type
--) {
2604 if (flags
& qtype_enforce_flag(type
)) {
2605 spin_lock(&dq_state_lock
);
2607 dquot_state_flag(DQUOT_LIMITS_ENABLED
, type
);
2608 spin_unlock(&dq_state_lock
);
2614 /* Generic routine for getting common part of quota structure */
2615 static void do_get_dqblk(struct dquot
*dquot
, struct qc_dqblk
*di
)
2617 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2619 memset(di
, 0, sizeof(*di
));
2620 spin_lock(&dquot
->dq_dqb_lock
);
2621 di
->d_spc_hardlimit
= dm
->dqb_bhardlimit
;
2622 di
->d_spc_softlimit
= dm
->dqb_bsoftlimit
;
2623 di
->d_ino_hardlimit
= dm
->dqb_ihardlimit
;
2624 di
->d_ino_softlimit
= dm
->dqb_isoftlimit
;
2625 di
->d_space
= dm
->dqb_curspace
+ dm
->dqb_rsvspace
;
2626 di
->d_ino_count
= dm
->dqb_curinodes
;
2627 di
->d_spc_timer
= dm
->dqb_btime
;
2628 di
->d_ino_timer
= dm
->dqb_itime
;
2629 spin_unlock(&dquot
->dq_dqb_lock
);
2632 int dquot_get_dqblk(struct super_block
*sb
, struct kqid qid
,
2633 struct qc_dqblk
*di
)
2635 struct dquot
*dquot
;
2637 dquot
= dqget(sb
, qid
);
2639 return PTR_ERR(dquot
);
2640 do_get_dqblk(dquot
, di
);
2645 EXPORT_SYMBOL(dquot_get_dqblk
);
2647 int dquot_get_next_dqblk(struct super_block
*sb
, struct kqid
*qid
,
2648 struct qc_dqblk
*di
)
2650 struct dquot
*dquot
;
2653 if (!sb
->dq_op
->get_next_id
)
2655 err
= sb
->dq_op
->get_next_id(sb
, qid
);
2658 dquot
= dqget(sb
, *qid
);
2660 return PTR_ERR(dquot
);
2661 do_get_dqblk(dquot
, di
);
2666 EXPORT_SYMBOL(dquot_get_next_dqblk
);
2668 #define VFS_QC_MASK \
2669 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2670 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2671 QC_SPC_TIMER | QC_INO_TIMER)
2673 /* Generic routine for setting common part of quota structure */
2674 static int do_set_dqblk(struct dquot
*dquot
, struct qc_dqblk
*di
)
2676 struct mem_dqblk
*dm
= &dquot
->dq_dqb
;
2677 int check_blim
= 0, check_ilim
= 0;
2678 struct mem_dqinfo
*dqi
= &sb_dqopt(dquot
->dq_sb
)->info
[dquot
->dq_id
.type
];
2680 if (di
->d_fieldmask
& ~VFS_QC_MASK
)
2683 if (((di
->d_fieldmask
& QC_SPC_SOFT
) &&
2684 di
->d_spc_softlimit
> dqi
->dqi_max_spc_limit
) ||
2685 ((di
->d_fieldmask
& QC_SPC_HARD
) &&
2686 di
->d_spc_hardlimit
> dqi
->dqi_max_spc_limit
) ||
2687 ((di
->d_fieldmask
& QC_INO_SOFT
) &&
2688 (di
->d_ino_softlimit
> dqi
->dqi_max_ino_limit
)) ||
2689 ((di
->d_fieldmask
& QC_INO_HARD
) &&
2690 (di
->d_ino_hardlimit
> dqi
->dqi_max_ino_limit
)))
2693 spin_lock(&dquot
->dq_dqb_lock
);
2694 if (di
->d_fieldmask
& QC_SPACE
) {
2695 dm
->dqb_curspace
= di
->d_space
- dm
->dqb_rsvspace
;
2697 set_bit(DQ_LASTSET_B
+ QIF_SPACE_B
, &dquot
->dq_flags
);
2700 if (di
->d_fieldmask
& QC_SPC_SOFT
)
2701 dm
->dqb_bsoftlimit
= di
->d_spc_softlimit
;
2702 if (di
->d_fieldmask
& QC_SPC_HARD
)
2703 dm
->dqb_bhardlimit
= di
->d_spc_hardlimit
;
2704 if (di
->d_fieldmask
& (QC_SPC_SOFT
| QC_SPC_HARD
)) {
2706 set_bit(DQ_LASTSET_B
+ QIF_BLIMITS_B
, &dquot
->dq_flags
);
2709 if (di
->d_fieldmask
& QC_INO_COUNT
) {
2710 dm
->dqb_curinodes
= di
->d_ino_count
;
2712 set_bit(DQ_LASTSET_B
+ QIF_INODES_B
, &dquot
->dq_flags
);
2715 if (di
->d_fieldmask
& QC_INO_SOFT
)
2716 dm
->dqb_isoftlimit
= di
->d_ino_softlimit
;
2717 if (di
->d_fieldmask
& QC_INO_HARD
)
2718 dm
->dqb_ihardlimit
= di
->d_ino_hardlimit
;
2719 if (di
->d_fieldmask
& (QC_INO_SOFT
| QC_INO_HARD
)) {
2721 set_bit(DQ_LASTSET_B
+ QIF_ILIMITS_B
, &dquot
->dq_flags
);
2724 if (di
->d_fieldmask
& QC_SPC_TIMER
) {
2725 dm
->dqb_btime
= di
->d_spc_timer
;
2727 set_bit(DQ_LASTSET_B
+ QIF_BTIME_B
, &dquot
->dq_flags
);
2730 if (di
->d_fieldmask
& QC_INO_TIMER
) {
2731 dm
->dqb_itime
= di
->d_ino_timer
;
2733 set_bit(DQ_LASTSET_B
+ QIF_ITIME_B
, &dquot
->dq_flags
);
2737 if (!dm
->dqb_bsoftlimit
||
2738 dm
->dqb_curspace
+ dm
->dqb_rsvspace
<= dm
->dqb_bsoftlimit
) {
2740 clear_bit(DQ_BLKS_B
, &dquot
->dq_flags
);
2741 } else if (!(di
->d_fieldmask
& QC_SPC_TIMER
))
2742 /* Set grace only if user hasn't provided his own... */
2743 dm
->dqb_btime
= ktime_get_real_seconds() + dqi
->dqi_bgrace
;
2746 if (!dm
->dqb_isoftlimit
||
2747 dm
->dqb_curinodes
<= dm
->dqb_isoftlimit
) {
2749 clear_bit(DQ_INODES_B
, &dquot
->dq_flags
);
2750 } else if (!(di
->d_fieldmask
& QC_INO_TIMER
))
2751 /* Set grace only if user hasn't provided his own... */
2752 dm
->dqb_itime
= ktime_get_real_seconds() + dqi
->dqi_igrace
;
2754 if (dm
->dqb_bhardlimit
|| dm
->dqb_bsoftlimit
|| dm
->dqb_ihardlimit
||
2756 clear_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2758 set_bit(DQ_FAKE_B
, &dquot
->dq_flags
);
2759 spin_unlock(&dquot
->dq_dqb_lock
);
2760 mark_dquot_dirty(dquot
);
2765 int dquot_set_dqblk(struct super_block
*sb
, struct kqid qid
,
2766 struct qc_dqblk
*di
)
2768 struct dquot
*dquot
;
2771 dquot
= dqget(sb
, qid
);
2772 if (IS_ERR(dquot
)) {
2773 rc
= PTR_ERR(dquot
);
2776 rc
= do_set_dqblk(dquot
, di
);
2781 EXPORT_SYMBOL(dquot_set_dqblk
);
2783 /* Generic routine for getting common part of quota file information */
2784 int dquot_get_state(struct super_block
*sb
, struct qc_state
*state
)
2786 struct mem_dqinfo
*mi
;
2787 struct qc_type_state
*tstate
;
2788 struct quota_info
*dqopt
= sb_dqopt(sb
);
2791 memset(state
, 0, sizeof(*state
));
2792 for (type
= 0; type
< MAXQUOTAS
; type
++) {
2793 if (!sb_has_quota_active(sb
, type
))
2795 tstate
= state
->s_state
+ type
;
2796 mi
= sb_dqopt(sb
)->info
+ type
;
2797 tstate
->flags
= QCI_ACCT_ENABLED
;
2798 spin_lock(&dq_data_lock
);
2799 if (mi
->dqi_flags
& DQF_SYS_FILE
)
2800 tstate
->flags
|= QCI_SYSFILE
;
2801 if (mi
->dqi_flags
& DQF_ROOT_SQUASH
)
2802 tstate
->flags
|= QCI_ROOT_SQUASH
;
2803 if (sb_has_quota_limits_enabled(sb
, type
))
2804 tstate
->flags
|= QCI_LIMITS_ENFORCED
;
2805 tstate
->spc_timelimit
= mi
->dqi_bgrace
;
2806 tstate
->ino_timelimit
= mi
->dqi_igrace
;
2807 if (dqopt
->files
[type
]) {
2808 tstate
->ino
= dqopt
->files
[type
]->i_ino
;
2809 tstate
->blocks
= dqopt
->files
[type
]->i_blocks
;
2811 tstate
->nextents
= 1; /* We don't know... */
2812 spin_unlock(&dq_data_lock
);
2816 EXPORT_SYMBOL(dquot_get_state
);
2818 /* Generic routine for setting common part of quota file information */
2819 int dquot_set_dqinfo(struct super_block
*sb
, int type
, struct qc_info
*ii
)
2821 struct mem_dqinfo
*mi
;
2824 if ((ii
->i_fieldmask
& QC_WARNS_MASK
) ||
2825 (ii
->i_fieldmask
& QC_RT_SPC_TIMER
))
2827 if (!sb_has_quota_active(sb
, type
))
2829 mi
= sb_dqopt(sb
)->info
+ type
;
2830 if (ii
->i_fieldmask
& QC_FLAGS
) {
2831 if ((ii
->i_flags
& QCI_ROOT_SQUASH
&&
2832 mi
->dqi_format
->qf_fmt_id
!= QFMT_VFS_OLD
))
2835 spin_lock(&dq_data_lock
);
2836 if (ii
->i_fieldmask
& QC_SPC_TIMER
)
2837 mi
->dqi_bgrace
= ii
->i_spc_timelimit
;
2838 if (ii
->i_fieldmask
& QC_INO_TIMER
)
2839 mi
->dqi_igrace
= ii
->i_ino_timelimit
;
2840 if (ii
->i_fieldmask
& QC_FLAGS
) {
2841 if (ii
->i_flags
& QCI_ROOT_SQUASH
)
2842 mi
->dqi_flags
|= DQF_ROOT_SQUASH
;
2844 mi
->dqi_flags
&= ~DQF_ROOT_SQUASH
;
2846 spin_unlock(&dq_data_lock
);
2847 mark_info_dirty(sb
, type
);
2848 /* Force write to disk */
2849 sb
->dq_op
->write_info(sb
, type
);
2852 EXPORT_SYMBOL(dquot_set_dqinfo
);
2854 const struct quotactl_ops dquot_quotactl_sysfile_ops
= {
2855 .quota_enable
= dquot_quota_enable
,
2856 .quota_disable
= dquot_quota_disable
,
2857 .quota_sync
= dquot_quota_sync
,
2858 .get_state
= dquot_get_state
,
2859 .set_info
= dquot_set_dqinfo
,
2860 .get_dqblk
= dquot_get_dqblk
,
2861 .get_nextdqblk
= dquot_get_next_dqblk
,
2862 .set_dqblk
= dquot_set_dqblk
2864 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops
);
2866 static int do_proc_dqstats(struct ctl_table
*table
, int write
,
2867 void *buffer
, size_t *lenp
, loff_t
*ppos
)
2869 unsigned int type
= (unsigned long *)table
->data
- dqstats
.stat
;
2870 s64 value
= percpu_counter_sum(&dqstats
.counter
[type
]);
2872 /* Filter negative values for non-monotonic counters */
2873 if (value
< 0 && (type
== DQST_ALLOC_DQUOTS
||
2874 type
== DQST_FREE_DQUOTS
))
2877 /* Update global table */
2878 dqstats
.stat
[type
] = value
;
2879 return proc_doulongvec_minmax(table
, write
, buffer
, lenp
, ppos
);
2882 static struct ctl_table fs_dqstats_table
[] = {
2884 .procname
= "lookups",
2885 .data
= &dqstats
.stat
[DQST_LOOKUPS
],
2886 .maxlen
= sizeof(unsigned long),
2888 .proc_handler
= do_proc_dqstats
,
2891 .procname
= "drops",
2892 .data
= &dqstats
.stat
[DQST_DROPS
],
2893 .maxlen
= sizeof(unsigned long),
2895 .proc_handler
= do_proc_dqstats
,
2898 .procname
= "reads",
2899 .data
= &dqstats
.stat
[DQST_READS
],
2900 .maxlen
= sizeof(unsigned long),
2902 .proc_handler
= do_proc_dqstats
,
2905 .procname
= "writes",
2906 .data
= &dqstats
.stat
[DQST_WRITES
],
2907 .maxlen
= sizeof(unsigned long),
2909 .proc_handler
= do_proc_dqstats
,
2912 .procname
= "cache_hits",
2913 .data
= &dqstats
.stat
[DQST_CACHE_HITS
],
2914 .maxlen
= sizeof(unsigned long),
2916 .proc_handler
= do_proc_dqstats
,
2919 .procname
= "allocated_dquots",
2920 .data
= &dqstats
.stat
[DQST_ALLOC_DQUOTS
],
2921 .maxlen
= sizeof(unsigned long),
2923 .proc_handler
= do_proc_dqstats
,
2926 .procname
= "free_dquots",
2927 .data
= &dqstats
.stat
[DQST_FREE_DQUOTS
],
2928 .maxlen
= sizeof(unsigned long),
2930 .proc_handler
= do_proc_dqstats
,
2933 .procname
= "syncs",
2934 .data
= &dqstats
.stat
[DQST_SYNCS
],
2935 .maxlen
= sizeof(unsigned long),
2937 .proc_handler
= do_proc_dqstats
,
2939 #ifdef CONFIG_PRINT_QUOTA_WARNING
2941 .procname
= "warnings",
2942 .data
= &flag_print_warnings
,
2943 .maxlen
= sizeof(int),
2945 .proc_handler
= proc_dointvec
,
2951 static struct ctl_table fs_table
[] = {
2953 .procname
= "quota",
2955 .child
= fs_dqstats_table
,
2960 static struct ctl_table sys_table
[] = {
2969 static int __init
dquot_init(void)
2972 unsigned long nr_hash
, order
;
2974 printk(KERN_NOTICE
"VFS: Disk quotas %s\n", __DQUOT_VERSION__
);
2976 register_sysctl_table(sys_table
);
2978 dquot_cachep
= kmem_cache_create("dquot",
2979 sizeof(struct dquot
), sizeof(unsigned long) * 4,
2980 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
2981 SLAB_MEM_SPREAD
|SLAB_PANIC
),
2985 dquot_hash
= (struct hlist_head
*)__get_free_pages(GFP_KERNEL
, order
);
2987 panic("Cannot create dquot hash table");
2989 for (i
= 0; i
< _DQST_DQSTAT_LAST
; i
++) {
2990 ret
= percpu_counter_init(&dqstats
.counter
[i
], 0, GFP_KERNEL
);
2992 panic("Cannot create dquot stat counters");
2995 /* Find power-of-two hlist_heads which can fit into allocation */
2996 nr_hash
= (1UL << order
) * PAGE_SIZE
/ sizeof(struct hlist_head
);
2997 dq_hash_bits
= ilog2(nr_hash
);
2999 nr_hash
= 1UL << dq_hash_bits
;
3000 dq_hash_mask
= nr_hash
- 1;
3001 for (i
= 0; i
< nr_hash
; i
++)
3002 INIT_HLIST_HEAD(dquot_hash
+ i
);
3004 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3005 " %ld bytes)\n", nr_hash
, order
, (PAGE_SIZE
<< order
));
3007 if (register_shrinker(&dqcache_shrinker
, "dquota-cache"))
3008 panic("Cannot register dquot shrinker");
3012 fs_initcall(dquot_init
);