quota.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 1982, 1986 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Robert Elz at The University of Melbourne.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * Version: $Id: quota.h,v 2.0 1996/11/17 16:48:14 mvw Exp mvw $
  33. */
  34. #ifndef _LINUX_QUOTA_
  35. #define _LINUX_QUOTA_
  36. #include <linux/errno.h>
  37. #include <linux/types.h>
  38. #define __DQUOT_VERSION__ "dquot_6.5.1"
  39. #define __DQUOT_NUM_VERSION__ 6*10000+5*100+1
  40. typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
  41. typedef __u64 qsize_t; /* Type in which we store sizes */
  42. /* Size of blocks in which are counted size limits */
  43. #define QUOTABLOCK_BITS 10
  44. #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
  45. /* Conversion routines from and to quota blocks */
  46. #define qb2kb(x) ((x) << (QUOTABLOCK_BITS-10))
  47. #define kb2qb(x) ((x) >> (QUOTABLOCK_BITS-10))
  48. #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
  49. #define MAXQUOTAS 2
  50. #define USRQUOTA 0 /* element used for user quotas */
  51. #define GRPQUOTA 1 /* element used for group quotas */
  52. /*
  53. * Definitions for the default names of the quotas files.
  54. */
  55. #define INITQFNAMES { \
  56. "user", /* USRQUOTA */ \
  57. "group", /* GRPQUOTA */ \
  58. "undefined", \
  59. };
  60. /*
  61. * Command definitions for the 'quotactl' system call.
  62. * The commands are broken into a main command defined below
  63. * and a subcommand that is used to convey the type of
  64. * quota that is being manipulated (see above).
  65. */
  66. #define SUBCMDMASK 0x00ff
  67. #define SUBCMDSHIFT 8
  68. #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
  69. #define Q_SYNC 0x800001 /* sync disk copy of a filesystems quotas */
  70. #define Q_QUOTAON 0x800002 /* turn quotas on */
  71. #define Q_QUOTAOFF 0x800003 /* turn quotas off */
  72. #define Q_GETFMT 0x800004 /* get quota format used on given filesystem */
  73. #define Q_GETINFO 0x800005 /* get information about quota files */
  74. #define Q_SETINFO 0x800006 /* set information about quota files */
  75. #define Q_GETQUOTA 0x800007 /* get user quota structure */
  76. #define Q_SETQUOTA 0x800008 /* set user quota structure */
  77. /*
  78. * Quota structure used for communication with userspace via quotactl
  79. * Following flags are used to specify which fields are valid
  80. */
  81. #define QIF_BLIMITS 1
  82. #define QIF_SPACE 2
  83. #define QIF_ILIMITS 4
  84. #define QIF_INODES 8
  85. #define QIF_BTIME 16
  86. #define QIF_ITIME 32
  87. #define QIF_LIMITS (QIF_BLIMITS | QIF_ILIMITS)
  88. #define QIF_USAGE (QIF_SPACE | QIF_INODES)
  89. #define QIF_TIMES (QIF_BTIME | QIF_ITIME)
  90. #define QIF_ALL (QIF_LIMITS | QIF_USAGE | QIF_TIMES)
  91. struct if_dqblk {
  92. __u64 dqb_bhardlimit;
  93. __u64 dqb_bsoftlimit;
  94. __u64 dqb_curspace;
  95. __u64 dqb_ihardlimit;
  96. __u64 dqb_isoftlimit;
  97. __u64 dqb_curinodes;
  98. __u64 dqb_btime;
  99. __u64 dqb_itime;
  100. __u32 dqb_valid;
  101. };
  102. /*
  103. * Structure used for setting quota information about file via quotactl
  104. * Following flags are used to specify which fields are valid
  105. */
  106. #define IIF_BGRACE 1
  107. #define IIF_IGRACE 2
  108. #define IIF_FLAGS 4
  109. #define IIF_ALL (IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
  110. struct if_dqinfo {
  111. __u64 dqi_bgrace;
  112. __u64 dqi_igrace;
  113. __u32 dqi_flags;
  114. __u32 dqi_valid;
  115. };
  116. /*
  117. * Definitions for quota netlink interface
  118. */
  119. #define QUOTA_NL_NOWARN 0
  120. #define QUOTA_NL_IHARDWARN 1 /* Inode hardlimit reached */
  121. #define QUOTA_NL_ISOFTLONGWARN 2 /* Inode grace time expired */
  122. #define QUOTA_NL_ISOFTWARN 3 /* Inode softlimit reached */
  123. #define QUOTA_NL_BHARDWARN 4 /* Block hardlimit reached */
  124. #define QUOTA_NL_BSOFTLONGWARN 5 /* Block grace time expired */
  125. #define QUOTA_NL_BSOFTWARN 6 /* Block softlimit reached */
  126. enum {
  127. QUOTA_NL_C_UNSPEC,
  128. QUOTA_NL_C_WARNING,
  129. __QUOTA_NL_C_MAX,
  130. };
  131. #define QUOTA_NL_C_MAX (__QUOTA_NL_C_MAX - 1)
  132. enum {
  133. QUOTA_NL_A_UNSPEC,
  134. QUOTA_NL_A_QTYPE,
  135. QUOTA_NL_A_EXCESS_ID,
  136. QUOTA_NL_A_WARNING,
  137. QUOTA_NL_A_DEV_MAJOR,
  138. QUOTA_NL_A_DEV_MINOR,
  139. QUOTA_NL_A_CAUSED_ID,
  140. __QUOTA_NL_A_MAX,
  141. };
  142. #define QUOTA_NL_A_MAX (__QUOTA_NL_A_MAX - 1)
  143. #ifdef __KERNEL__
  144. #include <linux/list.h>
  145. #include <linux/mutex.h>
  146. #include <linux/rwsem.h>
  147. #include <linux/spinlock.h>
  148. #include <linux/wait.h>
  149. #include <linux/dqblk_xfs.h>
  150. #include <linux/dqblk_v1.h>
  151. #include <linux/dqblk_v2.h>
  152. #include <asm/atomic.h>
  153. extern spinlock_t dq_data_lock;
  154. /* Maximal numbers of writes for quota operation (insert/delete/update)
  155. * (over VFS all formats) */
  156. #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
  157. #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
  158. #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
  159. #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
  160. /*
  161. * Data for one user/group kept in memory
  162. */
  163. struct mem_dqblk {
  164. __u32 dqb_bhardlimit; /* absolute limit on disk blks alloc */
  165. __u32 dqb_bsoftlimit; /* preferred limit on disk blks */
  166. qsize_t dqb_curspace; /* current used space */
  167. __u32 dqb_ihardlimit; /* absolute limit on allocated inodes */
  168. __u32 dqb_isoftlimit; /* preferred inode limit */
  169. __u32 dqb_curinodes; /* current # allocated inodes */
  170. time_t dqb_btime; /* time limit for excessive disk use */
  171. time_t dqb_itime; /* time limit for excessive inode use */
  172. };
  173. /*
  174. * Data for one quotafile kept in memory
  175. */
  176. struct quota_format_type;
  177. struct mem_dqinfo {
  178. struct quota_format_type *dqi_format;
  179. int dqi_fmt_id; /* Id of the dqi_format - used when turning
  180. * quotas on after remount RW */
  181. struct list_head dqi_dirty_list; /* List of dirty dquots */
  182. unsigned long dqi_flags;
  183. unsigned int dqi_bgrace;
  184. unsigned int dqi_igrace;
  185. qsize_t dqi_maxblimit;
  186. qsize_t dqi_maxilimit;
  187. union {
  188. struct v1_mem_dqinfo v1_i;
  189. struct v2_mem_dqinfo v2_i;
  190. } u;
  191. };
  192. struct super_block;
  193. #define DQF_MASK 0xffff /* Mask for format specific flags */
  194. #define DQF_INFO_DIRTY_B 16
  195. #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */
  196. extern void mark_info_dirty(struct super_block *sb, int type);
  197. #define info_dirty(info) test_bit(DQF_INFO_DIRTY_B, &(info)->dqi_flags)
  198. #define sb_dqopt(sb) (&(sb)->s_dquot)
  199. #define sb_dqinfo(sb, type) (sb_dqopt(sb)->info+(type))
  200. struct dqstats {
  201. int lookups;
  202. int drops;
  203. int reads;
  204. int writes;
  205. int cache_hits;
  206. int allocated_dquots;
  207. int free_dquots;
  208. int syncs;
  209. };
  210. extern struct dqstats dqstats;
  211. #define DQ_MOD_B 0 /* dquot modified since read */
  212. #define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */
  213. #define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */
  214. #define DQ_FAKE_B 3 /* no limits only usage */
  215. #define DQ_READ_B 4 /* dquot was read into memory */
  216. #define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */
  217. struct dquot {
  218. struct hlist_node dq_hash; /* Hash list in memory */
  219. struct list_head dq_inuse; /* List of all quotas */
  220. struct list_head dq_free; /* Free list element */
  221. struct list_head dq_dirty; /* List of dirty dquots */
  222. struct mutex dq_lock; /* dquot IO lock */
  223. atomic_t dq_count; /* Use count */
  224. wait_queue_head_t dq_wait_unused; /* Wait queue for dquot to become unused */
  225. struct super_block *dq_sb; /* superblock this applies to */
  226. unsigned int dq_id; /* ID this applies to (uid, gid) */
  227. loff_t dq_off; /* Offset of dquot on disk */
  228. unsigned long dq_flags; /* See DQ_* */
  229. short dq_type; /* Type of quota */
  230. struct mem_dqblk dq_dqb; /* Diskquota usage */
  231. };
  232. #define NODQUOT (struct dquot *)NULL
  233. #define QUOTA_OK 0
  234. #define NO_QUOTA 1
  235. /* Operations which must be implemented by each quota format */
  236. struct quota_format_ops {
  237. int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */
  238. int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */
  239. int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */
  240. int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */
  241. int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */
  242. int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */
  243. int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */
  244. };
  245. /* Operations working with dquots */
  246. struct dquot_operations {
  247. int (*initialize) (struct inode *, int);
  248. int (*drop) (struct inode *);
  249. int (*alloc_space) (struct inode *, qsize_t, int);
  250. int (*alloc_inode) (const struct inode *, unsigned long);
  251. int (*free_space) (struct inode *, qsize_t);
  252. int (*free_inode) (const struct inode *, unsigned long);
  253. int (*transfer) (struct inode *, struct iattr *);
  254. int (*write_dquot) (struct dquot *); /* Ordinary dquot write */
  255. int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */
  256. int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */
  257. int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */
  258. int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */
  259. };
  260. /* Operations handling requests from userspace */
  261. struct quotactl_ops {
  262. int (*quota_on)(struct super_block *, int, int, char *, int);
  263. int (*quota_off)(struct super_block *, int, int);
  264. int (*quota_sync)(struct super_block *, int);
  265. int (*get_info)(struct super_block *, int, struct if_dqinfo *);
  266. int (*set_info)(struct super_block *, int, struct if_dqinfo *);
  267. int (*get_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  268. int (*set_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  269. int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
  270. int (*set_xstate)(struct super_block *, unsigned int, int);
  271. int (*get_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  272. int (*set_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  273. };
  274. struct quota_format_type {
  275. int qf_fmt_id; /* Quota format id */
  276. struct quota_format_ops *qf_ops; /* Operations of format */
  277. struct module *qf_owner; /* Module implementing quota format */
  278. struct quota_format_type *qf_next;
  279. };
  280. #define DQUOT_USR_ENABLED 0x01 /* User diskquotas enabled */
  281. #define DQUOT_GRP_ENABLED 0x02 /* Group diskquotas enabled */
  282. #define DQUOT_USR_SUSPENDED 0x04 /* User diskquotas are off, but
  283. * we have necessary info in
  284. * memory to turn them on */
  285. #define DQUOT_GRP_SUSPENDED 0x08 /* The same for group quotas */
  286. struct quota_info {
  287. unsigned int flags; /* Flags for diskquotas on this device */
  288. struct mutex dqio_mutex; /* lock device while I/O in progress */
  289. struct mutex dqonoff_mutex; /* Serialize quotaon & quotaoff */
  290. struct rw_semaphore dqptr_sem; /* serialize ops using quota_info struct, pointers from inode to dquots */
  291. struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */
  292. struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */
  293. struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */
  294. };
  295. #define sb_has_quota_enabled(sb, type) ((type)==USRQUOTA ? \
  296. (sb_dqopt(sb)->flags & DQUOT_USR_ENABLED) : (sb_dqopt(sb)->flags & DQUOT_GRP_ENABLED))
  297. #define sb_any_quota_enabled(sb) (sb_has_quota_enabled(sb, USRQUOTA) | \
  298. sb_has_quota_enabled(sb, GRPQUOTA))
  299. #define sb_has_quota_suspended(sb, type) \
  300. ((type) == USRQUOTA ? (sb_dqopt(sb)->flags & DQUOT_USR_SUSPENDED) : \
  301. (sb_dqopt(sb)->flags & DQUOT_GRP_SUSPENDED))
  302. #define sb_any_quota_suspended(sb) (sb_has_quota_suspended(sb, USRQUOTA) | \
  303. sb_has_quota_suspended(sb, GRPQUOTA))
  304. int register_quota_format(struct quota_format_type *fmt);
  305. void unregister_quota_format(struct quota_format_type *fmt);
  306. struct quota_module_name {
  307. int qm_fmt_id;
  308. char *qm_mod_name;
  309. };
  310. #define INIT_QUOTA_MODULE_NAMES {\
  311. {QFMT_VFS_OLD, "quota_v1"},\
  312. {QFMT_VFS_V0, "quota_v2"},\
  313. {0, NULL}}
  314. #else
  315. # /* nodep */ include <sys/cdefs.h>
  316. __BEGIN_DECLS
  317. long quotactl __P ((unsigned int, const char *, int, caddr_t));
  318. __END_DECLS
  319. #endif /* __KERNEL__ */
  320. #endif /* _QUOTA_ */