1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
12 *
13 * Author: Marco van Wieringen <mvw@planets.elm.net>
14 *
15 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
16 *
17 * Revised list management to avoid races
18 * -- Bill Hawes, <whawes@star.net>, 9/98
19 *
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
27 * quota files
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
32 *
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
40 *
41 * Added dynamic quota structure allocation
42 * Jan Kara <jack@suse.cz> 12/2000
43 *
44 * Rewritten quota interface. Implemented new quota format and
45 * formats registering.
46 * Jan Kara, <jack@suse.cz>, 2001,2002
47 *
48 * New SMP locking.
49 * Jan Kara, <jack@suse.cz>, 10/2002
50 *
51 * Added journalled quota support, fix lock inversion problems
52 * Jan Kara, <jack@suse.cz>, 2003,2004
53 *
54 * (C) Copyright 1994 - 1997 Marco van Wieringen
55 */
56
57 #include <linux/errno.h>
58 #include <linux/kernel.h>
59 #include <linux/fs.h>
60 #include <linux/mount.h>
61 #include <linux/mm.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 */
84
85 #include <linux/uaccess.h>
86
87 /*
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.
98 *
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
102 *
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
105 *
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.
118 *
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().
124 *
125 * Lock ordering (including related VFS locks) is the following:
126 * s_umount > i_mutex > journal_lock > dquot->dq_lock > dqio_sem
127 */
128
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);
134
135 static DECLARE_WAIT_QUEUE_HEAD(dquot_ref_wq);
136
__quota_error(struct super_block * sb,const char * func,const char * fmt,...)137 void __quota_error(struct super_block *sb, const char *func,
138 const char *fmt, ...)
139 {
140 if (printk_ratelimit()) {
141 va_list args;
142 struct va_format vaf;
143
144 va_start(args, fmt);
145
146 vaf.fmt = fmt;
147 vaf.va = &args;
148
149 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
150 sb->s_id, func, &vaf);
151
152 va_end(args);
153 }
154 }
155 EXPORT_SYMBOL(__quota_error);
156
157 #if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
158 static char *quotatypes[] = INITQFNAMES;
159 #endif
160 static struct quota_format_type *quota_formats; /* List of registered formats */
161 static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
162
163 /* SLAB cache for dquot structures */
164 static struct kmem_cache *dquot_cachep;
165
register_quota_format(struct quota_format_type * fmt)166 int register_quota_format(struct quota_format_type *fmt)
167 {
168 spin_lock(&dq_list_lock);
169 fmt->qf_next = quota_formats;
170 quota_formats = fmt;
171 spin_unlock(&dq_list_lock);
172 return 0;
173 }
174 EXPORT_SYMBOL(register_quota_format);
175
unregister_quota_format(struct quota_format_type * fmt)176 void unregister_quota_format(struct quota_format_type *fmt)
177 {
178 struct quota_format_type **actqf;
179
180 spin_lock(&dq_list_lock);
181 for (actqf = "a_formats; *actqf && *actqf != fmt;
182 actqf = &(*actqf)->qf_next)
183 ;
184 if (*actqf)
185 *actqf = (*actqf)->qf_next;
186 spin_unlock(&dq_list_lock);
187 }
188 EXPORT_SYMBOL(unregister_quota_format);
189
find_quota_format(int id)190 static struct quota_format_type *find_quota_format(int id)
191 {
192 struct quota_format_type *actqf;
193
194 spin_lock(&dq_list_lock);
195 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
196 actqf = actqf->qf_next)
197 ;
198 if (!actqf || !try_module_get(actqf->qf_owner)) {
199 int qm;
200
201 spin_unlock(&dq_list_lock);
202
203 for (qm = 0; module_names[qm].qm_fmt_id &&
204 module_names[qm].qm_fmt_id != id; qm++)
205 ;
206 if (!module_names[qm].qm_fmt_id ||
207 request_module(module_names[qm].qm_mod_name))
208 return NULL;
209
210 spin_lock(&dq_list_lock);
211 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
212 actqf = actqf->qf_next)
213 ;
214 if (actqf && !try_module_get(actqf->qf_owner))
215 actqf = NULL;
216 }
217 spin_unlock(&dq_list_lock);
218 return actqf;
219 }
220
put_quota_format(struct quota_format_type * fmt)221 static void put_quota_format(struct quota_format_type *fmt)
222 {
223 module_put(fmt->qf_owner);
224 }
225
226 /*
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.
231 *
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.
234 *
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.
240 *
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.
245 *
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.
249 */
250
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;
255
256 struct dqstats dqstats;
257 EXPORT_SYMBOL(dqstats);
258
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);
262
263 static inline unsigned int
hashfn(const struct super_block * sb,struct kqid qid)264 hashfn(const struct super_block *sb, struct kqid qid)
265 {
266 unsigned int id = from_kqid(&init_user_ns, qid);
267 int type = qid.type;
268 unsigned long tmp;
269
270 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
271 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
272 }
273
274 /*
275 * Following list functions expect dq_list_lock to be held
276 */
insert_dquot_hash(struct dquot * dquot)277 static inline void insert_dquot_hash(struct dquot *dquot)
278 {
279 struct hlist_head *head;
280 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
281 hlist_add_head(&dquot->dq_hash, head);
282 }
283
remove_dquot_hash(struct dquot * dquot)284 static inline void remove_dquot_hash(struct dquot *dquot)
285 {
286 hlist_del_init(&dquot->dq_hash);
287 }
288
find_dquot(unsigned int hashent,struct super_block * sb,struct kqid qid)289 static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
290 struct kqid qid)
291 {
292 struct hlist_node *node;
293 struct dquot *dquot;
294
295 hlist_for_each (node, dquot_hash+hashent) {
296 dquot = hlist_entry(node, struct dquot, dq_hash);
297 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
298 return dquot;
299 }
300 return NULL;
301 }
302
303 /* Add a dquot to the tail of the free list */
put_dquot_last(struct dquot * dquot)304 static inline void put_dquot_last(struct dquot *dquot)
305 {
306 list_add_tail(&dquot->dq_free, &free_dquots);
307 dqstats_inc(DQST_FREE_DQUOTS);
308 }
309
remove_free_dquot(struct dquot * dquot)310 static inline void remove_free_dquot(struct dquot *dquot)
311 {
312 if (list_empty(&dquot->dq_free))
313 return;
314 list_del_init(&dquot->dq_free);
315 dqstats_dec(DQST_FREE_DQUOTS);
316 }
317
put_inuse(struct dquot * dquot)318 static inline void put_inuse(struct dquot *dquot)
319 {
320 /* We add to the back of inuse list so we don't have to restart
321 * when traversing this list and we block */
322 list_add_tail(&dquot->dq_inuse, &inuse_list);
323 dqstats_inc(DQST_ALLOC_DQUOTS);
324 }
325
remove_inuse(struct dquot * dquot)326 static inline void remove_inuse(struct dquot *dquot)
327 {
328 dqstats_dec(DQST_ALLOC_DQUOTS);
329 list_del(&dquot->dq_inuse);
330 }
331 /*
332 * End of list functions needing dq_list_lock
333 */
334
wait_on_dquot(struct dquot * dquot)335 static void wait_on_dquot(struct dquot *dquot)
336 {
337 mutex_lock(&dquot->dq_lock);
338 mutex_unlock(&dquot->dq_lock);
339 }
340
dquot_dirty(struct dquot * dquot)341 static inline int dquot_dirty(struct dquot *dquot)
342 {
343 return test_bit(DQ_MOD_B, &dquot->dq_flags);
344 }
345
mark_dquot_dirty(struct dquot * dquot)346 static inline int mark_dquot_dirty(struct dquot *dquot)
347 {
348 return dquot->dq_sb->dq_op->mark_dirty(dquot);
349 }
350
351 /* Mark dquot dirty in atomic manner, and return it's old dirty flag state */
dquot_mark_dquot_dirty(struct dquot * dquot)352 int dquot_mark_dquot_dirty(struct dquot *dquot)
353 {
354 int ret = 1;
355
356 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
357 return 0;
358
359 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
360 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
361
362 /* If quota is dirty already, we don't have to acquire dq_list_lock */
363 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
364 return 1;
365
366 spin_lock(&dq_list_lock);
367 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
368 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
369 info[dquot->dq_id.type].dqi_dirty_list);
370 ret = 0;
371 }
372 spin_unlock(&dq_list_lock);
373 return ret;
374 }
375 EXPORT_SYMBOL(dquot_mark_dquot_dirty);
376
377 /* Dirtify all the dquots - this can block when journalling */
mark_all_dquot_dirty(struct dquot * const * dquot)378 static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
379 {
380 int ret, err, cnt;
381
382 ret = err = 0;
383 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
384 if (dquot[cnt])
385 /* Even in case of error we have to continue */
386 ret = mark_dquot_dirty(dquot[cnt]);
387 if (!err)
388 err = ret;
389 }
390 return err;
391 }
392
dqput_all(struct dquot ** dquot)393 static inline void dqput_all(struct dquot **dquot)
394 {
395 unsigned int cnt;
396
397 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
398 dqput(dquot[cnt]);
399 }
400
clear_dquot_dirty(struct dquot * dquot)401 static inline int clear_dquot_dirty(struct dquot *dquot)
402 {
403 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
404 return test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags);
405
406 spin_lock(&dq_list_lock);
407 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags)) {
408 spin_unlock(&dq_list_lock);
409 return 0;
410 }
411 list_del_init(&dquot->dq_dirty);
412 spin_unlock(&dq_list_lock);
413 return 1;
414 }
415
mark_info_dirty(struct super_block * sb,int type)416 void mark_info_dirty(struct super_block *sb, int type)
417 {
418 spin_lock(&dq_data_lock);
419 sb_dqopt(sb)->info[type].dqi_flags |= DQF_INFO_DIRTY;
420 spin_unlock(&dq_data_lock);
421 }
422 EXPORT_SYMBOL(mark_info_dirty);
423
424 /*
425 * Read dquot from disk and alloc space for it
426 */
427
dquot_acquire(struct dquot * dquot)428 int dquot_acquire(struct dquot *dquot)
429 {
430 int ret = 0, ret2 = 0;
431 unsigned int memalloc;
432 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
433
434 mutex_lock(&dquot->dq_lock);
435 memalloc = memalloc_nofs_save();
436 if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
437 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
438 if (ret < 0)
439 goto out_iolock;
440 }
441 /* Make sure flags update is visible after dquot has been filled */
442 smp_mb__before_atomic();
443 set_bit(DQ_READ_B, &dquot->dq_flags);
444 /* Instantiate dquot if needed */
445 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
446 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
447 /* Write the info if needed */
448 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
449 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
450 dquot->dq_sb, dquot->dq_id.type);
451 }
452 if (ret < 0)
453 goto out_iolock;
454 if (ret2 < 0) {
455 ret = ret2;
456 goto out_iolock;
457 }
458 }
459 /*
460 * Make sure flags update is visible after on-disk struct has been
461 * allocated. Paired with smp_rmb() in dqget().
462 */
463 smp_mb__before_atomic();
464 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
465 out_iolock:
466 memalloc_nofs_restore(memalloc);
467 mutex_unlock(&dquot->dq_lock);
468 return ret;
469 }
470 EXPORT_SYMBOL(dquot_acquire);
471
472 /*
473 * Write dquot to disk
474 */
dquot_commit(struct dquot * dquot)475 int dquot_commit(struct dquot *dquot)
476 {
477 int ret = 0;
478 unsigned int memalloc;
479 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
480
481 mutex_lock(&dquot->dq_lock);
482 memalloc = memalloc_nofs_save();
483 if (!clear_dquot_dirty(dquot))
484 goto out_lock;
485 /* Inactive dquot can be only if there was error during read/init
486 * => we have better not writing it */
487 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
488 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
489 else
490 ret = -EIO;
491 out_lock:
492 memalloc_nofs_restore(memalloc);
493 mutex_unlock(&dquot->dq_lock);
494 return ret;
495 }
496 EXPORT_SYMBOL(dquot_commit);
497
498 /*
499 * Release dquot
500 */
dquot_release(struct dquot * dquot)501 int dquot_release(struct dquot *dquot)
502 {
503 int ret = 0, ret2 = 0;
504 unsigned int memalloc;
505 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
506
507 mutex_lock(&dquot->dq_lock);
508 memalloc = memalloc_nofs_save();
509 /* Check whether we are not racing with some other dqget() */
510 if (dquot_is_busy(dquot))
511 goto out_dqlock;
512 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
513 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
514 /* Write the info */
515 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
516 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
517 dquot->dq_sb, dquot->dq_id.type);
518 }
519 if (ret >= 0)
520 ret = ret2;
521 }
522 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
523 out_dqlock:
524 memalloc_nofs_restore(memalloc);
525 mutex_unlock(&dquot->dq_lock);
526 return ret;
527 }
528 EXPORT_SYMBOL(dquot_release);
529
dquot_destroy(struct dquot * dquot)530 void dquot_destroy(struct dquot *dquot)
531 {
532 kmem_cache_free(dquot_cachep, dquot);
533 }
534 EXPORT_SYMBOL(dquot_destroy);
535
do_destroy_dquot(struct dquot * dquot)536 static inline void do_destroy_dquot(struct dquot *dquot)
537 {
538 dquot->dq_sb->dq_op->destroy_dquot(dquot);
539 }
540
541 /* Invalidate all dquots on the list. Note that this function is called after
542 * quota is disabled and pointers from inodes removed so there cannot be new
543 * quota users. There can still be some users of quotas due to inodes being
544 * just deleted or pruned by prune_icache() (those are not attached to any
545 * list) or parallel quotactl call. We have to wait for such users.
546 */
invalidate_dquots(struct super_block * sb,int type)547 static void invalidate_dquots(struct super_block *sb, int type)
548 {
549 struct dquot *dquot, *tmp;
550
551 restart:
552 spin_lock(&dq_list_lock);
553 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
554 if (dquot->dq_sb != sb)
555 continue;
556 if (dquot->dq_id.type != type)
557 continue;
558 /* Wait for dquot users */
559 if (atomic_read(&dquot->dq_count)) {
560 dqgrab(dquot);
561 spin_unlock(&dq_list_lock);
562 /*
563 * Once dqput() wakes us up, we know it's time to free
564 * the dquot.
565 * IMPORTANT: we rely on the fact that there is always
566 * at most one process waiting for dquot to free.
567 * Otherwise dq_count would be > 1 and we would never
568 * wake up.
569 */
570 wait_event(dquot_ref_wq,
571 atomic_read(&dquot->dq_count) == 1);
572 dqput(dquot);
573 /* At this moment dquot() need not exist (it could be
574 * reclaimed by prune_dqcache(). Hence we must
575 * restart. */
576 goto restart;
577 }
578 /*
579 * Quota now has no users and it has been written on last
580 * dqput()
581 */
582 remove_dquot_hash(dquot);
583 remove_free_dquot(dquot);
584 remove_inuse(dquot);
585 do_destroy_dquot(dquot);
586 }
587 spin_unlock(&dq_list_lock);
588 }
589
590 /* Call callback for every active dquot on given filesystem */
dquot_scan_active(struct super_block * sb,int (* fn)(struct dquot * dquot,unsigned long priv),unsigned long priv)591 int dquot_scan_active(struct super_block *sb,
592 int (*fn)(struct dquot *dquot, unsigned long priv),
593 unsigned long priv)
594 {
595 struct dquot *dquot, *old_dquot = NULL;
596 int ret = 0;
597
598 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
599
600 spin_lock(&dq_list_lock);
601 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
602 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
603 continue;
604 if (dquot->dq_sb != sb)
605 continue;
606 /* Now we have active dquot so we can just increase use count */
607 atomic_inc(&dquot->dq_count);
608 spin_unlock(&dq_list_lock);
609 dqput(old_dquot);
610 old_dquot = dquot;
611 /*
612 * ->release_dquot() can be racing with us. Our reference
613 * protects us from new calls to it so just wait for any
614 * outstanding call and recheck the DQ_ACTIVE_B after that.
615 */
616 wait_on_dquot(dquot);
617 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
618 ret = fn(dquot, priv);
619 if (ret < 0)
620 goto out;
621 }
622 spin_lock(&dq_list_lock);
623 /* We are safe to continue now because our dquot could not
624 * be moved out of the inuse list while we hold the reference */
625 }
626 spin_unlock(&dq_list_lock);
627 out:
628 dqput(old_dquot);
629 return ret;
630 }
631 EXPORT_SYMBOL(dquot_scan_active);
632
633 /* Write all dquot structures to quota files */
dquot_writeback_dquots(struct super_block * sb,int type)634 int dquot_writeback_dquots(struct super_block *sb, int type)
635 {
636 struct list_head dirty;
637 struct dquot *dquot;
638 struct quota_info *dqopt = sb_dqopt(sb);
639 int cnt;
640 int err, ret = 0;
641
642 WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount));
643
644 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
645 if (type != -1 && cnt != type)
646 continue;
647 if (!sb_has_quota_active(sb, cnt))
648 continue;
649 spin_lock(&dq_list_lock);
650 /* Move list away to avoid livelock. */
651 list_replace_init(&dqopt->info[cnt].dqi_dirty_list, &dirty);
652 while (!list_empty(&dirty)) {
653 dquot = list_first_entry(&dirty, struct dquot,
654 dq_dirty);
655
656 WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
657
658 /* Now we have active dquot from which someone is
659 * holding reference so we can safely just increase
660 * use count */
661 dqgrab(dquot);
662 spin_unlock(&dq_list_lock);
663 err = sb->dq_op->write_dquot(dquot);
664 if (err) {
665 /*
666 * Clear dirty bit anyway to avoid infinite
667 * loop here.
668 */
669 clear_dquot_dirty(dquot);
670 if (!ret)
671 ret = err;
672 }
673 dqput(dquot);
674 spin_lock(&dq_list_lock);
675 }
676 spin_unlock(&dq_list_lock);
677 }
678
679 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
680 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
681 && info_dirty(&dqopt->info[cnt]))
682 sb->dq_op->write_info(sb, cnt);
683 dqstats_inc(DQST_SYNCS);
684
685 return ret;
686 }
687 EXPORT_SYMBOL(dquot_writeback_dquots);
688
689 /* Write all dquot structures to disk and make them visible from userspace */
dquot_quota_sync(struct super_block * sb,int type)690 int dquot_quota_sync(struct super_block *sb, int type)
691 {
692 struct quota_info *dqopt = sb_dqopt(sb);
693 int cnt;
694 int ret;
695
696 ret = dquot_writeback_dquots(sb, type);
697 if (ret)
698 return ret;
699 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
700 return 0;
701
702 /* This is not very clever (and fast) but currently I don't know about
703 * any other simple way of getting quota data to disk and we must get
704 * them there for userspace to be visible... */
705 if (sb->s_op->sync_fs) {
706 ret = sb->s_op->sync_fs(sb, 1);
707 if (ret)
708 return ret;
709 }
710 ret = sync_blockdev(sb->s_bdev);
711 if (ret)
712 return ret;
713
714 /*
715 * Now when everything is written we can discard the pagecache so
716 * that userspace sees the changes.
717 */
718 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
719 if (type != -1 && cnt != type)
720 continue;
721 if (!sb_has_quota_active(sb, cnt))
722 continue;
723 inode_lock(dqopt->files[cnt]);
724 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
725 inode_unlock(dqopt->files[cnt]);
726 }
727
728 return 0;
729 }
730 EXPORT_SYMBOL(dquot_quota_sync);
731
732 static unsigned long
dqcache_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)733 dqcache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
734 {
735 struct dquot *dquot;
736 unsigned long freed = 0;
737
738 spin_lock(&dq_list_lock);
739 while (!list_empty(&free_dquots) && sc->nr_to_scan) {
740 dquot = list_first_entry(&free_dquots, struct dquot, dq_free);
741 remove_dquot_hash(dquot);
742 remove_free_dquot(dquot);
743 remove_inuse(dquot);
744 do_destroy_dquot(dquot);
745 sc->nr_to_scan--;
746 freed++;
747 }
748 spin_unlock(&dq_list_lock);
749 return freed;
750 }
751
752 static unsigned long
dqcache_shrink_count(struct shrinker * shrink,struct shrink_control * sc)753 dqcache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
754 {
755 return vfs_pressure_ratio(
756 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS]));
757 }
758
759 static struct shrinker dqcache_shrinker = {
760 .count_objects = dqcache_shrink_count,
761 .scan_objects = dqcache_shrink_scan,
762 .seeks = DEFAULT_SEEKS,
763 };
764
765 /*
766 * Put reference to dquot
767 */
dqput(struct dquot * dquot)768 void dqput(struct dquot *dquot)
769 {
770 int ret;
771
772 if (!dquot)
773 return;
774 #ifdef CONFIG_QUOTA_DEBUG
775 if (!atomic_read(&dquot->dq_count)) {
776 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
777 quotatypes[dquot->dq_id.type],
778 from_kqid(&init_user_ns, dquot->dq_id));
779 BUG();
780 }
781 #endif
782 dqstats_inc(DQST_DROPS);
783 we_slept:
784 spin_lock(&dq_list_lock);
785 if (atomic_read(&dquot->dq_count) > 1) {
786 /* We have more than one user... nothing to do */
787 atomic_dec(&dquot->dq_count);
788 /* Releasing dquot during quotaoff phase? */
789 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
790 atomic_read(&dquot->dq_count) == 1)
791 wake_up(&dquot_ref_wq);
792 spin_unlock(&dq_list_lock);
793 return;
794 }
795 /* Need to release dquot? */
796 if (dquot_dirty(dquot)) {
797 spin_unlock(&dq_list_lock);
798 /* Commit dquot before releasing */
799 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
800 if (ret < 0) {
801 quota_error(dquot->dq_sb, "Can't write quota structure"
802 " (error %d). Quota may get out of sync!",
803 ret);
804 /*
805 * We clear dirty bit anyway, so that we avoid
806 * infinite loop here
807 */
808 clear_dquot_dirty(dquot);
809 }
810 goto we_slept;
811 }
812 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
813 spin_unlock(&dq_list_lock);
814 dquot->dq_sb->dq_op->release_dquot(dquot);
815 goto we_slept;
816 }
817 atomic_dec(&dquot->dq_count);
818 #ifdef CONFIG_QUOTA_DEBUG
819 /* sanity check */
820 BUG_ON(!list_empty(&dquot->dq_free));
821 #endif
822 put_dquot_last(dquot);
823 spin_unlock(&dq_list_lock);
824 }
825 EXPORT_SYMBOL(dqput);
826
dquot_alloc(struct super_block * sb,int type)827 struct dquot *dquot_alloc(struct super_block *sb, int type)
828 {
829 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
830 }
831 EXPORT_SYMBOL(dquot_alloc);
832
get_empty_dquot(struct super_block * sb,int type)833 static struct dquot *get_empty_dquot(struct super_block *sb, int type)
834 {
835 struct dquot *dquot;
836
837 dquot = sb->dq_op->alloc_dquot(sb, type);
838 if(!dquot)
839 return NULL;
840
841 mutex_init(&dquot->dq_lock);
842 INIT_LIST_HEAD(&dquot->dq_free);
843 INIT_LIST_HEAD(&dquot->dq_inuse);
844 INIT_HLIST_NODE(&dquot->dq_hash);
845 INIT_LIST_HEAD(&dquot->dq_dirty);
846 dquot->dq_sb = sb;
847 dquot->dq_id = make_kqid_invalid(type);
848 atomic_set(&dquot->dq_count, 1);
849 spin_lock_init(&dquot->dq_dqb_lock);
850
851 return dquot;
852 }
853
854 /*
855 * Get reference to dquot
856 *
857 * Locking is slightly tricky here. We are guarded from parallel quotaoff()
858 * destroying our dquot by:
859 * a) checking for quota flags under dq_list_lock and
860 * b) getting a reference to dquot before we release dq_list_lock
861 */
dqget(struct super_block * sb,struct kqid qid)862 struct dquot *dqget(struct super_block *sb, struct kqid qid)
863 {
864 unsigned int hashent = hashfn(sb, qid);
865 struct dquot *dquot, *empty = NULL;
866
867 if (!qid_has_mapping(sb->s_user_ns, qid))
868 return ERR_PTR(-EINVAL);
869
870 if (!sb_has_quota_active(sb, qid.type))
871 return ERR_PTR(-ESRCH);
872 we_slept:
873 spin_lock(&dq_list_lock);
874 spin_lock(&dq_state_lock);
875 if (!sb_has_quota_active(sb, qid.type)) {
876 spin_unlock(&dq_state_lock);
877 spin_unlock(&dq_list_lock);
878 dquot = ERR_PTR(-ESRCH);
879 goto out;
880 }
881 spin_unlock(&dq_state_lock);
882
883 dquot = find_dquot(hashent, sb, qid);
884 if (!dquot) {
885 if (!empty) {
886 spin_unlock(&dq_list_lock);
887 empty = get_empty_dquot(sb, qid.type);
888 if (!empty)
889 schedule(); /* Try to wait for a moment... */
890 goto we_slept;
891 }
892 dquot = empty;
893 empty = NULL;
894 dquot->dq_id = qid;
895 /* all dquots go on the inuse_list */
896 put_inuse(dquot);
897 /* hash it first so it can be found */
898 insert_dquot_hash(dquot);
899 spin_unlock(&dq_list_lock);
900 dqstats_inc(DQST_LOOKUPS);
901 } else {
902 if (!atomic_read(&dquot->dq_count))
903 remove_free_dquot(dquot);
904 atomic_inc(&dquot->dq_count);
905 spin_unlock(&dq_list_lock);
906 dqstats_inc(DQST_CACHE_HITS);
907 dqstats_inc(DQST_LOOKUPS);
908 }
909 /* Wait for dq_lock - after this we know that either dquot_release() is
910 * already finished or it will be canceled due to dq_count > 1 test */
911 wait_on_dquot(dquot);
912 /* Read the dquot / allocate space in quota file */
913 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
914 int err;
915
916 err = sb->dq_op->acquire_dquot(dquot);
917 if (err < 0) {
918 dqput(dquot);
919 dquot = ERR_PTR(err);
920 goto out;
921 }
922 }
923 /*
924 * Make sure following reads see filled structure - paired with
925 * smp_mb__before_atomic() in dquot_acquire().
926 */
927 smp_rmb();
928 #ifdef CONFIG_QUOTA_DEBUG
929 BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
930 #endif
931 out:
932 if (empty)
933 do_destroy_dquot(empty);
934
935 return dquot;
936 }
937 EXPORT_SYMBOL(dqget);
938
i_dquot(struct inode * inode)939 static inline struct dquot **i_dquot(struct inode *inode)
940 {
941 return inode->i_sb->s_op->get_dquots(inode);
942 }
943
dqinit_needed(struct inode * inode,int type)944 static int dqinit_needed(struct inode *inode, int type)
945 {
946 struct dquot * const *dquots;
947 int cnt;
948
949 if (IS_NOQUOTA(inode))
950 return 0;
951
952 dquots = i_dquot(inode);
953 if (type != -1)
954 return !dquots[type];
955 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
956 if (!dquots[cnt])
957 return 1;
958 return 0;
959 }
960
961 /* This routine is guarded by s_umount semaphore */
add_dquot_ref(struct super_block * sb,int type)962 static int add_dquot_ref(struct super_block *sb, int type)
963 {
964 struct inode *inode, *old_inode = NULL;
965 #ifdef CONFIG_QUOTA_DEBUG
966 int reserved = 0;
967 #endif
968 int err = 0;
969
970 spin_lock(&sb->s_inode_list_lock);
971 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
972 spin_lock(&inode->i_lock);
973 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
974 !atomic_read(&inode->i_writecount) ||
975 !dqinit_needed(inode, type)) {
976 spin_unlock(&inode->i_lock);
977 continue;
978 }
979 __iget(inode);
980 spin_unlock(&inode->i_lock);
981 spin_unlock(&sb->s_inode_list_lock);
982
983 #ifdef CONFIG_QUOTA_DEBUG
984 if (unlikely(inode_get_rsv_space(inode) > 0))
985 reserved = 1;
986 #endif
987 iput(old_inode);
988 err = __dquot_initialize(inode, type);
989 if (err) {
990 iput(inode);
991 goto out;
992 }
993
994 /*
995 * We hold a reference to 'inode' so it couldn't have been
996 * removed from s_inodes list while we dropped the
997 * s_inode_list_lock. We cannot iput the inode now as we can be
998 * holding the last reference and we cannot iput it under
999 * s_inode_list_lock. So we keep the reference and iput it
1000 * later.
1001 */
1002 old_inode = inode;
1003 cond_resched();
1004 spin_lock(&sb->s_inode_list_lock);
1005 }
1006 spin_unlock(&sb->s_inode_list_lock);
1007 iput(old_inode);
1008 out:
1009 #ifdef CONFIG_QUOTA_DEBUG
1010 if (reserved) {
1011 quota_error(sb, "Writes happened before quota was turned on "
1012 "thus quota information is probably inconsistent. "
1013 "Please run quotacheck(8)");
1014 }
1015 #endif
1016 return err;
1017 }
1018
1019 /*
1020 * Remove references to dquots from inode and add dquot to list for freeing
1021 * if we have the last reference to dquot
1022 */
remove_inode_dquot_ref(struct inode * inode,int type,struct list_head * tofree_head)1023 static void remove_inode_dquot_ref(struct inode *inode, int type,
1024 struct list_head *tofree_head)
1025 {
1026 struct dquot **dquots = i_dquot(inode);
1027 struct dquot *dquot = dquots[type];
1028
1029 if (!dquot)
1030 return;
1031
1032 dquots[type] = NULL;
1033 if (list_empty(&dquot->dq_free)) {
1034 /*
1035 * The inode still has reference to dquot so it can't be in the
1036 * free list
1037 */
1038 spin_lock(&dq_list_lock);
1039 list_add(&dquot->dq_free, tofree_head);
1040 spin_unlock(&dq_list_lock);
1041 } else {
1042 /*
1043 * Dquot is already in a list to put so we won't drop the last
1044 * reference here.
1045 */
1046 dqput(dquot);
1047 }
1048 }
1049
1050 /*
1051 * Free list of dquots
1052 * Dquots are removed from inodes and no new references can be got so we are
1053 * the only ones holding reference
1054 */
put_dquot_list(struct list_head * tofree_head)1055 static void put_dquot_list(struct list_head *tofree_head)
1056 {
1057 struct list_head *act_head;
1058 struct dquot *dquot;
1059
1060 act_head = tofree_head->next;
1061 while (act_head != tofree_head) {
1062 dquot = list_entry(act_head, struct dquot, dq_free);
1063 act_head = act_head->next;
1064 /* Remove dquot from the list so we won't have problems... */
1065 list_del_init(&dquot->dq_free);
1066 dqput(dquot);
1067 }
1068 }
1069
remove_dquot_ref(struct super_block * sb,int type,struct list_head * tofree_head)1070 static void remove_dquot_ref(struct super_block *sb, int type,
1071 struct list_head *tofree_head)
1072 {
1073 struct inode *inode;
1074 #ifdef CONFIG_QUOTA_DEBUG
1075 int reserved = 0;
1076 #endif
1077
1078 spin_lock(&sb->s_inode_list_lock);
1079 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1080 /*
1081 * We have to scan also I_NEW inodes because they can already
1082 * have quota pointer initialized. Luckily, we need to touch
1083 * only quota pointers and these have separate locking
1084 * (dq_data_lock).
1085 */
1086 spin_lock(&dq_data_lock);
1087 if (!IS_NOQUOTA(inode)) {
1088 #ifdef CONFIG_QUOTA_DEBUG
1089 if (unlikely(inode_get_rsv_space(inode) > 0))
1090 reserved = 1;
1091 #endif
1092 remove_inode_dquot_ref(inode, type, tofree_head);
1093 }
1094 spin_unlock(&dq_data_lock);
1095 }
1096 spin_unlock(&sb->s_inode_list_lock);
1097 #ifdef CONFIG_QUOTA_DEBUG
1098 if (reserved) {
1099 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1100 " was disabled thus quota information is probably "
1101 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1102 }
1103 #endif
1104 }
1105
1106 /* Gather all references from inodes and drop them */
drop_dquot_ref(struct super_block * sb,int type)1107 static void drop_dquot_ref(struct super_block *sb, int type)
1108 {
1109 LIST_HEAD(tofree_head);
1110
1111 if (sb->dq_op) {
1112 remove_dquot_ref(sb, type, &tofree_head);
1113 synchronize_srcu(&dquot_srcu);
1114 put_dquot_list(&tofree_head);
1115 }
1116 }
1117
1118 static inline
dquot_free_reserved_space(struct dquot * dquot,qsize_t number)1119 void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1120 {
1121 if (dquot->dq_dqb.dqb_rsvspace >= number)
1122 dquot->dq_dqb.dqb_rsvspace -= number;
1123 else {
1124 WARN_ON_ONCE(1);
1125 dquot->dq_dqb.dqb_rsvspace = 0;
1126 }
1127 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1128 dquot->dq_dqb.dqb_bsoftlimit)
1129 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1130 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1131 }
1132
dquot_decr_inodes(struct dquot * dquot,qsize_t number)1133 static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1134 {
1135 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1136 dquot->dq_dqb.dqb_curinodes >= number)
1137 dquot->dq_dqb.dqb_curinodes -= number;
1138 else
1139 dquot->dq_dqb.dqb_curinodes = 0;
1140 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1141 dquot->dq_dqb.dqb_itime = (time64_t) 0;
1142 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1143 }
1144
dquot_decr_space(struct dquot * dquot,qsize_t number)1145 static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1146 {
1147 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1148 dquot->dq_dqb.dqb_curspace >= number)
1149 dquot->dq_dqb.dqb_curspace -= number;
1150 else
1151 dquot->dq_dqb.dqb_curspace = 0;
1152 if (dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace <=
1153 dquot->dq_dqb.dqb_bsoftlimit)
1154 dquot->dq_dqb.dqb_btime = (time64_t) 0;
1155 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1156 }
1157
1158 struct dquot_warn {
1159 struct super_block *w_sb;
1160 struct kqid w_dq_id;
1161 short w_type;
1162 };
1163
warning_issued(struct dquot * dquot,const int warntype)1164 static int warning_issued(struct dquot *dquot, const int warntype)
1165 {
1166 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1167 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1168 ((warntype == QUOTA_NL_IHARDWARN ||
1169 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1170
1171 if (!flag)
1172 return 0;
1173 return test_and_set_bit(flag, &dquot->dq_flags);
1174 }
1175
1176 #ifdef CONFIG_PRINT_QUOTA_WARNING
1177 static int flag_print_warnings = 1;
1178
need_print_warning(struct dquot_warn * warn)1179 static int need_print_warning(struct dquot_warn *warn)
1180 {
1181 if (!flag_print_warnings)
1182 return 0;
1183
1184 switch (warn->w_dq_id.type) {
1185 case USRQUOTA:
1186 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1187 case GRPQUOTA:
1188 return in_group_p(warn->w_dq_id.gid);
1189 case PRJQUOTA:
1190 return 1;
1191 }
1192 return 0;
1193 }
1194
1195 /* Print warning to user which exceeded quota */
print_warning(struct dquot_warn * warn)1196 static void print_warning(struct dquot_warn *warn)
1197 {
1198 char *msg = NULL;
1199 struct tty_struct *tty;
1200 int warntype = warn->w_type;
1201
1202 if (warntype == QUOTA_NL_IHARDBELOW ||
1203 warntype == QUOTA_NL_ISOFTBELOW ||
1204 warntype == QUOTA_NL_BHARDBELOW ||
1205 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1206 return;
1207
1208 tty = get_current_tty();
1209 if (!tty)
1210 return;
1211 tty_write_message(tty, warn->w_sb->s_id);
1212 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1213 tty_write_message(tty, ": warning, ");
1214 else
1215 tty_write_message(tty, ": write failed, ");
1216 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1217 switch (warntype) {
1218 case QUOTA_NL_IHARDWARN:
1219 msg = " file limit reached.\r\n";
1220 break;
1221 case QUOTA_NL_ISOFTLONGWARN:
1222 msg = " file quota exceeded too long.\r\n";
1223 break;
1224 case QUOTA_NL_ISOFTWARN:
1225 msg = " file quota exceeded.\r\n";
1226 break;
1227 case QUOTA_NL_BHARDWARN:
1228 msg = " block limit reached.\r\n";
1229 break;
1230 case QUOTA_NL_BSOFTLONGWARN:
1231 msg = " block quota exceeded too long.\r\n";
1232 break;
1233 case QUOTA_NL_BSOFTWARN:
1234 msg = " block quota exceeded.\r\n";
1235 break;
1236 }
1237 tty_write_message(tty, msg);
1238 tty_kref_put(tty);
1239 }
1240 #endif
1241
prepare_warning(struct dquot_warn * warn,struct dquot * dquot,int warntype)1242 static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1243 int warntype)
1244 {
1245 if (warning_issued(dquot, warntype))
1246 return;
1247 warn->w_type = warntype;
1248 warn->w_sb = dquot->dq_sb;
1249 warn->w_dq_id = dquot->dq_id;
1250 }
1251
1252 /*
1253 * Write warnings to the console and send warning messages over netlink.
1254 *
1255 * Note that this function can call into tty and networking code.
1256 */
flush_warnings(struct dquot_warn * warn)1257 static void flush_warnings(struct dquot_warn *warn)
1258 {
1259 int i;
1260
1261 for (i = 0; i < MAXQUOTAS; i++) {
1262 if (warn[i].w_type == QUOTA_NL_NOWARN)
1263 continue;
1264 #ifdef CONFIG_PRINT_QUOTA_WARNING
1265 print_warning(&warn[i]);
1266 #endif
1267 quota_send_warning(warn[i].w_dq_id,
1268 warn[i].w_sb->s_dev, warn[i].w_type);
1269 }
1270 }
1271
ignore_hardlimit(struct dquot * dquot)1272 static int ignore_hardlimit(struct dquot *dquot)
1273 {
1274 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1275
1276 return capable(CAP_SYS_RESOURCE) &&
1277 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1278 !(info->dqi_flags & DQF_ROOT_SQUASH));
1279 }
1280
dquot_add_inodes(struct dquot * dquot,qsize_t inodes,struct dquot_warn * warn)1281 static int dquot_add_inodes(struct dquot *dquot, qsize_t inodes,
1282 struct dquot_warn *warn)
1283 {
1284 qsize_t newinodes;
1285 int ret = 0;
1286
1287 spin_lock(&dquot->dq_dqb_lock);
1288 newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1289 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1290 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1291 goto add;
1292
1293 if (dquot->dq_dqb.dqb_ihardlimit &&
1294 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1295 !ignore_hardlimit(dquot)) {
1296 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1297 ret = -EDQUOT;
1298 goto out;
1299 }
1300
1301 if (dquot->dq_dqb.dqb_isoftlimit &&
1302 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1303 dquot->dq_dqb.dqb_itime &&
1304 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_itime &&
1305 !ignore_hardlimit(dquot)) {
1306 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1307 ret = -EDQUOT;
1308 goto out;
1309 }
1310
1311 if (dquot->dq_dqb.dqb_isoftlimit &&
1312 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1313 dquot->dq_dqb.dqb_itime == 0) {
1314 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1315 dquot->dq_dqb.dqb_itime = ktime_get_real_seconds() +
1316 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1317 }
1318 add:
1319 dquot->dq_dqb.dqb_curinodes = newinodes;
1320
1321 out:
1322 spin_unlock(&dquot->dq_dqb_lock);
1323 return ret;
1324 }
1325
dquot_add_space(struct dquot * dquot,qsize_t space,qsize_t rsv_space,unsigned int flags,struct dquot_warn * warn)1326 static int dquot_add_space(struct dquot *dquot, qsize_t space,
1327 qsize_t rsv_space, unsigned int flags,
1328 struct dquot_warn *warn)
1329 {
1330 qsize_t tspace;
1331 struct super_block *sb = dquot->dq_sb;
1332 int ret = 0;
1333
1334 spin_lock(&dquot->dq_dqb_lock);
1335 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1336 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1337 goto finish;
1338
1339 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1340 + space + rsv_space;
1341
1342 if (dquot->dq_dqb.dqb_bhardlimit &&
1343 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1344 !ignore_hardlimit(dquot)) {
1345 if (flags & DQUOT_SPACE_WARN)
1346 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1347 ret = -EDQUOT;
1348 goto finish;
1349 }
1350
1351 if (dquot->dq_dqb.dqb_bsoftlimit &&
1352 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1353 dquot->dq_dqb.dqb_btime &&
1354 ktime_get_real_seconds() >= dquot->dq_dqb.dqb_btime &&
1355 !ignore_hardlimit(dquot)) {
1356 if (flags & DQUOT_SPACE_WARN)
1357 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1358 ret = -EDQUOT;
1359 goto finish;
1360 }
1361
1362 if (dquot->dq_dqb.dqb_bsoftlimit &&
1363 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1364 dquot->dq_dqb.dqb_btime == 0) {
1365 if (flags & DQUOT_SPACE_WARN) {
1366 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1367 dquot->dq_dqb.dqb_btime = ktime_get_real_seconds() +
1368 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1369 } else {
1370 /*
1371 * We don't allow preallocation to exceed softlimit so exceeding will
1372 * be always printed
1373 */
1374 ret = -EDQUOT;
1375 goto finish;
1376 }
1377 }
1378 finish:
1379 /*
1380 * We have to be careful and go through warning generation & grace time
1381 * setting even if DQUOT_SPACE_NOFAIL is set. That's why we check it
1382 * only here...
1383 */
1384 if (flags & DQUOT_SPACE_NOFAIL)
1385 ret = 0;
1386 if (!ret) {
1387 dquot->dq_dqb.dqb_rsvspace += rsv_space;
1388 dquot->dq_dqb.dqb_curspace += space;
1389 }
1390 spin_unlock(&dquot->dq_dqb_lock);
1391 return ret;
1392 }
1393
info_idq_free(struct dquot * dquot,qsize_t inodes)1394 static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1395 {
1396 qsize_t newinodes;
1397
1398 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1399 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1400 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1401 return QUOTA_NL_NOWARN;
1402
1403 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1404 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1405 return QUOTA_NL_ISOFTBELOW;
1406 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1407 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1408 return QUOTA_NL_IHARDBELOW;
1409 return QUOTA_NL_NOWARN;
1410 }
1411
info_bdq_free(struct dquot * dquot,qsize_t space)1412 static int info_bdq_free(struct dquot *dquot, qsize_t space)
1413 {
1414 qsize_t tspace;
1415
1416 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace;
1417
1418 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1419 tspace <= dquot->dq_dqb.dqb_bsoftlimit)
1420 return QUOTA_NL_NOWARN;
1421
1422 if (tspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1423 return QUOTA_NL_BSOFTBELOW;
1424 if (tspace >= dquot->dq_dqb.dqb_bhardlimit &&
1425 tspace - space < dquot->dq_dqb.dqb_bhardlimit)
1426 return QUOTA_NL_BHARDBELOW;
1427 return QUOTA_NL_NOWARN;
1428 }
1429
dquot_active(const struct inode * inode)1430 static int dquot_active(const struct inode *inode)
1431 {
1432 struct super_block *sb = inode->i_sb;
1433
1434 if (IS_NOQUOTA(inode))
1435 return 0;
1436 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1437 }
1438
1439 /*
1440 * Initialize quota pointers in inode
1441 *
1442 * It is better to call this function outside of any transaction as it
1443 * might need a lot of space in journal for dquot structure allocation.
1444 */
__dquot_initialize(struct inode * inode,int type)1445 static int __dquot_initialize(struct inode *inode, int type)
1446 {
1447 int cnt, init_needed = 0;
1448 struct dquot **dquots, *got[MAXQUOTAS] = {};
1449 struct super_block *sb = inode->i_sb;
1450 qsize_t rsv;
1451 int ret = 0;
1452
1453 if (!dquot_active(inode))
1454 return 0;
1455
1456 dquots = i_dquot(inode);
1457
1458 /* First get references to structures we might need. */
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1460 struct kqid qid;
1461 kprojid_t projid;
1462 int rc;
1463 struct dquot *dquot;
1464
1465 if (type != -1 && cnt != type)
1466 continue;
1467 /*
1468 * The i_dquot should have been initialized in most cases,
1469 * we check it without locking here to avoid unnecessary
1470 * dqget()/dqput() calls.
1471 */
1472 if (dquots[cnt])
1473 continue;
1474
1475 if (!sb_has_quota_active(sb, cnt))
1476 continue;
1477
1478 init_needed = 1;
1479
1480 switch (cnt) {
1481 case USRQUOTA:
1482 qid = make_kqid_uid(inode->i_uid);
1483 break;
1484 case GRPQUOTA:
1485 qid = make_kqid_gid(inode->i_gid);
1486 break;
1487 case PRJQUOTA:
1488 rc = inode->i_sb->dq_op->get_projid(inode, &projid);
1489 if (rc)
1490 continue;
1491 qid = make_kqid_projid(projid);
1492 break;
1493 }
1494 dquot = dqget(sb, qid);
1495 if (IS_ERR(dquot)) {
1496 /* We raced with somebody turning quotas off... */
1497 if (PTR_ERR(dquot) != -ESRCH) {
1498 ret = PTR_ERR(dquot);
1499 goto out_put;
1500 }
1501 dquot = NULL;
1502 }
1503 got[cnt] = dquot;
1504 }
1505
1506 /* All required i_dquot has been initialized */
1507 if (!init_needed)
1508 return 0;
1509
1510 spin_lock(&dq_data_lock);
1511 if (IS_NOQUOTA(inode))
1512 goto out_lock;
1513 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1514 if (type != -1 && cnt != type)
1515 continue;
1516 /* Avoid races with quotaoff() */
1517 if (!sb_has_quota_active(sb, cnt))
1518 continue;
1519 /* We could race with quotaon or dqget() could have failed */
1520 if (!got[cnt])
1521 continue;
1522 if (!dquots[cnt]) {
1523 dquots[cnt] = got[cnt];
1524 got[cnt] = NULL;
1525 /*
1526 * Make quota reservation system happy if someone
1527 * did a write before quota was turned on
1528 */
1529 rsv = inode_get_rsv_space(inode);
1530 if (unlikely(rsv)) {
1531 spin_lock(&inode->i_lock);
1532 /* Get reservation again under proper lock */
1533 rsv = __inode_get_rsv_space(inode);
1534 spin_lock(&dquots[cnt]->dq_dqb_lock);
1535 dquots[cnt]->dq_dqb.dqb_rsvspace += rsv;
1536 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1537 spin_unlock(&inode->i_lock);
1538 }
1539 }
1540 }
1541 out_lock:
1542 spin_unlock(&dq_data_lock);
1543 out_put:
1544 /* Drop unused references */
1545 dqput_all(got);
1546
1547 return ret;
1548 }
1549
dquot_initialize(struct inode * inode)1550 int dquot_initialize(struct inode *inode)
1551 {
1552 return __dquot_initialize(inode, -1);
1553 }
1554 EXPORT_SYMBOL(dquot_initialize);
1555
dquot_initialize_needed(struct inode * inode)1556 bool dquot_initialize_needed(struct inode *inode)
1557 {
1558 struct dquot **dquots;
1559 int i;
1560
1561 if (!dquot_active(inode))
1562 return false;
1563
1564 dquots = i_dquot(inode);
1565 for (i = 0; i < MAXQUOTAS; i++)
1566 if (!dquots[i] && sb_has_quota_active(inode->i_sb, i))
1567 return true;
1568 return false;
1569 }
1570 EXPORT_SYMBOL(dquot_initialize_needed);
1571
1572 /*
1573 * Release all quotas referenced by inode.
1574 *
1575 * This function only be called on inode free or converting
1576 * a file to quota file, no other users for the i_dquot in
1577 * both cases, so we needn't call synchronize_srcu() after
1578 * clearing i_dquot.
1579 */
__dquot_drop(struct inode * inode)1580 static void __dquot_drop(struct inode *inode)
1581 {
1582 int cnt;
1583 struct dquot **dquots = i_dquot(inode);
1584 struct dquot *put[MAXQUOTAS];
1585
1586 spin_lock(&dq_data_lock);
1587 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1588 put[cnt] = dquots[cnt];
1589 dquots[cnt] = NULL;
1590 }
1591 spin_unlock(&dq_data_lock);
1592 dqput_all(put);
1593 }
1594
dquot_drop(struct inode * inode)1595 void dquot_drop(struct inode *inode)
1596 {
1597 struct dquot * const *dquots;
1598 int cnt;
1599
1600 if (IS_NOQUOTA(inode))
1601 return;
1602
1603 /*
1604 * Test before calling to rule out calls from proc and such
1605 * where we are not allowed to block. Note that this is
1606 * actually reliable test even without the lock - the caller
1607 * must assure that nobody can come after the DQUOT_DROP and
1608 * add quota pointers back anyway.
1609 */
1610 dquots = i_dquot(inode);
1611 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1612 if (dquots[cnt])
1613 break;
1614 }
1615
1616 if (cnt < MAXQUOTAS)
1617 __dquot_drop(inode);
1618 }
1619 EXPORT_SYMBOL(dquot_drop);
1620
1621 /*
1622 * inode_reserved_space is managed internally by quota, and protected by
1623 * i_lock similar to i_blocks+i_bytes.
1624 */
inode_reserved_space(struct inode * inode)1625 static qsize_t *inode_reserved_space(struct inode * inode)
1626 {
1627 /* Filesystem must explicitly define it's own method in order to use
1628 * quota reservation interface */
1629 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1630 return inode->i_sb->dq_op->get_reserved_space(inode);
1631 }
1632
__inode_get_rsv_space(struct inode * inode)1633 static qsize_t __inode_get_rsv_space(struct inode *inode)
1634 {
1635 if (!inode->i_sb->dq_op->get_reserved_space)
1636 return 0;
1637 return *inode_reserved_space(inode);
1638 }
1639
inode_get_rsv_space(struct inode * inode)1640 static qsize_t inode_get_rsv_space(struct inode *inode)
1641 {
1642 qsize_t ret;
1643
1644 if (!inode->i_sb->dq_op->get_reserved_space)
1645 return 0;
1646 spin_lock(&inode->i_lock);
1647 ret = __inode_get_rsv_space(inode);
1648 spin_unlock(&inode->i_lock);
1649 return ret;
1650 }
1651
1652 /*
1653 * This functions updates i_blocks+i_bytes fields and quota information
1654 * (together with appropriate checks).
1655 *
1656 * NOTE: We absolutely rely on the fact that caller dirties the inode
1657 * (usually helpers in quotaops.h care about this) and holds a handle for
1658 * the current transaction so that dquot write and inode write go into the
1659 * same transaction.
1660 */
1661
1662 /*
1663 * This operation can block, but only after everything is updated
1664 */
__dquot_alloc_space(struct inode * inode,qsize_t number,int flags)1665 int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1666 {
1667 int cnt, ret = 0, index;
1668 struct dquot_warn warn[MAXQUOTAS];
1669 int reserve = flags & DQUOT_SPACE_RESERVE;
1670 struct dquot **dquots;
1671
1672 if (!dquot_active(inode)) {
1673 if (reserve) {
1674 spin_lock(&inode->i_lock);
1675 *inode_reserved_space(inode) += number;
1676 spin_unlock(&inode->i_lock);
1677 } else {
1678 inode_add_bytes(inode, number);
1679 }
1680 goto out;
1681 }
1682
1683 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1684 warn[cnt].w_type = QUOTA_NL_NOWARN;
1685
1686 dquots = i_dquot(inode);
1687 index = srcu_read_lock(&dquot_srcu);
1688 spin_lock(&inode->i_lock);
1689 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1690 if (!dquots[cnt])
1691 continue;
1692 if (reserve) {
1693 ret = dquot_add_space(dquots[cnt], 0, number, flags,
1694 &warn[cnt]);
1695 } else {
1696 ret = dquot_add_space(dquots[cnt], number, 0, flags,
1697 &warn[cnt]);
1698 }
1699 if (ret) {
1700 /* Back out changes we already did */
1701 for (cnt--; cnt >= 0; cnt--) {
1702 if (!dquots[cnt])
1703 continue;
1704 spin_lock(&dquots[cnt]->dq_dqb_lock);
1705 if (reserve)
1706 dquot_free_reserved_space(dquots[cnt],
1707 number);
1708 else
1709 dquot_decr_space(dquots[cnt], number);
1710 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1711 }
1712 spin_unlock(&inode->i_lock);
1713 goto out_flush_warn;
1714 }
1715 }
1716 if (reserve)
1717 *inode_reserved_space(inode) += number;
1718 else
1719 __inode_add_bytes(inode, number);
1720 spin_unlock(&inode->i_lock);
1721
1722 if (reserve)
1723 goto out_flush_warn;
1724 mark_all_dquot_dirty(dquots);
1725 out_flush_warn:
1726 srcu_read_unlock(&dquot_srcu, index);
1727 flush_warnings(warn);
1728 out:
1729 return ret;
1730 }
1731 EXPORT_SYMBOL(__dquot_alloc_space);
1732
1733 /*
1734 * This operation can block, but only after everything is updated
1735 */
dquot_alloc_inode(struct inode * inode)1736 int dquot_alloc_inode(struct inode *inode)
1737 {
1738 int cnt, ret = 0, index;
1739 struct dquot_warn warn[MAXQUOTAS];
1740 struct dquot * const *dquots;
1741
1742 if (!dquot_active(inode))
1743 return 0;
1744 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1745 warn[cnt].w_type = QUOTA_NL_NOWARN;
1746
1747 dquots = i_dquot(inode);
1748 index = srcu_read_lock(&dquot_srcu);
1749 spin_lock(&inode->i_lock);
1750 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1751 if (!dquots[cnt])
1752 continue;
1753 ret = dquot_add_inodes(dquots[cnt], 1, &warn[cnt]);
1754 if (ret) {
1755 for (cnt--; cnt >= 0; cnt--) {
1756 if (!dquots[cnt])
1757 continue;
1758 /* Back out changes we already did */
1759 spin_lock(&dquots[cnt]->dq_dqb_lock);
1760 dquot_decr_inodes(dquots[cnt], 1);
1761 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1762 }
1763 goto warn_put_all;
1764 }
1765 }
1766
1767 warn_put_all:
1768 spin_unlock(&inode->i_lock);
1769 if (ret == 0)
1770 mark_all_dquot_dirty(dquots);
1771 srcu_read_unlock(&dquot_srcu, index);
1772 flush_warnings(warn);
1773 return ret;
1774 }
1775 EXPORT_SYMBOL(dquot_alloc_inode);
1776
1777 /*
1778 * Convert in-memory reserved quotas to real consumed quotas
1779 */
dquot_claim_space_nodirty(struct inode * inode,qsize_t number)1780 int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1781 {
1782 struct dquot **dquots;
1783 int cnt, index;
1784
1785 if (!dquot_active(inode)) {
1786 spin_lock(&inode->i_lock);
1787 *inode_reserved_space(inode) -= number;
1788 __inode_add_bytes(inode, number);
1789 spin_unlock(&inode->i_lock);
1790 return 0;
1791 }
1792
1793 dquots = i_dquot(inode);
1794 index = srcu_read_lock(&dquot_srcu);
1795 spin_lock(&inode->i_lock);
1796 /* Claim reserved quotas to allocated quotas */
1797 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1798 if (dquots[cnt]) {
1799 struct dquot *dquot = dquots[cnt];
1800
1801 spin_lock(&dquot->dq_dqb_lock);
1802 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_rsvspace < number))
1803 number = dquot->dq_dqb.dqb_rsvspace;
1804 dquot->dq_dqb.dqb_curspace += number;
1805 dquot->dq_dqb.dqb_rsvspace -= number;
1806 spin_unlock(&dquot->dq_dqb_lock);
1807 }
1808 }
1809 /* Update inode bytes */
1810 *inode_reserved_space(inode) -= number;
1811 __inode_add_bytes(inode, number);
1812 spin_unlock(&inode->i_lock);
1813 mark_all_dquot_dirty(dquots);
1814 srcu_read_unlock(&dquot_srcu, index);
1815 return 0;
1816 }
1817 EXPORT_SYMBOL(dquot_claim_space_nodirty);
1818
1819 /*
1820 * Convert allocated space back to in-memory reserved quotas
1821 */
dquot_reclaim_space_nodirty(struct inode * inode,qsize_t number)1822 void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1823 {
1824 struct dquot **dquots;
1825 int cnt, index;
1826
1827 if (!dquot_active(inode)) {
1828 spin_lock(&inode->i_lock);
1829 *inode_reserved_space(inode) += number;
1830 __inode_sub_bytes(inode, number);
1831 spin_unlock(&inode->i_lock);
1832 return;
1833 }
1834
1835 dquots = i_dquot(inode);
1836 index = srcu_read_lock(&dquot_srcu);
1837 spin_lock(&inode->i_lock);
1838 /* Claim reserved quotas to allocated quotas */
1839 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1840 if (dquots[cnt]) {
1841 struct dquot *dquot = dquots[cnt];
1842
1843 spin_lock(&dquot->dq_dqb_lock);
1844 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1845 number = dquot->dq_dqb.dqb_curspace;
1846 dquot->dq_dqb.dqb_rsvspace += number;
1847 dquot->dq_dqb.dqb_curspace -= number;
1848 spin_unlock(&dquot->dq_dqb_lock);
1849 }
1850 }
1851 /* Update inode bytes */
1852 *inode_reserved_space(inode) += number;
1853 __inode_sub_bytes(inode, number);
1854 spin_unlock(&inode->i_lock);
1855 mark_all_dquot_dirty(dquots);
1856 srcu_read_unlock(&dquot_srcu, index);
1857 return;
1858 }
1859 EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1860
1861 /*
1862 * This operation can block, but only after everything is updated
1863 */
__dquot_free_space(struct inode * inode,qsize_t number,int flags)1864 void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1865 {
1866 unsigned int cnt;
1867 struct dquot_warn warn[MAXQUOTAS];
1868 struct dquot **dquots;
1869 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1870
1871 if (!dquot_active(inode)) {
1872 if (reserve) {
1873 spin_lock(&inode->i_lock);
1874 *inode_reserved_space(inode) -= number;
1875 spin_unlock(&inode->i_lock);
1876 } else {
1877 inode_sub_bytes(inode, number);
1878 }
1879 return;
1880 }
1881
1882 dquots = i_dquot(inode);
1883 index = srcu_read_lock(&dquot_srcu);
1884 spin_lock(&inode->i_lock);
1885 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1886 int wtype;
1887
1888 warn[cnt].w_type = QUOTA_NL_NOWARN;
1889 if (!dquots[cnt])
1890 continue;
1891 spin_lock(&dquots[cnt]->dq_dqb_lock);
1892 wtype = info_bdq_free(dquots[cnt], number);
1893 if (wtype != QUOTA_NL_NOWARN)
1894 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1895 if (reserve)
1896 dquot_free_reserved_space(dquots[cnt], number);
1897 else
1898 dquot_decr_space(dquots[cnt], number);
1899 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1900 }
1901 if (reserve)
1902 *inode_reserved_space(inode) -= number;
1903 else
1904 __inode_sub_bytes(inode, number);
1905 spin_unlock(&inode->i_lock);
1906
1907 if (reserve)
1908 goto out_unlock;
1909 mark_all_dquot_dirty(dquots);
1910 out_unlock:
1911 srcu_read_unlock(&dquot_srcu, index);
1912 flush_warnings(warn);
1913 }
1914 EXPORT_SYMBOL(__dquot_free_space);
1915
1916 /*
1917 * This operation can block, but only after everything is updated
1918 */
dquot_free_inode(struct inode * inode)1919 void dquot_free_inode(struct inode *inode)
1920 {
1921 unsigned int cnt;
1922 struct dquot_warn warn[MAXQUOTAS];
1923 struct dquot * const *dquots;
1924 int index;
1925
1926 if (!dquot_active(inode))
1927 return;
1928
1929 dquots = i_dquot(inode);
1930 index = srcu_read_lock(&dquot_srcu);
1931 spin_lock(&inode->i_lock);
1932 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1933 int wtype;
1934
1935 warn[cnt].w_type = QUOTA_NL_NOWARN;
1936 if (!dquots[cnt])
1937 continue;
1938 spin_lock(&dquots[cnt]->dq_dqb_lock);
1939 wtype = info_idq_free(dquots[cnt], 1);
1940 if (wtype != QUOTA_NL_NOWARN)
1941 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1942 dquot_decr_inodes(dquots[cnt], 1);
1943 spin_unlock(&dquots[cnt]->dq_dqb_lock);
1944 }
1945 spin_unlock(&inode->i_lock);
1946 mark_all_dquot_dirty(dquots);
1947 srcu_read_unlock(&dquot_srcu, index);
1948 flush_warnings(warn);
1949 }
1950 EXPORT_SYMBOL(dquot_free_inode);
1951
1952 /*
1953 * Transfer the number of inode and blocks from one diskquota to an other.
1954 * On success, dquot references in transfer_to are consumed and references
1955 * to original dquots that need to be released are placed there. On failure,
1956 * references are kept untouched.
1957 *
1958 * This operation can block, but only after everything is updated
1959 * A transaction must be started when entering this function.
1960 *
1961 * We are holding reference on transfer_from & transfer_to, no need to
1962 * protect them by srcu_read_lock().
1963 */
__dquot_transfer(struct inode * inode,struct dquot ** transfer_to)1964 int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1965 {
1966 qsize_t cur_space;
1967 qsize_t rsv_space = 0;
1968 qsize_t inode_usage = 1;
1969 struct dquot *transfer_from[MAXQUOTAS] = {};
1970 int cnt, ret = 0;
1971 char is_valid[MAXQUOTAS] = {};
1972 struct dquot_warn warn_to[MAXQUOTAS];
1973 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1974 struct dquot_warn warn_from_space[MAXQUOTAS];
1975
1976 if (IS_NOQUOTA(inode))
1977 return 0;
1978
1979 if (inode->i_sb->dq_op->get_inode_usage) {
1980 ret = inode->i_sb->dq_op->get_inode_usage(inode, &inode_usage);
1981 if (ret)
1982 return ret;
1983 }
1984
1985 /* Initialize the arrays */
1986 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1987 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1988 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1989 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1990 }
1991
1992 spin_lock(&dq_data_lock);
1993 spin_lock(&inode->i_lock);
1994 if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
1995 spin_unlock(&inode->i_lock);
1996 spin_unlock(&dq_data_lock);
1997 return 0;
1998 }
1999 cur_space = __inode_get_bytes(inode);
2000 rsv_space = __inode_get_rsv_space(inode);
2001 /*
2002 * Build the transfer_from list, check limits, and update usage in
2003 * the target structures.
2004 */
2005 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2006 /*
2007 * Skip changes for same uid or gid or for turned off quota-type.
2008 */
2009 if (!transfer_to[cnt])
2010 continue;
2011 /* Avoid races with quotaoff() */
2012 if (!sb_has_quota_active(inode->i_sb, cnt))
2013 continue;
2014 is_valid[cnt] = 1;
2015 transfer_from[cnt] = i_dquot(inode)[cnt];
2016 ret = dquot_add_inodes(transfer_to[cnt], inode_usage,
2017 &warn_to[cnt]);
2018 if (ret)
2019 goto over_quota;
2020 ret = dquot_add_space(transfer_to[cnt], cur_space, rsv_space,
2021 DQUOT_SPACE_WARN, &warn_to[cnt]);
2022 if (ret) {
2023 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2024 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2025 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2026 goto over_quota;
2027 }
2028 }
2029
2030 /* Decrease usage for source structures and update quota pointers */
2031 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2032 if (!is_valid[cnt])
2033 continue;
2034 /* Due to IO error we might not have transfer_from[] structure */
2035 if (transfer_from[cnt]) {
2036 int wtype;
2037
2038 spin_lock(&transfer_from[cnt]->dq_dqb_lock);
2039 wtype = info_idq_free(transfer_from[cnt], inode_usage);
2040 if (wtype != QUOTA_NL_NOWARN)
2041 prepare_warning(&warn_from_inodes[cnt],
2042 transfer_from[cnt], wtype);
2043 wtype = info_bdq_free(transfer_from[cnt],
2044 cur_space + rsv_space);
2045 if (wtype != QUOTA_NL_NOWARN)
2046 prepare_warning(&warn_from_space[cnt],
2047 transfer_from[cnt], wtype);
2048 dquot_decr_inodes(transfer_from[cnt], inode_usage);
2049 dquot_decr_space(transfer_from[cnt], cur_space);
2050 dquot_free_reserved_space(transfer_from[cnt],
2051 rsv_space);
2052 spin_unlock(&transfer_from[cnt]->dq_dqb_lock);
2053 }
2054 i_dquot(inode)[cnt] = transfer_to[cnt];
2055 }
2056 spin_unlock(&inode->i_lock);
2057 spin_unlock(&dq_data_lock);
2058
2059 mark_all_dquot_dirty(transfer_from);
2060 mark_all_dquot_dirty(transfer_to);
2061 flush_warnings(warn_to);
2062 flush_warnings(warn_from_inodes);
2063 flush_warnings(warn_from_space);
2064 /* Pass back references to put */
2065 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2066 if (is_valid[cnt])
2067 transfer_to[cnt] = transfer_from[cnt];
2068 return 0;
2069 over_quota:
2070 /* Back out changes we already did */
2071 for (cnt--; cnt >= 0; cnt--) {
2072 if (!is_valid[cnt])
2073 continue;
2074 spin_lock(&transfer_to[cnt]->dq_dqb_lock);
2075 dquot_decr_inodes(transfer_to[cnt], inode_usage);
2076 dquot_decr_space(transfer_to[cnt], cur_space);
2077 dquot_free_reserved_space(transfer_to[cnt], rsv_space);
2078 spin_unlock(&transfer_to[cnt]->dq_dqb_lock);
2079 }
2080 spin_unlock(&inode->i_lock);
2081 spin_unlock(&dq_data_lock);
2082 flush_warnings(warn_to);
2083 return ret;
2084 }
2085 EXPORT_SYMBOL(__dquot_transfer);
2086
2087 /* Wrapper for transferring ownership of an inode for uid/gid only
2088 * Called from FSXXX_setattr()
2089 */
dquot_transfer(struct inode * inode,struct iattr * iattr)2090 int dquot_transfer(struct inode *inode, struct iattr *iattr)
2091 {
2092 struct dquot *transfer_to[MAXQUOTAS] = {};
2093 struct dquot *dquot;
2094 struct super_block *sb = inode->i_sb;
2095 int ret;
2096
2097 if (!dquot_active(inode))
2098 return 0;
2099
2100 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
2101 dquot = dqget(sb, make_kqid_uid(iattr->ia_uid));
2102 if (IS_ERR(dquot)) {
2103 if (PTR_ERR(dquot) != -ESRCH) {
2104 ret = PTR_ERR(dquot);
2105 goto out_put;
2106 }
2107 dquot = NULL;
2108 }
2109 transfer_to[USRQUOTA] = dquot;
2110 }
2111 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid)){
2112 dquot = dqget(sb, make_kqid_gid(iattr->ia_gid));
2113 if (IS_ERR(dquot)) {
2114 if (PTR_ERR(dquot) != -ESRCH) {
2115 ret = PTR_ERR(dquot);
2116 goto out_put;
2117 }
2118 dquot = NULL;
2119 }
2120 transfer_to[GRPQUOTA] = dquot;
2121 }
2122 ret = __dquot_transfer(inode, transfer_to);
2123 out_put:
2124 dqput_all(transfer_to);
2125 return ret;
2126 }
2127 EXPORT_SYMBOL(dquot_transfer);
2128
2129 /*
2130 * Write info of quota file to disk
2131 */
dquot_commit_info(struct super_block * sb,int type)2132 int dquot_commit_info(struct super_block *sb, int type)
2133 {
2134 struct quota_info *dqopt = sb_dqopt(sb);
2135
2136 return dqopt->ops[type]->write_file_info(sb, type);
2137 }
2138 EXPORT_SYMBOL(dquot_commit_info);
2139
dquot_get_next_id(struct super_block * sb,struct kqid * qid)2140 int dquot_get_next_id(struct super_block *sb, struct kqid *qid)
2141 {
2142 struct quota_info *dqopt = sb_dqopt(sb);
2143
2144 if (!sb_has_quota_active(sb, qid->type))
2145 return -ESRCH;
2146 if (!dqopt->ops[qid->type]->get_next_id)
2147 return -ENOSYS;
2148 return dqopt->ops[qid->type]->get_next_id(sb, qid);
2149 }
2150 EXPORT_SYMBOL(dquot_get_next_id);
2151
2152 /*
2153 * Definitions of diskquota operations.
2154 */
2155 const struct dquot_operations dquot_operations = {
2156 .write_dquot = dquot_commit,
2157 .acquire_dquot = dquot_acquire,
2158 .release_dquot = dquot_release,
2159 .mark_dirty = dquot_mark_dquot_dirty,
2160 .write_info = dquot_commit_info,
2161 .alloc_dquot = dquot_alloc,
2162 .destroy_dquot = dquot_destroy,
2163 .get_next_id = dquot_get_next_id,
2164 };
2165 EXPORT_SYMBOL(dquot_operations);
2166
2167 /*
2168 * Generic helper for ->open on filesystems supporting disk quotas.
2169 */
dquot_file_open(struct inode * inode,struct file * file)2170 int dquot_file_open(struct inode *inode, struct file *file)
2171 {
2172 int error;
2173
2174 error = generic_file_open(inode, file);
2175 if (!error && (file->f_mode & FMODE_WRITE))
2176 error = dquot_initialize(inode);
2177 return error;
2178 }
2179 EXPORT_SYMBOL(dquot_file_open);
2180
vfs_cleanup_quota_inode(struct super_block * sb,int type)2181 static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2182 {
2183 struct quota_info *dqopt = sb_dqopt(sb);
2184 struct inode *inode = dqopt->files[type];
2185
2186 if (!inode)
2187 return;
2188 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2189 inode_lock(inode);
2190 inode->i_flags &= ~S_NOQUOTA;
2191 inode_unlock(inode);
2192 }
2193 dqopt->files[type] = NULL;
2194 iput(inode);
2195 }
2196
2197 /*
2198 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
2199 */
dquot_disable(struct super_block * sb,int type,unsigned int flags)2200 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2201 {
2202 int cnt;
2203 struct quota_info *dqopt = sb_dqopt(sb);
2204
2205 /* s_umount should be held in exclusive mode */
2206 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2207 up_read(&sb->s_umount);
2208
2209 /* Cannot turn off usage accounting without turning off limits, or
2210 * suspend quotas and simultaneously turn quotas off. */
2211 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2212 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2213 DQUOT_USAGE_ENABLED)))
2214 return -EINVAL;
2215
2216 /*
2217 * Skip everything if there's nothing to do. We have to do this because
2218 * sometimes we are called when fill_super() failed and calling
2219 * sync_fs() in such cases does no good.
2220 */
2221 if (!sb_any_quota_loaded(sb))
2222 return 0;
2223
2224 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2225 if (type != -1 && cnt != type)
2226 continue;
2227 if (!sb_has_quota_loaded(sb, cnt))
2228 continue;
2229
2230 if (flags & DQUOT_SUSPENDED) {
2231 spin_lock(&dq_state_lock);
2232 dqopt->flags |=
2233 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2234 spin_unlock(&dq_state_lock);
2235 } else {
2236 spin_lock(&dq_state_lock);
2237 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2238 /* Turning off suspended quotas? */
2239 if (!sb_has_quota_loaded(sb, cnt) &&
2240 sb_has_quota_suspended(sb, cnt)) {
2241 dqopt->flags &= ~dquot_state_flag(
2242 DQUOT_SUSPENDED, cnt);
2243 spin_unlock(&dq_state_lock);
2244 vfs_cleanup_quota_inode(sb, cnt);
2245 continue;
2246 }
2247 spin_unlock(&dq_state_lock);
2248 }
2249
2250 /* We still have to keep quota loaded? */
2251 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2252 continue;
2253
2254 /* Note: these are blocking operations */
2255 drop_dquot_ref(sb, cnt);
2256 invalidate_dquots(sb, cnt);
2257 /*
2258 * Now all dquots should be invalidated, all writes done so we
2259 * should be only users of the info. No locks needed.
2260 */
2261 if (info_dirty(&dqopt->info[cnt]))
2262 sb->dq_op->write_info(sb, cnt);
2263 if (dqopt->ops[cnt]->free_file_info)
2264 dqopt->ops[cnt]->free_file_info(sb, cnt);
2265 put_quota_format(dqopt->info[cnt].dqi_format);
2266 dqopt->info[cnt].dqi_flags = 0;
2267 dqopt->info[cnt].dqi_igrace = 0;
2268 dqopt->info[cnt].dqi_bgrace = 0;
2269 dqopt->ops[cnt] = NULL;
2270 }
2271
2272 /* Skip syncing and setting flags if quota files are hidden */
2273 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2274 goto put_inodes;
2275
2276 /* Sync the superblock so that buffers with quota data are written to
2277 * disk (and so userspace sees correct data afterwards). */
2278 if (sb->s_op->sync_fs)
2279 sb->s_op->sync_fs(sb, 1);
2280 sync_blockdev(sb->s_bdev);
2281 /* Now the quota files are just ordinary files and we can set the
2282 * inode flags back. Moreover we discard the pagecache so that
2283 * userspace sees the writes we did bypassing the pagecache. We
2284 * must also discard the blockdev buffers so that we see the
2285 * changes done by userspace on the next quotaon() */
2286 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2287 if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2288 inode_lock(dqopt->files[cnt]);
2289 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2290 inode_unlock(dqopt->files[cnt]);
2291 }
2292 if (sb->s_bdev)
2293 invalidate_bdev(sb->s_bdev);
2294 put_inodes:
2295 /* We are done when suspending quotas */
2296 if (flags & DQUOT_SUSPENDED)
2297 return 0;
2298
2299 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2300 if (!sb_has_quota_loaded(sb, cnt))
2301 vfs_cleanup_quota_inode(sb, cnt);
2302 return 0;
2303 }
2304 EXPORT_SYMBOL(dquot_disable);
2305
dquot_quota_off(struct super_block * sb,int type)2306 int dquot_quota_off(struct super_block *sb, int type)
2307 {
2308 return dquot_disable(sb, type,
2309 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2310 }
2311 EXPORT_SYMBOL(dquot_quota_off);
2312
2313 /*
2314 * Turn quotas on on a device
2315 */
2316
vfs_setup_quota_inode(struct inode * inode,int type)2317 static int vfs_setup_quota_inode(struct inode *inode, int type)
2318 {
2319 struct super_block *sb = inode->i_sb;
2320 struct quota_info *dqopt = sb_dqopt(sb);
2321
2322 if (is_bad_inode(inode))
2323 return -EUCLEAN;
2324 if (!S_ISREG(inode->i_mode))
2325 return -EACCES;
2326 if (IS_RDONLY(inode))
2327 return -EROFS;
2328 if (sb_has_quota_loaded(sb, type))
2329 return -EBUSY;
2330
2331 dqopt->files[type] = igrab(inode);
2332 if (!dqopt->files[type])
2333 return -EIO;
2334 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2335 /* We don't want quota and atime on quota files (deadlocks
2336 * possible) Also nobody should write to the file - we use
2337 * special IO operations which ignore the immutable bit. */
2338 inode_lock(inode);
2339 inode->i_flags |= S_NOQUOTA;
2340 inode_unlock(inode);
2341 /*
2342 * When S_NOQUOTA is set, remove dquot references as no more
2343 * references can be added
2344 */
2345 __dquot_drop(inode);
2346 }
2347 return 0;
2348 }
2349
dquot_load_quota_sb(struct super_block * sb,int type,int format_id,unsigned int flags)2350 int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
2351 unsigned int flags)
2352 {
2353 struct quota_format_type *fmt = find_quota_format(format_id);
2354 struct quota_info *dqopt = sb_dqopt(sb);
2355 int error;
2356
2357 /* Just unsuspend quotas? */
2358 BUG_ON(flags & DQUOT_SUSPENDED);
2359 /* s_umount should be held in exclusive mode */
2360 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2361 up_read(&sb->s_umount);
2362
2363 if (!fmt)
2364 return -ESRCH;
2365 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
2366 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
2367 error = -EINVAL;
2368 goto out_fmt;
2369 }
2370 /* Filesystems outside of init_user_ns not yet supported */
2371 if (sb->s_user_ns != &init_user_ns) {
2372 error = -EINVAL;
2373 goto out_fmt;
2374 }
2375 /* Usage always has to be set... */
2376 if (!(flags & DQUOT_USAGE_ENABLED)) {
2377 error = -EINVAL;
2378 goto out_fmt;
2379 }
2380 if (sb_has_quota_loaded(sb, type)) {
2381 error = -EBUSY;
2382 goto out_fmt;
2383 }
2384
2385 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2386 /* As we bypass the pagecache we must now flush all the
2387 * dirty data and invalidate caches so that kernel sees
2388 * changes from userspace. It is not enough to just flush
2389 * the quota file since if blocksize < pagesize, invalidation
2390 * of the cache could fail because of other unrelated dirty
2391 * data */
2392 sync_filesystem(sb);
2393 invalidate_bdev(sb->s_bdev);
2394 }
2395
2396 error = -EINVAL;
2397 if (!fmt->qf_ops->check_quota_file(sb, type))
2398 goto out_fmt;
2399
2400 dqopt->ops[type] = fmt->qf_ops;
2401 dqopt->info[type].dqi_format = fmt;
2402 dqopt->info[type].dqi_fmt_id = format_id;
2403 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2404 error = dqopt->ops[type]->read_file_info(sb, type);
2405 if (error < 0)
2406 goto out_fmt;
2407 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
2408 spin_lock(&dq_data_lock);
2409 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2410 spin_unlock(&dq_data_lock);
2411 }
2412 spin_lock(&dq_state_lock);
2413 dqopt->flags |= dquot_state_flag(flags, type);
2414 spin_unlock(&dq_state_lock);
2415
2416 error = add_dquot_ref(sb, type);
2417 if (error)
2418 dquot_disable(sb, type, flags);
2419
2420 return error;
2421 out_fmt:
2422 put_quota_format(fmt);
2423
2424 return error;
2425 }
2426 EXPORT_SYMBOL(dquot_load_quota_sb);
2427
2428 /*
2429 * More powerful function for turning on quotas on given quota inode allowing
2430 * setting of individual quota flags
2431 */
dquot_load_quota_inode(struct inode * inode,int type,int format_id,unsigned int flags)2432 int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2433 unsigned int flags)
2434 {
2435 int err;
2436
2437 err = vfs_setup_quota_inode(inode, type);
2438 if (err < 0)
2439 return err;
2440 err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2441 if (err < 0)
2442 vfs_cleanup_quota_inode(inode->i_sb, type);
2443 return err;
2444 }
2445 EXPORT_SYMBOL(dquot_load_quota_inode);
2446
2447 /* Reenable quotas on remount RW */
dquot_resume(struct super_block * sb,int type)2448 int dquot_resume(struct super_block *sb, int type)
2449 {
2450 struct quota_info *dqopt = sb_dqopt(sb);
2451 int ret = 0, cnt;
2452 unsigned int flags;
2453
2454 /* s_umount should be held in exclusive mode */
2455 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2456 up_read(&sb->s_umount);
2457
2458 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2459 if (type != -1 && cnt != type)
2460 continue;
2461 if (!sb_has_quota_suspended(sb, cnt))
2462 continue;
2463
2464 spin_lock(&dq_state_lock);
2465 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2466 DQUOT_LIMITS_ENABLED,
2467 cnt);
2468 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2469 spin_unlock(&dq_state_lock);
2470
2471 flags = dquot_generic_flag(flags, cnt);
2472 ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2473 flags);
2474 if (ret < 0)
2475 vfs_cleanup_quota_inode(sb, cnt);
2476 }
2477
2478 return ret;
2479 }
2480 EXPORT_SYMBOL(dquot_resume);
2481
dquot_quota_on(struct super_block * sb,int type,int format_id,const struct path * path)2482 int dquot_quota_on(struct super_block *sb, int type, int format_id,
2483 const struct path *path)
2484 {
2485 int error = security_quota_on(path->dentry);
2486 if (error)
2487 return error;
2488 /* Quota file not on the same filesystem? */
2489 if (path->dentry->d_sb != sb)
2490 error = -EXDEV;
2491 else
2492 error = dquot_load_quota_inode(d_inode(path->dentry), type,
2493 format_id, DQUOT_USAGE_ENABLED |
2494 DQUOT_LIMITS_ENABLED);
2495 return error;
2496 }
2497 EXPORT_SYMBOL(dquot_quota_on);
2498
2499 /*
2500 * This function is used when filesystem needs to initialize quotas
2501 * during mount time.
2502 */
dquot_quota_on_mount(struct super_block * sb,char * qf_name,int format_id,int type)2503 int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2504 int format_id, int type)
2505 {
2506 struct dentry *dentry;
2507 int error;
2508
2509 dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
2510 if (IS_ERR(dentry))
2511 return PTR_ERR(dentry);
2512
2513 error = security_quota_on(dentry);
2514 if (!error)
2515 error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
2516 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2517
2518 dput(dentry);
2519 return error;
2520 }
2521 EXPORT_SYMBOL(dquot_quota_on_mount);
2522
dquot_quota_enable(struct super_block * sb,unsigned int flags)2523 static int dquot_quota_enable(struct super_block *sb, unsigned int flags)
2524 {
2525 int ret;
2526 int type;
2527 struct quota_info *dqopt = sb_dqopt(sb);
2528
2529 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2530 return -ENOSYS;
2531 /* Accounting cannot be turned on while fs is mounted */
2532 flags &= ~(FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT);
2533 if (!flags)
2534 return -EINVAL;
2535 for (type = 0; type < MAXQUOTAS; type++) {
2536 if (!(flags & qtype_enforce_flag(type)))
2537 continue;
2538 /* Can't enforce without accounting */
2539 if (!sb_has_quota_usage_enabled(sb, type)) {
2540 ret = -EINVAL;
2541 goto out_err;
2542 }
2543 if (sb_has_quota_limits_enabled(sb, type)) {
2544 ret = -EBUSY;
2545 goto out_err;
2546 }
2547 spin_lock(&dq_state_lock);
2548 dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2549 spin_unlock(&dq_state_lock);
2550 }
2551 return 0;
2552 out_err:
2553 /* Backout enforcement enablement we already did */
2554 for (type--; type >= 0; type--) {
2555 if (flags & qtype_enforce_flag(type))
2556 dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2557 }
2558 /* Error code translation for better compatibility with XFS */
2559 if (ret == -EBUSY)
2560 ret = -EEXIST;
2561 return ret;
2562 }
2563
dquot_quota_disable(struct super_block * sb,unsigned int flags)2564 static int dquot_quota_disable(struct super_block *sb, unsigned int flags)
2565 {
2566 int ret;
2567 int type;
2568 struct quota_info *dqopt = sb_dqopt(sb);
2569
2570 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE))
2571 return -ENOSYS;
2572 /*
2573 * We don't support turning off accounting via quotactl. In principle
2574 * quota infrastructure can do this but filesystems don't expect
2575 * userspace to be able to do it.
2576 */
2577 if (flags &
2578 (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT | FS_QUOTA_PDQ_ACCT))
2579 return -EOPNOTSUPP;
2580
2581 /* Filter out limits not enabled */
2582 for (type = 0; type < MAXQUOTAS; type++)
2583 if (!sb_has_quota_limits_enabled(sb, type))
2584 flags &= ~qtype_enforce_flag(type);
2585 /* Nothing left? */
2586 if (!flags)
2587 return -EEXIST;
2588 for (type = 0; type < MAXQUOTAS; type++) {
2589 if (flags & qtype_enforce_flag(type)) {
2590 ret = dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
2591 if (ret < 0)
2592 goto out_err;
2593 }
2594 }
2595 return 0;
2596 out_err:
2597 /* Backout enforcement disabling we already did */
2598 for (type--; type >= 0; type--) {
2599 if (flags & qtype_enforce_flag(type)) {
2600 spin_lock(&dq_state_lock);
2601 dqopt->flags |=
2602 dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2603 spin_unlock(&dq_state_lock);
2604 }
2605 }
2606 return ret;
2607 }
2608
2609 /* Generic routine for getting common part of quota structure */
do_get_dqblk(struct dquot * dquot,struct qc_dqblk * di)2610 static void do_get_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2611 {
2612 struct mem_dqblk *dm = &dquot->dq_dqb;
2613
2614 memset(di, 0, sizeof(*di));
2615 spin_lock(&dquot->dq_dqb_lock);
2616 di->d_spc_hardlimit = dm->dqb_bhardlimit;
2617 di->d_spc_softlimit = dm->dqb_bsoftlimit;
2618 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2619 di->d_ino_softlimit = dm->dqb_isoftlimit;
2620 di->d_space = dm->dqb_curspace + dm->dqb_rsvspace;
2621 di->d_ino_count = dm->dqb_curinodes;
2622 di->d_spc_timer = dm->dqb_btime;
2623 di->d_ino_timer = dm->dqb_itime;
2624 spin_unlock(&dquot->dq_dqb_lock);
2625 }
2626
dquot_get_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * di)2627 int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2628 struct qc_dqblk *di)
2629 {
2630 struct dquot *dquot;
2631
2632 dquot = dqget(sb, qid);
2633 if (IS_ERR(dquot))
2634 return PTR_ERR(dquot);
2635 do_get_dqblk(dquot, di);
2636 dqput(dquot);
2637
2638 return 0;
2639 }
2640 EXPORT_SYMBOL(dquot_get_dqblk);
2641
dquot_get_next_dqblk(struct super_block * sb,struct kqid * qid,struct qc_dqblk * di)2642 int dquot_get_next_dqblk(struct super_block *sb, struct kqid *qid,
2643 struct qc_dqblk *di)
2644 {
2645 struct dquot *dquot;
2646 int err;
2647
2648 if (!sb->dq_op->get_next_id)
2649 return -ENOSYS;
2650 err = sb->dq_op->get_next_id(sb, qid);
2651 if (err < 0)
2652 return err;
2653 dquot = dqget(sb, *qid);
2654 if (IS_ERR(dquot))
2655 return PTR_ERR(dquot);
2656 do_get_dqblk(dquot, di);
2657 dqput(dquot);
2658
2659 return 0;
2660 }
2661 EXPORT_SYMBOL(dquot_get_next_dqblk);
2662
2663 #define VFS_QC_MASK \
2664 (QC_SPACE | QC_SPC_SOFT | QC_SPC_HARD | \
2665 QC_INO_COUNT | QC_INO_SOFT | QC_INO_HARD | \
2666 QC_SPC_TIMER | QC_INO_TIMER)
2667
2668 /* Generic routine for setting common part of quota structure */
do_set_dqblk(struct dquot * dquot,struct qc_dqblk * di)2669 static int do_set_dqblk(struct dquot *dquot, struct qc_dqblk *di)
2670 {
2671 struct mem_dqblk *dm = &dquot->dq_dqb;
2672 int check_blim = 0, check_ilim = 0;
2673 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2674
2675 if (di->d_fieldmask & ~VFS_QC_MASK)
2676 return -EINVAL;
2677
2678 if (((di->d_fieldmask & QC_SPC_SOFT) &&
2679 di->d_spc_softlimit > dqi->dqi_max_spc_limit) ||
2680 ((di->d_fieldmask & QC_SPC_HARD) &&
2681 di->d_spc_hardlimit > dqi->dqi_max_spc_limit) ||
2682 ((di->d_fieldmask & QC_INO_SOFT) &&
2683 (di->d_ino_softlimit > dqi->dqi_max_ino_limit)) ||
2684 ((di->d_fieldmask & QC_INO_HARD) &&
2685 (di->d_ino_hardlimit > dqi->dqi_max_ino_limit)))
2686 return -ERANGE;
2687
2688 spin_lock(&dquot->dq_dqb_lock);
2689 if (di->d_fieldmask & QC_SPACE) {
2690 dm->dqb_curspace = di->d_space - dm->dqb_rsvspace;
2691 check_blim = 1;
2692 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2693 }
2694
2695 if (di->d_fieldmask & QC_SPC_SOFT)
2696 dm->dqb_bsoftlimit = di->d_spc_softlimit;
2697 if (di->d_fieldmask & QC_SPC_HARD)
2698 dm->dqb_bhardlimit = di->d_spc_hardlimit;
2699 if (di->d_fieldmask & (QC_SPC_SOFT | QC_SPC_HARD)) {
2700 check_blim = 1;
2701 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2702 }
2703
2704 if (di->d_fieldmask & QC_INO_COUNT) {
2705 dm->dqb_curinodes = di->d_ino_count;
2706 check_ilim = 1;
2707 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2708 }
2709
2710 if (di->d_fieldmask & QC_INO_SOFT)
2711 dm->dqb_isoftlimit = di->d_ino_softlimit;
2712 if (di->d_fieldmask & QC_INO_HARD)
2713 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2714 if (di->d_fieldmask & (QC_INO_SOFT | QC_INO_HARD)) {
2715 check_ilim = 1;
2716 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2717 }
2718
2719 if (di->d_fieldmask & QC_SPC_TIMER) {
2720 dm->dqb_btime = di->d_spc_timer;
2721 check_blim = 1;
2722 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2723 }
2724
2725 if (di->d_fieldmask & QC_INO_TIMER) {
2726 dm->dqb_itime = di->d_ino_timer;
2727 check_ilim = 1;
2728 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2729 }
2730
2731 if (check_blim) {
2732 if (!dm->dqb_bsoftlimit ||
2733 dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
2734 dm->dqb_btime = 0;
2735 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2736 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
2737 /* Set grace only if user hasn't provided his own... */
2738 dm->dqb_btime = ktime_get_real_seconds() + dqi->dqi_bgrace;
2739 }
2740 if (check_ilim) {
2741 if (!dm->dqb_isoftlimit ||
2742 dm->dqb_curinodes <= dm->dqb_isoftlimit) {
2743 dm->dqb_itime = 0;
2744 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2745 } else if (!(di->d_fieldmask & QC_INO_TIMER))
2746 /* Set grace only if user hasn't provided his own... */
2747 dm->dqb_itime = ktime_get_real_seconds() + dqi->dqi_igrace;
2748 }
2749 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2750 dm->dqb_isoftlimit)
2751 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2752 else
2753 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2754 spin_unlock(&dquot->dq_dqb_lock);
2755 mark_dquot_dirty(dquot);
2756
2757 return 0;
2758 }
2759
dquot_set_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * di)2760 int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2761 struct qc_dqblk *di)
2762 {
2763 struct dquot *dquot;
2764 int rc;
2765
2766 dquot = dqget(sb, qid);
2767 if (IS_ERR(dquot)) {
2768 rc = PTR_ERR(dquot);
2769 goto out;
2770 }
2771 rc = do_set_dqblk(dquot, di);
2772 dqput(dquot);
2773 out:
2774 return rc;
2775 }
2776 EXPORT_SYMBOL(dquot_set_dqblk);
2777
2778 /* Generic routine for getting common part of quota file information */
dquot_get_state(struct super_block * sb,struct qc_state * state)2779 int dquot_get_state(struct super_block *sb, struct qc_state *state)
2780 {
2781 struct mem_dqinfo *mi;
2782 struct qc_type_state *tstate;
2783 struct quota_info *dqopt = sb_dqopt(sb);
2784 int type;
2785
2786 memset(state, 0, sizeof(*state));
2787 for (type = 0; type < MAXQUOTAS; type++) {
2788 if (!sb_has_quota_active(sb, type))
2789 continue;
2790 tstate = state->s_state + type;
2791 mi = sb_dqopt(sb)->info + type;
2792 tstate->flags = QCI_ACCT_ENABLED;
2793 spin_lock(&dq_data_lock);
2794 if (mi->dqi_flags & DQF_SYS_FILE)
2795 tstate->flags |= QCI_SYSFILE;
2796 if (mi->dqi_flags & DQF_ROOT_SQUASH)
2797 tstate->flags |= QCI_ROOT_SQUASH;
2798 if (sb_has_quota_limits_enabled(sb, type))
2799 tstate->flags |= QCI_LIMITS_ENFORCED;
2800 tstate->spc_timelimit = mi->dqi_bgrace;
2801 tstate->ino_timelimit = mi->dqi_igrace;
2802 if (dqopt->files[type]) {
2803 tstate->ino = dqopt->files[type]->i_ino;
2804 tstate->blocks = dqopt->files[type]->i_blocks;
2805 }
2806 tstate->nextents = 1; /* We don't know... */
2807 spin_unlock(&dq_data_lock);
2808 }
2809 return 0;
2810 }
2811 EXPORT_SYMBOL(dquot_get_state);
2812
2813 /* Generic routine for setting common part of quota file information */
dquot_set_dqinfo(struct super_block * sb,int type,struct qc_info * ii)2814 int dquot_set_dqinfo(struct super_block *sb, int type, struct qc_info *ii)
2815 {
2816 struct mem_dqinfo *mi;
2817 int err = 0;
2818
2819 if ((ii->i_fieldmask & QC_WARNS_MASK) ||
2820 (ii->i_fieldmask & QC_RT_SPC_TIMER))
2821 return -EINVAL;
2822 if (!sb_has_quota_active(sb, type))
2823 return -ESRCH;
2824 mi = sb_dqopt(sb)->info + type;
2825 if (ii->i_fieldmask & QC_FLAGS) {
2826 if ((ii->i_flags & QCI_ROOT_SQUASH &&
2827 mi->dqi_format->qf_fmt_id != QFMT_VFS_OLD))
2828 return -EINVAL;
2829 }
2830 spin_lock(&dq_data_lock);
2831 if (ii->i_fieldmask & QC_SPC_TIMER)
2832 mi->dqi_bgrace = ii->i_spc_timelimit;
2833 if (ii->i_fieldmask & QC_INO_TIMER)
2834 mi->dqi_igrace = ii->i_ino_timelimit;
2835 if (ii->i_fieldmask & QC_FLAGS) {
2836 if (ii->i_flags & QCI_ROOT_SQUASH)
2837 mi->dqi_flags |= DQF_ROOT_SQUASH;
2838 else
2839 mi->dqi_flags &= ~DQF_ROOT_SQUASH;
2840 }
2841 spin_unlock(&dq_data_lock);
2842 mark_info_dirty(sb, type);
2843 /* Force write to disk */
2844 sb->dq_op->write_info(sb, type);
2845 return err;
2846 }
2847 EXPORT_SYMBOL(dquot_set_dqinfo);
2848
2849 const struct quotactl_ops dquot_quotactl_sysfile_ops = {
2850 .quota_enable = dquot_quota_enable,
2851 .quota_disable = dquot_quota_disable,
2852 .quota_sync = dquot_quota_sync,
2853 .get_state = dquot_get_state,
2854 .set_info = dquot_set_dqinfo,
2855 .get_dqblk = dquot_get_dqblk,
2856 .get_nextdqblk = dquot_get_next_dqblk,
2857 .set_dqblk = dquot_set_dqblk
2858 };
2859 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
2860
do_proc_dqstats(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2861 static int do_proc_dqstats(struct ctl_table *table, int write,
2862 void *buffer, size_t *lenp, loff_t *ppos)
2863 {
2864 unsigned int type = (unsigned long *)table->data - dqstats.stat;
2865 s64 value = percpu_counter_sum(&dqstats.counter[type]);
2866
2867 /* Filter negative values for non-monotonic counters */
2868 if (value < 0 && (type == DQST_ALLOC_DQUOTS ||
2869 type == DQST_FREE_DQUOTS))
2870 value = 0;
2871
2872 /* Update global table */
2873 dqstats.stat[type] = value;
2874 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
2875 }
2876
2877 static struct ctl_table fs_dqstats_table[] = {
2878 {
2879 .procname = "lookups",
2880 .data = &dqstats.stat[DQST_LOOKUPS],
2881 .maxlen = sizeof(unsigned long),
2882 .mode = 0444,
2883 .proc_handler = do_proc_dqstats,
2884 },
2885 {
2886 .procname = "drops",
2887 .data = &dqstats.stat[DQST_DROPS],
2888 .maxlen = sizeof(unsigned long),
2889 .mode = 0444,
2890 .proc_handler = do_proc_dqstats,
2891 },
2892 {
2893 .procname = "reads",
2894 .data = &dqstats.stat[DQST_READS],
2895 .maxlen = sizeof(unsigned long),
2896 .mode = 0444,
2897 .proc_handler = do_proc_dqstats,
2898 },
2899 {
2900 .procname = "writes",
2901 .data = &dqstats.stat[DQST_WRITES],
2902 .maxlen = sizeof(unsigned long),
2903 .mode = 0444,
2904 .proc_handler = do_proc_dqstats,
2905 },
2906 {
2907 .procname = "cache_hits",
2908 .data = &dqstats.stat[DQST_CACHE_HITS],
2909 .maxlen = sizeof(unsigned long),
2910 .mode = 0444,
2911 .proc_handler = do_proc_dqstats,
2912 },
2913 {
2914 .procname = "allocated_dquots",
2915 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2916 .maxlen = sizeof(unsigned long),
2917 .mode = 0444,
2918 .proc_handler = do_proc_dqstats,
2919 },
2920 {
2921 .procname = "free_dquots",
2922 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2923 .maxlen = sizeof(unsigned long),
2924 .mode = 0444,
2925 .proc_handler = do_proc_dqstats,
2926 },
2927 {
2928 .procname = "syncs",
2929 .data = &dqstats.stat[DQST_SYNCS],
2930 .maxlen = sizeof(unsigned long),
2931 .mode = 0444,
2932 .proc_handler = do_proc_dqstats,
2933 },
2934 #ifdef CONFIG_PRINT_QUOTA_WARNING
2935 {
2936 .procname = "warnings",
2937 .data = &flag_print_warnings,
2938 .maxlen = sizeof(int),
2939 .mode = 0644,
2940 .proc_handler = proc_dointvec,
2941 },
2942 #endif
2943 { },
2944 };
2945
2946 static struct ctl_table fs_table[] = {
2947 {
2948 .procname = "quota",
2949 .mode = 0555,
2950 .child = fs_dqstats_table,
2951 },
2952 { },
2953 };
2954
2955 static struct ctl_table sys_table[] = {
2956 {
2957 .procname = "fs",
2958 .mode = 0555,
2959 .child = fs_table,
2960 },
2961 { },
2962 };
2963
dquot_init(void)2964 static int __init dquot_init(void)
2965 {
2966 int i, ret;
2967 unsigned long nr_hash, order;
2968
2969 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2970
2971 register_sysctl_table(sys_table);
2972
2973 dquot_cachep = kmem_cache_create("dquot",
2974 sizeof(struct dquot), sizeof(unsigned long) * 4,
2975 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2976 SLAB_MEM_SPREAD|SLAB_PANIC),
2977 NULL);
2978
2979 order = 0;
2980 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_KERNEL, order);
2981 if (!dquot_hash)
2982 panic("Cannot create dquot hash table");
2983
2984 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2985 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
2986 if (ret)
2987 panic("Cannot create dquot stat counters");
2988 }
2989
2990 /* Find power-of-two hlist_heads which can fit into allocation */
2991 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2992 dq_hash_bits = ilog2(nr_hash);
2993
2994 nr_hash = 1UL << dq_hash_bits;
2995 dq_hash_mask = nr_hash - 1;
2996 for (i = 0; i < nr_hash; i++)
2997 INIT_HLIST_HEAD(dquot_hash + i);
2998
2999 pr_info("VFS: Dquot-cache hash table entries: %ld (order %ld,"
3000 " %ld bytes)\n", nr_hash, order, (PAGE_SIZE << order));
3001
3002 if (register_shrinker(&dqcache_shrinker))
3003 panic("Cannot register dquot shrinker");
3004
3005 return 0;
3006 }
3007 fs_initcall(dquot_init);
3008