quota.h 15 KB

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