quota.h 14 KB

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