quota.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. #ifndef _LINUX_QUOTA_
  33. #define _LINUX_QUOTA_
  34. #include <linux/errno.h>
  35. #include <linux/types.h>
  36. #define __DQUOT_VERSION__ "dquot_6.5.1"
  37. #define __DQUOT_NUM_VERSION__ 6*10000+5*100+1
  38. #define MAXQUOTAS 2
  39. #define USRQUOTA 0 /* element used for user quotas */
  40. #define GRPQUOTA 1 /* element used for group quotas */
  41. /*
  42. * Definitions for the default names of the quotas files.
  43. */
  44. #define INITQFNAMES { \
  45. "user", /* USRQUOTA */ \
  46. "group", /* GRPQUOTA */ \
  47. "undefined", \
  48. };
  49. /*
  50. * Command definitions for the 'quotactl' system call.
  51. * The commands are broken into a main command defined below
  52. * and a subcommand that is used to convey the type of
  53. * quota that is being manipulated (see above).
  54. */
  55. #define SUBCMDMASK 0x00ff
  56. #define SUBCMDSHIFT 8
  57. #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
  58. #define Q_SYNC 0x800001 /* sync disk copy of a filesystems quotas */
  59. #define Q_QUOTAON 0x800002 /* turn quotas on */
  60. #define Q_QUOTAOFF 0x800003 /* turn quotas off */
  61. #define Q_GETFMT 0x800004 /* get quota format used on given filesystem */
  62. #define Q_GETINFO 0x800005 /* get information about quota files */
  63. #define Q_SETINFO 0x800006 /* set information about quota files */
  64. #define Q_GETQUOTA 0x800007 /* get user quota structure */
  65. #define Q_SETQUOTA 0x800008 /* set user quota structure */
  66. /* Size of block in which space limits are passed through the quota
  67. * interface */
  68. #define QIF_DQBLKSIZE_BITS 10
  69. #define QIF_DQBLKSIZE (1 << QIF_DQBLKSIZE_BITS)
  70. /*
  71. * Quota structure used for communication with userspace via quotactl
  72. * Following flags are used to specify which fields are valid
  73. */
  74. #define QIF_BLIMITS 1
  75. #define QIF_SPACE 2
  76. #define QIF_ILIMITS 4
  77. #define QIF_INODES 8
  78. #define QIF_BTIME 16
  79. #define QIF_ITIME 32
  80. #define QIF_LIMITS (QIF_BLIMITS | QIF_ILIMITS)
  81. #define QIF_USAGE (QIF_SPACE | QIF_INODES)
  82. #define QIF_TIMES (QIF_BTIME | QIF_ITIME)
  83. #define QIF_ALL (QIF_LIMITS | QIF_USAGE | QIF_TIMES)
  84. struct if_dqblk {
  85. __u64 dqb_bhardlimit;
  86. __u64 dqb_bsoftlimit;
  87. __u64 dqb_curspace;
  88. __u64 dqb_ihardlimit;
  89. __u64 dqb_isoftlimit;
  90. __u64 dqb_curinodes;
  91. __u64 dqb_btime;
  92. __u64 dqb_itime;
  93. __u32 dqb_valid;
  94. };
  95. /*
  96. * Structure used for setting quota information about file via quotactl
  97. * Following flags are used to specify which fields are valid
  98. */
  99. #define IIF_BGRACE 1
  100. #define IIF_IGRACE 2
  101. #define IIF_FLAGS 4
  102. #define IIF_ALL (IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
  103. struct if_dqinfo {
  104. __u64 dqi_bgrace;
  105. __u64 dqi_igrace;
  106. __u32 dqi_flags;
  107. __u32 dqi_valid;
  108. };
  109. /*
  110. * Definitions for quota netlink interface
  111. */
  112. #define QUOTA_NL_NOWARN 0
  113. #define QUOTA_NL_IHARDWARN 1 /* Inode hardlimit reached */
  114. #define QUOTA_NL_ISOFTLONGWARN 2 /* Inode grace time expired */
  115. #define QUOTA_NL_ISOFTWARN 3 /* Inode softlimit reached */
  116. #define QUOTA_NL_BHARDWARN 4 /* Block hardlimit reached */
  117. #define QUOTA_NL_BSOFTLONGWARN 5 /* Block grace time expired */
  118. #define QUOTA_NL_BSOFTWARN 6 /* Block softlimit reached */
  119. #define QUOTA_NL_IHARDBELOW 7 /* Usage got below inode hardlimit */
  120. #define QUOTA_NL_ISOFTBELOW 8 /* Usage got below inode softlimit */
  121. #define QUOTA_NL_BHARDBELOW 9 /* Usage got below block hardlimit */
  122. #define QUOTA_NL_BSOFTBELOW 10 /* Usage got below block softlimit */
  123. enum {
  124. QUOTA_NL_C_UNSPEC,
  125. QUOTA_NL_C_WARNING,
  126. __QUOTA_NL_C_MAX,
  127. };
  128. #define QUOTA_NL_C_MAX (__QUOTA_NL_C_MAX - 1)
  129. enum {
  130. QUOTA_NL_A_UNSPEC,
  131. QUOTA_NL_A_QTYPE,
  132. QUOTA_NL_A_EXCESS_ID,
  133. QUOTA_NL_A_WARNING,
  134. QUOTA_NL_A_DEV_MAJOR,
  135. QUOTA_NL_A_DEV_MINOR,
  136. QUOTA_NL_A_CAUSED_ID,
  137. __QUOTA_NL_A_MAX,
  138. };
  139. #define QUOTA_NL_A_MAX (__QUOTA_NL_A_MAX - 1)
  140. #ifdef __KERNEL__
  141. #include <linux/list.h>
  142. #include <linux/mutex.h>
  143. #include <linux/rwsem.h>
  144. #include <linux/spinlock.h>
  145. #include <linux/wait.h>
  146. #include <linux/dqblk_xfs.h>
  147. #include <linux/dqblk_v1.h>
  148. #include <linux/dqblk_v2.h>
  149. #include <asm/atomic.h>
  150. typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
  151. typedef long long qsize_t; /* Type in which we store sizes */
  152. extern spinlock_t dq_data_lock;
  153. /* Maximal numbers of writes for quota operation (insert/delete/update)
  154. * (over VFS all formats) */
  155. #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
  156. #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
  157. #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
  158. #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
  159. /*
  160. * Data for one user/group kept in memory
  161. */
  162. struct mem_dqblk {
  163. qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
  164. qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */
  165. qsize_t dqb_curspace; /* current used space */
  166. qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */
  167. qsize_t dqb_isoftlimit; /* preferred inode limit */
  168. qsize_t dqb_curinodes; /* current # allocated inodes */
  169. time_t dqb_btime; /* time limit for excessive disk use */
  170. time_t dqb_itime; /* time limit for excessive inode use */
  171. };
  172. /*
  173. * Data for one quotafile kept in memory
  174. */
  175. struct quota_format_type;
  176. struct mem_dqinfo {
  177. struct quota_format_type *dqi_format;
  178. int dqi_fmt_id; /* Id of the dqi_format - used when turning
  179. * quotas on after remount RW */
  180. struct list_head dqi_dirty_list; /* List of dirty dquots */
  181. unsigned long dqi_flags;
  182. unsigned int dqi_bgrace;
  183. unsigned int dqi_igrace;
  184. qsize_t dqi_maxblimit;
  185. qsize_t dqi_maxilimit;
  186. void *dqi_priv;
  187. };
  188. struct super_block;
  189. #define DQF_MASK 0xffff /* Mask for format specific flags */
  190. #define DQF_INFO_DIRTY_B 16
  191. #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */
  192. extern void mark_info_dirty(struct super_block *sb, int type);
  193. static inline int info_dirty(struct mem_dqinfo *info)
  194. {
  195. return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
  196. }
  197. struct dqstats {
  198. int lookups;
  199. int drops;
  200. int reads;
  201. int writes;
  202. int cache_hits;
  203. int allocated_dquots;
  204. int free_dquots;
  205. int syncs;
  206. };
  207. extern struct dqstats dqstats;
  208. #define DQ_MOD_B 0 /* dquot modified since read */
  209. #define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */
  210. #define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */
  211. #define DQ_FAKE_B 3 /* no limits only usage */
  212. #define DQ_READ_B 4 /* dquot was read into memory */
  213. #define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */
  214. struct dquot {
  215. struct hlist_node dq_hash; /* Hash list in memory */
  216. struct list_head dq_inuse; /* List of all quotas */
  217. struct list_head dq_free; /* Free list element */
  218. struct list_head dq_dirty; /* List of dirty dquots */
  219. struct mutex dq_lock; /* dquot IO lock */
  220. atomic_t dq_count; /* Use count */
  221. wait_queue_head_t dq_wait_unused; /* Wait queue for dquot to become unused */
  222. struct super_block *dq_sb; /* superblock this applies to */
  223. unsigned int dq_id; /* ID this applies to (uid, gid) */
  224. loff_t dq_off; /* Offset of dquot on disk */
  225. unsigned long dq_flags; /* See DQ_* */
  226. short dq_type; /* Type of quota */
  227. struct mem_dqblk dq_dqb; /* Diskquota usage */
  228. };
  229. #define NODQUOT (struct dquot *)NULL
  230. #define QUOTA_OK 0
  231. #define NO_QUOTA 1
  232. /* Operations which must be implemented by each quota format */
  233. struct quota_format_ops {
  234. int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */
  235. int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */
  236. int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */
  237. int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */
  238. int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */
  239. int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */
  240. int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */
  241. };
  242. /* Operations working with dquots */
  243. struct dquot_operations {
  244. int (*initialize) (struct inode *, int);
  245. int (*drop) (struct inode *);
  246. int (*alloc_space) (struct inode *, qsize_t, int);
  247. int (*alloc_inode) (const struct inode *, qsize_t);
  248. int (*free_space) (struct inode *, qsize_t);
  249. int (*free_inode) (const struct inode *, qsize_t);
  250. int (*transfer) (struct inode *, struct iattr *);
  251. int (*write_dquot) (struct dquot *); /* Ordinary dquot write */
  252. struct dquot *(*alloc_dquot)(struct super_block *, int); /* Allocate memory for new dquot */
  253. void (*destroy_dquot)(struct dquot *); /* Free memory for dquot */
  254. int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */
  255. int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */
  256. int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */
  257. int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */
  258. };
  259. /* Operations handling requests from userspace */
  260. struct quotactl_ops {
  261. int (*quota_on)(struct super_block *, int, int, char *, int);
  262. int (*quota_off)(struct super_block *, int, int);
  263. int (*quota_sync)(struct super_block *, int);
  264. int (*get_info)(struct super_block *, int, struct if_dqinfo *);
  265. int (*set_info)(struct super_block *, int, struct if_dqinfo *);
  266. int (*get_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  267. int (*set_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  268. int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
  269. int (*set_xstate)(struct super_block *, unsigned int, int);
  270. int (*get_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  271. int (*set_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  272. };
  273. struct quota_format_type {
  274. int qf_fmt_id; /* Quota format id */
  275. struct quota_format_ops *qf_ops; /* Operations of format */
  276. struct module *qf_owner; /* Module implementing quota format */
  277. struct quota_format_type *qf_next;
  278. };
  279. /* Quota state flags - they actually come in two flavors - for users and groups */
  280. enum {
  281. _DQUOT_USAGE_ENABLED = 0, /* Track disk usage for users */
  282. _DQUOT_LIMITS_ENABLED, /* Enforce quota limits for users */
  283. _DQUOT_SUSPENDED, /* User diskquotas are off, but
  284. * we have necessary info in
  285. * memory to turn them on */
  286. _DQUOT_STATE_FLAGS
  287. };
  288. #define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED)
  289. #define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED)
  290. #define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED)
  291. #define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
  292. DQUOT_SUSPENDED)
  293. /* Other quota flags */
  294. #define DQUOT_QUOTA_SYS_FILE (1 << 6) /* Quota file is a special
  295. * system file and user cannot
  296. * touch it. Filesystem is
  297. * responsible for setting
  298. * S_NOQUOTA, S_NOATIME flags
  299. */
  300. #define DQUOT_NEGATIVE_USAGE (1 << 7) /* Allow negative quota usage */
  301. static inline unsigned int dquot_state_flag(unsigned int flags, int type)
  302. {
  303. if (type == USRQUOTA)
  304. return flags;
  305. return flags << _DQUOT_STATE_FLAGS;
  306. }
  307. static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
  308. {
  309. if (type == USRQUOTA)
  310. return flags;
  311. return flags >> _DQUOT_STATE_FLAGS;
  312. }
  313. struct quota_info {
  314. unsigned int flags; /* Flags for diskquotas on this device */
  315. struct mutex dqio_mutex; /* lock device while I/O in progress */
  316. struct mutex dqonoff_mutex; /* Serialize quotaon & quotaoff */
  317. struct rw_semaphore dqptr_sem; /* serialize ops using quota_info struct, pointers from inode to dquots */
  318. struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */
  319. struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */
  320. struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */
  321. };
  322. int register_quota_format(struct quota_format_type *fmt);
  323. void unregister_quota_format(struct quota_format_type *fmt);
  324. struct quota_module_name {
  325. int qm_fmt_id;
  326. char *qm_mod_name;
  327. };
  328. #define INIT_QUOTA_MODULE_NAMES {\
  329. {QFMT_VFS_OLD, "quota_v1"},\
  330. {QFMT_VFS_V0, "quota_v2"},\
  331. {0, NULL}}
  332. #else
  333. # /* nodep */ include <sys/cdefs.h>
  334. __BEGIN_DECLS
  335. long quotactl __P ((unsigned int, const char *, int, caddr_t));
  336. __END_DECLS
  337. #endif /* __KERNEL__ */
  338. #endif /* _QUOTA_ */