File: /usr/src/linux/fs/dquot.c
1 /*
2 * Implementation of the diskquota system for the LINUX operating
3 * system. QUOTA is implemented using the BSD system call interface as
4 * the means of communication with the user level. Currently only the
5 * ext2 filesystem has support for disk quotas. Other filesystems may
6 * be added in the future. This file contains the generic routines
7 * called by the different filesystems on allocation of an inode or
8 * block. These routines take care of the administration needed to
9 * have a consistent diskquota tracking system. The ideas of both
10 * user and group quotas are based on the Melbourne quota system as
11 * used on BSD derived systems. The internal implementation is
12 * based on one of the several variants of the LINUX inode-subsystem
13 * with added complexity of the diskquota system.
14 *
15 * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
16 *
17 * Author: Marco van Wieringen <mvw@planets.elm.net>
18 *
19 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
20 *
21 * Revised list management to avoid races
22 * -- Bill Hawes, <whawes@star.net>, 9/98
23 *
24 * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
25 * As the consequence the locking was moved from dquot_decr_...(),
26 * dquot_incr_...() to calling functions.
27 * invalidate_dquots() now writes modified dquots.
28 * Serialized quota_off() and quota_on() for mount point.
29 * Fixed a few bugs in grow_dquots.
30 * Fixed deadlock in write_dquot() - we no longer account quotas on
31 * quota files
32 * remove_dquot_ref() moved to inode.c - it now traverses through inodes
33 * add_dquot_ref() restarts after blocking
34 * Added check for bogus uid and fixed check for group in quotactl.
35 * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
36 *
37 * (C) Copyright 1994 - 1997 Marco van Wieringen
38 */
39
40 #include <linux/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43
44 #include <linux/types.h>
45 #include <linux/string.h>
46 #include <linux/fcntl.h>
47 #include <linux/stat.h>
48 #include <linux/tty.h>
49 #include <linux/file.h>
50 #include <linux/slab.h>
51 #include <linux/mount.h>
52 #include <linux/smp.h>
53 #include <linux/smp_lock.h>
54 #include <linux/init.h>
55
56 #include <asm/uaccess.h>
57
58 #define __DQUOT_VERSION__ "dquot_6.4.0"
59
60 int nr_dquots, nr_free_dquots;
61 int max_dquots = NR_DQUOTS;
62
63 static char quotamessage[MAX_QUOTA_MESSAGE];
64 static char *quotatypes[] = INITQFNAMES;
65
66 static inline struct quota_mount_options *sb_dqopt(struct super_block *sb)
67 {
68 return &sb->s_dquot;
69 }
70
71 /*
72 * Dquot List Management:
73 * The quota code uses three lists for dquot management: the inuse_list,
74 * free_dquots, and dquot_hash[] array. A single dquot structure may be
75 * on all three lists, depending on its current state.
76 *
77 * All dquots are placed on the inuse_list when first created, and this
78 * list is used for the sync and invalidate operations, which must look
79 * at every dquot.
80 *
81 * Unused dquots (dq_count == 0) are added to the free_dquots list when
82 * freed, and this list is searched whenever we need an available dquot.
83 * Dquots are removed from the list as soon as they are used again, and
84 * nr_free_dquots gives the number of dquots on the list.
85 *
86 * Dquots with a specific identity (device, type and id) are placed on
87 * one of the dquot_hash[] hash chains. The provides an efficient search
88 * mechanism to lcoate a specific dquot.
89 */
90
91 static struct dquot *inuse_list;
92 static LIST_HEAD(free_dquots);
93 static struct dquot *dquot_hash[NR_DQHASH];
94 static int dquot_updating[NR_DQHASH];
95
96 static struct dqstats dqstats;
97 static DECLARE_WAIT_QUEUE_HEAD(dquot_wait);
98 static DECLARE_WAIT_QUEUE_HEAD(update_wait);
99
100 static void dqput(struct dquot *);
101 static struct dquot *dqduplicate(struct dquot *);
102
103 static inline char is_enabled(struct quota_mount_options *dqopt, short type)
104 {
105 switch (type) {
106 case USRQUOTA:
107 return((dqopt->flags & DQUOT_USR_ENABLED) != 0);
108 case GRPQUOTA:
109 return((dqopt->flags & DQUOT_GRP_ENABLED) != 0);
110 }
111 return(0);
112 }
113
114 static inline char sb_has_quota_enabled(struct super_block *sb, short type)
115 {
116 return is_enabled(sb_dqopt(sb), type);
117 }
118
119 static inline int const hashfn(kdev_t dev, unsigned int id, short type)
120 {
121 return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
122 }
123
124 static inline void insert_dquot_hash(struct dquot *dquot)
125 {
126 struct dquot **htable;
127
128 htable = &dquot_hash[hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type)];
129 if ((dquot->dq_hash_next = *htable) != NULL)
130 (*htable)->dq_hash_pprev = &dquot->dq_hash_next;
131 *htable = dquot;
132 dquot->dq_hash_pprev = htable;
133 }
134
135 static inline void hash_dquot(struct dquot *dquot)
136 {
137 insert_dquot_hash(dquot);
138 }
139
140 static inline void unhash_dquot(struct dquot *dquot)
141 {
142 if (dquot->dq_hash_pprev) {
143 if (dquot->dq_hash_next)
144 dquot->dq_hash_next->dq_hash_pprev = dquot->dq_hash_pprev;
145 *(dquot->dq_hash_pprev) = dquot->dq_hash_next;
146 dquot->dq_hash_pprev = NULL;
147 }
148 }
149
150 static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
151 {
152 struct dquot *dquot;
153
154 for (dquot = dquot_hash[hashent]; dquot; dquot = dquot->dq_hash_next)
155 if (dquot->dq_dev == dev && dquot->dq_id == id && dquot->dq_type == type)
156 break;
157 return dquot;
158 }
159
160 /* Add a dquot to the head of the free list */
161 static inline void put_dquot_head(struct dquot *dquot)
162 {
163 list_add(&dquot->dq_free, &free_dquots);
164 nr_free_dquots++;
165 }
166
167 /* Add a dquot to the tail of the free list */
168 static inline void put_dquot_last(struct dquot *dquot)
169 {
170 list_add(&dquot->dq_free, free_dquots.prev);
171 nr_free_dquots++;
172 }
173
174 static inline void remove_free_dquot(struct dquot *dquot)
175 {
176 /* sanity check */
177 if (list_empty(&dquot->dq_free)) {
178 printk("remove_free_dquot: dquot not on the free list??\n");
179 return; /* J.K. Just don't do anything */
180 }
181 list_del(&dquot->dq_free);
182 INIT_LIST_HEAD(&dquot->dq_free);
183 nr_free_dquots--;
184 }
185
186 static inline void put_inuse(struct dquot *dquot)
187 {
188 if ((dquot->dq_next = inuse_list) != NULL)
189 inuse_list->dq_pprev = &dquot->dq_next;
190 inuse_list = dquot;
191 dquot->dq_pprev = &inuse_list;
192 }
193
194 #if 0 /* currently not needed */
195 static inline void remove_inuse(struct dquot *dquot)
196 {
197 if (dquot->dq_pprev) {
198 if (dquot->dq_next)
199 dquot->dq_next->dq_pprev = dquot->dq_pprev;
200 *dquot->dq_pprev = dquot->dq_next;
201 dquot->dq_pprev = NULL;
202 }
203 }
204 #endif
205
206 static void __wait_on_dquot(struct dquot *dquot)
207 {
208 DECLARE_WAITQUEUE(wait, current);
209
210 add_wait_queue(&dquot->dq_wait, &wait);
211 repeat:
212 set_current_state(TASK_UNINTERRUPTIBLE);
213 if (dquot->dq_flags & DQ_LOCKED) {
214 schedule();
215 goto repeat;
216 }
217 remove_wait_queue(&dquot->dq_wait, &wait);
218 current->state = TASK_RUNNING;
219 }
220
221 static inline void wait_on_dquot(struct dquot *dquot)
222 {
223 if (dquot->dq_flags & DQ_LOCKED)
224 __wait_on_dquot(dquot);
225 }
226
227 static inline void lock_dquot(struct dquot *dquot)
228 {
229 wait_on_dquot(dquot);
230 dquot->dq_flags |= DQ_LOCKED;
231 }
232
233 static inline void unlock_dquot(struct dquot *dquot)
234 {
235 dquot->dq_flags &= ~DQ_LOCKED;
236 wake_up(&dquot->dq_wait);
237 }
238
239 /*
240 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
241 */
242 static void write_dquot(struct dquot *dquot)
243 {
244 short type = dquot->dq_type;
245 struct file *filp;
246 mm_segment_t fs;
247 loff_t offset;
248 ssize_t ret;
249 struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
250
251 lock_dquot(dquot);
252 if (!dquot->dq_sb) { /* Invalidated quota? */
253 unlock_dquot(dquot);
254 return;
255 }
256 down(sem);
257 filp = dquot->dq_sb->s_dquot.files[type];
258 offset = dqoff(dquot->dq_id);
259 fs = get_fs();
260 set_fs(KERNEL_DS);
261
262 /*
263 * Note: clear the DQ_MOD flag unconditionally,
264 * so we don't loop forever on failure.
265 */
266 dquot->dq_flags &= ~DQ_MOD;
267 ret = 0;
268 if (filp)
269 ret = filp->f_op->write(filp, (char *)&dquot->dq_dqb,
270 sizeof(struct dqblk), &offset);
271 if (ret != sizeof(struct dqblk))
272 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
273 kdevname(dquot->dq_dev));
274
275 set_fs(fs);
276 up(sem);
277 unlock_dquot(dquot);
278
279 dqstats.writes++;
280 }
281
282 static void read_dquot(struct dquot *dquot)
283 {
284 short type = dquot->dq_type;
285 struct file *filp;
286 mm_segment_t fs;
287 loff_t offset;
288
289 filp = dquot->dq_sb->s_dquot.files[type];
290 if (filp == (struct file *)NULL)
291 return;
292
293 lock_dquot(dquot);
294 if (!dquot->dq_sb) /* Invalidated quota? */
295 goto out_lock;
296 /* Now we are sure filp is valid - the dquot isn't invalidated */
297 down(&dquot->dq_sb->s_dquot.dqio_sem);
298 offset = dqoff(dquot->dq_id);
299 fs = get_fs();
300 set_fs(KERNEL_DS);
301 filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
302 up(&dquot->dq_sb->s_dquot.dqio_sem);
303 set_fs(fs);
304
305 if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
306 dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
307 dquot->dq_flags |= DQ_FAKE;
308 dqstats.reads++;
309 out_lock:
310 unlock_dquot(dquot);
311 }
312
313 /*
314 * Unhash and selectively clear the dquot structure,
315 * but preserve the use count, list pointers, and
316 * wait queue.
317 */
318 void clear_dquot(struct dquot *dquot)
319 {
320 /* unhash it first */
321 unhash_dquot(dquot);
322 dquot->dq_sb = NULL;
323 dquot->dq_flags = 0;
324 dquot->dq_referenced = 0;
325 memset(&dquot->dq_dqb, 0, sizeof(struct dqblk));
326 }
327
328 static void invalidate_dquots(struct super_block *sb, short type)
329 {
330 struct dquot *dquot, *next;
331 int need_restart;
332
333 restart:
334 next = inuse_list; /* Here it is better. Otherwise the restart doesn't have any sense ;-) */
335 need_restart = 0;
336 while ((dquot = next) != NULL) {
337 next = dquot->dq_next;
338 if (dquot->dq_sb != sb)
339 continue;
340 if (dquot->dq_type != type)
341 continue;
342 if (dquot->dq_flags & DQ_LOCKED) {
343 __wait_on_dquot(dquot);
344
345 /* Set the flag for another pass. */
346 need_restart = 1;
347 /*
348 * Make sure it's still the same dquot.
349 */
350 if (dquot->dq_sb != sb)
351 continue;
352 if (dquot->dq_type != type)
353 continue;
354 }
355 /*
356 * Because inodes needn't to be the only holders of dquot
357 * the quota needn't to be written to disk. So we write it
358 * ourselves before discarding the data just for sure...
359 */
360 if (dquot->dq_flags & DQ_MOD && dquot->dq_sb)
361 {
362 write_dquot(dquot);
363 need_restart = 1; /* We slept on IO */
364 }
365 clear_dquot(dquot);
366 }
367 /*
368 * If anything blocked, restart the operation
369 * to ensure we don't miss any dquots.
370 */
371 if (need_restart)
372 goto restart;
373 }
374
375 int sync_dquots(kdev_t dev, short type)
376 {
377 struct dquot *dquot, *next, *ddquot;
378 int need_restart;
379
380 restart:
381 next = inuse_list;
382 need_restart = 0;
383 while ((dquot = next) != NULL) {
384 next = dquot->dq_next;
385 if (dev && dquot->dq_dev != dev)
386 continue;
387 if (type != -1 && dquot->dq_type != type)
388 continue;
389 if (!dquot->dq_sb) /* Invalidated? */
390 continue;
391 if (!(dquot->dq_flags & (DQ_LOCKED | DQ_MOD)))
392 continue;
393
394 if ((ddquot = dqduplicate(dquot)) == NODQUOT)
395 continue;
396 if (ddquot->dq_flags & DQ_MOD)
397 write_dquot(ddquot);
398 dqput(ddquot);
399 /* Set the flag for another pass. */
400 need_restart = 1;
401 }
402 /*
403 * If anything blocked, restart the operation
404 * to ensure we don't miss any dquots.
405 */
406 if (need_restart)
407 goto restart;
408
409 dqstats.syncs++;
410 return(0);
411 }
412
413 /* NOTE: If you change this function please check whether dqput_blocks() works right... */
414 static void dqput(struct dquot *dquot)
415 {
416 if (!dquot)
417 return;
418 if (!dquot->dq_count) {
419 printk("VFS: dqput: trying to free free dquot\n");
420 printk("VFS: device %s, dquot of %s %d\n",
421 kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
422 dquot->dq_id);
423 return;
424 }
425
426 /*
427 * If the dq_sb pointer isn't initialized this entry needs no
428 * checking and doesn't need to be written. It's just an empty
429 * dquot that is put back on to the freelist.
430 */
431 if (dquot->dq_sb)
432 dqstats.drops++;
433 we_slept:
434 if (dquot->dq_count > 1) {
435 /* We have more than one user... We can simply decrement use count */
436 dquot->dq_count--;
437 return;
438 }
439 if (dquot->dq_flags & DQ_LOCKED) {
440 printk(KERN_ERR "VFS: Locked quota to be put on the free list.\n");
441 dquot->dq_flags &= ~DQ_LOCKED;
442 }
443 if (dquot->dq_sb && dquot->dq_flags & DQ_MOD) {
444 write_dquot(dquot);
445 goto we_slept;
446 }
447
448 /* sanity check */
449 if (!list_empty(&dquot->dq_free)) {
450 printk(KERN_ERR "dqput: dquot already on free list??\n");
451 dquot->dq_count--; /* J.K. Just decrementing use count seems safer... */
452 return;
453 }
454 dquot->dq_count--;
455 dquot->dq_flags &= ~DQ_MOD; /* Modified flag has no sense on free list */
456 /* Place at end of LRU free queue */
457 put_dquot_last(dquot);
458 wake_up(&dquot_wait);
459 }
460
461 static int grow_dquots(void)
462 {
463 struct dquot *dquot;
464 int cnt = 0;
465
466 while (cnt < 32) {
467 dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
468 if(!dquot)
469 return cnt;
470
471 nr_dquots++;
472 memset((caddr_t)dquot, 0, sizeof(struct dquot));
473 init_waitqueue_head(&dquot->dq_wait);
474 /* all dquots go on the inuse_list */
475 put_inuse(dquot);
476 put_dquot_head(dquot);
477 cnt++;
478 }
479 return cnt;
480 }
481
482 static struct dquot *find_best_candidate_weighted(void)
483 {
484 struct list_head *tmp = &free_dquots;
485 struct dquot *dquot, *best = NULL;
486 unsigned long myscore, bestscore = ~0U;
487 int limit = (nr_free_dquots > 128) ? nr_free_dquots >> 2 : 32;
488
489 while ((tmp = tmp->next) != &free_dquots && --limit) {
490 dquot = list_entry(tmp, struct dquot, dq_free);
491 /* This should never happen... */
492 if (dquot->dq_flags & (DQ_LOCKED | DQ_MOD))
493 continue;
494 myscore = dquot->dq_referenced;
495 if (myscore < bestscore) {
496 bestscore = myscore;
497 best = dquot;
498 }
499 }
500 return best;
501 }
502
503 static inline struct dquot *find_best_free(void)
504 {
505 struct list_head *tmp = &free_dquots;
506 struct dquot *dquot;
507 int limit = (nr_free_dquots > 1024) ? nr_free_dquots >> 5 : 32;
508
509 while ((tmp = tmp->next) != &free_dquots && --limit) {
510 dquot = list_entry(tmp, struct dquot, dq_free);
511 if (dquot->dq_referenced == 0)
512 return dquot;
513 }
514 return NULL;
515 }
516
517 struct dquot *get_empty_dquot(void)
518 {
519 struct dquot *dquot;
520 int shrink = 8; /* Number of times we should try to shrink dcache and icache */
521
522 repeat:
523 dquot = find_best_free();
524 if (!dquot)
525 goto pressure;
526 got_it:
527 /* Sanity checks */
528 if (dquot->dq_flags & DQ_LOCKED)
529 printk(KERN_ERR "VFS: Locked dquot on the free list\n");
530 if (dquot->dq_count != 0)
531 printk(KERN_ERR "VFS: free dquot count=%d\n", dquot->dq_count);
532
533 remove_free_dquot(dquot);
534 dquot->dq_count = 1;
535 /* unhash and selectively clear the structure */
536 clear_dquot(dquot);
537 return dquot;
538
539 pressure:
540 if (nr_dquots < max_dquots)
541 if (grow_dquots())
542 goto repeat;
543
544 dquot = find_best_candidate_weighted();
545 if (dquot)
546 goto got_it;
547 /*
548 * Try pruning the dcache to free up some dquots ...
549 */
550 if (shrink) {
551 printk(KERN_DEBUG "get_empty_dquot: pruning dcache and icache\n");
552 prune_dcache(128);
553 prune_icache(128);
554 shrink--;
555 goto repeat;
556 }
557
558 printk("VFS: No free dquots, contact mvw@planets.elm.net\n");
559 sleep_on(&dquot_wait);
560 goto repeat;
561 }
562
563 static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
564 {
565 unsigned int hashent = hashfn(sb->s_dev, id, type);
566 struct dquot *dquot, *empty = NULL;
567 struct quota_mount_options *dqopt = sb_dqopt(sb);
568
569 if (!is_enabled(dqopt, type))
570 return(NODQUOT);
571
572 we_slept:
573 if ((dquot = find_dquot(hashent, sb->s_dev, id, type)) == NULL) {
574 if (empty == NULL) {
575 dquot_updating[hashent]++;
576 empty = get_empty_dquot();
577 if (!--dquot_updating[hashent])
578 wake_up(&update_wait);
579 goto we_slept;
580 }
581 dquot = empty;
582 dquot->dq_id = id;
583 dquot->dq_type = type;
584 dquot->dq_dev = sb->s_dev;
585 dquot->dq_sb = sb;
586 /* hash it first so it can be found */
587 hash_dquot(dquot);
588 read_dquot(dquot);
589 } else {
590 if (!dquot->dq_count++) {
591 remove_free_dquot(dquot);
592 } else
593 dqstats.cache_hits++;
594 wait_on_dquot(dquot);
595 if (empty)
596 dqput(empty);
597 }
598
599 while (dquot_updating[hashent])
600 sleep_on(&update_wait);
601
602 if (!dquot->dq_sb) { /* Has somebody invalidated entry under us? */
603 /*
604 * Do it as if the quota was invalidated before we started
605 */
606 dqput(dquot);
607 return NODQUOT;
608 }
609 dquot->dq_referenced++;
610 dqstats.lookups++;
611
612 return dquot;
613 }
614
615 static struct dquot *dqduplicate(struct dquot *dquot)
616 {
617 if (dquot == NODQUOT || !dquot->dq_sb)
618 return NODQUOT;
619 dquot->dq_count++;
620 wait_on_dquot(dquot);
621 if (!dquot->dq_sb) {
622 dquot->dq_count--;
623 return NODQUOT;
624 }
625 dquot->dq_referenced++;
626 dqstats.lookups++;
627 return dquot;
628 }
629
630 static int dqinit_needed(struct inode *inode, short type)
631 {
632 int cnt;
633
634 if (IS_NOQUOTA(inode))
635 return 0;
636 if (type != -1)
637 return inode->i_dquot[type] == NODQUOT;
638 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
639 if (inode->i_dquot[cnt] == NODQUOT)
640 return 1;
641 return 0;
642 }
643
644 static void add_dquot_ref(struct super_block *sb, short type)
645 {
646 struct list_head *p;
647
648 if (!sb->dq_op)
649 return; /* nothing to do */
650
651 restart:
652 file_list_lock();
653 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
654 struct file *filp = list_entry(p, struct file, f_list);
655 struct inode *inode = filp->f_dentry->d_inode;
656 if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
657 struct vfsmount *mnt = mntget(filp->f_vfsmnt);
658 struct dentry *dentry = dget(filp->f_dentry);
659 file_list_unlock();
660 sb->dq_op->initialize(inode, type);
661 dput(dentry);
662 mntput(mnt);
663 /* As we may have blocked we had better restart... */
664 goto restart;
665 }
666 }
667 file_list_unlock();
668 }
669
670 /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
671 static inline int dqput_blocks(struct dquot *dquot)
672 {
673 if (dquot->dq_count == 1)
674 return 1;
675 return 0;
676 }
677
678 /* Remove references to dquots from inode - add dquot to list for freeing if needed */
679 int remove_inode_dquot_ref(struct inode *inode, short type, struct list_head *tofree_head)
680 {
681 struct dquot *dquot = inode->i_dquot[type];
682 int cnt;
683
684 inode->i_dquot[type] = NODQUOT;
685 /* any other quota in use? */
686 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
687 if (inode->i_dquot[cnt] != NODQUOT)
688 goto put_it;
689 }
690 inode->i_flags &= ~S_QUOTA;
691 put_it:
692 if (dquot != NODQUOT) {
693 if (dqput_blocks(dquot)) {
694 if (dquot->dq_count != 1)
695 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", dquot->dq_count);
696 list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
697 return 1;
698 } else {
699 dqput(dquot); /* We have guaranteed we won't block */
700 }
701 }
702 return 0;
703 }
704
705 /* Free list of dquots - called from inode.c */
706 void put_dquot_list(struct list_head *tofree_head)
707 {
708 struct list_head *act_head = tofree_head->next;
709 struct dquot *dquot;
710
711 /* So now we have dquots on the list... Just free them */
712 while (act_head != tofree_head) {
713 dquot = list_entry(act_head, struct dquot, dq_free);
714 act_head = act_head->next;
715 list_del(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
716 INIT_LIST_HEAD(&dquot->dq_free);
717 dqput(dquot);
718 }
719 }
720
721 static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
722 {
723 dquot->dq_curinodes += number;
724 dquot->dq_flags |= DQ_MOD;
725 }
726
727 static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
728 {
729 dquot->dq_curblocks += number;
730 dquot->dq_flags |= DQ_MOD;
731 }
732
733 static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
734 {
735 if (dquot->dq_curinodes > number)
736 dquot->dq_curinodes -= number;
737 else
738 dquot->dq_curinodes = 0;
739 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
740 dquot->dq_itime = (time_t) 0;
741 dquot->dq_flags &= ~DQ_INODES;
742 dquot->dq_flags |= DQ_MOD;
743 }
744
745 static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
746 {
747 if (dquot->dq_curblocks > number)
748 dquot->dq_curblocks -= number;
749 else
750 dquot->dq_curblocks = 0;
751 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
752 dquot->dq_btime = (time_t) 0;
753 dquot->dq_flags &= ~DQ_BLKS;
754 dquot->dq_flags |= DQ_MOD;
755 }
756
757 static inline int need_print_warning(struct dquot *dquot, int flag)
758 {
759 switch (dquot->dq_type) {
760 case USRQUOTA:
761 return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
762 case GRPQUOTA:
763 return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
764 }
765 return 0;
766 }
767
768 static void print_warning(struct dquot *dquot, int flag, const char *fmtstr)
769 {
770 if (!need_print_warning(dquot, flag))
771 return;
772 sprintf(quotamessage, fmtstr,
773 bdevname(dquot->dq_sb->s_dev), quotatypes[dquot->dq_type]);
774 tty_write_message(current->tty, quotamessage);
775 dquot->dq_flags |= flag;
776 }
777
778 static inline char ignore_hardlimit(struct dquot *dquot)
779 {
780 return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
781 }
782
783 static int check_idq(struct dquot *dquot, u_long inodes)
784 {
785 if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
786 return QUOTA_OK;
787
788 if (dquot->dq_ihardlimit &&
789 (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
790 !ignore_hardlimit(dquot)) {
791 print_warning(dquot, DQ_INODES, "%s: write failed, %s file limit reached\n");
792 return NO_QUOTA;
793 }
794
795 if (dquot->dq_isoftlimit &&
796 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
797 dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
798 !ignore_hardlimit(dquot)) {
799 print_warning(dquot, DQ_INODES, "%s: warning, %s file quota exceeded too long.\n");
800 return NO_QUOTA;
801 }
802
803 if (dquot->dq_isoftlimit &&
804 (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
805 dquot->dq_itime == 0) {
806 print_warning(dquot, 0, "%s: warning, %s file quota exceeded\n");
807 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[dquot->dq_type];
808 }
809
810 return QUOTA_OK;
811 }
812
813 static int check_bdq(struct dquot *dquot, u_long blocks, char prealloc)
814 {
815 if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
816 return QUOTA_OK;
817
818 if (dquot->dq_bhardlimit &&
819 (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
820 !ignore_hardlimit(dquot)) {
821 if (!prealloc)
822 print_warning(dquot, DQ_BLKS, "%s: write failed, %s disk limit reached.\n");
823 return NO_QUOTA;
824 }
825
826 if (dquot->dq_bsoftlimit &&
827 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
828 dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
829 !ignore_hardlimit(dquot)) {
830 if (!prealloc)
831 print_warning(dquot, DQ_BLKS, "%s: write failed, %s disk quota exceeded too long.\n");
832 return NO_QUOTA;
833 }
834
835 if (dquot->dq_bsoftlimit &&
836 (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
837 dquot->dq_btime == 0) {
838 if (!prealloc) {
839 print_warning(dquot, 0, "%s: warning, %s disk quota exceeded\n");
840 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
841 }
842 else
843 /*
844 * We don't allow preallocation to exceed softlimit so exceeding will
845 * be always printed
846 */
847 return NO_QUOTA;
848 }
849
850 return QUOTA_OK;
851 }
852
853 /*
854 * Initialize a dquot-struct with new quota info. This is used by the
855 * system call interface functions.
856 */
857 static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
858 {
859 struct dquot *dquot;
860 int error = -EFAULT;
861 struct dqblk dq_dqblk;
862
863 if (dqblk == (struct dqblk *)NULL)
864 return error;
865
866 if (flags & QUOTA_SYSCALL) {
867 if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
868 return(error);
869 } else
870 memcpy((caddr_t)&dq_dqblk, (caddr_t)dqblk, sizeof(struct dqblk));
871
872 if (sb && (dquot = dqget(sb, id, type)) != NODQUOT) {
873 lock_dquot(dquot);
874
875 if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
876 dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
877 dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
878 dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
879 dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
880 }
881
882 if ((flags & SET_QUOTA) || (flags & SET_USE)) {
883 if (dquot->dq_isoftlimit &&
884 dquot->dq_curinodes < dquot->dq_isoftlimit &&
885 dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
886 dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[type];
887 dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
888 if (dquot->dq_curinodes < dquot->dq_isoftlimit)
889 dquot->dq_flags &= ~DQ_INODES;
890 if (dquot->dq_bsoftlimit &&
891 dquot->dq_curblocks < dquot->dq_bsoftlimit &&
892 dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
893 dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[type];
894 dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
895 if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
896 dquot->dq_flags &= ~DQ_BLKS;
897 }
898
899 if (id == 0) {
900 dquot->dq_sb->s_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
901 dquot->dq_sb->s_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
902 }
903
904 if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
905 dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
906 dquot->dq_flags |= DQ_FAKE;
907 else
908 dquot->dq_flags &= ~DQ_FAKE;
909
910 dquot->dq_flags |= DQ_MOD;
911 unlock_dquot(dquot);
912 dqput(dquot);
913 }
914 return(0);
915 }
916
917 static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
918 {
919 struct dquot *dquot;
920 int error = -ESRCH;
921
922 if (!sb || !sb_has_quota_enabled(sb, type))
923 goto out;
924 dquot = dqget(sb, id, type);
925 if (dquot == NODQUOT)
926 goto out;
927
928 lock_dquot(dquot); /* We must protect against invalidating the quota */
929 error = -EFAULT;
930 if (dqblk && !copy_to_user(dqblk, &dquot->dq_dqb, sizeof(struct dqblk)))
931 error = 0;
932 unlock_dquot(dquot);
933 dqput(dquot);
934 out:
935 return error;
936 }
937
938 static int get_stats(caddr_t addr)
939 {
940 int error = -EFAULT;
941 struct dqstats stats;
942
943 dqstats.allocated_dquots = nr_dquots;
944 dqstats.free_dquots = nr_free_dquots;
945
946 /* make a copy, in case we page-fault in user space */
947 memcpy(&stats, &dqstats, sizeof(struct dqstats));
948 if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
949 error = 0;
950 return error;
951 }
952
953 static int quota_root_squash(struct super_block *sb, short type, int *addr)
954 {
955 int new_value, error;
956
957 if (!sb)
958 return(-ENODEV);
959
960 error = -EFAULT;
961 if (!copy_from_user(&new_value, addr, sizeof(int))) {
962 sb_dqopt(sb)->rsquash[type] = new_value;
963 error = 0;
964 }
965 return error;
966 }
967
968 /*
969 * This is a simple algorithm that calculates the size of a file in blocks.
970 * This is only used on filesystems that do not have an i_blocks count.
971 */
972 static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
973 {
974 u_long blocks;
975 u_long indirect;
976
977 if (!blksize_bits)
978 blksize_bits = BLOCK_SIZE_BITS;
979 blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
980 if (blocks > 10) {
981 indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
982 if (blocks > (10 + 256)) {
983 indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
984 if (blocks > (10 + 256 + (256 << 8)))
985 indirect++; /* triple indirect blocks */
986 }
987 blocks += indirect;
988 }
989 return blocks;
990 }
991
992 /*
993 * Externally referenced functions through dquot_operations in inode.
994 *
995 * Note: this is a blocking operation.
996 */
997 void dquot_initialize(struct inode *inode, short type)
998 {
999 struct dquot *dquot;
1000 unsigned int id = 0;
1001 short cnt;
1002
1003 lock_kernel();
1004 if (IS_NOQUOTA(inode)) {
1005 unlock_kernel();
1006 return;
1007 }
1008 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1009 if (type != -1 && cnt != type)
1010 continue;
1011
1012 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1013 continue;
1014
1015 if (inode->i_dquot[cnt] == NODQUOT) {
1016 switch (cnt) {
1017 case USRQUOTA:
1018 id = inode->i_uid;
1019 break;
1020 case GRPQUOTA:
1021 id = inode->i_gid;
1022 break;
1023 }
1024 dquot = dqget(inode->i_sb, id, cnt);
1025 if (dquot == NODQUOT)
1026 continue;
1027 if (inode->i_dquot[cnt] != NODQUOT) {
1028 dqput(dquot);
1029 continue;
1030 }
1031 inode->i_dquot[cnt] = dquot;
1032 inode->i_flags |= S_QUOTA;
1033 }
1034 }
1035 unlock_kernel();
1036 }
1037
1038 /*
1039 * Release all quota for the specified inode.
1040 *
1041 * Note: this is a blocking operation.
1042 */
1043 void dquot_drop(struct inode *inode)
1044 {
1045 struct dquot *dquot;
1046 short cnt;
1047
1048 lock_kernel();
1049 inode->i_flags &= ~S_QUOTA;
1050 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1051 if (inode->i_dquot[cnt] == NODQUOT)
1052 continue;
1053 dquot = inode->i_dquot[cnt];
1054 inode->i_dquot[cnt] = NODQUOT;
1055 dqput(dquot);
1056 }
1057 unlock_kernel();
1058 }
1059
1060 /*
1061 * Note: this is a blocking operation.
1062 */
1063 int dquot_alloc_block(const struct inode *inode, unsigned long number, char warn)
1064 {
1065 int cnt;
1066 struct dquot *dquot[MAXQUOTAS];
1067
1068 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1069 dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
1070 if (dquot[cnt] == NODQUOT)
1071 continue;
1072 lock_dquot(dquot[cnt]);
1073 if (check_bdq(dquot[cnt], number, warn))
1074 goto put_all;
1075 }
1076
1077 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1078 if (dquot[cnt] == NODQUOT)
1079 continue;
1080 dquot_incr_blocks(dquot[cnt], number);
1081 unlock_dquot(dquot[cnt]);
1082 dqput(dquot[cnt]);
1083 }
1084
1085 return QUOTA_OK;
1086 put_all:
1087 for (; cnt >= 0; cnt--) {
1088 if (dquot[cnt] == NODQUOT)
1089 continue;
1090 unlock_dquot(dquot[cnt]);
1091 dqput(dquot[cnt]);
1092 }
1093 return NO_QUOTA;
1094 }
1095
1096 /*
1097 * Note: this is a blocking operation.
1098 */
1099 int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1100 {
1101 int cnt;
1102 struct dquot *dquot[MAXQUOTAS];
1103
1104 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1105 dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
1106 if (dquot[cnt] == NODQUOT)
1107 continue;
1108 lock_dquot(dquot[cnt]);
1109 if (check_idq(dquot[cnt], number))
1110 goto put_all;
1111 }
1112
1113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1114 if (dquot[cnt] == NODQUOT)
1115 continue;
1116 dquot_incr_inodes(dquot[cnt], number);
1117 unlock_dquot(dquot[cnt]);
1118 dqput(dquot[cnt]);
1119 }
1120
1121 return QUOTA_OK;
1122 put_all:
1123 for (; cnt >= 0; cnt--) {
1124 if (dquot[cnt] == NODQUOT)
1125 continue;
1126 unlock_dquot(dquot[cnt]);
1127 dqput(dquot[cnt]);
1128 }
1129 return NO_QUOTA;
1130 }
1131
1132 /*
1133 * Note: this is a blocking operation.
1134 */
1135 void dquot_free_block(const struct inode *inode, unsigned long number)
1136 {
1137 unsigned short cnt;
1138 struct dquot *dquot;
1139
1140 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1141 dquot = inode->i_dquot[cnt];
1142 if (dquot == NODQUOT)
1143 continue;
1144 wait_on_dquot(dquot);
1145 dquot_decr_blocks(dquot, number);
1146 }
1147 }
1148
1149 /*
1150 * Note: this is a blocking operation.
1151 */
1152 void dquot_free_inode(const struct inode *inode, unsigned long number)
1153 {
1154 unsigned short cnt;
1155 struct dquot *dquot;
1156
1157 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1158 dquot = inode->i_dquot[cnt];
1159 if (dquot == NODQUOT)
1160 continue;
1161 wait_on_dquot(dquot);
1162 dquot_decr_inodes(dquot, number);
1163 }
1164 }
1165
1166 /*
1167 * Transfer the number of inode and blocks from one diskquota to an other.
1168 *
1169 * Note: this is a blocking operation.
1170 */
1171 int dquot_transfer(struct dentry *dentry, struct iattr *iattr)
1172 {
1173 struct inode *inode = dentry -> d_inode;
1174 unsigned long blocks;
1175 struct dquot *transfer_from[MAXQUOTAS];
1176 struct dquot *transfer_to[MAXQUOTAS];
1177 short cnt, disc;
1178 int error = -EDQUOT;
1179
1180 if (!inode)
1181 return -ENOENT;
1182 /* Arguably we could consider that as error, but... no fs - no quota */
1183 if (!inode->i_sb)
1184 return 0;
1185
1186 lock_kernel();
1187 /*
1188 * Build the transfer_from and transfer_to lists and check quotas to see
1189 * if operation is permitted.
1190 */
1191 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1192 transfer_from[cnt] = NODQUOT;
1193 transfer_to[cnt] = NODQUOT;
1194
1195 if (!sb_has_quota_enabled(inode->i_sb, cnt))
1196 continue;
1197
1198 switch (cnt) {
1199 case USRQUOTA:
1200 if (inode->i_uid == iattr->ia_uid)
1201 continue;
1202 /* We can get transfer_from from inode, can't we? */
1203 transfer_from[cnt] = dqget(inode->i_sb, inode->i_uid, cnt);
1204 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1205 break;
1206 case GRPQUOTA:
1207 if (inode->i_gid == iattr->ia_gid)
1208 continue;
1209 transfer_from[cnt] = dqget(inode->i_sb, inode->i_gid, cnt);
1210 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1211 break;
1212 }
1213
1214 /* Something bad (eg. quotaoff) happened while we were sleeping? */
1215 if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
1216 {
1217 if (transfer_from[cnt] != NODQUOT) {
1218 dqput(transfer_from[cnt]);
1219 transfer_from[cnt] = NODQUOT;
1220 }
1221 if (transfer_to[cnt] != NODQUOT) {
1222 dqput(transfer_to[cnt]);
1223 transfer_to[cnt] = NODQUOT;
1224 }
1225 continue;
1226 }
1227 /*
1228 * We have to lock the quotas to prevent races...
1229 */
1230 if (transfer_from[cnt] < transfer_to[cnt])
1231 {
1232 lock_dquot(transfer_from[cnt]);
1233 lock_dquot(transfer_to[cnt]);
1234 }
1235 else
1236 {
1237 lock_dquot(transfer_to[cnt]);
1238 lock_dquot(transfer_from[cnt]);
1239 }
1240
1241 /*
1242 * The entries might got invalidated while locking. The second
1243 * dqget() could block and so the first structure might got
1244 * invalidated or locked...
1245 */
1246 if (!transfer_to[cnt]->dq_sb || !transfer_from[cnt]->dq_sb) {
1247 cnt++;
1248 goto put_all;
1249 }
1250 }
1251
1252 /*
1253 * Find out if this filesystem uses i_blocks.
1254 */
1255 if (!inode->i_sb->s_blocksize)
1256 blocks = isize_to_blocks(inode->i_size, BLOCK_SIZE_BITS);
1257 else
1258 blocks = (inode->i_blocks >> 1);
1259 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1260 if (!transfer_to[cnt])
1261 continue;
1262 if (check_idq(transfer_to[cnt], 1) == NO_QUOTA ||
1263 check_bdq(transfer_to[cnt], blocks, 0) == NO_QUOTA) {
1264 cnt = MAXQUOTAS;
1265 goto put_all;
1266 }
1267 }
1268
1269 if ((error = notify_change(dentry, iattr)))
1270 goto put_all;
1271 /*
1272 * Finally perform the needed transfer from transfer_from to transfer_to,
1273 * and release any pointers to dquots not needed anymore.
1274 */
1275 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1276 /*
1277 * Skip changes for same uid or gid or for non-existing quota-type.
1278 */
1279 if (transfer_from[cnt] == NODQUOT && transfer_to[cnt] == NODQUOT)
1280 continue;
1281
1282 dquot_decr_inodes(transfer_from[cnt], 1);
1283 dquot_decr_blocks(transfer_from[cnt], blocks);
1284
1285 dquot_incr_inodes(transfer_to[cnt], 1);
1286 dquot_incr_blocks(transfer_to[cnt], blocks);
1287
1288 unlock_dquot(transfer_from[cnt]);
1289 dqput(transfer_from[cnt]);
1290 if (inode->i_dquot[cnt] != NODQUOT) {
1291 struct dquot *temp = inode->i_dquot[cnt];
1292 inode->i_dquot[cnt] = transfer_to[cnt];
1293 unlock_dquot(transfer_to[cnt]);
1294 dqput(temp);
1295 } else {
1296 unlock_dquot(transfer_to[cnt]);
1297 dqput(transfer_to[cnt]);
1298 }
1299 }
1300
1301 unlock_kernel();
1302 return 0;
1303 put_all:
1304 for (disc = 0; disc < cnt; disc++) {
1305 /* There should be none or both pointers set but... */
1306 if (transfer_to[disc] != NODQUOT) {
1307 unlock_dquot(transfer_to[disc]);
1308 dqput(transfer_to[disc]);
1309 }
1310 if (transfer_from[disc] != NODQUOT) {
1311 unlock_dquot(transfer_from[disc]);
1312 dqput(transfer_from[disc]);
1313 }
1314 }
1315 unlock_kernel();
1316 return error;
1317 }
1318
1319
1320 static int __init dquot_init(void)
1321 {
1322 printk(KERN_NOTICE "VFS: Diskquotas version %s initialized\n", __DQUOT_VERSION__);
1323 return 0;
1324 }
1325 __initcall(dquot_init);
1326
1327 /*
1328 * Definitions of diskquota operations.
1329 */
1330 struct dquot_operations dquot_operations = {
1331 dquot_initialize, /* mandatory */
1332 dquot_drop, /* mandatory */
1333 dquot_alloc_block,
1334 dquot_alloc_inode,
1335 dquot_free_block,
1336 dquot_free_inode,
1337 dquot_transfer
1338 };
1339
1340 static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
1341 {
1342 switch (type) {
1343 case USRQUOTA:
1344 dqopt->flags |= DQUOT_USR_ENABLED;
1345 break;
1346 case GRPQUOTA:
1347 dqopt->flags |= DQUOT_GRP_ENABLED;
1348 break;
1349 }
1350 }
1351
1352 static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
1353 {
1354 switch (type) {
1355 case USRQUOTA:
1356 dqopt->flags &= ~DQUOT_USR_ENABLED;
1357 break;
1358 case GRPQUOTA:
1359 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1360 break;
1361 }
1362 }
1363
1364 /* Function in inode.c - remove pointers to dquots in icache */
1365 extern void remove_dquot_ref(struct super_block *, short);
1366
1367 /*
1368 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
1369 */
1370 int quota_off(struct super_block *sb, short type)
1371 {
1372 struct file *filp;
1373 short cnt;
1374 int enabled = 0;
1375 struct quota_mount_options *dqopt = sb_dqopt(sb);
1376
1377 if (!sb)
1378 goto out;
1379
1380 /* We need to serialize quota_off() for device */
1381 down(&dqopt->dqoff_sem);
1382 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1383 if (type != -1 && cnt != type)
1384 continue;
1385 if (!is_enabled(dqopt, cnt))
1386 continue;
1387 reset_enable_flags(dqopt, cnt);
1388
1389 /* Note: these are blocking operations */
1390 remove_dquot_ref(sb, cnt);
1391 invalidate_dquots(sb, cnt);
1392
1393 /* Wait for any pending IO - remove me as soon as invalidate is more polite */
1394 down(&dqopt->dqio_sem);
1395 filp = dqopt->files[cnt];
1396 dqopt->files[cnt] = (struct file *)NULL;
1397 dqopt->inode_expire[cnt] = 0;
1398 dqopt->block_expire[cnt] = 0;
1399 up(&dqopt->dqio_sem);
1400 fput(filp);
1401 }
1402
1403 /*
1404 * Check whether any quota is still enabled,
1405 * and if not clear the dq_op pointer.
1406 */
1407 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1408 enabled |= is_enabled(dqopt, cnt);
1409 if (!enabled)
1410 sb->dq_op = NULL;
1411 up(&dqopt->dqoff_sem);
1412 out:
1413 return(0);
1414 }
1415
1416 static inline int check_quotafile_size(loff_t size)
1417 {
1418 ulong blocks = size >> BLOCK_SIZE_BITS;
1419 size_t off = size & (BLOCK_SIZE - 1);
1420
1421 return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
1422 }
1423
1424 static int quota_on(struct super_block *sb, short type, char *path)
1425 {
1426 struct file *f;
1427 struct inode *inode;
1428 struct dquot *dquot;
1429 struct quota_mount_options *dqopt = sb_dqopt(sb);
1430 char *tmp;
1431 int error;
1432
1433 if (is_enabled(dqopt, type))
1434 return -EBUSY;
1435
1436 down(&dqopt->dqoff_sem);
1437 tmp = getname(path);
1438 error = PTR_ERR(tmp);
1439 if (IS_ERR(tmp))
1440 goto out_lock;
1441
1442 f = filp_open(tmp, O_RDWR, 0600);
1443 putname(tmp);
1444
1445 error = PTR_ERR(f);
1446 if (IS_ERR(f))
1447 goto out_lock;
1448 error = -EIO;
1449 if (!f->f_op || !f->f_op->read || !f->f_op->write)
1450 goto out_f;
1451 inode = f->f_dentry->d_inode;
1452 error = -EACCES;
1453 if (!S_ISREG(inode->i_mode))
1454 goto out_f;
1455 error = -EINVAL;
1456 if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
1457 goto out_f;
1458 /* We don't want quota on quota files */
1459 dquot_drop(inode);
1460 inode->i_flags |= S_NOQUOTA;
1461
1462 set_enable_flags(dqopt, type);
1463 dqopt->files[type] = f;
1464
1465 dquot = dqget(sb, 0, type);
1466 dqopt->inode_expire[type] = (dquot != NODQUOT) ? dquot->dq_itime : MAX_IQ_TIME;
1467 dqopt->block_expire[type] = (dquot != NODQUOT) ? dquot->dq_btime : MAX_DQ_TIME;
1468 dqput(dquot);
1469
1470 sb->dq_op = &dquot_operations;
1471 add_dquot_ref(sb, type);
1472
1473 up(&dqopt->dqoff_sem);
1474 return 0;
1475
1476 out_f:
1477 filp_close(f, NULL);
1478 out_lock:
1479 up(&dqopt->dqoff_sem);
1480
1481 return error;
1482 }
1483
1484 /*
1485 * This is the system call interface. This communicates with
1486 * the user-level programs. Currently this only supports diskquota
1487 * calls. Maybe we need to add the process quotas etc. in the future,
1488 * but we probably should use rlimits for that.
1489 */
1490 asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
1491 {
1492 int cmds = 0, type = 0, flags = 0;
1493 kdev_t dev;
1494 struct super_block *sb = NULL;
1495 int ret = -EINVAL;
1496
1497 lock_kernel();
1498 cmds = cmd >> SUBCMDSHIFT;
1499 type = cmd & SUBCMDMASK;
1500
1501 if ((u_int) type >= MAXQUOTAS)
1502 goto out;
1503 if (id & ~0xFFFF)
1504 goto out;
1505
1506 ret = -EPERM;
1507 switch (cmds) {
1508 case Q_SYNC:
1509 case Q_GETSTATS:
1510 break;
1511 case Q_GETQUOTA:
1512 if (((type == USRQUOTA && current->euid != id) ||
1513 (type == GRPQUOTA && !in_egroup_p(id))) &&
1514 !capable(CAP_SYS_RESOURCE))
1515 goto out;
1516 break;
1517 default:
1518 if (!capable(CAP_SYS_RESOURCE))
1519 goto out;
1520 }
1521
1522 ret = -EINVAL;
1523 dev = NODEV;
1524 if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
1525 mode_t mode;
1526 struct nameidata nd;
1527
1528 ret = user_path_walk(special, &nd);
1529 if (ret)
1530 goto out;
1531
1532 dev = nd.dentry->d_inode->i_rdev;
1533 mode = nd.dentry->d_inode->i_mode;
1534 path_release(&nd);
1535
1536 ret = -ENOTBLK;
1537 if (!S_ISBLK(mode))
1538 goto out;
1539 sb = get_super(dev);
1540 }
1541
1542 ret = -EINVAL;
1543 switch (cmds) {
1544 case Q_QUOTAON:
1545 ret = sb ? quota_on(sb, type, (char *) addr) : -ENODEV;
1546 goto out;
1547 case Q_QUOTAOFF:
1548 ret = quota_off(sb, type);
1549 goto out;
1550 case Q_GETQUOTA:
1551 ret = get_quota(sb, id, type, (struct dqblk *) addr);
1552 goto out;
1553 case Q_SETQUOTA:
1554 flags |= SET_QUOTA;
1555 break;
1556 case Q_SETUSE:
1557 flags |= SET_USE;
1558 break;
1559 case Q_SETQLIM:
1560 flags |= SET_QLIMIT;
1561 break;
1562 case Q_SYNC:
1563 ret = sync_dquots(dev, type);
1564 goto out;
1565 case Q_GETSTATS:
1566 ret = get_stats(addr);
1567 goto out;
1568 case Q_RSQUASH:
1569 ret = quota_root_squash(sb, type, (int *) addr);
1570 goto out;
1571 default:
1572 goto out;
1573 }
1574
1575 flags |= QUOTA_SYSCALL;
1576
1577 ret = -ESRCH;
1578 if (sb && sb_has_quota_enabled(sb, type))
1579 ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);
1580 out:
1581 if (sb)
1582 drop_super(sb);
1583 unlock_kernel();
1584 return ret;
1585 }
1586