dquot.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:38k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  * Used struct list_head instead of own list struct
  38.  * Invalidation of dquots with dq_count > 0 no longer possible
  39.  * Improved free_dquots list management
  40.  * Quota and i_blocks are now updated in one place to avoid races
  41.  * Warnings are now delayed so we won't block in critical section
  42.  * Write updated not to require dquot lock
  43.  * Jan Kara, <jack@suse.cz>, 9/2000
  44.  *
  45.  * Added dynamic quota structure allocation
  46.  * Jan Kara <jack@suse.cz> 12/2000
  47.  *
  48.  * (C) Copyright 1994 - 1997 Marco van Wieringen 
  49.  */
  50. #include <linux/errno.h>
  51. #include <linux/kernel.h>
  52. #include <linux/fs.h>
  53. #include <linux/sched.h>
  54. #include <linux/types.h>
  55. #include <linux/string.h>
  56. #include <linux/fcntl.h>
  57. #include <linux/stat.h>
  58. #include <linux/tty.h>
  59. #include <linux/file.h>
  60. #include <linux/slab.h>
  61. #include <linux/smp_lock.h>
  62. #include <linux/init.h>
  63. #include <asm/uaccess.h>
  64. #define __DQUOT_VERSION__ "dquot_6.4.0"
  65. int nr_dquots, nr_free_dquots;
  66. static char *quotatypes[] = INITQFNAMES;
  67. static inline struct quota_mount_options *sb_dqopt(struct super_block *sb)
  68. {
  69. return &sb->s_dquot;
  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 to the end of 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. When dquot is
  85.  * invalidated it's completely released from memory.
  86.  *
  87.  * Dquots with a specific identity (device, type and id) are placed on
  88.  * one of the dquot_hash[] hash chains. The provides an efficient search
  89.  * mechanism to locate a specific dquot.
  90.  */
  91. /*
  92.  * Note that any operation which operates on dquot data (ie. dq_dqb) mustn't
  93.  * block while it's updating/reading it. Otherwise races would occur.
  94.  *
  95.  * Locked dquots might not be referenced in inodes - operations like
  96.  * add_dquot_space() does dqduplicate() and would complain. Currently
  97.  * dquot it locked only once in its existence - when it's being read
  98.  * to memory on first dqget() and at that time it can't be referenced
  99.  * from inode. Write operations on dquots don't hold dquot lock as they
  100.  * copy data to internal buffers before writing anyway and copying as well
  101.  * as any data update should be atomic. Also nobody can change used
  102.  * entries in dquot structure as this is done only when quota is destroyed
  103.  * and invalidate_dquots() waits for dquot to have dq_count == 0.
  104.  */
  105. static LIST_HEAD(inuse_list);
  106. static LIST_HEAD(free_dquots);
  107. static struct list_head dquot_hash[NR_DQHASH];
  108. static struct dqstats dqstats;
  109. static void dqput(struct dquot *);
  110. static struct dquot *dqduplicate(struct dquot *);
  111. static inline char is_enabled(struct quota_mount_options *dqopt, short type)
  112. {
  113. switch (type) {
  114. case USRQUOTA:
  115. return((dqopt->flags & DQUOT_USR_ENABLED) != 0);
  116. case GRPQUOTA:
  117. return((dqopt->flags & DQUOT_GRP_ENABLED) != 0);
  118. }
  119. return(0);
  120. }
  121. static inline char sb_has_quota_enabled(struct super_block *sb, short type)
  122. {
  123. return is_enabled(sb_dqopt(sb), type);
  124. }
  125. static inline int const hashfn(kdev_t dev, unsigned int id, short type)
  126. {
  127. return((HASHDEV(dev) ^ id) * (MAXQUOTAS - type)) % NR_DQHASH;
  128. }
  129. static inline void insert_dquot_hash(struct dquot *dquot)
  130. {
  131. struct list_head *head = dquot_hash + hashfn(dquot->dq_dev, dquot->dq_id, dquot->dq_type);
  132. list_add(&dquot->dq_hash, head);
  133. }
  134. static inline void remove_dquot_hash(struct dquot *dquot)
  135. {
  136. list_del(&dquot->dq_hash);
  137. INIT_LIST_HEAD(&dquot->dq_hash);
  138. }
  139. static inline struct dquot *find_dquot(unsigned int hashent, kdev_t dev, unsigned int id, short type)
  140. {
  141. struct list_head *head;
  142. struct dquot *dquot;
  143. for (head = dquot_hash[hashent].next; head != dquot_hash+hashent; head = head->next) {
  144. dquot = list_entry(head, struct dquot, dq_hash);
  145. if (dquot->dq_dev == dev && dquot->dq_id == id && dquot->dq_type == type)
  146. return dquot;
  147. }
  148. return NODQUOT;
  149. }
  150. /* Add a dquot to the head of the free list */
  151. static inline void put_dquot_head(struct dquot *dquot)
  152. {
  153. list_add(&dquot->dq_free, &free_dquots);
  154. nr_free_dquots++;
  155. }
  156. /* Add a dquot to the tail of the free list */
  157. static inline void put_dquot_last(struct dquot *dquot)
  158. {
  159. list_add(&dquot->dq_free, free_dquots.prev);
  160. nr_free_dquots++;
  161. }
  162. /* Move dquot to the head of free list (it must be already on it) */
  163. static inline void move_dquot_head(struct dquot *dquot)
  164. {
  165. list_del(&dquot->dq_free);
  166. list_add(&dquot->dq_free, &free_dquots);
  167. }
  168. static inline void remove_free_dquot(struct dquot *dquot)
  169. {
  170. if (list_empty(&dquot->dq_free))
  171. return;
  172. list_del(&dquot->dq_free);
  173. INIT_LIST_HEAD(&dquot->dq_free);
  174. nr_free_dquots--;
  175. }
  176. static inline void put_inuse(struct dquot *dquot)
  177. {
  178. /* We add to the back of inuse list so we don't have to restart
  179.  * when traversing this list and we block */
  180. list_add(&dquot->dq_inuse, inuse_list.prev);
  181. nr_dquots++;
  182. }
  183. static inline void remove_inuse(struct dquot *dquot)
  184. {
  185. nr_dquots--;
  186. list_del(&dquot->dq_inuse);
  187. }
  188. static void __wait_on_dquot(struct dquot *dquot)
  189. {
  190. DECLARE_WAITQUEUE(wait, current);
  191. add_wait_queue(&dquot->dq_wait_lock, &wait);
  192. repeat:
  193. set_current_state(TASK_UNINTERRUPTIBLE);
  194. if (dquot->dq_flags & DQ_LOCKED) {
  195. schedule();
  196. goto repeat;
  197. }
  198. remove_wait_queue(&dquot->dq_wait_lock, &wait);
  199. current->state = TASK_RUNNING;
  200. }
  201. static inline void wait_on_dquot(struct dquot *dquot)
  202. {
  203. if (dquot->dq_flags & DQ_LOCKED)
  204. __wait_on_dquot(dquot);
  205. }
  206. static inline void lock_dquot(struct dquot *dquot)
  207. {
  208. wait_on_dquot(dquot);
  209. dquot->dq_flags |= DQ_LOCKED;
  210. }
  211. static inline void unlock_dquot(struct dquot *dquot)
  212. {
  213. dquot->dq_flags &= ~DQ_LOCKED;
  214. wake_up(&dquot->dq_wait_lock);
  215. }
  216. static void __wait_dquot_unused(struct dquot *dquot)
  217. {
  218. DECLARE_WAITQUEUE(wait, current);
  219. add_wait_queue(&dquot->dq_wait_free, &wait);
  220. repeat:
  221. set_current_state(TASK_UNINTERRUPTIBLE);
  222. if (dquot->dq_count) {
  223. schedule();
  224. goto repeat;
  225. }
  226. remove_wait_queue(&dquot->dq_wait_free, &wait);
  227. current->state = TASK_RUNNING;
  228. }
  229. /*
  230.  * We don't have to be afraid of deadlocks as we never have quotas on quota files...
  231.  */
  232. static void write_dquot(struct dquot *dquot)
  233. {
  234. short type = dquot->dq_type;
  235. struct file *filp;
  236. mm_segment_t fs;
  237. loff_t offset;
  238. ssize_t ret;
  239. struct semaphore *sem = &dquot->dq_sb->s_dquot.dqio_sem;
  240. struct dqblk dqbuf;
  241. down(sem);
  242. filp = dquot->dq_sb->s_dquot.files[type];
  243. offset = dqoff(dquot->dq_id);
  244. fs = get_fs();
  245. set_fs(KERNEL_DS);
  246. /*
  247.  * Note: clear the DQ_MOD flag unconditionally,
  248.  * so we don't loop forever on failure.
  249.  */
  250. memcpy(&dqbuf, &dquot->dq_dqb, sizeof(struct dqblk));
  251. dquot->dq_flags &= ~DQ_MOD;
  252. ret = 0;
  253. if (filp)
  254. ret = filp->f_op->write(filp, (char *)&dqbuf, 
  255. sizeof(struct dqblk), &offset);
  256. if (ret != sizeof(struct dqblk))
  257. printk(KERN_WARNING "VFS: dquota write failed on dev %sn",
  258. kdevname(dquot->dq_dev));
  259. set_fs(fs);
  260. up(sem);
  261. dqstats.writes++;
  262. }
  263. static void read_dquot(struct dquot *dquot)
  264. {
  265. short type = dquot->dq_type;
  266. struct file *filp;
  267. mm_segment_t fs;
  268. loff_t offset;
  269. filp = dquot->dq_sb->s_dquot.files[type];
  270. if (filp == (struct file *)NULL)
  271. return;
  272. lock_dquot(dquot);
  273. if (!dquot->dq_sb) /* Invalidated quota? */
  274. goto out_lock;
  275. /* Now we are sure filp is valid - the dquot isn't invalidated */
  276. down(&dquot->dq_sb->s_dquot.dqio_sem);
  277. offset = dqoff(dquot->dq_id);
  278. fs = get_fs();
  279. set_fs(KERNEL_DS);
  280. filp->f_op->read(filp, (char *)&dquot->dq_dqb, sizeof(struct dqblk), &offset);
  281. up(&dquot->dq_sb->s_dquot.dqio_sem);
  282. set_fs(fs);
  283. if (dquot->dq_bhardlimit == 0 && dquot->dq_bsoftlimit == 0 &&
  284.     dquot->dq_ihardlimit == 0 && dquot->dq_isoftlimit == 0)
  285. dquot->dq_flags |= DQ_FAKE;
  286. dqstats.reads++;
  287. out_lock:
  288. unlock_dquot(dquot);
  289. }
  290. /* Invalidate all dquots on the list, wait for all users. Note that this function is called
  291.  * after quota is disabled so no new quota might be created. As we only insert to the end of
  292.  * inuse list, we don't have to restart searching... */
  293. static void invalidate_dquots(struct super_block *sb, short type)
  294. {
  295. struct dquot *dquot;
  296. struct list_head *head;
  297. restart:
  298. for (head = inuse_list.next; head != &inuse_list; head = head->next) {
  299. dquot = list_entry(head, struct dquot, dq_inuse);
  300. if (dquot->dq_sb != sb)
  301. continue;
  302. if (dquot->dq_type != type)
  303. continue;
  304. dquot->dq_flags |= DQ_INVAL;
  305. if (dquot->dq_count)
  306. /*
  307.  *  Wait for any users of quota. As we have already cleared the flags in
  308.  *  superblock and cleared all pointers from inodes we are assured
  309.  *  that there will be no new users of this quota.
  310.  */
  311. __wait_dquot_unused(dquot);
  312. /* Quota now have no users and it has been written on last dqput() */
  313. remove_dquot_hash(dquot);
  314. remove_free_dquot(dquot);
  315. remove_inuse(dquot);
  316. kmem_cache_free(dquot_cachep, dquot);
  317. goto restart;
  318. }
  319. }
  320. int sync_dquots(kdev_t dev, short type)
  321. {
  322. struct list_head *head;
  323. struct dquot *dquot;
  324. lock_kernel();
  325. restart:
  326. for (head = inuse_list.next; head != &inuse_list; head = head->next) {
  327. dquot = list_entry(head, struct dquot, dq_inuse);
  328. if (dev && dquot->dq_dev != dev)
  329. continue;
  330.                 if (type != -1 && dquot->dq_type != type)
  331. continue;
  332. if (!dquot->dq_sb) /* Invalidated? */
  333. continue;
  334. if (!(dquot->dq_flags & (DQ_MOD | DQ_LOCKED)))
  335. continue;
  336. /* Raise use count so quota won't be invalidated. We can't use dqduplicate() as it does too many tests */
  337. dquot->dq_count++;
  338. if (dquot->dq_flags & DQ_LOCKED)
  339. wait_on_dquot(dquot);
  340. if (dquot->dq_flags & DQ_MOD)
  341. write_dquot(dquot);
  342. dqput(dquot);
  343. goto restart;
  344. }
  345. dqstats.syncs++;
  346. unlock_kernel();
  347. return 0;
  348. }
  349. /* Free unused dquots from cache */
  350. static void prune_dqcache(int count)
  351. {
  352. struct list_head *head;
  353. struct dquot *dquot;
  354. head = free_dquots.prev;
  355. while (head != &free_dquots && count) {
  356. dquot = list_entry(head, struct dquot, dq_free);
  357. remove_dquot_hash(dquot);
  358. remove_free_dquot(dquot);
  359. remove_inuse(dquot);
  360. kmem_cache_free(dquot_cachep, dquot);
  361. count--;
  362. head = free_dquots.prev;
  363. }
  364. }
  365. int shrink_dqcache_memory(int priority, unsigned int gfp_mask)
  366. {
  367. lock_kernel();
  368. prune_dqcache(nr_free_dquots / (priority + 1));
  369. unlock_kernel();
  370. return kmem_cache_shrink(dquot_cachep);
  371. }
  372. /* NOTE: If you change this function please check whether dqput_blocks() works right... */
  373. static void dqput(struct dquot *dquot)
  374. {
  375. if (!dquot)
  376. return;
  377. if (!dquot->dq_count) {
  378. printk("VFS: dqput: trying to free free dquotn");
  379. printk("VFS: device %s, dquot of %s %dn",
  380. kdevname(dquot->dq_dev), quotatypes[dquot->dq_type],
  381. dquot->dq_id);
  382. return;
  383. }
  384. dqstats.drops++;
  385. we_slept:
  386. if (dquot->dq_count > 1) {
  387. /* We have more than one user... We can simply decrement use count */
  388. dquot->dq_count--;
  389. return;
  390. }
  391. if (dquot->dq_flags & DQ_MOD) {
  392. write_dquot(dquot);
  393. goto we_slept;
  394. }
  395. /* sanity check */
  396. if (!list_empty(&dquot->dq_free)) {
  397. printk(KERN_ERR "dqput: dquot already on free list??n");
  398. dquot->dq_count--; /* J.K. Just decrementing use count seems safer... */
  399. return;
  400. }
  401. dquot->dq_count--;
  402. /* If dquot is going to be invalidated invalidate_dquots() is going to free it so */
  403. if (!(dquot->dq_flags & DQ_INVAL))
  404. put_dquot_last(dquot); /* Place at end of LRU free queue */
  405. wake_up(&dquot->dq_wait_free);
  406. }
  407. static struct dquot *get_empty_dquot(void)
  408. {
  409. struct dquot *dquot;
  410. dquot = kmem_cache_alloc(dquot_cachep, SLAB_KERNEL);
  411. if(!dquot)
  412. return NODQUOT;
  413. memset((caddr_t)dquot, 0, sizeof(struct dquot));
  414. init_waitqueue_head(&dquot->dq_wait_free);
  415. init_waitqueue_head(&dquot->dq_wait_lock);
  416. INIT_LIST_HEAD(&dquot->dq_free);
  417. INIT_LIST_HEAD(&dquot->dq_inuse);
  418. INIT_LIST_HEAD(&dquot->dq_hash);
  419. dquot->dq_count = 1;
  420. /* all dquots go on the inuse_list */
  421. put_inuse(dquot);
  422. return dquot;
  423. }
  424. static struct dquot *dqget(struct super_block *sb, unsigned int id, short type)
  425. {
  426. unsigned int hashent = hashfn(sb->s_dev, id, type);
  427. struct dquot *dquot, *empty = NODQUOT;
  428. struct quota_mount_options *dqopt = sb_dqopt(sb);
  429. we_slept:
  430.         if (!is_enabled(dqopt, type)) {
  431. if (empty)
  432. dqput(empty);
  433.                 return NODQUOT;
  434. }
  435. if ((dquot = find_dquot(hashent, sb->s_dev, id, type)) == NODQUOT) {
  436. if (empty == NODQUOT) {
  437. if ((empty = get_empty_dquot()) == NODQUOT)
  438. schedule(); /* Try to wait for a moment... */
  439. goto we_slept;
  440. }
  441. dquot = empty;
  442.          dquot->dq_id = id;
  443.          dquot->dq_type = type;
  444.          dquot->dq_dev = sb->s_dev;
  445.          dquot->dq_sb = sb;
  446. /* hash it first so it can be found */
  447. insert_dquot_hash(dquot);
  448.          read_dquot(dquot);
  449. } else {
  450. if (!dquot->dq_count++)
  451. remove_free_dquot(dquot);
  452. dqstats.cache_hits++;
  453. wait_on_dquot(dquot);
  454. if (empty)
  455. dqput(empty);
  456. }
  457. if (!dquot->dq_sb) { /* Has somebody invalidated entry under us? */
  458. printk(KERN_ERR "VFS: dqget(): Quota invalidated in dqget()!n");
  459. dqput(dquot);
  460. return NODQUOT;
  461. }
  462. dquot->dq_referenced++;
  463. dqstats.lookups++;
  464. return dquot;
  465. }
  466. static struct dquot *dqduplicate(struct dquot *dquot)
  467. {
  468. if (dquot == NODQUOT)
  469. return NODQUOT;
  470. dquot->dq_count++;
  471. if (!dquot->dq_sb) {
  472. printk(KERN_ERR "VFS: dqduplicate(): Invalidated quota to be duplicated!n");
  473. dquot->dq_count--;
  474. return NODQUOT;
  475. }
  476. if (dquot->dq_flags & DQ_LOCKED)
  477. printk(KERN_ERR "VFS: dqduplicate(): Locked quota to be duplicated!n");
  478. dquot->dq_referenced++;
  479. dqstats.lookups++;
  480. return dquot;
  481. }
  482. static int dqinit_needed(struct inode *inode, short type)
  483. {
  484. int cnt;
  485. if (IS_NOQUOTA(inode))
  486. return 0;
  487. if (type != -1)
  488. return inode->i_dquot[type] == NODQUOT;
  489. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  490. if (inode->i_dquot[cnt] == NODQUOT)
  491. return 1;
  492. return 0;
  493. }
  494. static void add_dquot_ref(struct super_block *sb, short type)
  495. {
  496. struct list_head *p;
  497. if (!sb->dq_op)
  498. return; /* nothing to do */
  499. restart:
  500. file_list_lock();
  501. for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
  502. struct file *filp = list_entry(p, struct file, f_list);
  503. struct inode *inode = filp->f_dentry->d_inode;
  504. if (filp->f_mode & FMODE_WRITE && dqinit_needed(inode, type)) {
  505. struct vfsmount *mnt = mntget(filp->f_vfsmnt);
  506. struct dentry *dentry = dget(filp->f_dentry);
  507. file_list_unlock();
  508. sb->dq_op->initialize(inode, type);
  509. dput(dentry);
  510. mntput(mnt);
  511. /* As we may have blocked we had better restart... */
  512. goto restart;
  513. }
  514. }
  515. file_list_unlock();
  516. }
  517. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  518. static inline int dqput_blocks(struct dquot *dquot)
  519. {
  520. if (dquot->dq_count == 1)
  521. return 1;
  522. return 0;
  523. }
  524. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  525. int remove_inode_dquot_ref(struct inode *inode, short type, struct list_head *tofree_head)
  526. {
  527. struct dquot *dquot = inode->i_dquot[type];
  528. int cnt;
  529. inode->i_dquot[type] = NODQUOT;
  530. /* any other quota in use? */
  531. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  532. if (inode->i_dquot[cnt] != NODQUOT)
  533. goto put_it;
  534. }
  535. inode->i_flags &= ~S_QUOTA;
  536. put_it:
  537. if (dquot != NODQUOT) {
  538. if (dqput_blocks(dquot)) {
  539. if (dquot->dq_count != 1)
  540. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.n", dquot->dq_count);
  541. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  542. return 1;
  543. }
  544. else
  545. dqput(dquot);   /* We have guaranteed we won't block */
  546. }
  547. return 0;
  548. }
  549. /* Free list of dquots - called from inode.c */
  550. void put_dquot_list(struct list_head *tofree_head)
  551. {
  552. struct list_head *act_head;
  553. struct dquot *dquot;
  554. lock_kernel();
  555. act_head = tofree_head->next;
  556. /* So now we have dquots on the list... Just free them */
  557. while (act_head != tofree_head) {
  558. dquot = list_entry(act_head, struct dquot, dq_free);
  559. act_head = act_head->next;
  560. list_del(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  561. INIT_LIST_HEAD(&dquot->dq_free);
  562. dqput(dquot);
  563. }
  564. unlock_kernel();
  565. }
  566. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  567. {
  568. dquot->dq_curinodes += number;
  569. dquot->dq_flags |= DQ_MOD;
  570. }
  571. static inline void dquot_incr_blocks(struct dquot *dquot, unsigned long number)
  572. {
  573. dquot->dq_curblocks += number;
  574. dquot->dq_flags |= DQ_MOD;
  575. }
  576. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  577. {
  578. if (dquot->dq_curinodes > number)
  579. dquot->dq_curinodes -= number;
  580. else
  581. dquot->dq_curinodes = 0;
  582. if (dquot->dq_curinodes < dquot->dq_isoftlimit)
  583. dquot->dq_itime = (time_t) 0;
  584. dquot->dq_flags &= ~DQ_INODES;
  585. dquot->dq_flags |= DQ_MOD;
  586. }
  587. static inline void dquot_decr_blocks(struct dquot *dquot, unsigned long number)
  588. {
  589. if (dquot->dq_curblocks > number)
  590. dquot->dq_curblocks -= number;
  591. else
  592. dquot->dq_curblocks = 0;
  593. if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
  594. dquot->dq_btime = (time_t) 0;
  595. dquot->dq_flags &= ~DQ_BLKS;
  596. dquot->dq_flags |= DQ_MOD;
  597. }
  598. static inline int need_print_warning(struct dquot *dquot, int flag)
  599. {
  600. switch (dquot->dq_type) {
  601. case USRQUOTA:
  602. return current->fsuid == dquot->dq_id && !(dquot->dq_flags & flag);
  603. case GRPQUOTA:
  604. return in_group_p(dquot->dq_id) && !(dquot->dq_flags & flag);
  605. }
  606. return 0;
  607. }
  608. /* Values of warnings */
  609. #define NOWARN 0
  610. #define IHARDWARN 1
  611. #define ISOFTLONGWARN 2
  612. #define ISOFTWARN 3
  613. #define BHARDWARN 4
  614. #define BSOFTLONGWARN 5
  615. #define BSOFTWARN 6
  616. /* Print warning to user which exceeded quota */
  617. static void print_warning(struct dquot *dquot, const char warntype)
  618. {
  619. char *msg = NULL;
  620. int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS :
  621.   ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES : 0);
  622. if (!need_print_warning(dquot, flag))
  623. return;
  624. dquot->dq_flags |= flag;
  625. tty_write_message(current->tty, (char *)bdevname(dquot->dq_sb->s_dev));
  626. if (warntype == ISOFTWARN || warntype == BSOFTWARN)
  627. tty_write_message(current->tty, ": warning, ");
  628. else
  629. tty_write_message(current->tty, ": write failed, ");
  630. tty_write_message(current->tty, quotatypes[dquot->dq_type]);
  631. switch (warntype) {
  632. case IHARDWARN:
  633. msg = " file limit reached.n";
  634. break;
  635. case ISOFTLONGWARN:
  636. msg = " file quota exceeded too long.n";
  637. break;
  638. case ISOFTWARN:
  639. msg = " file quota exceeded.n";
  640. break;
  641. case BHARDWARN:
  642. msg = " block limit reached.n";
  643. break;
  644. case BSOFTLONGWARN:
  645. msg = " block quota exceeded too long.n";
  646. break;
  647. case BSOFTWARN:
  648. msg = " block quota exceeded.n";
  649. break;
  650. }
  651. tty_write_message(current->tty, msg);
  652. }
  653. static inline void flush_warnings(struct dquot **dquots, char *warntype)
  654. {
  655. int i;
  656. for (i = 0; i < MAXQUOTAS; i++)
  657. if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
  658. print_warning(dquots[i], warntype[i]);
  659. }
  660. static inline char ignore_hardlimit(struct dquot *dquot)
  661. {
  662. return capable(CAP_SYS_RESOURCE) && !dquot->dq_sb->s_dquot.rsquash[dquot->dq_type];
  663. }
  664. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  665. {
  666. *warntype = NOWARN;
  667. if (inodes <= 0 || dquot->dq_flags & DQ_FAKE)
  668. return QUOTA_OK;
  669. if (dquot->dq_ihardlimit &&
  670.    (dquot->dq_curinodes + inodes) > dquot->dq_ihardlimit &&
  671.             !ignore_hardlimit(dquot)) {
  672. *warntype = IHARDWARN;
  673. return NO_QUOTA;
  674. }
  675. if (dquot->dq_isoftlimit &&
  676.    (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
  677.     dquot->dq_itime && CURRENT_TIME >= dquot->dq_itime &&
  678.             !ignore_hardlimit(dquot)) {
  679. *warntype = ISOFTLONGWARN;
  680. return NO_QUOTA;
  681. }
  682. if (dquot->dq_isoftlimit &&
  683.    (dquot->dq_curinodes + inodes) > dquot->dq_isoftlimit &&
  684.     dquot->dq_itime == 0) {
  685. *warntype = ISOFTWARN;
  686. dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[dquot->dq_type];
  687. }
  688. return QUOTA_OK;
  689. }
  690. static int check_bdq(struct dquot *dquot, ulong blocks, char prealloc, char *warntype)
  691. {
  692. *warntype = 0;
  693. if (blocks <= 0 || dquot->dq_flags & DQ_FAKE)
  694. return QUOTA_OK;
  695. if (dquot->dq_bhardlimit &&
  696.    (dquot->dq_curblocks + blocks) > dquot->dq_bhardlimit &&
  697.             !ignore_hardlimit(dquot)) {
  698. if (!prealloc)
  699. *warntype = BHARDWARN;
  700. return NO_QUOTA;
  701. }
  702. if (dquot->dq_bsoftlimit &&
  703.    (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
  704.     dquot->dq_btime && CURRENT_TIME >= dquot->dq_btime &&
  705.             !ignore_hardlimit(dquot)) {
  706. if (!prealloc)
  707. *warntype = BSOFTLONGWARN;
  708. return NO_QUOTA;
  709. }
  710. if (dquot->dq_bsoftlimit &&
  711.    (dquot->dq_curblocks + blocks) > dquot->dq_bsoftlimit &&
  712.     dquot->dq_btime == 0) {
  713. if (!prealloc) {
  714. *warntype = BSOFTWARN;
  715. dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[dquot->dq_type];
  716. }
  717. else
  718. /*
  719.  * We don't allow preallocation to exceed softlimit so exceeding will
  720.  * be always printed
  721.  */
  722. return NO_QUOTA;
  723. }
  724. return QUOTA_OK;
  725. }
  726. /*
  727.  * Initialize a dquot-struct with new quota info. This is used by the
  728.  * system call interface functions.
  729.  */ 
  730. static int set_dqblk(struct super_block *sb, int id, short type, int flags, struct dqblk *dqblk)
  731. {
  732. struct dquot *dquot;
  733. int error = -EFAULT;
  734. struct dqblk dq_dqblk;
  735. if (copy_from_user(&dq_dqblk, dqblk, sizeof(struct dqblk)))
  736. return error;
  737. if (sb && (dquot = dqget(sb, id, type)) != NODQUOT) {
  738. /* We can't block while changing quota structure... */
  739. if (id > 0 && ((flags & SET_QUOTA) || (flags & SET_QLIMIT))) {
  740. dquot->dq_bhardlimit = dq_dqblk.dqb_bhardlimit;
  741. dquot->dq_bsoftlimit = dq_dqblk.dqb_bsoftlimit;
  742. dquot->dq_ihardlimit = dq_dqblk.dqb_ihardlimit;
  743. dquot->dq_isoftlimit = dq_dqblk.dqb_isoftlimit;
  744. }
  745. if ((flags & SET_QUOTA) || (flags & SET_USE)) {
  746. if (dquot->dq_isoftlimit &&
  747.     dquot->dq_curinodes < dquot->dq_isoftlimit &&
  748.     dq_dqblk.dqb_curinodes >= dquot->dq_isoftlimit)
  749. dquot->dq_itime = CURRENT_TIME + dquot->dq_sb->s_dquot.inode_expire[type];
  750. dquot->dq_curinodes = dq_dqblk.dqb_curinodes;
  751. if (dquot->dq_curinodes < dquot->dq_isoftlimit)
  752. dquot->dq_flags &= ~DQ_INODES;
  753. if (dquot->dq_bsoftlimit &&
  754.     dquot->dq_curblocks < dquot->dq_bsoftlimit &&
  755.     dq_dqblk.dqb_curblocks >= dquot->dq_bsoftlimit)
  756. dquot->dq_btime = CURRENT_TIME + dquot->dq_sb->s_dquot.block_expire[type];
  757. dquot->dq_curblocks = dq_dqblk.dqb_curblocks;
  758. if (dquot->dq_curblocks < dquot->dq_bsoftlimit)
  759. dquot->dq_flags &= ~DQ_BLKS;
  760. }
  761. if (id == 0) {
  762. dquot->dq_sb->s_dquot.block_expire[type] = dquot->dq_btime = dq_dqblk.dqb_btime;
  763. dquot->dq_sb->s_dquot.inode_expire[type] = dquot->dq_itime = dq_dqblk.dqb_itime;
  764. }
  765. if (dq_dqblk.dqb_bhardlimit == 0 && dq_dqblk.dqb_bsoftlimit == 0 &&
  766.     dq_dqblk.dqb_ihardlimit == 0 && dq_dqblk.dqb_isoftlimit == 0)
  767. dquot->dq_flags |= DQ_FAKE;
  768. else
  769. dquot->dq_flags &= ~DQ_FAKE;
  770. dquot->dq_flags |= DQ_MOD;
  771. dqput(dquot);
  772. }
  773. return 0;
  774. }
  775. static int get_quota(struct super_block *sb, int id, short type, struct dqblk *dqblk)
  776. {
  777. struct dquot *dquot;
  778. struct dqblk data;
  779. int error = -ESRCH;
  780. if (!sb || !sb_has_quota_enabled(sb, type))
  781. goto out;
  782. dquot = dqget(sb, id, type);
  783. if (dquot == NODQUOT)
  784. goto out;
  785. memcpy(&data, &dquot->dq_dqb, sizeof(struct dqblk));        /* We copy data to preserve them from changing */
  786. dqput(dquot);
  787. error = -EFAULT;
  788. if (dqblk && !copy_to_user(dqblk, &data, sizeof(struct dqblk)))
  789. error = 0;
  790. out:
  791. return error;
  792. }
  793. static int get_stats(caddr_t addr)
  794. {
  795. int error = -EFAULT;
  796. struct dqstats stats;
  797. dqstats.allocated_dquots = nr_dquots;
  798. dqstats.free_dquots = nr_free_dquots;
  799. /* make a copy, in case we page-fault in user space */
  800. memcpy(&stats, &dqstats, sizeof(struct dqstats));
  801. if (!copy_to_user(addr, &stats, sizeof(struct dqstats)))
  802. error = 0;
  803. return error;
  804. }
  805. static int quota_root_squash(struct super_block *sb, short type, int *addr)
  806. {
  807. int new_value, error;
  808. if (!sb)
  809. return(-ENODEV);
  810. error = -EFAULT;
  811. if (!copy_from_user(&new_value, addr, sizeof(int))) {
  812. sb_dqopt(sb)->rsquash[type] = new_value;
  813. error = 0;
  814. }
  815. return error;
  816. }
  817. #if 0 /* We are not going to support filesystems without i_blocks... */
  818. /*
  819.  * This is a simple algorithm that calculates the size of a file in blocks.
  820.  * This is only used on filesystems that do not have an i_blocks count.
  821.  */
  822. static u_long isize_to_blocks(loff_t isize, size_t blksize_bits)
  823. {
  824. u_long blocks;
  825. u_long indirect;
  826. if (!blksize_bits)
  827. blksize_bits = BLOCK_SIZE_BITS;
  828. blocks = (isize >> blksize_bits) + ((isize & ~((1 << blksize_bits)-1)) ? 1 : 0);
  829. if (blocks > 10) {
  830. indirect = ((blocks - 11) >> 8) + 1; /* single indirect blocks */
  831. if (blocks > (10 + 256)) {
  832. indirect += ((blocks - 267) >> 16) + 1; /* double indirect blocks */
  833. if (blocks > (10 + 256 + (256 << 8)))
  834. indirect++; /* triple indirect blocks */
  835. }
  836. blocks += indirect;
  837. }
  838. return blocks;
  839. }
  840. #endif
  841. /*
  842.  * Externally referenced functions through dquot_operations in inode.
  843.  *
  844.  * Note: this is a blocking operation.
  845.  */
  846. void dquot_initialize(struct inode *inode, short type)
  847. {
  848. struct dquot *dquot[MAXQUOTAS];
  849. unsigned int id = 0;
  850. short cnt;
  851. if (IS_NOQUOTA(inode))
  852. return;
  853. /* Build list of quotas to initialize... We can block here */
  854. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  855. dquot[cnt] = NODQUOT;
  856. if (type != -1 && cnt != type)
  857. continue;
  858. if (!sb_has_quota_enabled(inode->i_sb, cnt))
  859. continue;
  860. if (inode->i_dquot[cnt] == NODQUOT) {
  861. switch (cnt) {
  862. case USRQUOTA:
  863. id = inode->i_uid;
  864. break;
  865. case GRPQUOTA:
  866. id = inode->i_gid;
  867. break;
  868. }
  869. dquot[cnt] = dqget(inode->i_sb, id, cnt);
  870. }
  871. }
  872. /* NOBLOCK START: Here we shouldn't block */
  873. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  874. if (dquot[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt) || inode->i_dquot[cnt] != NODQUOT)
  875. continue;
  876. inode->i_dquot[cnt] = dquot[cnt];
  877. dquot[cnt] = NODQUOT;
  878. inode->i_flags |= S_QUOTA;
  879. }
  880. /* NOBLOCK END */
  881. /* Put quotas which we didn't use */
  882. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  883. if (dquot[cnt] != NODQUOT)
  884. dqput(dquot[cnt]);
  885. }
  886. /*
  887.  * Release all quota for the specified inode.
  888.  *
  889.  * Note: this is a blocking operation.
  890.  */
  891. void dquot_drop(struct inode *inode)
  892. {
  893. struct dquot *dquot;
  894. short cnt;
  895. inode->i_flags &= ~S_QUOTA;
  896. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  897. if (inode->i_dquot[cnt] == NODQUOT)
  898. continue;
  899. dquot = inode->i_dquot[cnt];
  900. inode->i_dquot[cnt] = NODQUOT;
  901. dqput(dquot);
  902. }
  903. }
  904. /*
  905.  * This operation can block, but only after everything is updated
  906.  */
  907. int dquot_alloc_block(struct inode *inode, unsigned long number, char warn)
  908. {
  909. int cnt, ret = NO_QUOTA;
  910. struct dquot *dquot[MAXQUOTAS];
  911. char warntype[MAXQUOTAS];
  912. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  913. dquot[cnt] = NODQUOT;
  914. warntype[cnt] = NOWARN;
  915. }
  916. /* NOBLOCK Start */
  917. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  918. dquot[cnt] = dqduplicate(inode->i_dquot[cnt]);
  919. if (dquot[cnt] == NODQUOT)
  920. continue;
  921. if (check_bdq(dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  922. goto warn_put_all;
  923. }
  924. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  925. if (dquot[cnt] == NODQUOT)
  926. continue;
  927. dquot_incr_blocks(dquot[cnt], number);
  928. }
  929. inode->i_blocks += number << (BLOCK_SIZE_BITS - 9);
  930. /* NOBLOCK End */
  931. ret = QUOTA_OK;
  932. warn_put_all:
  933. flush_warnings(dquot, warntype);
  934. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  935. if (dquot[cnt] != NODQUOT)
  936. dqput(dquot[cnt]);
  937. return ret;
  938. }
  939. /*
  940.  * This operation can block, but only after everything is updated
  941.  */
  942. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  943. {
  944. int cnt, ret = NO_QUOTA;
  945. struct dquot *dquot[MAXQUOTAS];
  946. char warntype[MAXQUOTAS];
  947. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  948. dquot[cnt] = NODQUOT;
  949. warntype[cnt] = NOWARN;
  950. }
  951. /* NOBLOCK Start */
  952. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  953. dquot[cnt] = dqduplicate(inode -> i_dquot[cnt]);
  954. if (dquot[cnt] == NODQUOT)
  955. continue;
  956. if (check_idq(dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  957. goto warn_put_all;
  958. }
  959. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  960. if (dquot[cnt] == NODQUOT)
  961. continue;
  962. dquot_incr_inodes(dquot[cnt], number);
  963. }
  964. /* NOBLOCK End */
  965. ret = QUOTA_OK;
  966. warn_put_all:
  967. flush_warnings(dquot, warntype);
  968. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  969. if (dquot[cnt] != NODQUOT)
  970. dqput(dquot[cnt]);
  971. return ret;
  972. }
  973. /*
  974.  * This is a non-blocking operation.
  975.  */
  976. void dquot_free_block(struct inode *inode, unsigned long number)
  977. {
  978. unsigned short cnt;
  979. struct dquot *dquot;
  980. /* NOBLOCK Start */
  981. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  982. dquot = dqduplicate(inode->i_dquot[cnt]);
  983. if (dquot == NODQUOT)
  984. continue;
  985. dquot_decr_blocks(dquot, number);
  986. dqput(dquot);
  987. }
  988. inode->i_blocks -= number << (BLOCK_SIZE_BITS - 9);
  989. /* NOBLOCK End */
  990. }
  991. /*
  992.  * This is a non-blocking operation.
  993.  */
  994. void dquot_free_inode(const struct inode *inode, unsigned long number)
  995. {
  996. unsigned short cnt;
  997. struct dquot *dquot;
  998. /* NOBLOCK Start */
  999. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1000. dquot = dqduplicate(inode->i_dquot[cnt]);
  1001. if (dquot == NODQUOT)
  1002. continue;
  1003. dquot_decr_inodes(dquot, number);
  1004. dqput(dquot);
  1005. }
  1006. /* NOBLOCK End */
  1007. }
  1008. /*
  1009.  * Transfer the number of inode and blocks from one diskquota to an other.
  1010.  *
  1011.  * This operation can block, but only after everything is updated
  1012.  */
  1013. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1014. {
  1015. unsigned long blocks;
  1016. struct dquot *transfer_from[MAXQUOTAS];
  1017. struct dquot *transfer_to[MAXQUOTAS];
  1018. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1019.     chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1020. char warntype[MAXQUOTAS];
  1021. /* Clear the arrays */
  1022. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1023. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1024. warntype[cnt] = NOWARN;
  1025. }
  1026. /* First build the transfer_to list - here we can block on reading of dquots... */
  1027. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1028. if (!sb_has_quota_enabled(inode->i_sb, cnt))
  1029. continue;
  1030. switch (cnt) {
  1031. case USRQUOTA:
  1032. if (!chuid)
  1033. continue;
  1034. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1035. break;
  1036. case GRPQUOTA:
  1037. if (!chgid)
  1038. continue;
  1039. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1040. break;
  1041. }
  1042. }
  1043. /* NOBLOCK START: From now on we shouldn't block */
  1044. blocks = (inode->i_blocks >> 1);
  1045. /* Build the transfer_from list and check the limits */
  1046. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1047. /* The second test can fail when quotaoff is in progress... */
  1048. if (transfer_to[cnt] == NODQUOT || !sb_has_quota_enabled(inode->i_sb, cnt))
  1049. continue;
  1050. transfer_from[cnt] = dqduplicate(inode->i_dquot[cnt]);
  1051. if (transfer_from[cnt] == NODQUOT) /* Can happen on quotafiles (quota isn't initialized on them)... */
  1052. continue;
  1053. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1054.     check_bdq(transfer_to[cnt], blocks, 0, warntype+cnt) == NO_QUOTA)
  1055. goto warn_put_all;
  1056. }
  1057. /*
  1058.  * Finally perform the needed transfer from transfer_from to transfer_to
  1059.  */
  1060. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1061. /*
  1062.  * Skip changes for same uid or gid or for non-existing quota-type.
  1063.  */
  1064. if (transfer_from[cnt] == NODQUOT || transfer_to[cnt] == NODQUOT)
  1065. continue;
  1066. dquot_decr_inodes(transfer_from[cnt], 1);
  1067. dquot_decr_blocks(transfer_from[cnt], blocks);
  1068. dquot_incr_inodes(transfer_to[cnt], 1);
  1069. dquot_incr_blocks(transfer_to[cnt], blocks);
  1070. if (inode->i_dquot[cnt] == NODQUOT)
  1071. BUG();
  1072. inode->i_dquot[cnt] = transfer_to[cnt];
  1073. /*
  1074.  * We've got to release transfer_from[] twice - once for dquot_transfer() and
  1075.  * once for inode. We don't want to release transfer_to[] as it's now placed in inode
  1076.  */
  1077. transfer_to[cnt] = transfer_from[cnt];
  1078. }
  1079. /* NOBLOCK END. From now on we can block as we wish */
  1080. ret = QUOTA_OK;
  1081. warn_put_all:
  1082. flush_warnings(transfer_to, warntype);
  1083. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1084. if (transfer_to[cnt] != NODQUOT)
  1085. dqput(transfer_to[cnt]);
  1086. if (transfer_from[cnt] != NODQUOT)
  1087. dqput(transfer_from[cnt]);
  1088. }
  1089. return ret;
  1090. }
  1091. static int __init dquot_init(void)
  1092. {
  1093. int i;
  1094. for (i = 0; i < NR_DQHASH; i++)
  1095. INIT_LIST_HEAD(dquot_hash + i);
  1096. printk(KERN_NOTICE "VFS: Diskquotas version %s initializedn", __DQUOT_VERSION__);
  1097. return 0;
  1098. }
  1099. __initcall(dquot_init);
  1100. /*
  1101.  * Definitions of diskquota operations.
  1102.  */
  1103. struct dquot_operations dquot_operations = {
  1104. dquot_initialize, /* mandatory */
  1105. dquot_drop, /* mandatory */
  1106. dquot_alloc_block,
  1107. dquot_alloc_inode,
  1108. dquot_free_block,
  1109. dquot_free_inode,
  1110. dquot_transfer
  1111. };
  1112. static inline void set_enable_flags(struct quota_mount_options *dqopt, short type)
  1113. {
  1114. switch (type) {
  1115. case USRQUOTA:
  1116. dqopt->flags |= DQUOT_USR_ENABLED;
  1117. break;
  1118. case GRPQUOTA:
  1119. dqopt->flags |= DQUOT_GRP_ENABLED;
  1120. break;
  1121. }
  1122. }
  1123. static inline void reset_enable_flags(struct quota_mount_options *dqopt, short type)
  1124. {
  1125. switch (type) {
  1126. case USRQUOTA:
  1127. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1128. break;
  1129. case GRPQUOTA:
  1130. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1131. break;
  1132. }
  1133. }
  1134. /* Function in inode.c - remove pointers to dquots in icache */
  1135. extern void remove_dquot_ref(struct super_block *, short);
  1136. /*
  1137.  * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1138.  */
  1139. int quota_off(struct super_block *sb, short type)
  1140. {
  1141. struct file *filp;
  1142. short cnt;
  1143. struct quota_mount_options *dqopt = sb_dqopt(sb);
  1144. lock_kernel();
  1145. if (!sb)
  1146. goto out;
  1147. /* We need to serialize quota_off() for device */
  1148. down(&dqopt->dqoff_sem);
  1149. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1150. if (type != -1 && cnt != type)
  1151. continue;
  1152. if (!is_enabled(dqopt, cnt))
  1153. continue;
  1154. reset_enable_flags(dqopt, cnt);
  1155. /* Note: these are blocking operations */
  1156. remove_dquot_ref(sb, cnt);
  1157. invalidate_dquots(sb, cnt);
  1158. filp = dqopt->files[cnt];
  1159. dqopt->files[cnt] = (struct file *)NULL;
  1160. dqopt->inode_expire[cnt] = 0;
  1161. dqopt->block_expire[cnt] = 0;
  1162. fput(filp);
  1163. }
  1164. up(&dqopt->dqoff_sem);
  1165. out:
  1166. unlock_kernel();
  1167. return 0;
  1168. }
  1169. static inline int check_quotafile_size(loff_t size)
  1170. {
  1171. ulong blocks = size >> BLOCK_SIZE_BITS;
  1172. size_t off = size & (BLOCK_SIZE - 1);
  1173. return !(((blocks % sizeof(struct dqblk)) * BLOCK_SIZE + off % sizeof(struct dqblk)) % sizeof(struct dqblk));
  1174. }
  1175. static int quota_on(struct super_block *sb, short type, char *path)
  1176. {
  1177. struct file *f;
  1178. struct inode *inode;
  1179. struct dquot *dquot;
  1180. struct quota_mount_options *dqopt = sb_dqopt(sb);
  1181. char *tmp;
  1182. int error;
  1183. if (is_enabled(dqopt, type))
  1184. return -EBUSY;
  1185. down(&dqopt->dqoff_sem);
  1186. tmp = getname(path);
  1187. error = PTR_ERR(tmp);
  1188. if (IS_ERR(tmp))
  1189. goto out_lock;
  1190. f = filp_open(tmp, O_RDWR, 0600);
  1191. putname(tmp);
  1192. error = PTR_ERR(f);
  1193. if (IS_ERR(f))
  1194. goto out_lock;
  1195. error = -EIO;
  1196. if (!f->f_op || !f->f_op->read || !f->f_op->write)
  1197. goto out_f;
  1198. inode = f->f_dentry->d_inode;
  1199. error = -EACCES;
  1200. if (!S_ISREG(inode->i_mode))
  1201. goto out_f;
  1202. error = -EINVAL;
  1203. if (inode->i_size == 0 || !check_quotafile_size(inode->i_size))
  1204. goto out_f;
  1205. /* We don't want quota on quota files */
  1206. dquot_drop(inode);
  1207. inode->i_flags |= S_NOQUOTA;
  1208. dqopt->files[type] = f;
  1209. sb->dq_op = &dquot_operations;
  1210. set_enable_flags(dqopt, type);
  1211. dquot = dqget(sb, 0, type);
  1212. dqopt->inode_expire[type] = (dquot != NODQUOT) ? dquot->dq_itime : MAX_IQ_TIME;
  1213. dqopt->block_expire[type] = (dquot != NODQUOT) ? dquot->dq_btime : MAX_DQ_TIME;
  1214. dqput(dquot);
  1215. add_dquot_ref(sb, type);
  1216. up(&dqopt->dqoff_sem);
  1217. return 0;
  1218. out_f:
  1219. filp_close(f, NULL);
  1220. out_lock:
  1221. up(&dqopt->dqoff_sem);
  1222. return error; 
  1223. }
  1224. /*
  1225.  * This is the system call interface. This communicates with
  1226.  * the user-level programs. Currently this only supports diskquota
  1227.  * calls. Maybe we need to add the process quotas etc. in the future,
  1228.  * but we probably should use rlimits for that.
  1229.  */
  1230. asmlinkage long sys_quotactl(int cmd, const char *special, int id, caddr_t addr)
  1231. {
  1232. int cmds = 0, type = 0, flags = 0;
  1233. kdev_t dev;
  1234. struct super_block *sb = NULL;
  1235. int ret = -EINVAL;
  1236. lock_kernel();
  1237. cmds = cmd >> SUBCMDSHIFT;
  1238. type = cmd & SUBCMDMASK;
  1239. if ((u_int) type >= MAXQUOTAS)
  1240. goto out;
  1241. if (id & ~0xFFFF)
  1242. goto out;
  1243. ret = -EPERM;
  1244. switch (cmds) {
  1245. case Q_SYNC:
  1246. case Q_GETSTATS:
  1247. break;
  1248. case Q_GETQUOTA:
  1249. if (((type == USRQUOTA && current->euid != id) ||
  1250.      (type == GRPQUOTA && !in_egroup_p(id))) &&
  1251.     !capable(CAP_SYS_ADMIN))
  1252. goto out;
  1253. break;
  1254. default:
  1255. if (!capable(CAP_SYS_ADMIN))
  1256. goto out;
  1257. }
  1258. ret = -EINVAL;
  1259. dev = NODEV;
  1260. if (special != NULL || (cmds != Q_SYNC && cmds != Q_GETSTATS)) {
  1261. mode_t mode;
  1262. struct nameidata nd;
  1263. ret = user_path_walk(special, &nd);
  1264. if (ret)
  1265. goto out;
  1266. dev = nd.dentry->d_inode->i_rdev;
  1267. mode = nd.dentry->d_inode->i_mode;
  1268. path_release(&nd);
  1269. ret = -ENOTBLK;
  1270. if (!S_ISBLK(mode))
  1271. goto out;
  1272. ret = -ENODEV;
  1273. sb = get_super(dev);
  1274. if (!sb)
  1275. goto out;
  1276. }
  1277. ret = -EINVAL;
  1278. switch (cmds) {
  1279. case Q_QUOTAON:
  1280. ret = quota_on(sb, type, (char *) addr);
  1281. goto out;
  1282. case Q_QUOTAOFF:
  1283. ret = quota_off(sb, type);
  1284. goto out;
  1285. case Q_GETQUOTA:
  1286. ret = get_quota(sb, id, type, (struct dqblk *) addr);
  1287. goto out;
  1288. case Q_SETQUOTA:
  1289. flags |= SET_QUOTA;
  1290. break;
  1291. case Q_SETUSE:
  1292. flags |= SET_USE;
  1293. break;
  1294. case Q_SETQLIM:
  1295. flags |= SET_QLIMIT;
  1296. break;
  1297. case Q_SYNC:
  1298. ret = sync_dquots(dev, type);
  1299. goto out;
  1300. case Q_GETSTATS:
  1301. ret = get_stats(addr);
  1302. goto out;
  1303. case Q_RSQUASH:
  1304. ret = quota_root_squash(sb, type, (int *) addr);
  1305. goto out;
  1306. default:
  1307. goto out;
  1308. }
  1309. ret = -NODEV;
  1310. if (sb && sb_has_quota_enabled(sb, type))
  1311. ret = set_dqblk(sb, id, type, flags, (struct dqblk *) addr);
  1312. out:
  1313. if (sb)
  1314. drop_super(sb);
  1315. unlock_kernel();
  1316. return ret;
  1317. }